SQLiteQuery.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "stdafx.h"
  2. #include "SQLiteQuery.h"
  3. #include "SQLiteStore.h"
  4. namespace OTSSQLITE
  5. {
  6. CSQLiteQuery::CSQLiteQuery(CppSQLite3QueryPtr a_query)
  7. : m_query(a_query)
  8. {
  9. }
  10. CSQLiteQuery::~CSQLiteQuery(void)
  11. {
  12. }
  13. int CSQLiteQuery::GetColCount()
  14. {
  15. if (m_query)
  16. {
  17. try
  18. {
  19. return m_query->numFields();
  20. }
  21. catch (CppSQLite3Exception& ex)
  22. {
  23. LogErrorTrace(__FILE__,__LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  24. ASSERT(FALSE);
  25. }
  26. }
  27. return -1;
  28. }
  29. int CSQLiteQuery::GetColType(const int a_nColIndex)
  30. {
  31. if (m_query)
  32. {
  33. try
  34. {
  35. return m_query->fieldDataType(a_nColIndex);
  36. }
  37. catch (CppSQLite3Exception& ex)
  38. {
  39. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  40. ASSERT(FALSE);
  41. }
  42. }
  43. return -1;
  44. }
  45. CString CSQLiteQuery::GetColName(const int a_nColIndex)
  46. {
  47. if (m_query)
  48. {
  49. try
  50. {
  51. return m_query->fieldName(a_nColIndex);
  52. }
  53. catch (CppSQLite3Exception& ex)
  54. {
  55. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  56. ASSERT(FALSE);
  57. }
  58. }
  59. return _T("");
  60. }
  61. CString CSQLiteQuery::GetColValue(const int a_nColIndex)
  62. {
  63. if (m_query)
  64. {
  65. try
  66. {
  67. return m_query->fieldValue(a_nColIndex);
  68. }
  69. catch (CppSQLite3Exception& ex)
  70. {
  71. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  72. ASSERT(FALSE);
  73. }
  74. }
  75. return _T("");
  76. }
  77. int CSQLiteQuery::GetColIntValue(const int a_nColIndex, const int a_nNullValue /*= 0*/)
  78. {
  79. if (m_query)
  80. {
  81. try
  82. {
  83. return m_query->getIntField(a_nColIndex, a_nNullValue);
  84. }
  85. catch (CppSQLite3Exception& ex)
  86. {
  87. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  88. ASSERT(FALSE);
  89. }
  90. }
  91. return a_nNullValue;
  92. }
  93. double CSQLiteQuery::GetColFloatValue(const int a_nColIndex, const double a_dNullValue /*= 0.0*/)
  94. {
  95. if (m_query)
  96. {
  97. try
  98. {
  99. return m_query->getFloatField(a_nColIndex, a_dNullValue);
  100. }
  101. catch (CppSQLite3Exception& ex)
  102. {
  103. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  104. ASSERT(FALSE);
  105. }
  106. }
  107. return a_dNullValue;
  108. }
  109. CString CSQLiteQuery::GetColStringValue(const int a_nColIndex, LPCTSTR a_sNullValue /*= _T("")*/)
  110. {
  111. if (m_query)
  112. {
  113. try
  114. {
  115. return m_query->getStringField(a_nColIndex, a_sNullValue);
  116. }
  117. catch (CppSQLite3Exception& ex)
  118. {
  119. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  120. ASSERT(FALSE);
  121. }
  122. }
  123. return a_sNullValue;
  124. }
  125. const unsigned char* CSQLiteQuery::GetColBlobValue(const int a_nColIndex, int& a_nLen)
  126. {
  127. if (m_query)
  128. {
  129. try
  130. {
  131. return m_query->getBlobField(a_nColIndex, a_nLen);
  132. }
  133. catch (CppSQLite3Exception& ex)
  134. {
  135. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  136. ASSERT(FALSE);
  137. }
  138. }
  139. return nullptr;
  140. }
  141. BOOL CSQLiteQuery::IsColNull(const int a_nColIndex)
  142. {
  143. if (m_query)
  144. {
  145. try
  146. {
  147. return m_query->fieldIsNull(a_nColIndex) ? TRUE : FALSE;
  148. }
  149. catch (CppSQLite3Exception& ex)
  150. {
  151. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  152. ASSERT(FALSE);
  153. }
  154. }
  155. return TRUE;
  156. }
  157. BOOL CSQLiteQuery::IsValid()
  158. {
  159. return (m_query) ? TRUE : FALSE;
  160. }
  161. BOOL CSQLiteQuery::IsEOF()
  162. {
  163. if (m_query)
  164. {
  165. try
  166. {
  167. return m_query->eof() ? TRUE : FALSE;
  168. }
  169. catch (CppSQLite3Exception& ex)
  170. {
  171. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  172. _ASSERT(FALSE);
  173. }
  174. }
  175. ASSERT(FALSE);
  176. return TRUE;
  177. }
  178. BOOL CSQLiteQuery::NextRow()
  179. {
  180. if (m_query)
  181. {
  182. try
  183. {
  184. m_query->nextRow();
  185. return TRUE;
  186. }
  187. catch (CppSQLite3Exception& ex)
  188. {
  189. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  190. ASSERT(FALSE);
  191. }
  192. }
  193. ASSERT(FALSE);
  194. return FALSE;
  195. }
  196. void CSQLiteQuery::Close()
  197. {
  198. if (m_query)
  199. {
  200. try
  201. {
  202. m_query->finalize();
  203. }
  204. catch (CppSQLite3Exception& ex)
  205. {
  206. LogErrorTrace(__FILE__, __LINE__, CSQLiteStore::GetExceptionErrorString(ex));
  207. ASSERT(FALSE);
  208. }
  209. }
  210. }
  211. }