CppSQLite3.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library.
  3. //
  4. // Copyright (c) 2004..2007 Rob Groves. All Rights Reserved. rob.groves@btinternet.com
  5. //
  6. // Permission to use, copy, modify, and distribute this software and its
  7. // documentation for any purpose, without fee, and without a written
  8. // agreement, is hereby granted, provided that the above copyright notice,
  9. // this paragraph and the following two paragraphs appear in all copies,
  10. // modifications, and distributions.
  11. //
  12. // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
  13. // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
  14. // PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
  15. // EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. //
  17. // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19. // PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
  20. // ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION
  21. // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  22. //
  23. // V3.0 03/08/2004 -Initial Version for sqlite3
  24. //
  25. // V3.1 16/09/2004 -Implemented getXXXXField using sqlite3 functions
  26. // -Added CppSQLiteDB3::tableExists()
  27. //
  28. // V3.2 01/07/2005 -Fixed execScalar to handle a NULL result
  29. // 12/07/2007 -Added CppSQLiteDB::IsAutoCommitOn()
  30. // -Added int64 functions to CppSQLite3Query
  31. // -Added Name based parameter binding to CppSQLite3Statement.
  32. ////////////////////////////////////////////////////////////////////////////////
  33. #ifndef _CppSQLite3_H_
  34. #define _CppSQLite3_H_
  35. #include "sqlite3.h"
  36. #include <cstdio>
  37. #include <cstring>
  38. #define CPPSQLITE_ERROR 1000
  39. class CppSQLite3Exception
  40. {
  41. public:
  42. CppSQLite3Exception(const int nErrCode,
  43. char* szErrMess,
  44. bool bDeleteMsg=true);
  45. CppSQLite3Exception(const CppSQLite3Exception& e);
  46. virtual ~CppSQLite3Exception();
  47. const int errorCode() { return mnErrCode; }
  48. const char* errorMessage() { return mpszErrMess; }
  49. static const char* errorCodeAsString(int nErrCode);
  50. private:
  51. int mnErrCode;
  52. char* mpszErrMess;
  53. };
  54. class CppSQLite3Buffer
  55. {
  56. public:
  57. CppSQLite3Buffer();
  58. ~CppSQLite3Buffer();
  59. const char* format(const char* szFormat, ...);
  60. operator const char*() { return mpBuf; }
  61. void clear();
  62. private:
  63. char* mpBuf;
  64. };
  65. class CppSQLite3Binary
  66. {
  67. public:
  68. CppSQLite3Binary();
  69. ~CppSQLite3Binary();
  70. void setBinary(const unsigned char* pBuf, int nLen);
  71. void setEncoded(const unsigned char* pBuf);
  72. const unsigned char* getEncoded();
  73. const unsigned char* getBinary();
  74. int getBinaryLength();
  75. unsigned char* allocBuffer(int nLen);
  76. void clear();
  77. private:
  78. unsigned char* mpBuf;
  79. int mnBinaryLen;
  80. int mnBufferLen;
  81. int mnEncodedLen;
  82. bool mbEncoded;
  83. };
  84. class CppSQLite3Query
  85. {
  86. public:
  87. CppSQLite3Query();
  88. CppSQLite3Query(const CppSQLite3Query& rQuery);
  89. CppSQLite3Query(sqlite3* pDB,
  90. sqlite3_stmt* pVM,
  91. bool bEof,
  92. bool bOwnVM=true);
  93. CppSQLite3Query& operator=(const CppSQLite3Query& rQuery);
  94. virtual ~CppSQLite3Query();
  95. int numFields();
  96. int fieldIndex(const char* szField);
  97. const char* fieldName(int nCol);
  98. const char* fieldDeclType(int nCol);
  99. int fieldDataType(int nCol);
  100. const char* fieldValue(int nField);
  101. const char* fieldValue(const char* szField);
  102. int getIntField(int nField, int nNullValue=0);
  103. int getIntField(const char* szField, int nNullValue=0);
  104. sqlite_int64 getInt64Field(int nField, sqlite_int64 nNullValue=0);
  105. sqlite_int64 getInt64Field(const char* szField, sqlite_int64 nNullValue=0);
  106. double getFloatField(int nField, double fNullValue=0.0);
  107. double getFloatField(const char* szField, double fNullValue=0.0);
  108. const char* getStringField(int nField, const char* szNullValue="");
  109. const char* getStringField(const char* szField, const char* szNullValue="");
  110. const unsigned char* getBlobField(int nField, int& nLen);
  111. const unsigned char* getBlobField(const char* szField, int& nLen);
  112. bool fieldIsNull(int nField);
  113. bool fieldIsNull(const char* szField);
  114. bool eof();
  115. void nextRow();
  116. void finalize();
  117. private:
  118. void checkVM();
  119. sqlite3* mpDB;
  120. sqlite3_stmt* mpVM;
  121. bool mbEof;
  122. int mnCols;
  123. bool mbOwnVM;
  124. };
  125. class CppSQLite3Table
  126. {
  127. public:
  128. CppSQLite3Table();
  129. CppSQLite3Table(const CppSQLite3Table& rTable);
  130. CppSQLite3Table(char** paszResults, int nRows, int nCols);
  131. virtual ~CppSQLite3Table();
  132. CppSQLite3Table& operator=(const CppSQLite3Table& rTable);
  133. int numFields();
  134. int numRows();
  135. const char* fieldName(int nCol);
  136. const char* fieldValue(int nField);
  137. const char* fieldValue(const char* szField);
  138. int getIntField(int nField, int nNullValue=0);
  139. int getIntField(const char* szField, int nNullValue=0);
  140. double getFloatField(int nField, double fNullValue=0.0);
  141. double getFloatField(const char* szField, double fNullValue=0.0);
  142. const char* getStringField(int nField, const char* szNullValue="");
  143. const char* getStringField(const char* szField, const char* szNullValue="");
  144. bool fieldIsNull(int nField);
  145. bool fieldIsNull(const char* szField);
  146. void setRow(int nRow);
  147. void finalize();
  148. private:
  149. void checkResults();
  150. int mnCols;
  151. int mnRows;
  152. int mnCurrentRow;
  153. char** mpaszResults;
  154. };
  155. class CppSQLite3Statement
  156. {
  157. public:
  158. CppSQLite3Statement();
  159. CppSQLite3Statement(const CppSQLite3Statement& rStatement);
  160. CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM);
  161. virtual ~CppSQLite3Statement();
  162. CppSQLite3Statement& operator=(const CppSQLite3Statement& rStatement);
  163. int execDML();
  164. CppSQLite3Query execQuery();
  165. void bind(int nParam, const char* szValue);
  166. void bind(int nParam, const int nValue);
  167. void bind(int nParam, const double dwValue);
  168. void bind(int nParam, const unsigned char* blobValue, int nLen);
  169. void bindNull(int nParam);
  170. int bindParameterIndex(const char* szParam);
  171. void bind(const char* szParam, const char* szValue);
  172. void bind(const char* szParam, const int nValue);
  173. void bind(const char* szParam, const double dwValue);
  174. void bind(const char* szParam, const unsigned char* blobValue, int nLen);
  175. void bindNull(const char* szParam);
  176. void reset();
  177. void finalize();
  178. private:
  179. void checkDB();
  180. void checkVM();
  181. sqlite3* mpDB;
  182. sqlite3_stmt* mpVM;
  183. };
  184. class CppSQLite3DB
  185. {
  186. public:
  187. CppSQLite3DB();
  188. virtual ~CppSQLite3DB();
  189. void open(const char* szFile);
  190. void close();
  191. bool tableExists(const char* szTable);
  192. int execDML(const char* szSQL);
  193. CppSQLite3Query execQuery(const char* szSQL);
  194. int execScalar(const char* szSQL, int nNullValue=0);
  195. CppSQLite3Table getTable(const char* szSQL);
  196. CppSQLite3Statement compileStatement(const char* szSQL);
  197. sqlite_int64 lastRowId();
  198. void interrupt() { sqlite3_interrupt(mpDB); }
  199. void setBusyTimeout(int nMillisecs);
  200. static const char* SQLiteVersion() { return SQLITE_VERSION; }
  201. static const char* SQLiteHeaderVersion() { return SQLITE_VERSION; }
  202. static const char* SQLiteLibraryVersion() { return sqlite3_libversion(); }
  203. static int SQLiteLibraryVersionNumber() { return sqlite3_libversion_number(); }
  204. bool IsAutoCommitOn();
  205. private:
  206. CppSQLite3DB(const CppSQLite3DB& db);
  207. CppSQLite3DB& operator=(const CppSQLite3DB& db);
  208. sqlite3_stmt* compile(const char* szSQL);
  209. void checkDB();
  210. sqlite3* mpDB;
  211. int mnBusyTimeoutMs;
  212. };
  213. #endif