SQLiteDB.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "SQLiteDB.h"
  4. #include <wtypes.h>
  5. #include "DBStoreBase.h"
  6. #include "DBTableBase.h"
  7. namespace OTSSQLITE
  8. {
  9. CSQLiteDB::CSQLiteDB(CDBStoreBasePtr a_datastore,CDBTableBasePtr a_tableInfo)
  10. {
  11. m_datastorePtr = a_datastore;
  12. m_tableInfo = a_tableInfo;
  13. }
  14. CSQLiteDB::~CSQLiteDB(void)
  15. {
  16. }
  17. BOOL CSQLiteDB::Init(const BOOL a_bClean /*= FALSE*/)
  18. {
  19. if (IsDBExist())
  20. {
  21. if (a_bClean)
  22. {
  23. DeleteTable();
  24. }
  25. else
  26. {
  27. return TRUE;
  28. }
  29. }
  30. return CreateTable();
  31. }
  32. BOOL CSQLiteDB::IsDBExist()
  33. {
  34. auto datastorePtr = GetDatastore();
  35. ASSERT(datastorePtr);
  36. if (!datastorePtr)
  37. {
  38. return FALSE;
  39. }
  40. auto tableInfoPtr = GetTableInfo();
  41. ASSERT(tableInfoPtr);
  42. if (!tableInfoPtr)
  43. {
  44. return FALSE;
  45. }
  46. return datastorePtr->IsTableExists(tableInfoPtr->GetTableName());
  47. }
  48. CDBQueryBasePtr CSQLiteDB::GetTableQuery(LPCTSTR a_sOrderColumnName /*= nullptr*/)
  49. {
  50. auto datastorePtr = GetDatastore();
  51. ASSERT(datastorePtr);
  52. if (!datastorePtr)
  53. {
  54. return CDBQueryBasePtr();
  55. }
  56. auto tableInfoPtr = GetTableInfo();
  57. ASSERT(tableInfoPtr);
  58. if (!tableInfoPtr) { return CDBQueryBasePtr(); }
  59. CString sTableName = tableInfoPtr->GetTableName();
  60. if (!datastorePtr->IsTableExists(sTableName))
  61. {
  62. return CDBQueryBasePtr();
  63. }
  64. auto query = datastorePtr->QueryByTableName(sTableName, a_sOrderColumnName);
  65. ASSERT(query);
  66. if (!query || !query->IsValid())
  67. {
  68. LogErrorTrace(__FILE__, __LINE__, _T("Invalid table(%s) query command."), (LPCTSTR)tableInfoPtr->GetTableName());
  69. return CDBQueryBasePtr();
  70. }
  71. // do the table related valid checking
  72. auto cols1 = query->GetColCount();
  73. auto cols2 = tableInfoPtr->GetColumnCount();
  74. if (cols1!=cols2)
  75. {
  76. LogErrorTrace(__FILE__, __LINE__, _T(sTableName+":Invalid table columns(%d): should be %d"), query->GetColCount(), tableInfoPtr->GetColumnCount());
  77. }
  78. for (int i = 0; i < cols1; i++)
  79. {
  80. if (query->GetColName(i) != tableInfoPtr->GetColumnName(i))
  81. {
  82. AfxMessageBox(_T("The culumn name define error. Please adjust and try again!"));
  83. LogErrorTrace(__FILE__, __LINE__, _T("The culumn define is incorrect."));
  84. break;
  85. }
  86. }
  87. return query;
  88. }
  89. CDBQueryBasePtr CSQLiteDB::GetCommandStringQuery(LPCTSTR a_commandStr /*= nullptr*/)
  90. {
  91. auto datastorePtr = GetDatastore();
  92. ASSERT(datastorePtr);
  93. auto query = datastorePtr->QueryByCommand(a_commandStr);
  94. ASSERT(query);
  95. return query;
  96. }
  97. OTSSQLITE::CDBTableBasePtr CSQLiteDB::GetTableInfo()
  98. {
  99. return m_tableInfo;
  100. }
  101. BOOL CSQLiteDB::CreateTable(const BOOL a_bForce /*= FALSE*/)
  102. {
  103. auto datastorePtr = GetDatastore();
  104. ASSERT(datastorePtr);
  105. if (!datastorePtr)
  106. {
  107. return FALSE;
  108. }
  109. auto tableInfoPtr = GetTableInfo();
  110. ASSERT(tableInfoPtr);
  111. if (!tableInfoPtr)
  112. {
  113. return FALSE;
  114. }
  115. if (datastorePtr->IsTableExists(tableInfoPtr->GetTableName()))
  116. {
  117. if (a_bForce)
  118. {
  119. if (!DeleteTable())
  120. {
  121. LogErrorTrace(__FILE__, __LINE__, _T("CreateTable:delete table (%s) failed."), (LPCTSTR)tableInfoPtr->GetTableName());
  122. ASSERT(FALSE);
  123. return FALSE;
  124. }
  125. }
  126. else
  127. {
  128. return TRUE;
  129. }
  130. }
  131. // create table
  132. CString sSQLCommand = tableInfoPtr->GetCreateTableCommandString();
  133. return datastorePtr->RunCommand(sSQLCommand);
  134. }
  135. BOOL CSQLiteDB::DeleteTable()
  136. {
  137. auto datastorePtr = GetDatastore();
  138. ASSERT(datastorePtr);
  139. if (!datastorePtr)
  140. {
  141. return FALSE;
  142. }
  143. auto tableInfoPtr = GetTableInfo();
  144. ASSERT(tableInfoPtr);
  145. if (!tableInfoPtr)
  146. {
  147. return FALSE;
  148. }
  149. if (!datastorePtr->IsTableExists(tableInfoPtr->GetTableName()))
  150. {
  151. return TRUE;
  152. }
  153. // create table
  154. CString sSQLCommand = tableInfoPtr->GetDeleteTableCommandString();
  155. return datastorePtr->RunCommand(sSQLCommand);
  156. }
  157. // Removes all rows.
  158. BOOL CSQLiteDB::RemoveAllRows()
  159. {
  160. auto datastorePtr = GetDatastore();
  161. ASSERT(datastorePtr);
  162. if (!datastorePtr)
  163. {
  164. return FALSE;
  165. }
  166. auto tableInfoPtr = GetTableInfo();
  167. ASSERT(tableInfoPtr);
  168. if (!tableInfoPtr)
  169. {
  170. return FALSE;
  171. }
  172. if (!datastorePtr->IsTableExists(tableInfoPtr->GetTableName()))
  173. {
  174. LogInfoTrace(__FILE__,__LINE__,_T("Didn't remove all rows from table(%s): table not exist"), tableInfoPtr->GetTableName());
  175. return TRUE;
  176. }
  177. // create table
  178. CString sSQLCommand = tableInfoPtr->GetRemoveAllRowsCommandString();
  179. return datastorePtr->RunCommand(sSQLCommand);
  180. }
  181. CString CSQLiteDB::GetReadColumnError(const int a_nColId)
  182. {
  183. auto tableInfoPtr = GetTableInfo();
  184. ASSERT(tableInfoPtr);
  185. if (!tableInfoPtr)
  186. {
  187. return _T("");
  188. }
  189. CString sColumnName = tableInfoPtr->GetColumnName(a_nColId);
  190. CString sErrorMessage;
  191. sErrorMessage.Format(_T("Read column(%s) from table(%s) failed"), (LPCTSTR)sColumnName, (LPCTSTR)tableInfoPtr->GetTableName());
  192. return sErrorMessage;
  193. }
  194. OTSSQLITE::CDBBasePtr CreateNewSQLiteDB(CDBStoreBasePtr a_datastore, CDBTableBasePtr a_tableInfo)
  195. {
  196. return CDBBasePtr(new CSQLiteDB(a_datastore,a_tableInfo));
  197. }
  198. }