exception.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2.0, as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://oss.oracle.com/licenses/universal-foss-exception.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. * See the GNU General Public License, version 2.0, for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #ifndef _SQL_EXCEPTION_H_
  31. #define _SQL_EXCEPTION_H_
  32. #include "build_config.h"
  33. #include <stdexcept>
  34. #include <string>
  35. #include <memory>
  36. namespace sql
  37. {
  38. #if (__cplusplus < 201103L)
  39. #define MEMORY_ALLOC_OPERATORS(Class) \
  40. void* operator new(size_t size) throw (std::bad_alloc) { return ::operator new(size); } \
  41. void* operator new(size_t, void*) throw(); \
  42. void* operator new(size_t, const std::nothrow_t&) throw(); \
  43. void* operator new[](size_t) throw (std::bad_alloc); \
  44. void* operator new[](size_t, void*) throw(); \
  45. void* operator new[](size_t, const std::nothrow_t&) throw(); \
  46. void* operator new(size_t N, std::allocator<Class>&);
  47. #else
  48. #define MEMORY_ALLOC_OPERATORS(Class) \
  49. void* operator new(size_t size){ return ::operator new(size); } \
  50. void* operator new(size_t, void*) noexcept; \
  51. void* operator new(size_t, const std::nothrow_t&) noexcept; \
  52. void* operator new[](size_t); \
  53. void* operator new[](size_t, void*) noexcept; \
  54. void* operator new[](size_t, const std::nothrow_t&) noexcept; \
  55. void* operator new(size_t N, std::allocator<Class>&);
  56. #endif
  57. #ifdef _WIN32
  58. #pragma warning (disable : 4290)
  59. //warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
  60. #pragma warning(push)
  61. #pragma warning(disable: 4275)
  62. #endif
  63. class CPPCONN_PUBLIC_FUNC SQLException : public std::runtime_error
  64. {
  65. #ifdef _WIN32
  66. #pragma warning(pop)
  67. #endif
  68. protected:
  69. const std::string sql_state;
  70. const int errNo;
  71. public:
  72. SQLException(const SQLException& e) : std::runtime_error(e.what()), sql_state(e.sql_state), errNo(e.errNo) {}
  73. SQLException(const std::string& reason, const std::string& SQLState, int vendorCode) :
  74. std::runtime_error (reason ),
  75. sql_state (SQLState ),
  76. errNo (vendorCode)
  77. {}
  78. SQLException(const std::string& reason, const std::string& SQLState) : std::runtime_error(reason), sql_state(SQLState), errNo(0) {}
  79. SQLException(const std::string& reason) : std::runtime_error(reason), sql_state("HY000"), errNo(0) {}
  80. SQLException() : std::runtime_error(""), sql_state("HY000"), errNo(0) {}
  81. const std::string & getSQLState() const
  82. {
  83. return sql_state;
  84. }
  85. const char * getSQLStateCStr() const
  86. {
  87. return sql_state.c_str();
  88. }
  89. int getErrorCode() const
  90. {
  91. return errNo;
  92. }
  93. virtual ~SQLException() throw () {};
  94. protected:
  95. MEMORY_ALLOC_OPERATORS(SQLException)
  96. };
  97. struct CPPCONN_PUBLIC_FUNC MethodNotImplementedException : public SQLException
  98. {
  99. MethodNotImplementedException(const MethodNotImplementedException& e) : SQLException(e.what(), e.sql_state, e.errNo) { }
  100. MethodNotImplementedException(const std::string& reason) : SQLException(reason, "", 0) {}
  101. };
  102. struct CPPCONN_PUBLIC_FUNC InvalidArgumentException : public SQLException
  103. {
  104. InvalidArgumentException(const InvalidArgumentException& e) : SQLException(e.what(), e.sql_state, e.errNo) { }
  105. InvalidArgumentException(const std::string& reason) : SQLException(reason, "", 0) {}
  106. };
  107. struct CPPCONN_PUBLIC_FUNC InvalidInstanceException : public SQLException
  108. {
  109. InvalidInstanceException(const InvalidInstanceException& e) : SQLException(e.what(), e.sql_state, e.errNo) { }
  110. InvalidInstanceException(const std::string& reason) : SQLException(reason, "", 0) {}
  111. };
  112. struct CPPCONN_PUBLIC_FUNC NonScrollableException : public SQLException
  113. {
  114. NonScrollableException(const NonScrollableException& e) : SQLException(e.what(), e.sql_state, e.errNo) { }
  115. NonScrollableException(const std::string& reason) : SQLException(reason, "", 0) {}
  116. };
  117. struct CPPCONN_PUBLIC_FUNC SQLUnsupportedOptionException : public SQLException
  118. {
  119. SQLUnsupportedOptionException(const SQLUnsupportedOptionException& e, const std::string conn_option) :
  120. SQLException(e.what(), e.sql_state, e.errNo),
  121. option(conn_option )
  122. {}
  123. SQLUnsupportedOptionException(const std::string& reason, const std::string conn_option) :
  124. SQLException(reason, "", 0),
  125. option(conn_option )
  126. {}
  127. const char *getConnectionOption() const
  128. {
  129. return option.c_str();
  130. }
  131. ~SQLUnsupportedOptionException() throw () {};
  132. protected:
  133. const std::string option;
  134. };
  135. } /* namespace sql */
  136. #endif /* _SQL_EXCEPTION_H_ */