CppSQLite3.h 7.0 KB

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