CppSQLite3.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #include <string>
  39. using namespace std;
  40. #define CPPSQLITE_ERROR 1000
  41. class CppSQLite3Exception
  42. {
  43. public:
  44. CppSQLite3Exception(const int nErrCode,
  45. char* szErrMess,
  46. bool bDeleteMsg=true);
  47. CppSQLite3Exception(const CppSQLite3Exception& e);
  48. virtual ~CppSQLite3Exception();
  49. const int errorCode() { return mnErrCode; }
  50. const char* errorMessage() { return mpszErrMess; }
  51. static const char* errorCodeAsString(int nErrCode);
  52. private:
  53. int mnErrCode;
  54. char* mpszErrMess;
  55. };
  56. class CppSQLite3Buffer
  57. {
  58. public:
  59. CppSQLite3Buffer();
  60. ~CppSQLite3Buffer();
  61. const char* format(const char* szFormat, ...);
  62. operator const char*() { return mpBuf; }
  63. void clear();
  64. private:
  65. char* mpBuf;
  66. };
  67. class CppSQLite3Binary
  68. {
  69. public:
  70. CppSQLite3Binary();
  71. ~CppSQLite3Binary();
  72. void setBinary(const unsigned char* pBuf, int nLen);
  73. void setEncoded(const unsigned char* pBuf);
  74. const unsigned char* getEncoded();
  75. const unsigned char* getBinary();
  76. int getBinaryLength();
  77. unsigned char* allocBuffer(int nLen);
  78. void clear();
  79. private:
  80. unsigned char* mpBuf;
  81. int mnBinaryLen;
  82. int mnBufferLen;
  83. int mnEncodedLen;
  84. bool mbEncoded;
  85. };
  86. class CppSQLite3Query
  87. {
  88. public:
  89. CppSQLite3Query();
  90. CppSQLite3Query(const CppSQLite3Query& rQuery);
  91. CppSQLite3Query(sqlite3* pDB,
  92. sqlite3_stmt* pVM,
  93. bool bEof,
  94. bool bOwnVM=true);
  95. CppSQLite3Query& operator=(const CppSQLite3Query& rQuery);
  96. virtual ~CppSQLite3Query();
  97. int numFields();
  98. int fieldIndex(const char* szField);
  99. const char* fieldName(int nCol);
  100. const char* fieldDeclType(int nCol);
  101. int fieldDataType(int nCol);
  102. const char* fieldValue(int nField);
  103. const char* fieldValue(const char* szField);
  104. int getIntField(int nField, int nNullValue=0);
  105. int getIntField(const char* szField, int nNullValue=0);
  106. sqlite_int64 getInt64Field(int nField, sqlite_int64 nNullValue=0);
  107. sqlite_int64 getInt64Field(const char* szField, sqlite_int64 nNullValue=0);
  108. double getFloatField(int nField, double fNullValue=0.0);
  109. double getFloatField(const char* szField, double fNullValue=0.0);
  110. const char* getStringField(int nField, const char* szNullValue="");
  111. const char* getStringField(const char* szField, const char* szNullValue="");
  112. const unsigned char* getBlobField(int nField, int& nLen);
  113. const unsigned char* getBlobField(const char* szField, int& nLen);
  114. bool fieldIsNull(int nField);
  115. bool fieldIsNull(const char* szField);
  116. bool eof();
  117. void nextRow();
  118. void finalize();
  119. private:
  120. void checkVM();
  121. sqlite3* mpDB;
  122. sqlite3_stmt* mpVM;
  123. bool mbEof;
  124. int mnCols;
  125. bool mbOwnVM;
  126. };
  127. class CppSQLite3Table
  128. {
  129. public:
  130. CppSQLite3Table();
  131. CppSQLite3Table(const CppSQLite3Table& rTable);
  132. CppSQLite3Table(char** paszResults, int nRows, int nCols);
  133. virtual ~CppSQLite3Table();
  134. CppSQLite3Table& operator=(const CppSQLite3Table& rTable);
  135. int numFields();
  136. int numRows();
  137. const char* fieldName(int nCol);
  138. const char* fieldValue(int nField);
  139. const char* fieldValue(const char* szField);
  140. int getIntField(int nField, int nNullValue=0);
  141. int getIntField(const char* szField, int nNullValue=0);
  142. double getFloatField(int nField, double fNullValue=0.0);
  143. double getFloatField(const char* szField, double fNullValue=0.0);
  144. const char* getStringField(int nField, const char* szNullValue="");
  145. const char* getStringField(const char* szField, const char* szNullValue="");
  146. bool fieldIsNull(int nField);
  147. bool fieldIsNull(const char* szField);
  148. void setRow(int nRow);
  149. void finalize();
  150. private:
  151. void checkResults();
  152. int mnCols;
  153. int mnRows;
  154. int mnCurrentRow;
  155. char** mpaszResults;
  156. };
  157. class CppSQLite3Statement
  158. {
  159. public:
  160. CppSQLite3Statement();
  161. CppSQLite3Statement(const CppSQLite3Statement& rStatement);
  162. CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM);
  163. virtual ~CppSQLite3Statement();
  164. CppSQLite3Statement& operator=(const CppSQLite3Statement& rStatement);
  165. int execDML();
  166. CppSQLite3Query execQuery();
  167. void bind(int nParam, const char* szValue);
  168. void bind(int nParam, const int nValue);
  169. void bind(int nParam, const double dwValue);
  170. void bind(int nParam, const sqlite3_int64 dwValue);
  171. void bind(int nParam, const unsigned char* blobValue, int nLen);
  172. void bindNull(int nParam);
  173. int bindParameterIndex(const char* szParam);
  174. void bind(const char* szParam, const char* szValue);
  175. void bind(const char* szParam, const int nValue);
  176. void bind(const char* szParam, const double dwValue);
  177. void bind(const char* szParam, const unsigned char* blobValue, int nLen);
  178. void bind(const char* szParam, const sqlite3_int64 dwValue);
  179. void bindNull(const char* szParam);
  180. void reset();
  181. void finalize();
  182. private:
  183. void checkDB();
  184. void checkVM();
  185. sqlite3* mpDB;
  186. sqlite3_stmt* mpVM;
  187. };
  188. class CppSQLite3DB
  189. {
  190. public:
  191. CppSQLite3DB();
  192. virtual ~CppSQLite3DB();
  193. void open(const char* szFile);
  194. bool is_open(){ return mpDB != NULL; }
  195. void close();
  196. bool tableExists(const char* szTable);
  197. int execDML(const char* szSQL);
  198. CppSQLite3Query execQuery(const char* szSQL);
  199. int execScalar(const char* szSQL, int nNullValue=0);
  200. CppSQLite3Table getTable(const char* szSQL);
  201. CppSQLite3Statement compileStatement(const char* szSQL);
  202. sqlite_int64 lastRowId();
  203. void interrupt() { sqlite3_interrupt(mpDB); }
  204. void setBusyTimeout(int nMillisecs);
  205. static const char* SQLiteVersion() { return SQLITE_VERSION; }
  206. static const char* SQLiteHeaderVersion() { return SQLITE_VERSION; }
  207. static const char* SQLiteLibraryVersion() { return sqlite3_libversion(); }
  208. static int SQLiteLibraryVersionNumber() { return sqlite3_libversion_number(); }
  209. bool IsAutoCommitOn();
  210. private:
  211. CppSQLite3DB(const CppSQLite3DB& db);
  212. CppSQLite3DB& operator=(const CppSQLite3DB& db);
  213. sqlite3_stmt* compile(const char* szSQL);
  214. void checkDB();
  215. sqlite3* mpDB;
  216. int mnBusyTimeoutMs;
  217. string m_strDbPath;//Êý¾Ý¿â·¾¶
  218. };
  219. #endif