ElementChemistryDB.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "ElementChemistryDB.h"
  4. #include "ElementChemistryTable.h"
  5. namespace OTSSQLITE
  6. {
  7. CElementChemistryDB::CElementChemistryDB(CDBStoreBasePtr a_datastore)
  8. {
  9. m_tableInfo.reset(new CElementChemistryTable());
  10. myDB = CreateNewSQLiteDB(a_datastore,m_tableInfo);
  11. }
  12. CElementChemistryDB::~CElementChemistryDB()
  13. {
  14. }
  15. CElementChemistriesList CElementChemistryDB::GetElementChemistryListById(const long a_nXrayId, const long a_nFieldId, const long a_nElementSize)
  16. {
  17. CElementChemistriesList listElementChemistry;
  18. if (!m_listPosXrayInfo.empty())
  19. {
  20. for (auto& xrayPointInfo : m_listPosXrayInfo)
  21. {
  22. if (xrayPointInfo->GetIndex() == (DWORD)a_nXrayId && xrayPointInfo->GetScanFieldId() == a_nFieldId && xrayPointInfo->GetElementNum() == a_nElementSize)
  23. {
  24. listElementChemistry = xrayPointInfo->GetElementQuantifyData();
  25. }
  26. }
  27. }
  28. else
  29. {
  30. // read element list
  31. for (int i = 0; i < a_nElementSize; i++)
  32. {
  33. auto tableQuery = GetQueryById(a_nXrayId, a_nFieldId, i, a_nElementSize);
  34. ASSERT(tableQuery);
  35. if (!tableQuery)
  36. {
  37. return listElementChemistry;
  38. }
  39. CPosXrayInfoPtr pXrayInfo = ReadPosXrayInfo(tableQuery);
  40. ASSERT(pXrayInfo);
  41. if (!pXrayInfo)
  42. {
  43. return listElementChemistry;
  44. }
  45. CElementChemistriesList listElement = pXrayInfo->GetElementQuantifyData();
  46. //CElementChemistryPtr pElement = CElementChemistryPtr(new CElementChemistry(*listElement[0].get()));
  47. CElementChemistryPtr pElement = listElement[0];
  48. listElementChemistry.push_back(pElement);
  49. }
  50. }
  51. return listElementChemistry;
  52. }
  53. CPosXrayInfoList& CElementChemistryDB::GetXrayInfoList(const BOOL a_bForce/* = FALSE*/)
  54. {
  55. if (a_bForce)
  56. {
  57. m_listPosXrayInfo.clear();
  58. }
  59. if (m_listPosXrayInfo.size() == 0)
  60. {
  61. ReadXrayPointInfoList();
  62. }
  63. return m_listPosXrayInfo;
  64. }
  65. BOOL CElementChemistryDB::SaveElementChemistriesList(const CPosXraysList& a_xrayPointList)
  66. {
  67. if (!Init())
  68. {
  69. ASSERT(FALSE);
  70. return FALSE;
  71. }
  72. auto tableInfoPtr = GetTableInfo();
  73. ASSERT(tableInfoPtr);
  74. if (!tableInfoPtr)
  75. {
  76. return FALSE;
  77. }
  78. auto datastorePtr = GetDatastore();
  79. ASSERT(datastorePtr);
  80. if (!datastorePtr)
  81. {
  82. return FALSE;
  83. }
  84. CString sInsertFormat = tableInfoPtr->GetInsertCommandFormatString(TRUE);
  85. CString sSQLCommand;
  86. for (auto& xrayPointInfo : a_xrayPointList)
  87. {
  88. CElementChemistriesList listElemnentChemistries = xrayPointInfo->GetElementQuantifyData();
  89. int nSize = (int)listElemnentChemistries.size();
  90. int nElementIndex = 0;
  91. for (auto& pElementChemistry : listElemnentChemistries)
  92. {
  93. sSQLCommand.Format(sInsertFormat,
  94. xrayPointInfo->GetIndex(),
  95. xrayPointInfo->GetScanFieldId(),
  96. nElementIndex,
  97. nSize,
  98. pElementChemistry->GetName(),
  99. pElementChemistry->GetPercentage());
  100. if (!datastorePtr->RunCommand(sSQLCommand))
  101. {
  102. LogErrorTrace(__FILE__, __LINE__, _T("Insert element(%d:%d:%d:%d) failed: %s command error"),
  103. xrayPointInfo->GetIndex(),
  104. xrayPointInfo->GetScanFieldId(),
  105. nElementIndex,
  106. nSize,
  107. pElementChemistry->GetName(),
  108. pElementChemistry->GetPercentage(),
  109. sSQLCommand);
  110. ASSERT(FALSE);
  111. return FALSE;
  112. }
  113. nElementIndex++;
  114. }
  115. }
  116. return TRUE;
  117. }
  118. BOOL CElementChemistryDB::SaveElementChemistriesList(const CPosXrayPtr a_pxrayPoint)
  119. {
  120. if (!Init())
  121. {
  122. ASSERT(FALSE);
  123. return FALSE;
  124. }
  125. auto tableInfoPtr = GetTableInfo();
  126. ASSERT(tableInfoPtr);
  127. if (!tableInfoPtr)
  128. {
  129. return FALSE;
  130. }
  131. auto datastorePtr = GetDatastore();
  132. ASSERT(datastorePtr);
  133. if (!datastorePtr)
  134. {
  135. return FALSE;
  136. }
  137. CString sInsertFormat = tableInfoPtr->GetInsertCommandFormatString(TRUE);
  138. CString sSQLCommand;
  139. CElementChemistriesList listElemnentChemistries = a_pxrayPoint->GetElementQuantifyData();
  140. int nSize = (int)listElemnentChemistries.size();
  141. int nElementIndex = 0;
  142. for (auto& pElementChemistry : listElemnentChemistries)
  143. {
  144. sSQLCommand.Format(sInsertFormat,
  145. a_pxrayPoint->GetIndex(),
  146. a_pxrayPoint->GetScanFieldId(),
  147. nElementIndex,
  148. nSize,
  149. pElementChemistry->GetName(),
  150. pElementChemistry->GetPercentage(),
  151. pElementChemistry->GetMolarPercentage());
  152. if (!datastorePtr->RunCommand(sSQLCommand))
  153. {
  154. LogErrorTrace(__FILE__, __LINE__, _T("Insert element(%d:%d:%d:%d) failed: %s command error"),
  155. a_pxrayPoint->GetIndex(),
  156. a_pxrayPoint->GetScanFieldId(),
  157. nElementIndex,
  158. nSize,
  159. pElementChemistry->GetName(),
  160. pElementChemistry->GetPercentage(),
  161. pElementChemistry->GetMolarPercentage(),
  162. sSQLCommand);
  163. ASSERT(FALSE);
  164. return FALSE;
  165. }
  166. nElementIndex++;
  167. }
  168. return TRUE;
  169. }
  170. BOOL CElementChemistryDB::DeleteElementChemistryById(const long a_nFieldId, const long a_nXrayId)
  171. {
  172. if (!m_listPosXrayInfo.empty())
  173. {
  174. 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); });
  175. }
  176. auto tableInfoPtr = GetTableInfo();
  177. ASSERT(tableInfoPtr);
  178. if (!tableInfoPtr)
  179. {
  180. return FALSE;
  181. }
  182. auto datastorePtr = GetDatastore();
  183. ASSERT(datastorePtr);
  184. if (!datastorePtr)
  185. {
  186. return FALSE;
  187. }
  188. CString sTableName = tableInfoPtr->GetTableName();
  189. if (!datastorePtr->IsTableExists(sTableName))
  190. {
  191. LogTrace(__FILE__, __LINE__, _T("Table %s not exist"), sTableName);
  192. return TRUE;
  193. }
  194. CString sXrayIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN);
  195. CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN);
  196. CString sSQLCommand;
  197. sSQLCommand.Format(_T("DELETE FROM \'%s\' WHERE %s = %d AND %s = %d AND %s = %d AND %s = %d;"),
  198. (LPCTSTR)tableInfoPtr->GetTableName(),
  199. (LPCTSTR)sXrayIdColumnName,
  200. a_nXrayId,
  201. (LPCTSTR)sFieldIdColumnName,
  202. a_nFieldId);
  203. return datastorePtr->RunCommand(sSQLCommand);
  204. }
  205. CDBTableBasePtr CElementChemistryDB::GetTableInfo()
  206. {
  207. /*if (!m_tableInfo)
  208. {
  209. m_tableInfo.reset(new CElementChemistryTable);
  210. }*/
  211. return m_tableInfo;
  212. }
  213. BOOL CElementChemistryDB::Init(const BOOL a_bClean /*= FALSE*/)
  214. {
  215. return myDB->Init();
  216. }
  217. BOOL CElementChemistryDB::CreateTable(const BOOL a_bForce /*= FALSE*/)
  218. {
  219. return myDB->CreateTable(a_bForce);
  220. }
  221. BOOL CElementChemistryDB::DeleteTable()
  222. {
  223. return myDB->DeleteTable();
  224. }
  225. BOOL CElementChemistryDB::RemoveAllRows()
  226. {
  227. return myDB->RemoveAllRows();
  228. }
  229. BOOL CElementChemistryDB::IsDBExist()
  230. {
  231. return myDB->IsDBExist();
  232. }
  233. OTSSQLITE::CDBStoreBasePtr CElementChemistryDB::GetDatastore()
  234. {
  235. return myDB->GetDatastore();
  236. }
  237. OTSSQLITE::CDBQueryBasePtr CElementChemistryDB::GetTableQuery(LPCTSTR a_sOrderColumnName /*= nullptr*/)
  238. {
  239. return myDB->GetTableQuery(a_sOrderColumnName);
  240. }
  241. BOOL CElementChemistryDB::ReadXrayPointInfoList()
  242. {
  243. auto tableInfoPtr = GetTableInfo();
  244. ASSERT(tableInfoPtr);
  245. if (!tableInfoPtr)
  246. {
  247. return FALSE;
  248. }
  249. auto query = GetTableQuery();
  250. ASSERT(query);
  251. if (!query)
  252. {
  253. return FALSE;
  254. }
  255. m_listPosXrayInfo = ReadXrayPointInfoList(query);
  256. return TRUE;
  257. }
  258. CPosXrayInfoList CElementChemistryDB::ReadXrayPointInfoList(CDBQueryBasePtr a_query)
  259. {
  260. CPosXrayInfoList xrayPointInfoVec;
  261. int nRowId = 0;
  262. int nWrongItems = 0;
  263. while (!a_query->IsEOF())
  264. {
  265. auto xrayPointInfo = ReadPosXrayInfo(a_query); //current x-ray point
  266. if (!xrayPointInfo)
  267. {
  268. LogErrorTrace(__FILE__, __LINE__, _T("Read xray point info item failed: row id: %d"), nRowId);
  269. nWrongItems++;
  270. }
  271. else
  272. {
  273. if (!xrayPointInfoVec.empty())
  274. {
  275. int a_nXrayId = xrayPointInfo->GetIndex();
  276. CElementChemistriesList listElementNew = xrayPointInfo->GetElementQuantifyData();
  277. int nIndex = 0;
  278. for (auto xrayInfo : xrayPointInfoVec)
  279. {
  280. xrayPointInfoVec.erase(xrayPointInfoVec.begin() + nIndex);
  281. if (xrayInfo->GetIndex() == a_nXrayId)
  282. {
  283. CElementChemistriesList listElementOld = xrayInfo->GetElementQuantifyData();
  284. for (auto pElement : listElementNew)
  285. {
  286. listElementOld.push_back(CElementChemistryPtr(new CElementChemistry(*pElement.get())));
  287. }
  288. xrayPointInfo->SetElementQuantifyData(listElementOld);
  289. break;
  290. }
  291. nIndex++;
  292. }
  293. }
  294. xrayPointInfoVec.push_back(xrayPointInfo);
  295. }
  296. a_query->NextRow();
  297. nRowId++;
  298. }
  299. return xrayPointInfoVec;
  300. }
  301. CPosXrayInfoPtr CElementChemistryDB::ReadPosXrayInfo(CDBQueryBasePtr a_query)
  302. {
  303. int nCol;
  304. CPosXrayInfoPtr xrayPointPtr(new CPosXrayInfo());
  305. int nXrayIdNow;
  306. int nFieldIdNow;
  307. nCol = (int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN;
  308. nXrayIdNow = a_query->GetColIntValue(nCol, -1);
  309. xrayPointPtr->SetIndex(nXrayIdNow);
  310. nCol = (int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN;
  311. nFieldIdNow = a_query->GetColIntValue(nCol, -1);
  312. xrayPointPtr->SetScanFieldId(nFieldIdNow);
  313. nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN;
  314. int nElementIndex = a_query->GetColIntValue(nCol, -1);
  315. nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN;
  316. int nElementTotal = a_query->GetColIntValue(nCol, -1);
  317. xrayPointPtr->SetElementNum(nElementTotal);
  318. if (nElementIndex > nElementTotal - 1)
  319. {
  320. return nullptr;
  321. }
  322. CElementChemistryPtr pElementChemistry = CElementChemistryPtr(new CElementChemistry());
  323. nCol = (int)CElementChemistryTable::ColumnID::S_NAME - (int)CElementChemistryTable::ColumnID::MIN;
  324. pElementChemistry->SetName(a_query->GetColStringValue(nCol, _T("")));
  325. nCol = (int)CElementChemistryTable::ColumnID::F_PERCENTAGE - (int)CElementChemistryTable::ColumnID::MIN;
  326. pElementChemistry->SetPercentage(a_query->GetColFloatValue(nCol, -1));
  327. CElementChemistriesList listElementChemistries;
  328. listElementChemistries.push_back(pElementChemistry);
  329. a_query->NextRow();
  330. int nXrayNew, nFieldIdNew;
  331. int nRowId = 0;
  332. while (!a_query->IsEOF())
  333. {
  334. nCol = (int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN;
  335. nXrayNew = a_query->GetColIntValue(nCol, -1);
  336. nCol = (int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN;
  337. nFieldIdNew = a_query->GetColIntValue(nCol, -1);
  338. if (nXrayNew == nXrayIdNow && nFieldIdNew == nFieldIdNow)
  339. {
  340. nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN;
  341. int nElementIndex = a_query->GetColIntValue(nCol, -1);
  342. nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN;
  343. int nElementTotal = a_query->GetColIntValue(nCol, -1);
  344. if (nElementIndex > nElementTotal - 1)
  345. {
  346. return nullptr;
  347. }
  348. CElementChemistryPtr pElementChemistry = CElementChemistryPtr(new CElementChemistry());
  349. nCol = (int)CElementChemistryTable::ColumnID::S_NAME - (int)CElementChemistryTable::ColumnID::MIN;
  350. pElementChemistry->SetName(a_query->GetColStringValue(nCol, _T("")));
  351. nCol = (int)CElementChemistryTable::ColumnID::F_PERCENTAGE - (int)CElementChemistryTable::ColumnID::MIN;
  352. pElementChemistry->SetPercentage(a_query->GetColFloatValue(nCol, -1));
  353. listElementChemistries.push_back(pElementChemistry);
  354. if (nElementIndex == nElementTotal - 1)
  355. {
  356. break;
  357. }
  358. }
  359. else
  360. {
  361. continue;
  362. }
  363. a_query->NextRow();
  364. nRowId++;
  365. }
  366. xrayPointPtr->SetElementQuantifyData(listElementChemistries);
  367. return xrayPointPtr;
  368. }
  369. CDBQueryBasePtr CElementChemistryDB::GetQueryById(const long a_nXrayId,const long a_nFieldId, const long a_nElementId, const long a_nElementSize)
  370. {
  371. CDBQueryBasePtr query;
  372. auto datastorePtr = GetDatastore();
  373. ASSERT(datastorePtr);
  374. if (!datastorePtr)
  375. {
  376. return query;
  377. }
  378. auto tableInfoPtr = GetTableInfo();
  379. ASSERT(tableInfoPtr);
  380. if (!tableInfoPtr)
  381. {
  382. return query;
  383. }
  384. CString sXrayIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN);
  385. CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN);
  386. CString sElementIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN);
  387. CString sElementNumColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN);
  388. CString sSQLCommand;
  389. sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = %d AND %s = %d AND %s = %d AND %s = %d;"),
  390. (LPCTSTR)tableInfoPtr->GetTableName(),
  391. (LPCTSTR)sFieldIdColumnName,
  392. a_nFieldId,
  393. (LPCTSTR)sXrayIdColumnName,
  394. a_nXrayId,
  395. (LPCTSTR)sElementIdColumnName,
  396. a_nElementId,
  397. (LPCTSTR)sElementNumColumnName,
  398. a_nElementSize);
  399. query = datastorePtr->QueryByCommand(sSQLCommand);
  400. ASSERT(query);
  401. if (!query || !query->IsValid())
  402. {
  403. LogErrorTrace(__FILE__, __LINE__, _T("Invalid quary command (%s)."), (LPCTSTR)sSQLCommand);
  404. ASSERT(FALSE);
  405. return (CDBQueryBasePtr());
  406. }
  407. // do the table related valid checking
  408. if (query->GetColCount() != GetTableInfo()->GetColumnCount())
  409. {
  410. LogErrorTrace(__FILE__, __LINE__, _T("query col num value is %d, but not %d"), query->GetColCount(), GetTableInfo()->GetColumnCount());
  411. ASSERT(FALSE);
  412. return (CDBQueryBasePtr());
  413. }
  414. return query;
  415. }
  416. //CDBQueryBasePtr CElementChemistryDB::GetQueryById(const long a_nXrayId, const long a_nFieldId)
  417. //{
  418. // CDBQueryBasePtr query;
  419. // auto datastorePtr = GetDatastore();
  420. // ASSERT(datastorePtr);
  421. // if (!datastorePtr)
  422. // {
  423. // return query;
  424. // }
  425. // auto tableInfoPtr = GetTableInfo();
  426. // ASSERT(tableInfoPtr);
  427. // if (!tableInfoPtr)
  428. // {
  429. // return query;
  430. // }
  431. // CString sXrayIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN);
  432. // CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN);
  433. //
  434. // CString sSQLCommand;
  435. // sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = %d AND %s = %d;"),
  436. // (LPCTSTR)tableInfoPtr->GetTableName(),
  437. // (LPCTSTR)sFieldIdColumnName,
  438. // a_nFieldId,
  439. // (LPCTSTR)sXrayIdColumnName,
  440. // a_nXrayId);
  441. // query = datastorePtr->QueryByCommand(sSQLCommand);
  442. // ASSERT(query);
  443. // if (!query || !query->IsValid())
  444. // {
  445. // LogErrorTrace(__FILE__, __LINE__, _T("Invalid quary command (%s)."), (LPCTSTR)sSQLCommand);
  446. // ASSERT(FALSE);
  447. // return (CDBQueryBasePtr());
  448. // }
  449. // // do the table related valid checking
  450. // if (query->GetColCount() != GetTableInfo()->GetColumnCount())
  451. // {
  452. // LogErrorTrace(__FILE__, __LINE__, _T("query col num value is %d, but not %d"), query->GetColCount(), GetTableInfo()->GetColumnCount());
  453. // ASSERT(FALSE);
  454. // return (CDBQueryBasePtr());
  455. // }
  456. // return query;
  457. //}
  458. }