123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- #pragma once
- #include "stdafx.h"
- #include "ElementChemistryDB.h"
- #include "ElementChemistryTable.h"
- namespace OTSSQLITE
- {
- CElementChemistryDB::CElementChemistryDB(CDBStoreBasePtr a_datastore)
- {
- m_tableInfo.reset(new CElementChemistryTable());
- myDB = CreateNewSQLiteDB(a_datastore,m_tableInfo);
-
- }
-
- CElementChemistryDB::~CElementChemistryDB()
- {
- }
- CElementChemistriesList CElementChemistryDB::GetElementChemistryListById(const long a_nXrayId, const long a_nFieldId, const long a_nElementSize)
- {
- CElementChemistriesList listElementChemistry;
- if (!m_listPosXrayInfo.empty())
- {
- for (auto& xrayPointInfo : m_listPosXrayInfo)
- {
- if (xrayPointInfo->GetIndex() == (DWORD)a_nXrayId && xrayPointInfo->GetScanFieldId() == a_nFieldId && xrayPointInfo->GetElementNum() == a_nElementSize)
- {
- listElementChemistry = xrayPointInfo->GetElementQuantifyData();
- }
- }
- }
- else
- {
- // read element list
- for (int i = 0; i < a_nElementSize; i++)
- {
- auto tableQuery = GetQueryById(a_nXrayId, a_nFieldId, i, a_nElementSize);
- ASSERT(tableQuery);
- if (!tableQuery)
- {
- return listElementChemistry;
- }
- auto pXrayInfoMap = ReadPosXrayInfo(tableQuery);
- if (pXrayInfoMap.size()==0)
- {
- return listElementChemistry;
- }
- std::vector<int> idvec;
- idvec.push_back(a_nFieldId);
- idvec.push_back(a_nXrayId);
- listElementChemistry = pXrayInfoMap[idvec]->GetElementQuantifyData();
-
- }
- }
- return listElementChemistry;
- }
-
-
- //}
- BOOL CElementChemistryDB::SaveElementChemistriesList(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)
- {
- CElementChemistriesList listElemnentChemistries = xrayPointInfo->GetElementQuantifyData();
- int nSize = (int)listElemnentChemistries.size();
- int nElementIndex = 0;
- for (auto& pElementChemistry : listElemnentChemistries)
- {
- sSQLCommand.Format(sInsertFormat,
- xrayPointInfo->GetIndex(),
- xrayPointInfo->GetScanFieldId(),
- nElementIndex,
- nSize,
- pElementChemistry->GetName(),
- pElementChemistry->GetPercentage());
- if (!datastorePtr->RunCommand(sSQLCommand))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Insert element(%d:%d:%d:%d) failed: %s command error"),
- xrayPointInfo->GetIndex(),
- xrayPointInfo->GetScanFieldId(),
- nElementIndex,
- nSize,
- pElementChemistry->GetName(),
- pElementChemistry->GetPercentage(),
- sSQLCommand);
- ASSERT(FALSE);
- return FALSE;
- }
- nElementIndex++;
- }
- }
- return TRUE;
- }
- BOOL CElementChemistryDB::SaveElementChemistriesList(const CPosXrayPtr a_pxrayPoint)
- {
- 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;
- CElementChemistriesList listElemnentChemistries = a_pxrayPoint->GetElementQuantifyData();
- int nSize = (int)listElemnentChemistries.size();
- int nElementIndex = 0;
- for (auto& pElementChemistry : listElemnentChemistries)
- {
- sSQLCommand.Format(sInsertFormat,
- a_pxrayPoint->GetIndex(),
- a_pxrayPoint->GetScanFieldId(),
- nElementIndex,
- nSize,
- pElementChemistry->GetName(),
- pElementChemistry->GetPercentage(),
- pElementChemistry->GetMolarPercentage());
- if (!datastorePtr->RunCommand(sSQLCommand))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Insert element(%d:%d:%d:%d) failed: %s command error"),
- a_pxrayPoint->GetIndex(),
- a_pxrayPoint->GetScanFieldId(),
- nElementIndex,
- nSize,
- pElementChemistry->GetName(),
- pElementChemistry->GetPercentage(),
- pElementChemistry->GetMolarPercentage(),
- sSQLCommand);
- ASSERT(FALSE);
- return FALSE;
- }
- nElementIndex++;
- }
-
- return TRUE;
- }
- BOOL CElementChemistryDB::DeleteElementChemistryById(const long a_nFieldId, const long a_nXrayId)
- {
- if (!m_listPosXrayInfo.empty())
- {
- std::remove_if(m_listPosXrayInfo.begin(), m_listPosXrayInfo.end(), [a_nFieldId, a_nXrayId](const CPosXrayPtr& xrayPointInfo) { return( xrayPointInfo->GetIndex() == (DWORD)a_nXrayId)&&(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 sXrayIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN);
- CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN);
-
- CString sSQLCommand;
- sSQLCommand.Format(_T("DELETE FROM \'%s\' WHERE %s = %d AND %s = %d AND %s = %d AND %s = %d;"),
- (LPCTSTR)tableInfoPtr->GetTableName(),
- (LPCTSTR)sXrayIdColumnName,
- a_nXrayId,
- (LPCTSTR)sFieldIdColumnName,
- a_nFieldId);
- return datastorePtr->RunCommand(sSQLCommand);
- }
- CDBTableBasePtr CElementChemistryDB::GetTableInfo()
- {
-
- return m_tableInfo;
- }
-
- BOOL CElementChemistryDB::Init(const BOOL a_bClean /*= FALSE*/)
- {
- return myDB->Init();
- }
- BOOL CElementChemistryDB::CreateTable(const BOOL a_bForce /*= FALSE*/)
- {
- return myDB->CreateTable(a_bForce);
- }
- BOOL CElementChemistryDB::DeleteTable()
- {
- return myDB->DeleteTable();
- }
- BOOL CElementChemistryDB::RemoveAllRows()
- {
- return myDB->RemoveAllRows();
- }
- BOOL CElementChemistryDB::IsDBExist()
- {
- return myDB->IsDBExist();
- }
- OTSSQLITE::CDBStoreBasePtr CElementChemistryDB::GetDatastore()
- {
- return myDB->GetDatastore();
- }
- OTSSQLITE::CDBQueryBasePtr CElementChemistryDB::GetTableQuery(LPCTSTR a_sOrderColumnName /*= nullptr*/)
- {
- return myDB->GetTableQuery(a_sOrderColumnName);
- }
-
-
- std::map<std::vector<int>, CPosXrayPtr> CElementChemistryDB::ReadPosXrayInfo(OTSSQLITE::CDBQueryBasePtr a_query)
- {
- std::map<std::vector<int>, CPosXrayPtr> mapXrayInfo;
- CDBQueryBasePtr query;
- if (a_query == nullptr)
- {
- auto tableInfoPtr = GetTableInfo();
- ASSERT(tableInfoPtr);
- if (!tableInfoPtr)
- {
- return mapXrayInfo;
- }
- query = GetTableQuery();
- ASSERT(query);
- if (!query)
- {
- return mapXrayInfo;
- }
- }
- else
- {
- query = a_query;
- }
-
- while (!query->IsEOF())
- {
- CPosXrayPtr xrayInfo;
- int nCol;
-
- int nXrayIdNow;
- int nFieldIdNow;
- nCol = (int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN;
- nXrayIdNow = query->GetColIntValue(nCol, -1);
-
- nCol = (int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN;
- nFieldIdNow = query->GetColIntValue(nCol, -1);
-
- std::vector <int> fldvec;
- fldvec.push_back(nFieldIdNow);
- fldvec.push_back(nXrayIdNow);
- if (mapXrayInfo.find(fldvec) != mapXrayInfo.end())
- {
- xrayInfo = mapXrayInfo[fldvec];
- }
- else
- {
- CPosXrayPtr xrayPointPtr(new CPosXray());
- mapXrayInfo[fldvec] = xrayPointPtr;
- xrayPointPtr->SetIndex(nXrayIdNow);
- xrayPointPtr->SetScanFieldId(nFieldIdNow);
- xrayInfo = xrayPointPtr;
- }
- nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN;
- int nElementIndex = query->GetColIntValue(nCol, -1);
- nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN;
- int nElementTotal = query->GetColIntValue(nCol, -1);
- xrayInfo->SetElementNum(nElementTotal);
- CElementChemistryPtr pElementChemistry = CElementChemistryPtr(new CElementChemistry());
- nCol = (int)CElementChemistryTable::ColumnID::S_NAME - (int)CElementChemistryTable::ColumnID::MIN;
- pElementChemistry->SetName(query->GetColStringValue(nCol, _T("")));
- nCol = (int)CElementChemistryTable::ColumnID::F_PERCENTAGE - (int)CElementChemistryTable::ColumnID::MIN;
- pElementChemistry->SetPercentage(query->GetColFloatValue(nCol, -1));
-
- xrayInfo->AddElementQuantifyData(pElementChemistry);
- query->NextRow();
- }
- return mapXrayInfo;
-
- }
-
- CDBQueryBasePtr CElementChemistryDB::GetQueryById(const long a_nXrayId,const long a_nFieldId, const long a_nElementId, const long a_nElementSize)
- {
- CDBQueryBasePtr query;
- auto datastorePtr = GetDatastore();
- ASSERT(datastorePtr);
- if (!datastorePtr)
- {
- return query;
- }
- auto tableInfoPtr = GetTableInfo();
- ASSERT(tableInfoPtr);
- if (!tableInfoPtr)
- {
- return query;
- }
- CString sXrayIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN);
- CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN);
- CString sElementIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN);
- CString sElementNumColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN);
- CString sSQLCommand;
- sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = %d AND %s = %d AND %s = %d AND %s = %d;"),
- (LPCTSTR)tableInfoPtr->GetTableName(),
- (LPCTSTR)sFieldIdColumnName,
- a_nFieldId,
- (LPCTSTR)sXrayIdColumnName,
- a_nXrayId,
- (LPCTSTR)sElementIdColumnName,
- a_nElementId,
- (LPCTSTR)sElementNumColumnName,
- a_nElementSize);
- 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;
- }
-
-
- }
|