db_sqlite3.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SQLite Database
  2. #include "db/db_sqlite3.h"
  3. #include <assert.h>
  4. static const char kNULL[] = "\0\0\0";
  5. namespace ndb
  6. {
  7. //////////////////////////////////////////////////////////////////////////////
  8. // SQLiteDB
  9. SQLiteDB::SQLiteDB()
  10. {
  11. sqlite3_ = NULL;
  12. }
  13. SQLiteDB::SQLiteDB(SQLiteDB& src)
  14. {
  15. sqlite3_ = src.sqlite3_;
  16. src.sqlite3_ = NULL;
  17. }
  18. SQLiteDB::~SQLiteDB()
  19. {
  20. Interrupt();
  21. Close();
  22. }
  23. SQLiteDB& SQLiteDB::operator=(SQLiteDB& src)
  24. {
  25. Interrupt();
  26. Close();
  27. sqlite3_ = src.sqlite3_;
  28. src.sqlite3_ = NULL;
  29. return *this;
  30. }
  31. bool SQLiteDB::Open(int flags)
  32. {
  33. return Open(":memory:", "", flags);
  34. }
  35. bool SQLiteDB::Open(const char* filename,
  36. const std::string &key,
  37. int flags/* = modeReadWrite|modeCreate|modeSerialized*/)
  38. {
  39. Interrupt();
  40. Close();
  41. if (filename == NULL)
  42. return false;
  43. int r = sqlite3_open_v2(filename, &sqlite3_, flags, NULL);
  44. if (r != SQLITE_OK)
  45. {
  46. Close();
  47. }
  48. else
  49. {
  50. if (key.length())
  51. {
  52. if( SQLITE_OK != sqlite3_key(sqlite3_,key.c_str(),(int)key.length()))
  53. r = sqlite3_rekey(sqlite3_,key.c_str(),(int)key.length());
  54. }
  55. }
  56. return r == SQLITE_OK;
  57. }
  58. bool SQLiteDB::Close()
  59. {
  60. if (sqlite3_ == NULL)
  61. return true;
  62. /*
  63. * If sqlite3_close() is called on a database connection that still has
  64. * outstanding prepared statements or BLOB handles, then it returns SQLITE_BUSY
  65. */
  66. if (SQLITE_OK != sqlite3_close(sqlite3_))
  67. return false;
  68. sqlite3_ = NULL;
  69. return true;
  70. }
  71. bool SQLiteDB::Compact()
  72. {
  73. if (SQLITE_OK != Query("VACUUM"))
  74. return false;
  75. return true;
  76. }
  77. bool SQLiteDB::IsValid() const
  78. {
  79. return sqlite3_ != NULL;
  80. }
  81. int SQLiteDB::GetLastErrorCode() const
  82. {
  83. if (sqlite3_ == NULL)
  84. return SQLITE_OK;
  85. return sqlite3_errcode(sqlite3_);
  86. }
  87. int SQLiteDB::GetLastExtendedErrorCode() const
  88. {
  89. if (sqlite3_ == NULL)
  90. return SQLITE_OK;
  91. return sqlite3_extended_errcode(sqlite3_);
  92. }
  93. const char* SQLiteDB::GetLastErrorMessage() const
  94. {
  95. assert(sqlite3_);
  96. if (sqlite3_ == NULL)
  97. return kNULL;
  98. return sqlite3_errmsg(sqlite3_);
  99. }
  100. int SQLiteDB::GetChanges() const
  101. {
  102. assert(sqlite3_);
  103. if (sqlite3_ == NULL)
  104. return -1;
  105. return sqlite3_changes(sqlite3_);
  106. }
  107. int SQLiteDB::GetTotalChanges() const
  108. {
  109. assert(sqlite3_);
  110. if (sqlite3_ == NULL)
  111. return -1;
  112. return sqlite3_total_changes(sqlite3_);
  113. }
  114. bool SQLiteDB::DoesTableExist(const char* table_name) const
  115. {
  116. return DoesTableOrIndexExist(table_name, "table");
  117. }
  118. bool SQLiteDB::DoesIndexExist(const char* index_name) const
  119. {
  120. return DoesTableOrIndexExist(index_name, "index");
  121. }
  122. bool SQLiteDB::DoesTableOrIndexExist(
  123. const char* name, const char* type) const
  124. {
  125. SQLiteStatement stat;
  126. Query(stat, "SELECT name FROM sqlite_master WHERE type=? AND name=?");
  127. if (!stat.IsValid())
  128. return false;
  129. // Bind is 1 based
  130. stat.BindText(1, type);
  131. stat.BindText(2, name);
  132. return stat.NextRow() == SQLITE_ROW;
  133. }
  134. bool SQLiteDB::DoesColumnExist(
  135. const char* table_name, const char* column_name) const
  136. {
  137. std::string sql("PRAGMA TABLE_INFO(");
  138. sql.append(table_name);
  139. sql.append(")");
  140. SQLiteStatement stat;
  141. Query(stat, sql.c_str(), (int)sql.size());
  142. if (!stat.IsValid())
  143. return false;
  144. while (stat.NextRow() == SQLITE_ROW) {
  145. if (!strcmp(stat.GetTextField(1), column_name))
  146. return true;
  147. }
  148. return false;
  149. }
  150. sqlite3_int64 SQLiteDB::GetLastInsertRowId() const
  151. {
  152. assert(sqlite3_);
  153. if (sqlite3_ == NULL)
  154. return -1;
  155. return sqlite3_last_insert_rowid(sqlite3_);
  156. }
  157. int SQLiteDB::Query(SQLiteStatement& statement, const char* sql_text, int length) const
  158. {
  159. statement.Finalize();
  160. if (!sqlite3_ || !sql_text)
  161. return SQLITE_MISUSE;
  162. /*
  163. * SQLite documents say:
  164. * If the caller knows that the supplied string is nul-terminated,
  165. * then there is a small performance advantage to be gained by passing an nByte parameter
  166. * that is equal to the number of bytes in the input string including the nul-terminator bytes.
  167. */
  168. if (length < 0)
  169. length = (int)strlen(sql_text) + 1;
  170. return sqlite3_prepare_v2(sqlite3_, sql_text, length, &statement.stmt_, NULL);
  171. }
  172. int SQLiteDB::Query(const char* sql_text, SQLiteQueryDelegate delegate, void* param) const
  173. {
  174. if (!sqlite3_ || !sql_text)
  175. return SQLITE_MISUSE;
  176. return sqlite3_exec(sqlite3_, sql_text, delegate, param, NULL);
  177. }
  178. int SQLiteDB::Query(const char* sql_text, SQLiteResultTable& table) const
  179. {
  180. if (!sqlite3_ || !sql_text)
  181. return SQLITE_MISUSE;
  182. // 先清空结果表
  183. table.Free();
  184. int r = sqlite3_get_table(sqlite3_, sql_text,
  185. &table.result_column_, &table.row_, &table.col_, NULL);
  186. if (r == SQLITE_OK)
  187. table.result_ = table.result_column_ + table.col_;
  188. return r;
  189. }
  190. void SQLiteDB::Interrupt()
  191. {
  192. if (!sqlite3_)
  193. return;
  194. sqlite3_interrupt(sqlite3_);
  195. }
  196. int SQLiteDB::SetBusyTimeout(int ms)
  197. {
  198. if (!sqlite3_)
  199. return SQLITE_MISUSE;
  200. return sqlite3_busy_timeout(sqlite3_, ms);
  201. }
  202. int SQLiteDB::GetVersion()
  203. {
  204. return sqlite3_libversion_number();
  205. }
  206. //////////////////////////////////////////////////////////////////////////////
  207. // SQLiteResultTable
  208. SQLiteResultTable::SQLiteResultTable()
  209. {
  210. Reset();
  211. }
  212. SQLiteResultTable::SQLiteResultTable(SQLiteResultTable& src)
  213. {
  214. row_ = src.row_;
  215. col_ = src.col_;
  216. result_ = src.result_;
  217. result_column_ = src.result_column_;
  218. src.Reset();
  219. }
  220. SQLiteResultTable::~SQLiteResultTable()
  221. {
  222. Free();
  223. }
  224. SQLiteResultTable& SQLiteResultTable::operator=(SQLiteResultTable& src)
  225. {
  226. row_ = src.row_;
  227. col_ = src.col_;
  228. result_ = src.result_;
  229. result_column_ = src.result_column_;
  230. src.Reset();
  231. return *this;
  232. }
  233. void SQLiteResultTable::Free()
  234. {
  235. if (result_column_ != NULL)
  236. {
  237. sqlite3_free_table(result_column_);
  238. Reset();
  239. }
  240. }
  241. void SQLiteResultTable::Reset()
  242. {
  243. row_ = 0;
  244. col_ = 0;
  245. result_ = NULL;
  246. result_column_ = NULL;
  247. }
  248. bool SQLiteResultTable::IsEmpty() const
  249. {
  250. return result_column_ == 0;
  251. }
  252. int SQLiteResultTable::GetRowCount() const
  253. {
  254. return row_;
  255. }
  256. int SQLiteResultTable::GetColumnCount() const
  257. {
  258. return col_;
  259. }
  260. const char* SQLiteResultTable::GetValue(int row, int col) const
  261. {
  262. if (!result_ || row >= row_ ||
  263. col >= col_ || row < 0 || col < 0)
  264. {
  265. return 0;
  266. }
  267. return result_[row * col_ + col];
  268. }
  269. const char** const SQLiteResultTable::operator[](int row) const
  270. {
  271. // 为了效率,这里不检查越界,就像STL那样
  272. return (const char**)result_ + row * col_;
  273. }
  274. const char* SQLiteResultTable::GetColumnName(int col) const
  275. {
  276. if (!result_column_ || col < 0 || col >= col_)
  277. return 0;
  278. return result_column_[col];
  279. }
  280. //////////////////////////////////////////////////////////////////////////////
  281. // SQLiteStatement
  282. SQLiteStatement::SQLiteStatement()
  283. {
  284. stmt_ = NULL;
  285. eof_ = true;
  286. }
  287. SQLiteStatement::SQLiteStatement(SQLiteStatement& src)
  288. {
  289. stmt_ = src.stmt_;
  290. eof_ = src.eof_;
  291. src.stmt_ = NULL;
  292. src.eof_ = true;
  293. }
  294. SQLiteStatement::~SQLiteStatement()
  295. {
  296. Finalize();
  297. }
  298. SQLiteStatement& SQLiteStatement::operator=(SQLiteStatement& src)
  299. {
  300. Finalize();
  301. stmt_ = src.stmt_;
  302. eof_ = src.eof_;
  303. src.stmt_ = NULL;
  304. src.eof_ = true;
  305. return *this;
  306. }
  307. int SQLiteStatement::Finalize()
  308. {
  309. int r = SQLITE_OK;
  310. if (stmt_ != NULL)
  311. {
  312. r = sqlite3_finalize(stmt_);
  313. if (r == SQLITE_OK)
  314. {
  315. stmt_ = NULL;
  316. eof_ = true;
  317. }
  318. }
  319. return r;
  320. }
  321. int SQLiteStatement::Rewind()
  322. {
  323. int r = SQLITE_MISUSE;
  324. if (stmt_ != NULL)
  325. {
  326. r = sqlite3_reset(stmt_);
  327. if (SQLITE_OK == r)
  328. {
  329. eof_ = false;
  330. return SQLITE_OK;
  331. }
  332. }
  333. return r;
  334. }
  335. int SQLiteStatement::NextRow()
  336. {
  337. if (stmt_ == NULL)
  338. return SQLITE_MISUSE;
  339. int r = sqlite3_step(stmt_);
  340. if (r == SQLITE_DONE)
  341. eof_ = true;
  342. return r;
  343. }
  344. bool SQLiteStatement::IsEof()
  345. {
  346. return eof_;
  347. }
  348. bool SQLiteStatement::IsNullField(int col)
  349. {
  350. return GetFieldValue(col) != NULL;
  351. }
  352. const void* SQLiteStatement::GetBlobField(int col)
  353. {
  354. if (stmt_ == NULL)
  355. return NULL;
  356. return sqlite3_column_blob(stmt_, col);
  357. }
  358. int SQLiteStatement::GetBytesField(int col)
  359. {
  360. if (stmt_ == NULL)
  361. return 0;
  362. return sqlite3_column_bytes(stmt_, col);
  363. }
  364. int SQLiteStatement::GetBytes16Field(int col)
  365. {
  366. if (stmt_ == NULL)
  367. return 0;
  368. return sqlite3_column_bytes16(stmt_, col);
  369. }
  370. double SQLiteStatement::GetDoubleField(int col)
  371. {
  372. if (stmt_ == NULL)
  373. return 0.0;
  374. return sqlite3_column_double(stmt_, col);
  375. }
  376. int SQLiteStatement::GetIntField(int col)
  377. {
  378. if (stmt_ == NULL)
  379. return 0;
  380. return sqlite3_column_int(stmt_, col);
  381. }
  382. sqlite3_int64 SQLiteStatement::GetInt64Field(int col)
  383. {
  384. if (stmt_ == NULL)
  385. return 0;
  386. return sqlite3_column_int64(stmt_, col);
  387. }
  388. const char* SQLiteStatement::GetTextField(int col)
  389. {
  390. if (stmt_ == NULL)
  391. return NULL;
  392. return (const char*)sqlite3_column_text(stmt_, col);
  393. }
  394. const void* SQLiteStatement::GetText16Field(int col)
  395. {
  396. if (stmt_ == NULL)
  397. return NULL;
  398. return sqlite3_column_text16(stmt_, col);
  399. }
  400. int SQLiteStatement::GetTypeField(int col)
  401. {
  402. if (stmt_ == NULL)
  403. return 0;
  404. return sqlite3_column_type(stmt_, col);
  405. }
  406. sqlite3_value* SQLiteStatement::GetFieldValue(int col)
  407. {
  408. if (stmt_ == NULL)
  409. return NULL;
  410. return sqlite3_column_value(stmt_, col);
  411. }
  412. int SQLiteStatement::GetFieldBytes(int col)
  413. {
  414. if (stmt_ == NULL)
  415. return 0;
  416. return sqlite3_column_bytes(stmt_, col);
  417. }
  418. int SQLiteStatement::GetFieldBytes16(int col)
  419. {
  420. if (stmt_ == NULL)
  421. return 0;
  422. return sqlite3_column_bytes16(stmt_, col);
  423. }
  424. int SQLiteStatement::BindBlob(int index, const void* data, int size_in_bytes)
  425. {
  426. if (stmt_ != NULL)
  427. return sqlite3_bind_blob(stmt_, index, data, size_in_bytes, SQLITE_STATIC);
  428. return SQLITE_MISUSE;
  429. }
  430. int SQLiteStatement::BindDouble(int index, double data)
  431. {
  432. if (stmt_ != NULL)
  433. return sqlite3_bind_double(stmt_, index, data);
  434. return SQLITE_MISUSE;
  435. }
  436. int SQLiteStatement::BindInt(int index, int data)
  437. {
  438. if (stmt_ != NULL)
  439. return sqlite3_bind_int(stmt_, index, data);
  440. return SQLITE_MISUSE;
  441. }
  442. int SQLiteStatement::BindInt64(int index, int64_t data)
  443. {
  444. if (stmt_ != NULL)
  445. return sqlite3_bind_int64(stmt_, index, data);
  446. return SQLITE_MISUSE;
  447. }
  448. int SQLiteStatement::BindNull(int index)
  449. {
  450. if (stmt_ != NULL)
  451. return sqlite3_bind_null(stmt_, index);
  452. return SQLITE_MISUSE;
  453. }
  454. int SQLiteStatement::BindText(int index, const char* data)
  455. {
  456. return BindText(index, data, (int)strlen(data));
  457. }
  458. int SQLiteStatement::BindText(int index, const char* data, int size_in_bytes)
  459. {
  460. if (stmt_ != NULL)
  461. return sqlite3_bind_text(stmt_, index, data, size_in_bytes, SQLITE_STATIC);
  462. return SQLITE_MISUSE;
  463. }
  464. int SQLiteStatement::BindText16(int index, const wchar_t* data)
  465. {
  466. return BindText16(index, data, (int)wcslen(data) * sizeof(wchar_t));
  467. }
  468. int SQLiteStatement::BindText16(int index, const wchar_t* data, int size_in_bytes)
  469. {
  470. if (stmt_ != NULL)
  471. return sqlite3_bind_text16(stmt_, index, data, size_in_bytes, SQLITE_STATIC);
  472. return SQLITE_MISUSE;
  473. }
  474. int SQLiteStatement::BindValue(int index, const sqlite3_value* data)
  475. {
  476. if (stmt_ != NULL && data != NULL)
  477. return sqlite3_bind_value(stmt_, index, data);
  478. return SQLITE_MISUSE;
  479. }
  480. int SQLiteStatement::BindZeroBlob(int index, int size)
  481. {
  482. if (stmt_ != NULL)
  483. return sqlite3_bind_zeroblob(stmt_, index, size);
  484. return SQLITE_MISUSE;
  485. }
  486. //////////////////////////////////////////////////////////////////////////////
  487. // SQLiteAutoTransaction
  488. SQLiteAutoTransaction::SQLiteAutoTransaction(SQLiteDB* db)
  489. : db_(db), need_commit_(false)
  490. {
  491. Begin();
  492. }
  493. SQLiteAutoTransaction::~SQLiteAutoTransaction()
  494. {
  495. if (need_commit_)
  496. Commit();
  497. }
  498. bool SQLiteAutoTransaction::Begin()
  499. {
  500. bool result = false;
  501. if (db_ && !need_commit_)
  502. {
  503. if (db_->Query("BEGIN") == SQLITE_OK)
  504. result = true;
  505. need_commit_ = true;
  506. }
  507. return result;
  508. }
  509. bool SQLiteAutoTransaction::Commit()
  510. {
  511. bool result = 0;
  512. if (db_ != NULL)
  513. {
  514. if (db_->Query("END") == SQLITE_OK)
  515. result = true;
  516. need_commit_ = false;
  517. }
  518. return result;
  519. }
  520. bool SQLiteAutoTransaction::Rollback()
  521. {
  522. bool result = false;
  523. if (db_ != NULL)
  524. {
  525. if (db_->Query("ROLLBACK") == SQLITE_OK)
  526. result = true;
  527. need_commit_ = false;
  528. }
  529. return result;
  530. }
  531. } // namespace ndb