| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 | #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)	{	}			CPosXrayInfoList CPosXrayInfoDB::GetXrayInfoListByFieldId(const long a_nFieldId)	{		CPosXrayInfoList 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;	}	CPosXrayInfoList& 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 CPosXraysList& 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 CPosXrayInfoList& 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 CPosXrayInfoPtr& 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;	}	CPosXrayInfoList CPosXrayInfoDB::ReadXrayPointInfoListByQuery(CDBQueryBasePtr a_query)	{		CPosXrayInfoList 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;	}	CPosXrayInfoPtr CPosXrayInfoDB::ReadXrayPointInfo(CDBQueryBasePtr a_query)	{		int nCol;		CPosXrayInfoPtr xrayPointPtr(new CPosXrayInfo());		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;	}}
 |