DbSqlite.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "sqlite3.h"
  2. #ifndef _DB_SQLITE_H_
  3. #define _DB_SQLITE_H_
  4. /*----------------------------------------------------------------------------*
  5. * DbSQLite.h
  6. *
  7. * 6 APR 2005
  8. *
  9. * This source code is derived from the SQLiteWrapper source produced by
  10. * rene.nyffenegger@adp-gmbh.ch. I altered the original source while
  11. * packaging it for use with Microsoft MFC.
  12. *
  13. * The primary objective in this exercise was to make the wrapper
  14. * suitable for both MCBS and Unicode because Unicdoe is native to the
  15. * Windows CE OS. There are three key differences between this wrapper
  16. * and the author's original source.
  17. *
  18. * First, all std::string variables were converted to Microsoft's generic
  19. * string pointers using LPCTSTR. This should be familiar to developer's
  20. * accustomed to working with MFC. This also means that you should rely
  21. * upon the the Microsoft TEXT or _T macros for hard-coded character strings.
  22. *
  23. * Second, I have change the class SQLiteStatement to CSqlStatement and
  24. * SQLiteWrapper to CDbSQLite. This was primarily a matter of preference
  25. * since most MFC developer's recoginize Microsoft's convention for the
  26. * CFunction nomenclature.
  27. *
  28. * Finally, I have added the header SQLite3i.h with typedefs to the various
  29. * sqlite3.h functions. These type definitions are "internal" accessors
  30. * to the sqlite3 functions utilizing either the UTF-8 or UTF-16 variation
  31. * as appropriate.
  32. *
  33. * The remaining comments at the beginning of this file are the author's
  34. * original copyright message.
  35. *----------------------------------------------------------------------------*
  36. * SQLiteWrapper.h
  37. *
  38. * Copyright (C) 2004 Ren?Nyffenegger
  39. *
  40. * This source code is provided 'as-is', without any express or implied
  41. * warranty. In no event will the author be held liable for any damages
  42. * arising from the use of this software.
  43. *
  44. * Permission is granted to anyone to use this software for any purpose,
  45. * including commercial applications, and to alter it and redistribute it
  46. * freely, subject to the following restrictions:
  47. *
  48. * 1. The origin of this source code must not be misrepresented; you must not
  49. * claim that you wrote the original source code. If you use this source code
  50. * in a product, an acknowledgment in the product documentation would be
  51. * appreciated but is not required.
  52. *
  53. * 2. Altered source versions must be plainly marked as such, and must not be
  54. * misrepresented as being the original source code.
  55. *
  56. * 3. This notice may not be removed or altered from any source distribution.
  57. *
  58. * Ren?Nyffenegger rene.nyffenegger@adp-gmbh.ch
  59. *----------------------------------------------------------------------------*/
  60. #include <vector>
  61. #include "sqlite3.h"
  62. #include "sqlite3i.h"
  63. #include <WTypesbase.h>
  64. #include <afxstr.h>
  65. class CSqlStatement
  66. {
  67. public:
  68. CSqlStatement();
  69. // CSqlStatement(const CSqlStatement&);
  70. virtual ~CSqlStatement();
  71. private:
  72. //
  73. // CSqlStatement's ctor only to be called by CDbSQLite
  74. //
  75. friend class CDbSQLite;
  76. CSqlStatement(LPCTSTR statement, sqlite3* db);
  77. public:
  78. typedef enum
  79. {
  80. eInteger = SQLITE_INTEGER,
  81. eFloat = SQLITE_FLOAT,
  82. eText = SQLITE_TEXT,
  83. eBlob = SQLITE_BLOB,
  84. eNull = SQLITE_NULL,
  85. }
  86. EDataType;
  87. EDataType DataType(int pos_zero_indexed);
  88. int Fields();
  89. LPCTSTR FieldName(int pos_zero_indexed);
  90. LPCTSTR FieldType(int pos_zero_indexed);
  91. int ValueInt(int pos_zero_indexed);
  92. LPCTSTR ValueString(int pos_zero_indexed);
  93. bool Bind(int pos_zero_indexed, LPCTSTR value);
  94. bool Bind(int pos_zero_indexed, double value);
  95. bool Bind(int pos_zero_indexed, int value);
  96. bool BindNull(int pos_zero_indexed);
  97. bool Execute();
  98. bool NextRow();
  99. // Call Reset if not depending on the NextRow cleaning up.
  100. // For example for select count(*) statements.
  101. bool Reset();
  102. bool RestartSelect();
  103. private:
  104. sqlite3_stmt* m_stmt;
  105. CString m_szText;
  106. };
  107. class CDbSQLite
  108. {
  109. public:
  110. CDbSQLite();
  111. virtual ~CDbSQLite();
  112. bool Open(LPCTSTR db_file);
  113. class ResultRecord
  114. {
  115. public:
  116. std::vector<LPCTSTR> m_fields;
  117. };
  118. class ResultTable
  119. {
  120. friend class CDbSQLite;
  121. public:
  122. ResultTable() : ptr_cur_record_(0) {}
  123. std::vector<ResultRecord> m_records;
  124. ResultRecord* next()
  125. {
  126. if (ptr_cur_record_ < m_records.size())
  127. {
  128. return &(m_records[ptr_cur_record_++]);
  129. }
  130. return 0;
  131. }
  132. private:
  133. void reset()
  134. {
  135. m_records.clear();
  136. ptr_cur_record_ = 0;
  137. }
  138. private:
  139. unsigned int ptr_cur_record_;
  140. };
  141. bool SelectStatement(LPCTSTR stmt, ResultTable& res);
  142. bool DirectStatement(LPCTSTR stmt);
  143. CSqlStatement* Statement(LPCTSTR stmt);
  144. LPCTSTR LastError();
  145. //
  146. // Transaction related
  147. //
  148. bool Begin();
  149. bool Commit();
  150. bool Rollback();
  151. private:
  152. static int SelectCallback(void *p_data, int num_fields, char **p_fields, char **p_col_names);
  153. sqlite3* m_db;
  154. CString m_szName;
  155. CString m_szText;
  156. };
  157. #endif
  158.