#pragma once #include "stdafx.h" #include "PosXrayInfoDB.h" #include "PosXrayInfoTable.h" namespace OTSSQLITE { CPosXrayInfoDB::CPosXrayInfoDB(CDBStoreBasePtr a_datastore) { m_tableInfo.reset(new CPosXrayInfoTable()); myDB = CreateNewSQLiteDB(a_datastore,m_tableInfo); } CPosXrayInfoDB::~CPosXrayInfoDB(void) { } CPosXrayList CPosXrayInfoDB::GetXrayInfoListByFieldId(const long a_nFieldId) { CPosXrayList xrayPointInfoVec; if (!m_listPosXrayInfo.empty()) { for (auto& xrayPointInfo : m_listPosXrayInfo) { if (xrayPointInfo->GetScanFieldId() == (DWORD)a_nFieldId) { xrayPointInfoVec.push_back(xrayPointInfo); } } } else { auto tableQuery = GetQueryByFieldId(a_nFieldId); ASSERT(tableQuery); if (!tableQuery) { return xrayPointInfoVec; } xrayPointInfoVec = ReadXrayPointInfoListByQuery(tableQuery); } return xrayPointInfoVec; } CPosXrayList& CPosXrayInfoDB::GetXrayInfoList(const BOOL a_bForce/* = FALSE*/,int fldId) { if (a_bForce) { m_listPosXrayInfo.clear(); } if (m_listPosXrayInfo.size() == 0) { GetXrayInfoListByFieldId(fldId); } return m_listPosXrayInfo; } BOOL CPosXrayInfoDB::SaveXrayInfoList(const CPosXrayList& a_xrayPointList) { if (!Init()) { ASSERT(FALSE); return FALSE; } auto tableInfoPtr = GetTableInfo(); ASSERT(tableInfoPtr); if (!tableInfoPtr) { return FALSE; } auto datastorePtr = GetDatastore(); ASSERT(datastorePtr); if (!datastorePtr) { return FALSE; } CString sInsertFormat = tableInfoPtr->GetInsertCommandFormatString(TRUE); CString sSQLCommand; for (auto& xrayPointInfo : a_xrayPointList) { sSQLCommand.Format(sInsertFormat, xrayPointInfo->GetIndex(), xrayPointInfo->GetPosition().x, xrayPointInfo->GetPosition().y, xrayPointInfo->GetScanFieldId(), xrayPointInfo->GetPartTagId(), xrayPointInfo->GetFeatureId(), xrayPointInfo->GetElementNum()); if (!datastorePtr->RunCommand(sSQLCommand)) { LogErrorTrace(__FILE__,__LINE__,_T("Insert xray info(%d:%d:%d:%d:%d) position(%d:%d) failed: %s command error"), xrayPointInfo->GetIndex(), xrayPointInfo->GetScanFieldId(), xrayPointInfo->GetPartTagId(), xrayPointInfo->GetFeatureId(), xrayPointInfo->GetElementNum(), xrayPointInfo->GetPosition().x, xrayPointInfo->GetPosition().y, sSQLCommand); ASSERT(FALSE); return FALSE; } } return TRUE; } /*BOOL CPosXrayInfoDB::SaveXrayInfoList(const CPosXraysList& a_xrayPointInfoList) { if (!Init()) { ASSERT(FALSE); return FALSE; } auto tableInfoPtr = GetTableInfo(); ASSERT(tableInfoPtr); if (!tableInfoPtr) { return FALSE; } auto datastorePtr = GetDatastore(); ASSERT(datastorePtr); if (!datastorePtr) { return FALSE; } CString sInsertFormat = tableInfoPtr->GetInsertCommandFormatString(TRUE); CString sSQLCommand; for (auto& xrayPointInfo : a_xrayPointInfoList) { sSQLCommand.Format(sInsertFormat, xrayPointInfo->GetIndex(), xrayPointInfo->GetPosition().x, xrayPointInfo->GetPosition().y, xrayPointInfo->GetScanFieldId(), xrayPointInfo->GetPartTagId(), xrayPointInfo->GetFeatureId(), xrayPointInfo->GetElementNum()); if (!datastorePtr->RunCommand(sSQLCommand)) { LogErrorTrace(__FILE__, __LINE__, _T("Insert xray info(%d:%d:%d:%d:%d) position(%d:%d) failed: %s command error"), xrayPointInfo->GetIndex(), xrayPointInfo->GetScanFieldId(), xrayPointInfo->GetPartTagId(), xrayPointInfo->GetFeatureId(), xrayPointInfo->GetElementNum(), xrayPointInfo->GetPosition().x, xrayPointInfo->GetPosition().y, sSQLCommand); ASSERT(FALSE); return FALSE; } } return TRUE; }*/ BOOL CPosXrayInfoDB::DeleteXrayPointInfoListByFieldId(const long a_nFieldId) { if (!m_listPosXrayInfo.empty()) { std::remove_if(m_listPosXrayInfo.begin(), m_listPosXrayInfo.end(), [a_nFieldId](const CPosXrayPtr& xrayPointInfo) { return xrayPointInfo->GetScanFieldId() == (DWORD)a_nFieldId; }); } auto tableInfoPtr = GetTableInfo(); ASSERT(tableInfoPtr); if (!tableInfoPtr) { return FALSE; } auto datastorePtr = GetDatastore(); ASSERT(datastorePtr); if (!datastorePtr) { return FALSE; } CString sTableName = tableInfoPtr->GetTableName(); if (!datastorePtr->IsTableExists(sTableName)) { LogTrace(__FILE__,__LINE__,_T("Table %s not exist"), sTableName); return TRUE; } CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CPosXrayInfoTable::ColumnID::N_FIELD_ID - (int)CPosXrayInfoTable::ColumnID::MIN); CString sSQLCommand; sSQLCommand.Format(_T("DELETE FROM \'%s\' WHERE %s = %d;"), (LPCTSTR)tableInfoPtr->GetTableName(), (LPCTSTR)sFieldIdColumnName, a_nFieldId); return datastorePtr->RunCommand(sSQLCommand); } CDBTableBasePtr CPosXrayInfoDB::GetTableInfo() { /* if (!m_tableInfo) { m_tableInfo.reset(new CPosXrayInfoTable); }*/ return m_tableInfo; } BOOL CPosXrayInfoDB::Init(const BOOL a_bClean /*= FALSE*/) { return myDB->Init(a_bClean); } BOOL CPosXrayInfoDB::CreateTable(const BOOL a_bForce /*= FALSE*/) { return myDB->CreateTable(a_bForce); } BOOL CPosXrayInfoDB::DeleteTable() { return myDB->DeleteTable(); } BOOL CPosXrayInfoDB::RemoveAllRows() { return myDB->RemoveAllRows(); } BOOL CPosXrayInfoDB::IsDBExist() { return myDB->IsDBExist(); } OTSSQLITE::CDBStoreBasePtr CPosXrayInfoDB::GetDatastore() { return myDB->GetDatastore(); } OTSSQLITE::CDBQueryBasePtr CPosXrayInfoDB::GetTableQuery(LPCTSTR a_sOrderColumnName /*= nullptr*/) { return myDB->GetTableQuery(); } BOOL CPosXrayInfoDB::ReadXrayPointInfoList() { /*auto tableInfoPtr = GetTableInfo(); ASSERT(tableInfoPtr); if (!tableInfoPtr) { return FALSE; }*/ auto query = GetTableQuery(); ASSERT(query); if (!query) { return FALSE; } m_listPosXrayInfo = ReadXrayPointInfoListByQuery(query); return TRUE; } CPosXrayList CPosXrayInfoDB::ReadXrayPointInfoListByQuery(CDBQueryBasePtr a_query) { CPosXrayList xrayPointInfoVec; xrayPointInfoVec.clear(); int nRowId = 0; int nWrongItems = 0; while (!a_query->IsEOF()) { auto xrayPointInfo = ReadXrayPointInfo(a_query); if (!xrayPointInfo) { LogErrorTrace(__FILE__,__LINE__,_T("Read xray point info item failed: row id: %d"), nRowId); nWrongItems++; } else { xrayPointInfoVec.push_back(xrayPointInfo); } a_query->NextRow(); nRowId++; } return xrayPointInfoVec; } CPosXrayPtr CPosXrayInfoDB::ReadXrayPointInfo(CDBQueryBasePtr a_query) { int nCol; CPosXrayPtr xrayPointPtr(new CPosXray()); nCol = (int)CPosXrayInfoTable::ColumnID::N_INDEX - (int)CPosXrayInfoTable::ColumnID::MIN; xrayPointPtr->SetIndex(a_query->GetColIntValue(nCol, -1)); CPoint xrayPosition; nCol = (int)CPosXrayInfoTable::ColumnID::N_POS_X - (int)CPosXrayInfoTable::ColumnID::MIN; xrayPosition.x = a_query->GetColIntValue(nCol, -1); nCol = (int)CPosXrayInfoTable::ColumnID::N_POS_Y - (int)CPosXrayInfoTable::ColumnID::MIN; xrayPosition.y = a_query->GetColIntValue(nCol, -1); xrayPointPtr->SetPosition(xrayPosition); nCol = (int)CPosXrayInfoTable::ColumnID::N_FIELD_ID - (int)CPosXrayInfoTable::ColumnID::MIN; xrayPointPtr->SetScanFieldId(a_query->GetColIntValue(nCol, -1)); nCol = (int)CPosXrayInfoTable::ColumnID::N_PARTICLE_ID - (int)CPosXrayInfoTable::ColumnID::MIN; xrayPointPtr->SetPartTagId(a_query->GetColIntValue(nCol, -1)); nCol = (int)CPosXrayInfoTable::ColumnID::N_FEATURE_ID - (int)CPosXrayInfoTable::ColumnID::MIN; xrayPointPtr->SetFeatureId(a_query->GetColIntValue(nCol, -1)); nCol = (int)CPosXrayInfoTable::ColumnID::N_ELEMENT_NUM - (int)CPosXrayInfoTable::ColumnID::MIN; xrayPointPtr->SetElementNum(a_query->GetColIntValue(nCol, -1)); return xrayPointPtr; } CDBQueryBasePtr CPosXrayInfoDB::GetQueryByFieldId(const long a_nFieldId) { CDBQueryBasePtr query; auto datastorePtr = GetDatastore(); ASSERT(datastorePtr); if (!datastorePtr) { return query; } auto tableInfoPtr = GetTableInfo(); ASSERT(tableInfoPtr); if (!tableInfoPtr) { return query; } CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CPosXrayInfoTable::ColumnID::N_FIELD_ID - (int)CPosXrayInfoTable::ColumnID::MIN); CString sSQLCommand; sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = %d;"), (LPCTSTR)tableInfoPtr->GetTableName(), (LPCTSTR)sFieldIdColumnName, a_nFieldId); query = datastorePtr->QueryByCommand(sSQLCommand); ASSERT(query); if (!query || !query->IsValid()) { LogErrorTrace(__FILE__,__LINE__,_T("Invalid quary command (%s)."), (LPCTSTR)sSQLCommand); ASSERT(FALSE); return (CDBQueryBasePtr()); } // do the table related valid checking if (query->GetColCount() != GetTableInfo()->GetColumnCount()) { LogErrorTrace(__FILE__,__LINE__,_T("query col num value is %d, but not %d"), query->GetColCount(), GetTableInfo()->GetColumnCount()); ASSERT(FALSE); return (CDBQueryBasePtr()); } return query; } }