GenInfoDB.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "GenInfoDB.h"
  4. #include "DBConst.h"
  5. #include "InformationTable.h"
  6. namespace OTSSQLITE
  7. {
  8. CGenInfoDB::CGenInfoDB(CDBStoreBasePtr a_datastore):CInformationDB(a_datastore),
  9. m_sFileVersion(_T(""))
  10. {
  11. }
  12. CGenInfoDB::~CGenInfoDB(void)
  13. {
  14. }
  15. BOOL CGenInfoDB::Init(const BOOL /*a_bClean = FALSE*/)
  16. {
  17. if (IsDBExist()) { return TRUE; }
  18. if (CreateTable())
  19. {
  20. auto datastorePtr = GetDatastore() ;
  21. ASSERT(datastorePtr);
  22. if (!datastorePtr)
  23. {
  24. return FALSE;
  25. }
  26. if (!InsertTimeStampRow(g_sTableItemNameCreateTime, datastorePtr->GetFileName()))
  27. {
  28. LogErrorTrace(__FILE__,__LINE__,_T("Save %s item to table(%s) failed"), (LPCTSTR)g_sTableItemNameCreateTime, (LPCTSTR)GetTableInfo()->GetTableName());
  29. }
  30. if (!InsertStringRow(g_sTableItemNameFileVersion, g_sDBFileVersion))
  31. {
  32. LogErrorTrace(__FILE__,__LINE__,_T("Save %s item to table(%s) failed"), (LPCTSTR)g_sTableItemNameFileVersion, (LPCTSTR)GetTableInfo()->GetTableName());
  33. ASSERT(FALSE);
  34. return FALSE;
  35. }
  36. if (!InsertTimeStampRow(g_sTableItemNameTimeStart, datastorePtr->GetFileName()))
  37. {
  38. LogErrorTrace(__FILE__, __LINE__, _T("Save %s item to table(%s) failed"), (LPCTSTR)g_sTableItemNameCreateTime, (LPCTSTR)GetTableInfo()->GetTableName());
  39. }
  40. if (!InsertTimeStampRow(g_sTableItemNameTimeEnd, datastorePtr->GetFileName()))
  41. {
  42. LogErrorTrace(__FILE__, __LINE__, _T("Save %s item to table(%s) failed"), (LPCTSTR)g_sTableItemNameCreateTime, (LPCTSTR)GetTableInfo()->GetTableName());
  43. }
  44. CString resultSta("0");//in default ,initialize it to 0,it will be updated in the running process.
  45. if (!InsertStringRow(g_sTableItemNameResultStatus, resultSta))
  46. {
  47. LogErrorTrace(__FILE__, __LINE__, _T("Save %s item to table(%s) failed"), (LPCTSTR)g_sTableItemNameFileVersion, (LPCTSTR)GetTableInfo()->GetTableName());
  48. ASSERT(FALSE);
  49. return FALSE;
  50. }
  51. m_sFileVersion = g_sDBFileVersion;
  52. return TRUE;
  53. }
  54. ASSERT(FALSE);
  55. return FALSE;
  56. }
  57. CString CGenInfoDB::GetFileVersion()
  58. {
  59. m_sFileVersion.Trim();
  60. if (m_sFileVersion.IsEmpty())
  61. {
  62. // read in file version if it is empty
  63. auto datastorePtr = GetDatastore();
  64. ASSERT(datastorePtr);
  65. if (!datastorePtr)
  66. {
  67. return m_sFileVersion;
  68. }
  69. auto tableInfoPtr = GetTableInfo();
  70. ASSERT(tableInfoPtr);
  71. if (!tableInfoPtr)
  72. {
  73. return m_sFileVersion;
  74. }
  75. CString sItemName = tableInfoPtr->GetColumnName((int)CInformationTable::ColumnID::ITEM - (int)CInformationTable::ColumnID::MIN);
  76. CString sSQLCommand;
  77. sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = \'%s\';"),
  78. (LPCTSTR)tableInfoPtr->GetTableName(),
  79. (LPCTSTR)sItemName,
  80. (LPCTSTR)g_sTableItemNameFileVersion);
  81. auto query = datastorePtr->QueryByCommand(sSQLCommand);
  82. ASSERT(query);
  83. if (!query)
  84. {
  85. return m_sFileVersion;
  86. }
  87. ASSERT(query->IsValid());
  88. if (!query->IsValid())
  89. {
  90. return m_sFileVersion;
  91. }
  92. if (query->IsEOF())
  93. {
  94. LogErrorTrace(__FILE__,__LINE__,_T("Could not read file version from datastore."));
  95. return m_sFileVersion;
  96. }
  97. int nCol = (int)CInformationTable::ColumnID::CONTENT - (int)CInformationTable::ColumnID::MIN;
  98. m_sFileVersion = query->GetColStringValue(nCol);
  99. }
  100. return m_sFileVersion;
  101. }
  102. CString CGenInfoDB::GetTableItemNameTimeStart() {
  103. return CString(g_sTableItemNameTimeStart);
  104. }
  105. CString CGenInfoDB::GetTableItemNameTimeEnd() {
  106. return CString(g_sTableItemNameTimeEnd);
  107. }
  108. CString CGenInfoDB::GetTableItemNameResultStatus() {
  109. return CString(g_sTableItemNameResultStatus);
  110. }
  111. }