db_sqlite3.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SQLite Database
  2. #ifndef DB_DB_SQLITE3_H_
  3. #define DB_DB_SQLITE3_H_
  4. #ifndef SQLITE_HAS_CODEC
  5. #define SQLITE_HAS_CODEC
  6. #endif // SQLITE_HAS_CODEC
  7. #include <string>
  8. #include "base/base_types.h"
  9. #include "db/db_export.h"
  10. #if defined(OS_WIN)
  11. #include "../../third_party/wxsqlite3/sqlite3/include/sqlite3.h"
  12. #else
  13. #include <sqlite3.h>
  14. #endif
  15. namespace ndb
  16. {
  17. typedef int (*SQLiteQueryDelegate)(void*, int, char**, char**);
  18. class SQLiteDB;
  19. /*
  20. * Purpose Query result class
  21. */
  22. class DB_EXPORT SQLiteResultTable
  23. {
  24. friend class SQLiteDB;
  25. public:
  26. SQLiteResultTable();
  27. SQLiteResultTable(SQLiteResultTable& src);
  28. virtual ~SQLiteResultTable();
  29. SQLiteResultTable& operator=(SQLiteResultTable& src);
  30. int GetRowCount() const;
  31. int GetColumnCount() const;
  32. const char* GetValue(int row, int col) const;
  33. const char** const operator[](int row) const;
  34. const char* GetColumnName(int col) const;
  35. bool IsEmpty() const;
  36. void Free();
  37. private:
  38. void Reset();
  39. int row_;
  40. int col_;
  41. char** result_column_;
  42. char** result_;
  43. };
  44. /*
  45. * Purpose Query statement class
  46. */
  47. class DB_EXPORT SQLiteStatement
  48. {
  49. friend class SQLiteDB;
  50. public:
  51. SQLiteStatement();
  52. SQLiteStatement(SQLiteStatement& src);
  53. virtual ~SQLiteStatement();
  54. SQLiteStatement& operator=(SQLiteStatement& src);
  55. int Finalize();
  56. bool IsValid() const { return !!stmt_; }
  57. public:
  58. /*
  59. * Purpose Before the first result
  60. */
  61. int Rewind();
  62. /*
  63. * Purpose Execute SQL command and return the next row result
  64. * Return SQLITE_DONE means execute finish, you should reset the statement before execute again.
  65. * SQLITE_ROW means return row result, you can get value by statement method.
  66. * SQLITE_ERROR something wrong
  67. * SQLITE_BUSY db is busy, if it happend on update operation you should redo the sql command or rollback.
  68. */
  69. int NextRow();
  70. /*
  71. * Purpose Check the record is at the end or not.
  72. */
  73. bool IsEof();
  74. /*
  75. * Purpose Get data from column£¨http://www.sqlite.org/c3ref/column_blob.html£©
  76. * col The index of the column in the result table based 0.
  77. */
  78. bool IsNullField(int col);
  79. const void* GetBlobField(int col);
  80. int GetBytesField(int col);
  81. int GetBytes16Field(int col);
  82. double GetDoubleField(int col);
  83. int GetIntField(int col);
  84. sqlite3_int64 GetInt64Field(int col);
  85. const char* GetTextField(int col);
  86. const void* GetText16Field(int col);
  87. int GetTypeField(int col);
  88. sqlite3_value* GetFieldValue(int col);
  89. /*
  90. * Purpose Get length of a column£¨http://www.sqlite.org/c3ref/column_blob.html£©
  91. */
  92. int GetFieldBytes(int col);
  93. int GetFieldBytes16(int col);
  94. /*
  95. * Purpose Bind parameter to SQL command£¨http://www.sqlite.org/c3ref/bind_blob.html£©
  96. * index The index of the column to bind based 1 (NOTE: not 0!!!)
  97. * Remark 'size' parameter of BindText/BindText16 is the bytes (not the characters) of the data
  98. */
  99. int BindBlob(int index, const void* data, int size_in_bytes);
  100. int BindDouble(int index, double data);
  101. int BindInt(int index, int data);
  102. int BindInt64(int index, int64_t data);
  103. int BindNull(int index);
  104. int BindText(int index, const char* data);
  105. int BindText(int index, const char* data, int size_in_bytes);
  106. int BindText16(int index, const wchar_t* data);
  107. int BindText16(int index, const wchar_t* data, int size_in_bytes);
  108. int BindValue(int index, const sqlite3_value* data);
  109. int BindZeroBlob(int index, int size);
  110. private:
  111. bool eof_;
  112. sqlite3_stmt* stmt_;
  113. };
  114. /*
  115. * Purpose Auto transaction class
  116. * Remark The transaction will be update when the object destruct.
  117. */
  118. class DB_EXPORT SQLiteAutoTransaction
  119. {
  120. public:
  121. SQLiteAutoTransaction(SQLiteDB* db);
  122. virtual ~SQLiteAutoTransaction();
  123. bool Begin();
  124. bool Commit();
  125. bool Rollback();
  126. private:
  127. SQLiteDB* db_;
  128. bool need_commit_;
  129. };
  130. /*
  131. * Purpose DB operation by sqlite3
  132. * Remark UTF-8 encoding
  133. */
  134. class DB_EXPORT SQLiteDB
  135. {
  136. friend class SQLiteStatement;
  137. public:
  138. enum OpenFlags
  139. {
  140. modeReadOnly = SQLITE_OPEN_READONLY, // Read only
  141. modeReadWrite = SQLITE_OPEN_READWRITE, // Read and write mode, it will failed when the file is not exist
  142. modeCreate = SQLITE_OPEN_CREATE, // If the file not exist then create it. you can use it only with modeReadWrite
  143. modeMultiThread = SQLITE_OPEN_NOMUTEX, // No mutex mode, different thread can operate different db connection
  144. modeSerialized = SQLITE_OPEN_FULLMUTEX, // Mutex mode, different thread can operate one db connection
  145. // modeSharedCache = SQLITE_OPEN_SHAREDCACHE, // Share cache
  146. };
  147. SQLiteDB();
  148. SQLiteDB(SQLiteDB& src);
  149. virtual ~SQLiteDB();
  150. SQLiteDB& operator=(SQLiteDB& src);
  151. /*
  152. * Purpose Open a database file
  153. * flags Open flag
  154. */
  155. bool Open(int flags = modeReadWrite|modeCreate|modeSerialized);
  156. /*
  157. * Purpose Open/Create an database file
  158. * filename Database file name
  159. * ":memory:" means open a memory database
  160. * NULL means open a temporarily database and it will be deleted when close it
  161. * key support encrypt key with max length: 32 characters, see function:
  162. * CodecGenerateEncryptionKey(Codec* codec, char* userPassword, int passwordLength,
  163. * unsigned char encryptionKey[KEYLENGTH])
  164. * in file: wxsqlite3\sqlite3\secure\src\codec.c
  165. * flags Open flag
  166. */
  167. bool Open(const char* filename,
  168. const std::string &key,
  169. int flags = modeReadWrite|modeCreate|modeSerialized);
  170. /*
  171. * Purpose Close database
  172. */
  173. bool Close();
  174. /*
  175. * Purpose Compact database for memory fragment
  176. */
  177. bool Compact();
  178. bool IsValid() const;
  179. int GetLastErrorCode() const;
  180. int GetLastExtendedErrorCode() const;
  181. const char* GetLastErrorMessage() const;
  182. int GetChanges() const;
  183. int GetTotalChanges() const;
  184. sqlite3_int64 GetLastInsertRowId() const;
  185. bool DoesTableExist(const char* table_name) const;
  186. bool DoesIndexExist(const char* index_name) const;
  187. bool DoesColumnExist(const char* table_name, const char* column_name) const;
  188. /*
  189. * Purpose Execute the SQL command and prepare return result
  190. * statement Return result, you can call NextRow() to get real result
  191. * sql_text SQL command
  192. * length Length of SQL command.
  193. * Remark SQLite command£ºhttp://www.sqlite.org/lang.html
  194. * Transaction is recommended.
  195. */
  196. int Query(SQLiteStatement& statement, const char* sql_text, int length = -1) const;
  197. /*
  198. * Purpose Execute the SQL command.
  199. * sql_text SQL command
  200. * delegate Result callback
  201. * param Custom parameter
  202. */
  203. int Query(const char* sql_text, SQLiteQueryDelegate delegate = NULL, void* param = NULL) const;
  204. /*
  205. * Purpose Execute the SQL command and put the result into result table.
  206. * sql_text SQL command
  207. * table Result table
  208. * Remark This function is low efficiency
  209. */
  210. int Query(const char* sql_text, SQLiteResultTable& table) const;
  211. /*
  212. * Purpose Interrupt all the operation
  213. */
  214. void Interrupt();
  215. /*
  216. * Purpose Set the waiting time that will return SQLITE_BUSY when the database is locked by a thread.
  217. * ms Waiting time, the function will disable when ms is less then 0.
  218. */
  219. int SetBusyTimeout(int ms);
  220. /*
  221. * Purpose SQLite Version
  222. */
  223. static int GetVersion();
  224. private:
  225. bool DoesTableOrIndexExist(const char* name, const char* type) const;
  226. mutable sqlite3* sqlite3_;
  227. };
  228. } // namespace ndb
  229. #endif // DB_DB_SQLITE3_H_