DbSqlite.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*----------------------------------------------------------------------------*
  2. * DbSQLite.h
  3. *
  4. * 6 APR 2005
  5. *
  6. * This source code is derived from the SQLiteWrapper source produced by
  7. * rene.nyffenegger@adp-gmbh.ch. I altered the original source while
  8. * packaging it for use with Microsoft MFC.
  9. *
  10. * The primary objective in this exercise was to make the wrapper
  11. * suitable for both MCBS and Unicode because Unicdoe is native to the
  12. * Windows CE OS. There are three key differences between this wrapper
  13. * and the author's original source.
  14. *
  15. * First, all std::string variables were converted to Microsoft's generic
  16. * string pointers using LPCTSTR. This should be familiar to developer's
  17. * accustomed to working with MFC. This also means that you should rely
  18. * upon the the Microsoft TEXT or _T macros for hard-coded character strings.
  19. *
  20. * Second, I have change the class SQLiteStatement to CSqlStatement and
  21. * SQLiteWrapper to CDbSQLite. This was primarily a matter of preference
  22. * since most MFC developer's recoginize Microsoft's convention for the
  23. * CFunction nomenclature.
  24. *
  25. * Finally, I have added the header SQLite3i.h with typedefs to the various
  26. * sqlite3.h functions. These type definitions are "internal" accessors
  27. * to the sqlite3 functions utilizing either the UTF-8 or UTF-16 variation
  28. * as appropriate.
  29. *
  30. * The remaining comments at the beginning of this file are the author's
  31. * original copyright message.
  32. *----------------------------------------------------------------------------*
  33. * SQLiteWrapper.h
  34. *
  35. * Copyright (C) 2004 Ren?Nyffenegger
  36. *
  37. * This source code is provided 'as-is', without any express or implied
  38. * warranty. In no event will the author be held liable for any damages
  39. * arising from the use of this software.
  40. *
  41. * Permission is granted to anyone to use this software for any purpose,
  42. * including commercial applications, and to alter it and redistribute it
  43. * freely, subject to the following restrictions:
  44. *
  45. * 1. The origin of this source code must not be misrepresented; you must not
  46. * claim that you wrote the original source code. If you use this source code
  47. * in a product, an acknowledgment in the product documentation would be
  48. * appreciated but is not required.
  49. *
  50. * 2. Altered source versions must be plainly marked as such, and must not be
  51. * misrepresented as being the original source code.
  52. *
  53. * 3. This notice may not be removed or altered from any source distribution.
  54. *
  55. * Ren?Nyffenegger rene.nyffenegger@adp-gmbh.ch
  56. *----------------------------------------------------------------------------*/
  57. //#include "stdafx.h"
  58. #include "pch.h"
  59. #include "DbSQLite.h"
  60. // Default ctor.
  61. //
  62. CSqlStatement::CSqlStatement()
  63. {
  64. m_stmt = NULL;
  65. }
  66. CSqlStatement::CSqlStatement(LPCTSTR statement, sqlite3* db)
  67. {
  68. m_stmt = NULL;
  69. int rc = _sqlite3_prepare(db, statement, -1, &m_stmt, 0);
  70. if (rc != SQLITE_OK)
  71. {
  72. m_stmt = NULL;
  73. }
  74. }
  75. //
  76. // Do we really want to finalize or do we want to leave that up to
  77. // the user?
  78. //
  79. CSqlStatement::~CSqlStatement()
  80. {
  81. if (m_stmt)
  82. {
  83. sqlite3_finalize(m_stmt);
  84. }
  85. }
  86. bool
  87. CSqlStatement::Bind(int pos_zero_indexed, LPCTSTR value)
  88. {
  89. bool fResult = TRUE;
  90. CString v = CString(value);
  91. int rc = _sqlite3_bind_text(m_stmt, pos_zero_indexed + 1,
  92. v, v.GetLength(), SQLITE_TRANSIENT);
  93. if (rc != SQLITE_OK)
  94. {
  95. fResult = false;
  96. }
  97. return fResult;
  98. }
  99. bool
  100. CSqlStatement::Bind(int pos_zero_indexed, double value)
  101. {
  102. bool fResult = TRUE;
  103. int rc = _sqlite3_bind_double(m_stmt, pos_zero_indexed + 1, value);
  104. if (rc != SQLITE_OK)
  105. {
  106. fResult = false;
  107. }
  108. return fResult;
  109. }
  110. bool
  111. CSqlStatement::Bind(int pos_zero_indexed, int value)
  112. {
  113. bool fResult = TRUE;
  114. int rc = _sqlite3_bind_int(m_stmt, pos_zero_indexed + 1, value);
  115. if (rc != SQLITE_OK)
  116. {
  117. fResult = false;
  118. }
  119. return fResult;
  120. }
  121. bool
  122. CSqlStatement::BindNull(int pos_zero_indexed)
  123. {
  124. bool fResult = TRUE;
  125. int rc = _sqlite3_bind_null(m_stmt, pos_zero_indexed + 1);
  126. if (rc != SQLITE_OK)
  127. {
  128. fResult = false;
  129. }
  130. return fResult;
  131. }
  132. bool
  133. CSqlStatement::Execute()
  134. {
  135. CString szMessage;
  136. CString szCaption = _T("CSqlStatement::Execute");
  137. int rc = _sqlite3_step(m_stmt);
  138. if (rc == SQLITE_BUSY)
  139. {
  140. szMessage = _T("SQLITE_BUSY");
  141. ::MessageBox(0, szMessage, szCaption, MB_OK);
  142. return false;
  143. }
  144. if (rc == SQLITE_ERROR)
  145. {
  146. szMessage = _T("SQLITE_ERROR");
  147. ::MessageBox(0, szMessage, szCaption, MB_OK);
  148. return false;
  149. }
  150. if (rc == SQLITE_MISUSE)
  151. {
  152. szMessage = _T("SQLITE_MISUSE");
  153. ::MessageBox(0, szMessage, szCaption, MB_OK);
  154. return false;
  155. }
  156. if (rc != SQLITE_DONE)
  157. {
  158. //_sqlite3_reset(m_stmt);
  159. return false;
  160. }
  161. _sqlite3_reset(m_stmt);
  162. return true;
  163. }
  164. int
  165. CSqlStatement::Fields()
  166. {
  167. return _sqlite3_column_count(m_stmt);
  168. }
  169. LPCTSTR
  170. CSqlStatement::FieldName(int pos_zero_indexed)
  171. {
  172. m_szText = LPCTSTR(_sqlite3_column_name(m_stmt, pos_zero_indexed));
  173. return m_szText;
  174. }
  175. CSqlStatement::EDataType
  176. CSqlStatement::DataType(int pos_zero_indexed)
  177. {
  178. return EDataType(_sqlite3_column_type(m_stmt, pos_zero_indexed));
  179. }
  180. LPCTSTR
  181. CSqlStatement::FieldType(int pos_zero_indexed)
  182. {
  183. int dt = _sqlite3_column_type(m_stmt, pos_zero_indexed);
  184. switch (dt)
  185. {
  186. case SQLITE_INTEGER:
  187. m_szText = _T("INTEGER");
  188. break;
  189. case SQLITE_FLOAT:
  190. m_szText = _T("FLOAT");
  191. break;
  192. case SQLITE_TEXT:
  193. m_szText = _T("TEXT");
  194. break;
  195. case SQLITE_BLOB:
  196. m_szText = _T("BLOBL");
  197. break;
  198. case SQLITE_NULL:
  199. m_szText = _T("NULL");
  200. break;
  201. }
  202. return m_szText;
  203. }
  204. int
  205. CSqlStatement::ValueInt(int pos_zero_indexed)
  206. {
  207. return _sqlite3_column_int(m_stmt, pos_zero_indexed);
  208. }
  209. LPCTSTR
  210. CSqlStatement::ValueString(int pos_zero_indexed)
  211. {
  212. m_szText.Format(_T("%s"), _sqlite3_column_text(m_stmt, pos_zero_indexed));
  213. return m_szText;
  214. }
  215. bool
  216. CSqlStatement::RestartSelect()
  217. {
  218. _sqlite3_reset(m_stmt);
  219. return true;
  220. }
  221. bool
  222. CSqlStatement::Reset()
  223. {
  224. int rc = _sqlite3_step(m_stmt);
  225. _sqlite3_reset(m_stmt);
  226. if (rc == SQLITE_ROW) return true;
  227. return false;
  228. }
  229. bool
  230. CSqlStatement::NextRow()
  231. {
  232. CString szError;
  233. int rc = _sqlite3_step(m_stmt);
  234. if (rc == SQLITE_ROW)
  235. {
  236. return true;
  237. }
  238. if (rc == SQLITE_DONE)
  239. {
  240. _sqlite3_reset(m_stmt);
  241. return false;
  242. }
  243. else if (rc == SQLITE_MISUSE)
  244. {
  245. szError = _T("CSqlStatement::NextRow SQLITE_MISUSE");
  246. }
  247. else if (rc == SQLITE_BUSY)
  248. {
  249. szError = _T("CSqlStatement::NextRow SQLITE_BUSY");
  250. }
  251. else if (rc == SQLITE_ERROR)
  252. {
  253. szError = _T("CSqlStatement::NextRow SQLITE_ERROR");
  254. }
  255. ::MessageBox(0, szError, 0, 0);
  256. return false;
  257. }
  258. //
  259. // ctor
  260. //
  261. CDbSQLite::CDbSQLite()
  262. {
  263. m_db = NULL;
  264. }
  265. CDbSQLite::~CDbSQLite()
  266. {
  267. if (m_db)
  268. {
  269. sqlite3_close(m_db);
  270. }
  271. }
  272. bool
  273. CDbSQLite::Open(LPCTSTR db_file) //´ò¿ªÊý¾Ý¿â
  274. {
  275. bool fResult = TRUE;
  276. m_szName = db_file;
  277. int rc = _sqlite3_open(db_file, &m_db);
  278. if (rc != SQLITE_OK)
  279. {
  280. fResult = false;
  281. }
  282. return fResult;
  283. }
  284. //
  285. // Corrected memory leak.
  286. //
  287. bool
  288. CDbSQLite::DirectStatement(LPCTSTR stmt)
  289. {
  290. CSqlStatement* pStatement = this->Statement(stmt);
  291. bool fResult = pStatement->Execute();
  292. delete pStatement;
  293. return fResult;
  294. }
  295. bool
  296. CDbSQLite::SelectStatement(LPCTSTR stmt, ResultTable& res)
  297. {
  298. LPSTR lpsz = NULL;
  299. int len = 0;
  300. int rc = 0;
  301. char* errmsg = NULL;
  302. bool fResult = TRUE;
  303. res.reset();
  304. len = (int)_tcslen(stmt);
  305. lpsz = (LPSTR)LocalAlloc(LMEM_ZEROINIT, len + 2);
  306. // A UTF-16 version of sqlite_exec does not exist at this time,
  307. // so we will convert the string if necessary.
  308. #if defined(_UNICODE) || defined(UNICODE)
  309. ::WideCharToMultiByte(CP_ACP, 0, stmt, -1, lpsz, len, 0, 0);
  310. #else
  311. memcpy(lpsz, stmt, len);
  312. #endif
  313. rc = sqlite3_exec(m_db, lpsz, SelectCallback, static_cast<void*>(&res), &errmsg);
  314. if (rc != SQLITE_OK)
  315. {
  316. fResult = false;
  317. }
  318. LocalFree(lpsz);
  319. return fResult;
  320. }
  321. // TODO parameter p_col_names
  322. int
  323. CDbSQLite::SelectCallback(void *p_data, int num_fields, char **p_fields, char** /*p_col_names*/)
  324. {
  325. ResultTable* res = reinterpret_cast<ResultTable*>(p_data);
  326. ResultRecord record;
  327. for (int i = 0; i < num_fields; i++)
  328. {
  329. CString szField = CString(p_fields[i]);
  330. record.m_fields.push_back(szField);
  331. }
  332. res->m_records.push_back(record);
  333. return 0;
  334. }
  335. CSqlStatement*
  336. CDbSQLite::Statement(LPCTSTR statement)
  337. {
  338. CSqlStatement* stmt = NULL;
  339. stmt = new CSqlStatement(statement, m_db);
  340. return stmt;
  341. }
  342. LPCTSTR
  343. CDbSQLite::LastError()
  344. {
  345. m_szText = (LPCTSTR)_sqlite3_errmsg(m_db);
  346. return m_szText;
  347. }
  348. bool
  349. CDbSQLite::Begin()
  350. {
  351. m_szText = _T("begin");
  352. return DirectStatement(m_szText);
  353. }
  354. bool
  355. CDbSQLite::Commit()
  356. {
  357. m_szText = _T("commit");
  358. return DirectStatement(m_szText);
  359. }
  360. bool
  361. CDbSQLite::Rollback()
  362. {
  363. m_szText = _T("rollback");
  364. return DirectStatement(m_szText);
  365. }
  366.