123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #pragma once
- #include "stdafx.h"
- #include "SQLiteDB.h"
- #include <wtypes.h>
- #include "DBStoreBase.h"
- #include "DBTableBase.h"
- namespace OTSSQLITE
- {
- CSQLiteDB::CSQLiteDB(CDBStoreBasePtr a_datastore,CDBTableBasePtr a_tableInfo)
- {
- m_datastorePtr = a_datastore;
- m_tableInfo = a_tableInfo;
- }
- CSQLiteDB::~CSQLiteDB(void)
- {
- }
- BOOL CSQLiteDB::Init(const BOOL a_bClean /*= FALSE*/)
- {
- if (IsDBExist())
- {
- if (a_bClean)
- {
- DeleteTable();
- }
- else
- {
- return TRUE;
- }
- }
- return CreateTable();
- }
- BOOL CSQLiteDB::IsDBExist()
- {
- auto datastorePtr = GetDatastore();
- ASSERT(datastorePtr);
- if (!datastorePtr)
- {
- return FALSE;
- }
-
- auto tableInfoPtr = GetTableInfo();
- ASSERT(tableInfoPtr);
- if (!tableInfoPtr)
- {
- return FALSE;
- }
- return datastorePtr->IsTableExists(tableInfoPtr->GetTableName());
- }
- CDBQueryBasePtr CSQLiteDB::GetTableQuery(LPCTSTR a_sOrderColumnName /*= nullptr*/)
- {
- auto datastorePtr = GetDatastore();
- ASSERT(datastorePtr);
- if (!datastorePtr)
- {
- return CDBQueryBasePtr();
- }
- auto tableInfoPtr = GetTableInfo();
- ASSERT(tableInfoPtr);
- if (!tableInfoPtr) { return CDBQueryBasePtr(); }
- CString sTableName = tableInfoPtr->GetTableName();
- if (!datastorePtr->IsTableExists(sTableName))
- {
- return CDBQueryBasePtr();
- }
- auto query = datastorePtr->QueryByTableName(sTableName, a_sOrderColumnName);
- ASSERT(query);
- if (!query || !query->IsValid())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Invalid table(%s) query command."), (LPCTSTR)tableInfoPtr->GetTableName());
- return CDBQueryBasePtr();
- }
- // do the table related valid checking
- auto cols1 = query->GetColCount();
- auto cols2 = tableInfoPtr->GetColumnCount();
- if (cols1!=cols2)
- {
- LogErrorTrace(__FILE__, __LINE__, _T(sTableName+":Invalid table columns(%d): should be %d"), query->GetColCount(), tableInfoPtr->GetColumnCount());
- }
- for (int i = 0; i < cols1; i++)
- {
- if (query->GetColName(i) != tableInfoPtr->GetColumnName(i))
- {
- AfxMessageBox(_T("The culumn name define error. Please adjust and try again!"));
- LogErrorTrace(__FILE__, __LINE__, _T("The culumn define is incorrect."));
- break;
- }
- }
- return query;
- }
- CDBQueryBasePtr CSQLiteDB::GetCommandStringQuery(LPCTSTR a_commandStr /*= nullptr*/)
- {
- auto datastorePtr = GetDatastore();
- ASSERT(datastorePtr);
-
-
- auto query = datastorePtr->QueryByCommand(a_commandStr);
- ASSERT(query);
-
- return query;
- }
- OTSSQLITE::CDBTableBasePtr CSQLiteDB::GetTableInfo()
- {
- return m_tableInfo;
-
- }
- BOOL CSQLiteDB::CreateTable(const BOOL a_bForce /*= FALSE*/)
- {
- auto datastorePtr = GetDatastore();
- ASSERT(datastorePtr);
- if (!datastorePtr)
- {
- return FALSE;
- }
- auto tableInfoPtr = GetTableInfo();
- ASSERT(tableInfoPtr);
- if (!tableInfoPtr)
- {
- return FALSE;
- }
- if (datastorePtr->IsTableExists(tableInfoPtr->GetTableName()))
- {
- if (a_bForce)
- {
- if (!DeleteTable())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CreateTable:delete table (%s) failed."), (LPCTSTR)tableInfoPtr->GetTableName());
- ASSERT(FALSE);
- return FALSE;
- }
- }
- else
- {
- return TRUE;
- }
- }
- // create table
- CString sSQLCommand = tableInfoPtr->GetCreateTableCommandString();
- return datastorePtr->RunCommand(sSQLCommand);
- }
- BOOL CSQLiteDB::DeleteTable()
- {
- auto datastorePtr = GetDatastore();
- ASSERT(datastorePtr);
- if (!datastorePtr)
- {
- return FALSE;
- }
- auto tableInfoPtr = GetTableInfo();
- ASSERT(tableInfoPtr);
- if (!tableInfoPtr)
- {
- return FALSE;
- }
- if (!datastorePtr->IsTableExists(tableInfoPtr->GetTableName()))
- {
- return TRUE;
- }
- // create table
- CString sSQLCommand = tableInfoPtr->GetDeleteTableCommandString();
- return datastorePtr->RunCommand(sSQLCommand);
- }
- // Removes all rows.
- BOOL CSQLiteDB::RemoveAllRows()
- {
- auto datastorePtr = GetDatastore();
- ASSERT(datastorePtr);
- if (!datastorePtr)
- {
- return FALSE;
- }
- auto tableInfoPtr = GetTableInfo();
- ASSERT(tableInfoPtr);
- if (!tableInfoPtr)
- {
- return FALSE;
- }
- if (!datastorePtr->IsTableExists(tableInfoPtr->GetTableName()))
- {
- LogInfoTrace(__FILE__,__LINE__,_T("Didn't remove all rows from table(%s): table not exist"), tableInfoPtr->GetTableName());
- return TRUE;
- }
- // create table
- CString sSQLCommand = tableInfoPtr->GetRemoveAllRowsCommandString();
- return datastorePtr->RunCommand(sSQLCommand);
- }
- CString CSQLiteDB::GetReadColumnError(const int a_nColId)
- {
- auto tableInfoPtr = GetTableInfo();
- ASSERT(tableInfoPtr);
- if (!tableInfoPtr)
- {
- return _T("");
- }
- CString sColumnName = tableInfoPtr->GetColumnName(a_nColId);
- CString sErrorMessage;
- sErrorMessage.Format(_T("Read column(%s) from table(%s) failed"), (LPCTSTR)sColumnName, (LPCTSTR)tableInfoPtr->GetTableName());
- return sErrorMessage;
- }
- OTSSQLITE::CDBBasePtr CreateNewSQLiteDB(CDBStoreBasePtr a_datastore, CDBTableBasePtr a_tableInfo)
- {
- return CDBBasePtr(new CSQLiteDB(a_datastore,a_tableInfo));
- }
- }
|