InformationDB.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "InformationDB.h"
  4. #include "InformationTable.h"
  5. #include<stdlib.h>
  6. namespace OTSSQLITE
  7. {
  8. CInformationDB::CInformationDB(CDBStoreBasePtr a_datastore)
  9. {
  10. m_tableInfo.reset(new CInformationTable());
  11. myDB = CreateNewSQLiteDB(a_datastore,m_tableInfo);
  12. }
  13. CInformationDB::~CInformationDB(void)
  14. {
  15. }
  16. BOOL CInformationDB::DeleteRow(LPCTSTR a_sItemName)
  17. {
  18. ASSERT(a_sItemName);
  19. if (!a_sItemName)
  20. {
  21. LogErrorTrace(__FILE__, __LINE__, _T("DeleteRow:invalid item name"));
  22. return FALSE;
  23. }
  24. auto datastorePtr = GetDatastore();
  25. ASSERT(datastorePtr);
  26. if (!datastorePtr)
  27. {
  28. LogErrorTrace(__FILE__, __LINE__, _T("DeleteRow:invalid data base store."));
  29. return FALSE;
  30. }
  31. auto tableInfoPtr = GetTableInfo();
  32. CString sNameColumnName = tableInfoPtr->GetColumnName((int)CInformationTable::ColumnID::ITEM - (int)CInformationTable::ColumnID::MIN);
  33. CString sSQLCommand;
  34. sSQLCommand.Format(_T("DELETE FROM \'%s\' WHERE %s = \'%s\'"), (LPCTSTR)tableInfoPtr->GetTableName(), (LPCTSTR)sNameColumnName, a_sItemName);
  35. return datastorePtr->RunCommand(sSQLCommand);
  36. }
  37. BOOL CInformationDB::InsertRow(LPCTSTR a_sItemName, LPCTSTR a_sItemContent, LPCTSTR a_sItemComment /*= nullptr*/)
  38. {
  39. ASSERT(a_sItemName);
  40. if (!a_sItemName)
  41. {
  42. LogErrorTrace(__FILE__, __LINE__, _T("InsertRow:invalid item name"));
  43. return FALSE;
  44. }
  45. ASSERT(a_sItemContent);
  46. if (!a_sItemContent)
  47. {
  48. LogErrorTrace(__FILE__, __LINE__, _T("InsertRow:invalid item value"));
  49. return FALSE;
  50. }
  51. auto datastorePtr = GetDatastore();
  52. ASSERT(datastorePtr);
  53. if (!datastorePtr)
  54. {
  55. LogErrorTrace(__FILE__, __LINE__, _T("InsertRow:invalid data base store."));
  56. return FALSE;
  57. }
  58. CString sComment("NULL");
  59. if (a_sItemComment)
  60. {
  61. sComment.Format(_T("%s"), a_sItemComment);
  62. }
  63. CString sFormat = GetTableInfo()->GetInsertCommandFormatString();
  64. CString sSQLCommand;
  65. sSQLCommand.Format(sFormat,
  66. a_sItemName,
  67. a_sItemContent,
  68. sComment);
  69. return datastorePtr->RunCommand(sSQLCommand);
  70. }
  71. BOOL CInformationDB::UpdateRow(LPCTSTR a_sItemName, LPCTSTR a_sItemContent, LPCTSTR a_sItemComment /*= nullptr*/)
  72. {
  73. ASSERT(a_sItemName);
  74. if (!a_sItemName)
  75. {
  76. LogErrorTrace(__FILE__, __LINE__, _T("UpdateRow:invalid item name"));
  77. return FALSE;
  78. }
  79. ASSERT(a_sItemContent);
  80. if (!a_sItemContent)
  81. {
  82. LogErrorTrace(__FILE__, __LINE__, _T("UpdateRow:invalid item value"));
  83. return FALSE;
  84. }
  85. auto datastorePtr = GetDatastore();
  86. ASSERT(datastorePtr);
  87. if (!datastorePtr)
  88. {
  89. LogErrorTrace(__FILE__, __LINE__, _T("UpdateRow:invalid data base store."));
  90. return FALSE;
  91. }
  92. CString sComment("NULL");
  93. if (a_sItemComment)
  94. {
  95. sComment.Format(_T("\'%s\'"), a_sItemComment);
  96. }
  97. std::vector<int> vColIndexes;
  98. int nItemIndex = (int)CInformationTable::ColumnID::ITEM - (int)CInformationTable::ColumnID::MIN;
  99. vColIndexes.push_back((int)CInformationTable::ColumnID::CONTENT - (int)CInformationTable::ColumnID::MIN);
  100. vColIndexes.push_back((int)CInformationTable::ColumnID::COMMENT - (int)CInformationTable::ColumnID::MIN);
  101. CString sFormat = GetTableInfo()->GetUpdateCommandFormatString(vColIndexes, nItemIndex);
  102. CString sSQLCommand;
  103. sSQLCommand.Format(sFormat,
  104. a_sItemContent,
  105. sComment,
  106. a_sItemName);
  107. return datastorePtr->RunCommand(sSQLCommand);
  108. }
  109. BOOL CInformationDB::InsertStringRow(LPCTSTR a_sItemName, LPCTSTR a_sItemContent, LPCTSTR a_sItemComment /*= nullptr*/)
  110. {
  111. CString sContent;
  112. //sContent.Format(_T("\'%s\'"), a_sItemContent);
  113. sContent.Format(_T("%s"), a_sItemContent);
  114. if (!InsertRow(a_sItemName, sContent, a_sItemComment))
  115. {
  116. LogErrorTrace(__FILE__,__LINE__,_T("Insert string row failed: %s, %s"), a_sItemName, a_sItemContent);
  117. ASSERT(FALSE);
  118. return FALSE;
  119. }
  120. return TRUE;
  121. }
  122. BOOL CInformationDB::UpdateStringRow(LPCTSTR a_sItemName, LPCTSTR a_sItemContent, LPCTSTR a_sItemComment /*= nullptr*/)
  123. {
  124. CString sContent;
  125. //sContent.Format(_T("\'%s\'"), a_sItemContent);
  126. sContent.Format(_T("%s"), a_sItemContent);
  127. if (!UpdateRow(a_sItemName, sContent, a_sItemComment))
  128. {
  129. LogErrorTrace(__FILE__,__LINE__,_T("Update string row failed: %s, %s"), a_sItemName, a_sItemContent);
  130. ASSERT(FALSE);
  131. return FALSE;
  132. }
  133. return TRUE;
  134. }
  135. BOOL CInformationDB::InsertIntegerRow(LPCTSTR a_sItemName, int a_nItemContent, LPCTSTR a_sItemComment /*= nullptr*/)
  136. {
  137. CString sContent;
  138. sContent.Format(_T("%d"), a_nItemContent);
  139. return InsertRow(a_sItemName, sContent, a_sItemComment);
  140. }
  141. BOOL CInformationDB::UpdateIntegerRow(LPCTSTR a_sItemName, int a_nItemContent, LPCTSTR a_sItemComment /*= nullptr*/)
  142. {
  143. CString sContent;
  144. sContent.Format(_T("%d"), a_nItemContent);
  145. return UpdateRow(a_sItemName, sContent, a_sItemComment);
  146. }
  147. BOOL CInformationDB::InsertDoubleRow(LPCTSTR a_sItemName, double a_dItemContent, LPCTSTR a_sItemComment /*= nullptr*/)
  148. {
  149. CString sContent;
  150. sContent.Format(_T("%f"), a_dItemContent);
  151. return InsertRow(a_sItemName, sContent, a_sItemComment);
  152. }
  153. BOOL CInformationDB::UpdateDoubleRow(LPCTSTR a_sItemName, double a_dItemContent, LPCTSTR a_sItemComment /*= nullptr*/)
  154. {
  155. CString sContent;
  156. sContent.Format(_T("%f"), a_dItemContent);
  157. return UpdateRow(a_sItemName, sContent, a_sItemComment);
  158. }
  159. BOOL CInformationDB::InsertTimeStampRow(LPCTSTR a_sItemName, LPCTSTR a_sItemComment /*= nullptr*/)
  160. {
  161. SYSTEMTIME m_time;
  162. GetLocalTime(&m_time);
  163. char szDateTime[100] = { 0 };
  164. sprintf_s(szDateTime, "%02d-%02d-%02d %02d:%02d:%02d", m_time.wYear, m_time.wMonth,
  165. m_time.wDay, m_time.wHour, m_time.wMinute, m_time.wSecond);
  166. std::string time(szDateTime);
  167. return InsertRow(a_sItemName, CString(time.c_str()), a_sItemComment);
  168. //return InsertRow(a_sItemName, _T("CURRENT_TIMESTAMP"), a_sItemComment);
  169. }
  170. BOOL CInformationDB::UpdateTimeStampRow(LPCTSTR a_sItemName, LPCTSTR a_sItemComment /*= nullptr*/)
  171. {
  172. SYSTEMTIME m_time;
  173. GetLocalTime(&m_time);
  174. char szDateTime[100] = { 0 };
  175. sprintf_s(szDateTime, "%02d-%02d-%02d %02d:%02d:%02d", m_time.wYear, m_time.wMonth,
  176. m_time.wDay, m_time.wHour, m_time.wMinute, m_time.wSecond);
  177. std::string time(szDateTime);
  178. return UpdateRow(a_sItemName, CString(time.c_str()), a_sItemComment);
  179. }
  180. BOOL CInformationDB::GetStringValue(LPCTSTR a_sItemName, CString& a_sString)
  181. {
  182. auto datastorePtr = GetDatastore();
  183. ASSERT(datastorePtr);
  184. if (!datastorePtr)
  185. {
  186. return FALSE;
  187. }
  188. auto tableInfoPtr = GetTableInfo();
  189. ASSERT(tableInfoPtr);
  190. if (!tableInfoPtr)
  191. {
  192. return FALSE;
  193. }
  194. CString sNameColumnName = tableInfoPtr->GetColumnName((int)CInformationTable::ColumnID::ITEM - (int)CInformationTable::ColumnID::MIN);
  195. CString sSQLCommand;
  196. sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = \'%s\'"),
  197. (LPCTSTR)tableInfoPtr->GetTableName(),
  198. (LPCTSTR)sNameColumnName,
  199. a_sItemName);
  200. auto query = datastorePtr->QueryByCommand(sSQLCommand);
  201. ASSERT(query);
  202. if (!query)
  203. {
  204. return FALSE;
  205. }
  206. int nCol = (int)CInformationTable::ColumnID::CONTENT - (int)CInformationTable::ColumnID::MIN;
  207. a_sString = query->GetColStringValue(nCol);
  208. return TRUE;
  209. }
  210. BOOL CInformationDB::GetIntValue(LPCTSTR a_sItemName, int& a_nValue)
  211. {
  212. auto datastorePtr = GetDatastore();
  213. ASSERT(datastorePtr);
  214. if (!datastorePtr)
  215. {
  216. return FALSE;
  217. }
  218. auto tableInfoPtr = GetTableInfo();
  219. ASSERT(tableInfoPtr);
  220. if (!tableInfoPtr)
  221. {
  222. return FALSE;
  223. }
  224. CString sNameColumnName = tableInfoPtr->GetColumnName((int)CInformationTable::ColumnID::ITEM - (int)CInformationTable::ColumnID::MIN);
  225. CString sSQLCommand;
  226. sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = \'%s\'"),
  227. (LPCTSTR)tableInfoPtr->GetTableName(),
  228. (LPCTSTR)sNameColumnName,
  229. a_sItemName);
  230. auto query = datastorePtr->QueryByCommand(sSQLCommand);
  231. ASSERT(query);
  232. if (!query)
  233. {
  234. return FALSE;
  235. }
  236. int nCol = (int)CInformationTable::ColumnID::CONTENT - (int)CInformationTable::ColumnID::MIN;
  237. a_nValue = query->GetColIntValue(nCol);
  238. return TRUE;
  239. }
  240. CDBTableBasePtr CInformationDB::GetTableInfo()
  241. {
  242. /*if (!m_tableInfo)
  243. {
  244. m_tableInfo.reset(new CInformationTable);
  245. }
  246. ASSERT(m_tableInfo);*/
  247. return m_tableInfo;
  248. }
  249. BOOL CInformationDB::Init(const BOOL a_bClean /*= FALSE*/)
  250. {
  251. return myDB->Init(a_bClean);
  252. }
  253. BOOL CInformationDB::CreateTable(const BOOL a_bForce /*= FALSE*/)
  254. {
  255. return myDB->CreateTable(a_bForce);
  256. }
  257. BOOL CInformationDB::DeleteTable()
  258. {
  259. return myDB->DeleteTable();
  260. }
  261. BOOL CInformationDB::RemoveAllRows()
  262. {
  263. return myDB->RemoveAllRows();
  264. }
  265. BOOL CInformationDB::IsDBExist()
  266. {
  267. return myDB->IsDBExist();
  268. }
  269. OTSSQLITE::CDBStoreBasePtr CInformationDB::GetDatastore()
  270. {
  271. return myDB->GetDatastore();
  272. }
  273. OTSSQLITE::CDBQueryBasePtr CInformationDB::GetTableQuery(LPCTSTR a_sOrderColumnName /*= nullptr*/)
  274. {
  275. return myDB->GetTableQuery(a_sOrderColumnName);
  276. }
  277. }