CppSQLite3.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * CppSQLite
  3. * Developed by Rob Groves <rob.groves@btinternet.com>
  4. * Maintained by NeoSmart Technologies <http://neosmart.net/>
  5. * See LICENSE file for copyright and license info
  6. */
  7. #if defined(_MSC_VER) || defined(WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32)
  8. #ifdef _OCC_P_SQLITETOOL_SDK
  9. #define _OCC_P_SQLITETOOL_API __declspec(dllexport)
  10. #else
  11. #define _OCC_P_SQLITETOOL_API __declspec(dllimport)
  12. #endif
  13. #else //linux
  14. #ifdef _OCC_P_SQLITETOOL_SDK
  15. #define _OCC_P_SQLITETOOL_API __attribute__ ((visibility("default")))
  16. #else
  17. #define _OCC_P_SQLITETOOL_API __attribute__ ((visibility("default")))
  18. #endif
  19. #endif // WIN64||WIN32
  20. #ifndef CppSQLite3_H
  21. #define CppSQLite3_H
  22. #include <cstdio>
  23. #include <cstring>
  24. #include "sqlite3.h"
  25. #define CPPSQLITE_ERROR 1000
  26. namespace detail_SQLite3
  27. {
  28. /**
  29. * RAII class for managing memory allocated by sqlite
  30. */
  31. class SQLite3Memory
  32. {
  33. public:
  34. // Default constructor
  35. SQLite3Memory();
  36. // Constructor that allocates memory of a given size
  37. SQLite3Memory(int nBufferLen);
  38. // Constructor that formats a string with sqlite memory allocation
  39. SQLite3Memory(const char* szFormat, va_list list);
  40. // Destructor
  41. ~SQLite3Memory();
  42. // Copy constructor
  43. SQLite3Memory(SQLite3Memory const& other);
  44. // Copy assignment
  45. SQLite3Memory& operator=(SQLite3Memory const& lhs);
  46. // Move constructor
  47. SQLite3Memory(SQLite3Memory&& other);
  48. // Move assignment
  49. SQLite3Memory& operator=(SQLite3Memory&& lhs);
  50. // Swap operation
  51. void swap(SQLite3Memory& other);
  52. int getLength() const { return mnBufferLen; }
  53. void* getBuffer() const { return mpBuf; }
  54. void clear();
  55. private:
  56. int mnBufferLen;
  57. void* mpBuf;
  58. };
  59. }
  60. class _OCC_P_SQLITETOOL_API CppSQLite3Exception
  61. {
  62. public:
  63. CppSQLite3Exception(const int nErrCode,
  64. const char* szErrMess,
  65. bool bDeleteMsg=true);
  66. CppSQLite3Exception(const CppSQLite3Exception& e);
  67. virtual ~CppSQLite3Exception();
  68. const int errorCode() const { return mnErrCode; }
  69. const char* errorMessage() const { return mpszErrMess; }
  70. static const char* errorCodeAsString(int nErrCode);
  71. private:
  72. int mnErrCode;
  73. char* mpszErrMess;
  74. };
  75. class _OCC_P_SQLITETOOL_API CppSQLite3Buffer
  76. {
  77. public:
  78. const char* format(const char* szFormat, ...);
  79. operator const char*() { return static_cast<char const*>(mBuf.getBuffer()); }
  80. void clear();
  81. private:
  82. detail_SQLite3::SQLite3Memory mBuf;
  83. };
  84. class _OCC_P_SQLITETOOL_API CppSQLite3Binary
  85. {
  86. public:
  87. CppSQLite3Binary();
  88. ~CppSQLite3Binary();
  89. void setBinary(const unsigned char* pBuf, int nLen);
  90. void setEncoded(const unsigned char* pBuf);
  91. const unsigned char* getEncoded();
  92. const unsigned char* getBinary();
  93. int getBinaryLength();
  94. unsigned char* allocBuffer(int nLen);
  95. void clear();
  96. private:
  97. unsigned char* mpBuf;
  98. int mnBinaryLen;
  99. int mnBufferLen;
  100. int mnEncodedLen;
  101. bool mbEncoded;
  102. };
  103. class _OCC_P_SQLITETOOL_API CppSQLite3Query
  104. {
  105. public:
  106. CppSQLite3Query();
  107. CppSQLite3Query(const CppSQLite3Query& rQuery);
  108. CppSQLite3Query(sqlite3* pDB,
  109. sqlite3_stmt* pVM,
  110. bool bEof,
  111. bool bOwnVM=true);
  112. CppSQLite3Query& operator=(const CppSQLite3Query& rQuery);
  113. virtual ~CppSQLite3Query();
  114. int numFields() const;
  115. int fieldIndex(const char* szField) const;
  116. const char* fieldName(int nCol) const;
  117. const char* fieldDeclType(int nCol) const;
  118. int fieldDataType(int nCol) const;
  119. const char* fieldValue(int nField) const;
  120. const char* fieldValue(const char* szField) const;
  121. int getIntField(int nField, int nNullValue=0) const;
  122. int getIntField(const char* szField, int nNullValue=0) const;
  123. long long getInt64Field(int nField, long long nNullValue=0) const;
  124. long long getInt64Field(const char* szField, long long nNullValue=0) const;
  125. double getFloatField(int nField, double fNullValue=0.0) const;
  126. double getFloatField(const char* szField, double fNullValue=0.0) const;
  127. const char* getStringField(int nField, const char* szNullValue="") const;
  128. const char* getStringField(const char* szField, const char* szNullValue="") const;
  129. const unsigned char* getBlobField(int nField, int& nLen) const;
  130. const unsigned char* getBlobField(const char* szField, int& nLen) const;
  131. bool fieldIsNull(int nField) const;
  132. bool fieldIsNull(const char* szField) const;
  133. bool eof() const;
  134. void nextRow();
  135. void finalize();
  136. private:
  137. void checkVM() const;
  138. sqlite3* mpDB;
  139. sqlite3_stmt* mpVM;
  140. bool mbEof;
  141. int mnCols;
  142. bool mbOwnVM;
  143. };
  144. class _OCC_P_SQLITETOOL_API CppSQLite3Table
  145. {
  146. public:
  147. CppSQLite3Table();
  148. CppSQLite3Table(const CppSQLite3Table& rTable);
  149. CppSQLite3Table(char** paszResults, int nRows, int nCols);
  150. virtual ~CppSQLite3Table();
  151. CppSQLite3Table& operator=(const CppSQLite3Table& rTable);
  152. int numFields() const;
  153. int numRows() const;
  154. const char* fieldName(int nCol) const;
  155. const char* fieldValue(int nField) const;
  156. const char* fieldValue(const char* szField) const;
  157. int getIntField(int nField, int nNullValue=0) const;
  158. int getIntField(const char* szField, int nNullValue=0) const;
  159. double getFloatField(int nField, double fNullValue=0.0) const;
  160. double getFloatField(const char* szField, double fNullValue=0.0) const;
  161. const char* getStringField(int nField, const char* szNullValue="") const;
  162. const char* getStringField(const char* szField, const char* szNullValue="") const;
  163. bool fieldIsNull(int nField) const;
  164. bool fieldIsNull(const char* szField) const;
  165. void setRow(int nRow);
  166. void finalize();
  167. private:
  168. void checkResults() const;
  169. int mnCols;
  170. int mnRows;
  171. int mnCurrentRow;
  172. char** mpaszResults;
  173. };
  174. class _OCC_P_SQLITETOOL_API CppSQLite3Statement
  175. {
  176. public:
  177. CppSQLite3Statement();
  178. CppSQLite3Statement(const CppSQLite3Statement& rStatement);
  179. CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM);
  180. virtual ~CppSQLite3Statement();
  181. CppSQLite3Statement& operator=(const CppSQLite3Statement& rStatement);
  182. int execDML();
  183. CppSQLite3Query execQuery();
  184. void bind(int nParam, const char* szValue);
  185. void bind(int nParam, const int nValue);
  186. void bind(int nParam, const long long nValue);
  187. void bind(int nParam, const double dwValue);
  188. void bind(int nParam, const unsigned char* blobValue, int nLen);
  189. void bindNull(int nParam);
  190. void reset();
  191. void finalize();
  192. private:
  193. void checkDB() const;
  194. void checkVM() const;
  195. sqlite3* mpDB;
  196. sqlite3_stmt* mpVM;
  197. };
  198. class _OCC_P_SQLITETOOL_API CppSQLite3DB
  199. {
  200. public:
  201. CppSQLite3DB();
  202. virtual ~CppSQLite3DB();
  203. void open(const char* szFile);
  204. void close();
  205. bool tableExists(const char* szTable);
  206. int execDML(const char* szSQL);
  207. CppSQLite3Query execQuery(const char* szSQL);
  208. int execScalar(const char* szSQL);
  209. CppSQLite3Table getTable(const char* szSQL);
  210. CppSQLite3Statement compileStatement(const char* szSQL);
  211. sqlite_int64 lastRowId() const;
  212. void interrupt() { sqlite3_interrupt(mpDB); }
  213. void setBusyTimeout(int nMillisecs);
  214. static const char* SQLiteVersion() { return SQLITE_VERSION; }
  215. void Serialize(const char * zFilename,int IsSave);
  216. private:
  217. CppSQLite3DB(const CppSQLite3DB& db);
  218. CppSQLite3DB& operator=(const CppSQLite3DB& db);
  219. sqlite3_stmt* compile(const char* szSQL);
  220. void checkDB() const;
  221. sqlite3* mpDB;
  222. int mnBusyTimeoutMs;
  223. };
  224. #endif