GenInfoDB.cpp 3.5 KB

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