#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; } CPosXrayInfoPtr pXrayInfo = ReadPosXrayInfo(tableQuery); ASSERT(pXrayInfo); if (!pXrayInfo) { return listElementChemistry; } CElementChemistriesList listElement = pXrayInfo->GetElementQuantifyData(); //CElementChemistryPtr pElement = CElementChemistryPtr(new CElementChemistry(*listElement[0].get())); CElementChemistryPtr pElement = listElement[0]; listElementChemistry.push_back(pElement); } } return listElementChemistry; } CPosXrayInfoList& CElementChemistryDB::GetXrayInfoList(const BOOL a_bForce/* = FALSE*/) { if (a_bForce) { m_listPosXrayInfo.clear(); } if (m_listPosXrayInfo.size() == 0) { ReadXrayPointInfoList(); } return m_listPosXrayInfo; } BOOL CElementChemistryDB::SaveElementChemistriesList(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) { 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 CPosXrayInfoPtr& 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() { /*if (!m_tableInfo) { m_tableInfo.reset(new CElementChemistryTable); }*/ 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); } BOOL CElementChemistryDB::ReadXrayPointInfoList() { auto tableInfoPtr = GetTableInfo(); ASSERT(tableInfoPtr); if (!tableInfoPtr) { return FALSE; } auto query = GetTableQuery(); ASSERT(query); if (!query) { return FALSE; } m_listPosXrayInfo = ReadXrayPointInfoList(query); return TRUE; } CPosXrayInfoList CElementChemistryDB::ReadXrayPointInfoList(CDBQueryBasePtr a_query) { CPosXrayInfoList xrayPointInfoVec; int nRowId = 0; int nWrongItems = 0; while (!a_query->IsEOF()) { auto xrayPointInfo = ReadPosXrayInfo(a_query); //current x-ray point if (!xrayPointInfo) { LogErrorTrace(__FILE__, __LINE__, _T("Read xray point info item failed: row id: %d"), nRowId); nWrongItems++; } else { if (!xrayPointInfoVec.empty()) { int a_nXrayId = xrayPointInfo->GetIndex(); CElementChemistriesList listElementNew = xrayPointInfo->GetElementQuantifyData(); int nIndex = 0; for (auto xrayInfo : xrayPointInfoVec) { xrayPointInfoVec.erase(xrayPointInfoVec.begin() + nIndex); if (xrayInfo->GetIndex() == a_nXrayId) { CElementChemistriesList listElementOld = xrayInfo->GetElementQuantifyData(); for (auto pElement : listElementNew) { listElementOld.push_back(CElementChemistryPtr(new CElementChemistry(*pElement.get()))); } xrayPointInfo->SetElementQuantifyData(listElementOld); break; } nIndex++; } } xrayPointInfoVec.push_back(xrayPointInfo); } a_query->NextRow(); nRowId++; } return xrayPointInfoVec; } CPosXrayInfoPtr CElementChemistryDB::ReadPosXrayInfo(CDBQueryBasePtr a_query) { int nCol; CPosXrayInfoPtr xrayPointPtr(new CPosXrayInfo()); int nXrayIdNow; int nFieldIdNow; nCol = (int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN; nXrayIdNow = a_query->GetColIntValue(nCol, -1); xrayPointPtr->SetIndex(nXrayIdNow); nCol = (int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN; nFieldIdNow = a_query->GetColIntValue(nCol, -1); xrayPointPtr->SetScanFieldId(nFieldIdNow); nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN; int nElementIndex = a_query->GetColIntValue(nCol, -1); nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN; int nElementTotal = a_query->GetColIntValue(nCol, -1); xrayPointPtr->SetElementNum(nElementTotal); if (nElementIndex > nElementTotal - 1) { return nullptr; } CElementChemistryPtr pElementChemistry = CElementChemistryPtr(new CElementChemistry()); nCol = (int)CElementChemistryTable::ColumnID::S_NAME - (int)CElementChemistryTable::ColumnID::MIN; pElementChemistry->SetName(a_query->GetColStringValue(nCol, _T(""))); nCol = (int)CElementChemistryTable::ColumnID::F_PERCENTAGE - (int)CElementChemistryTable::ColumnID::MIN; pElementChemistry->SetPercentage(a_query->GetColFloatValue(nCol, -1)); CElementChemistriesList listElementChemistries; listElementChemistries.push_back(pElementChemistry); a_query->NextRow(); int nXrayNew, nFieldIdNew; int nRowId = 0; while (!a_query->IsEOF()) { nCol = (int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN; nXrayNew = a_query->GetColIntValue(nCol, -1); nCol = (int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN; nFieldIdNew = a_query->GetColIntValue(nCol, -1); if (nXrayNew == nXrayIdNow && nFieldIdNew == nFieldIdNow) { nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN; int nElementIndex = a_query->GetColIntValue(nCol, -1); nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN; int nElementTotal = a_query->GetColIntValue(nCol, -1); if (nElementIndex > nElementTotal - 1) { return nullptr; } CElementChemistryPtr pElementChemistry = CElementChemistryPtr(new CElementChemistry()); nCol = (int)CElementChemistryTable::ColumnID::S_NAME - (int)CElementChemistryTable::ColumnID::MIN; pElementChemistry->SetName(a_query->GetColStringValue(nCol, _T(""))); nCol = (int)CElementChemistryTable::ColumnID::F_PERCENTAGE - (int)CElementChemistryTable::ColumnID::MIN; pElementChemistry->SetPercentage(a_query->GetColFloatValue(nCol, -1)); listElementChemistries.push_back(pElementChemistry); if (nElementIndex == nElementTotal - 1) { break; } } else { continue; } a_query->NextRow(); nRowId++; } xrayPointPtr->SetElementQuantifyData(listElementChemistries); return xrayPointPtr; } 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; } //CDBQueryBasePtr CElementChemistryDB::GetQueryById(const long a_nXrayId, 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 sXrayIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN); // CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN); // // CString sSQLCommand; // sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = %d AND %s = %d;"), // (LPCTSTR)tableInfoPtr->GetTableName(), // (LPCTSTR)sFieldIdColumnName, // a_nFieldId, // (LPCTSTR)sXrayIdColumnName, // a_nXrayId); // 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; //} }