Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4. #include <stdexcept>
  5.  
  6. #include "sqlite3.h"
  7.  
  8. namespace sqlite {
  9.  
  10.     class sqlite_exception: public std::runtime_error {
  11.     public:
  12.         sqlite_exception(const char* msg, std::string sql, int code = -1): runtime_error(msg), code(code), sql(sql) {}
  13.         sqlite_exception(int code, std::string sql): runtime_error(sqlite3_errstr(code)), code(code), sql(sql) {}
  14.         int get_code() const {return code & 0xFF;}
  15.         int get_extended_code() const {return code;}
  16.         std::string get_sql() const {return sql;}
  17.     private:
  18.         int code;
  19.         std::string sql;
  20.     };
  21.  
  22.     namespace errors {
  23.         //One more or less trivial derived error class for each SQLITE error.
  24.         //Note the following are not errors so have no classes:
  25.         //SQLITE_OK, SQLITE_NOTICE, SQLITE_WARNING, SQLITE_ROW, SQLITE_DONE
  26.         //
  27.         //Note these names are exact matches to the names of the SQLITE error codes.
  28. #define SQLITE_MODERN_CPP_ERROR_CODE(NAME,name,derived) \
  29.         class name: public sqlite_exception { using sqlite_exception::sqlite_exception; };\
  30.         derived
  31. #define SQLITE_MODERN_CPP_ERROR_CODE_EXTENDED(BASE,SUB,base,sub) \
  32.         class base ## _ ## sub: public base { using base::base; };
  33. #include "lists/error_codes.h"
  34. #undef SQLITE_MODERN_CPP_ERROR_CODE_EXTENDED
  35. #undef SQLITE_MODERN_CPP_ERROR_CODE
  36.  
  37.         //Some additional errors are here for the C++ interface
  38.         class more_rows: public sqlite_exception { using sqlite_exception::sqlite_exception; };
  39.         class no_rows: public sqlite_exception { using sqlite_exception::sqlite_exception; };
  40.         class more_statements: public sqlite_exception { using sqlite_exception::sqlite_exception; }; // Prepared statements can only contain one statement
  41.         class invalid_utf16: public sqlite_exception { using sqlite_exception::sqlite_exception; };
  42.  
  43.         static void throw_sqlite_error(const int& error_code, const std::string &sql = "") {
  44.             switch(error_code & 0xFF) {
  45. #define SQLITE_MODERN_CPP_ERROR_CODE(NAME,name,derived)     \
  46.                 case SQLITE_ ## NAME: switch(error_code) {          \
  47.                     derived                                           \
  48.                     default: throw name(error_code, sql); \
  49.                 }
  50. #define SQLITE_MODERN_CPP_ERROR_CODE_EXTENDED(BASE,SUB,base,sub) \
  51.                     case SQLITE_ ## BASE ## _ ## SUB: throw base ## _ ## sub(error_code, sql);
  52. #include "lists/error_codes.h"
  53. #undef SQLITE_MODERN_CPP_ERROR_CODE_EXTENDED
  54. #undef SQLITE_MODERN_CPP_ERROR_CODE
  55.                 default: throw sqlite_exception(error_code, sql);
  56.             }
  57.         }
  58.     }
  59.     namespace exceptions = errors;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement