CppSQLite3.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library.
  3. //
  4. // Copyright (c) 2004 Rob Groves. All Rights Reserved. [email protected]
  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. #ifndef _CppSQLite3_H_
  29. #define _CppSQLite3_H_
  30. #include "sqlite3.h"
  31. #include <cstdio>
  32. #include <cstring>
  33. #define CPPSQLITE_ERROR 1000
  34. #ifdef _UNICODE
  35. #define SQLITE3_ERRMSG(mpDB) const TCHAR* szError = (const TCHAR*)sqlite3_errmsg16(mpDB)
  36. #else
  37. #define SQLITE3_ERRMSG(mpDB) const TCHAR* szError = sqlite3_errmsg(mpDB)
  38. #endif
  39. int sqlite3_encode_binary(const unsigned char *in, int n, unsigned char *out);
  40. int sqlite3_decode_binary(const unsigned char *in, unsigned char *out);
  41. class CppSQLite3Exception
  42. {
  43. public:
  44. CppSQLite3Exception(const int nErrCode,
  45. TCHAR* szErrMess,
  46. bool bDeleteMsg=true);
  47. CppSQLite3Exception(const CppSQLite3Exception& e);
  48. virtual ~CppSQLite3Exception();
  49. const int errorCode() { return mnErrCode; }
  50. const TCHAR* errorMessage() { return mpszErrMess; }
  51. static const TCHAR* errorCodeAsString(int nErrCode);
  52. private:
  53. int mnErrCode;
  54. TCHAR mpszErrMess[1000];
  55. };
  56. class CppSQLite3Query
  57. {
  58. public:
  59. CppSQLite3Query();
  60. CppSQLite3Query(const CppSQLite3Query& rQuery);
  61. CppSQLite3Query(sqlite3* pDB,
  62. sqlite3_stmt* pVM,
  63. bool bEof,
  64. bool bOwnVM=true);
  65. CppSQLite3Query& operator=(const CppSQLite3Query& rQuery);
  66. virtual ~CppSQLite3Query();
  67. int numFields();
  68. int fieldIndex(const TCHAR* szField);
  69. const TCHAR* fieldName(int nCol);
  70. const TCHAR* fieldDeclType(int nCol);
  71. int fieldDataType(int nCol);
  72. const TCHAR* fieldValue(int nField);
  73. const TCHAR* fieldValue(const TCHAR* szField);
  74. int getIntField(int nField, int nNullValue=0);
  75. int getIntField(const TCHAR* szField, int nNullValue=0);
  76. double getFloatField(int nField, double fNullValue=0.0);
  77. double getFloatField(const TCHAR* szField, double fNullValue=0.0);
  78. const TCHAR* getStringField(int nField, const TCHAR* szNullValue=_T(""));
  79. const TCHAR* getStringField(const TCHAR* szField, const TCHAR* szNullValue=_T(""));
  80. const unsigned char* getBlobField(int nField, int& nLen);
  81. const unsigned char* getBlobField(const TCHAR* szField, int& nLen);
  82. int getBlobFieldSize(const TCHAR* szField);
  83. int getBlobFieldSize(int nField);
  84. bool fieldIsNull(int nField);
  85. bool fieldIsNull(const TCHAR* szField);
  86. bool eof();
  87. void nextRow();
  88. void finalize();
  89. private:
  90. void checkVM();
  91. sqlite3* mpDB;
  92. sqlite3_stmt* mpVM;
  93. bool mbEof;
  94. int mnCols;
  95. bool mbOwnVM;
  96. };
  97. class CppSQLite3Statement
  98. {
  99. public:
  100. CppSQLite3Statement();
  101. CppSQLite3Statement(const CppSQLite3Statement& rStatement);
  102. CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM);
  103. virtual ~CppSQLite3Statement();
  104. CppSQLite3Statement& operator=(const CppSQLite3Statement& rStatement);
  105. int execDML();
  106. CppSQLite3Query execQuery();
  107. void bind(int nParam, const TCHAR* szValue);
  108. void bind(int nParam, const int nValue);
  109. void bind(int nParam, const double dwValue);
  110. void bind(int nParam, const unsigned char* blobValue, int nLen);
  111. void bindNull(int nParam);
  112. void reset();
  113. void finalize();
  114. private:
  115. void checkDB();
  116. void checkVM();
  117. sqlite3* mpDB;
  118. sqlite3_stmt* mpVM;
  119. };
  120. class CppSQLite3DB
  121. {
  122. public:
  123. CppSQLite3DB();
  124. virtual ~CppSQLite3DB();
  125. void open(const TCHAR* szFile);
  126. void SetRegexCaseInsensitive(bool insensitive);
  127. bool close();
  128. bool tableExists(const TCHAR* szTable);
  129. int execDMLEx(LPCTSTR szSQL,...);
  130. int execDML(const TCHAR* szSQL);
  131. CppSQLite3Query execQueryEx(LPCTSTR szSQL,...);
  132. CppSQLite3Query execQuery(const TCHAR* szSQL);
  133. int execScalarEx(LPCTSTR szSQL,...);
  134. int execScalar(const TCHAR* szSQL);
  135. CppSQLite3Statement compileStatement(const TCHAR* szSQL);
  136. sqlite_int64 lastRowId();
  137. void interrupt() { sqlite3_interrupt(mpDB); }
  138. void setBusyTimeout(int nMillisecs);
  139. static const TCHAR* SQLiteVersion() { return _T(SQLITE_VERSION); }
  140. bool IsDatabaseOpen() { return mpDB != NULL; }
  141. private:
  142. CppSQLite3DB(const CppSQLite3DB& db);
  143. CppSQLite3DB& operator=(const CppSQLite3DB& db);
  144. sqlite3_stmt* compile(const TCHAR* szSQL);
  145. void checkDB();
  146. sqlite3* mpDB;
  147. int mnBusyTimeoutMs;
  148. };
  149. #endif