ElementChemistryDB.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. auto pXrayInfoMap = ReadPosXrayInfo(tableQuery);
  40. if (pXrayInfoMap.size()==0)
  41. {
  42. return listElementChemistry;
  43. }
  44. std::vector<int> idvec;
  45. idvec.push_back(a_nFieldId);
  46. idvec.push_back(a_nXrayId);
  47. listElementChemistry = pXrayInfoMap[idvec]->GetElementQuantifyData();
  48. }
  49. }
  50. return listElementChemistry;
  51. }
  52. //}
  53. BOOL CElementChemistryDB::SaveElementChemistriesList(const CPosXrayList& a_xrayPointList)
  54. {
  55. if (!Init())
  56. {
  57. ASSERT(FALSE);
  58. return FALSE;
  59. }
  60. auto tableInfoPtr = GetTableInfo();
  61. ASSERT(tableInfoPtr);
  62. if (!tableInfoPtr)
  63. {
  64. return FALSE;
  65. }
  66. auto datastorePtr = GetDatastore();
  67. ASSERT(datastorePtr);
  68. if (!datastorePtr)
  69. {
  70. return FALSE;
  71. }
  72. CString sInsertFormat = tableInfoPtr->GetInsertCommandFormatString(TRUE);
  73. CString sSQLCommand;
  74. for (auto& xrayPointInfo : a_xrayPointList)
  75. {
  76. CElementChemistriesList listElemnentChemistries = xrayPointInfo->GetElementQuantifyData();
  77. int nSize = (int)listElemnentChemistries.size();
  78. int nElementIndex = 0;
  79. for (auto& pElementChemistry : listElemnentChemistries)
  80. {
  81. sSQLCommand.Format(sInsertFormat,
  82. xrayPointInfo->GetIndex(),
  83. xrayPointInfo->GetScanFieldId(),
  84. nElementIndex,
  85. nSize,
  86. pElementChemistry->GetName(),
  87. pElementChemistry->GetPercentage());
  88. if (!datastorePtr->RunCommand(sSQLCommand))
  89. {
  90. LogErrorTrace(__FILE__, __LINE__, _T("Insert element(%d:%d:%d:%d) failed: %s command error"),
  91. xrayPointInfo->GetIndex(),
  92. xrayPointInfo->GetScanFieldId(),
  93. nElementIndex,
  94. nSize,
  95. pElementChemistry->GetName(),
  96. pElementChemistry->GetPercentage(),
  97. sSQLCommand);
  98. ASSERT(FALSE);
  99. return FALSE;
  100. }
  101. nElementIndex++;
  102. }
  103. }
  104. return TRUE;
  105. }
  106. BOOL CElementChemistryDB::SaveElementChemistriesList(const CPosXrayPtr a_pxrayPoint)
  107. {
  108. if (!Init())
  109. {
  110. ASSERT(FALSE);
  111. return FALSE;
  112. }
  113. auto tableInfoPtr = GetTableInfo();
  114. ASSERT(tableInfoPtr);
  115. if (!tableInfoPtr)
  116. {
  117. return FALSE;
  118. }
  119. auto datastorePtr = GetDatastore();
  120. ASSERT(datastorePtr);
  121. if (!datastorePtr)
  122. {
  123. return FALSE;
  124. }
  125. CString sInsertFormat = tableInfoPtr->GetInsertCommandFormatString(TRUE);
  126. CString sSQLCommand;
  127. CElementChemistriesList listElemnentChemistries = a_pxrayPoint->GetElementQuantifyData();
  128. int nSize = (int)listElemnentChemistries.size();
  129. int nElementIndex = 0;
  130. for (auto& pElementChemistry : listElemnentChemistries)
  131. {
  132. sSQLCommand.Format(sInsertFormat,
  133. a_pxrayPoint->GetIndex(),
  134. a_pxrayPoint->GetScanFieldId(),
  135. nElementIndex,
  136. nSize,
  137. pElementChemistry->GetName(),
  138. pElementChemistry->GetPercentage(),
  139. pElementChemistry->GetMolarPercentage());
  140. if (!datastorePtr->RunCommand(sSQLCommand))
  141. {
  142. LogErrorTrace(__FILE__, __LINE__, _T("Insert element(%d:%d:%d:%d) failed: %s command error"),
  143. a_pxrayPoint->GetIndex(),
  144. a_pxrayPoint->GetScanFieldId(),
  145. nElementIndex,
  146. nSize,
  147. pElementChemistry->GetName(),
  148. pElementChemistry->GetPercentage(),
  149. pElementChemistry->GetMolarPercentage(),
  150. sSQLCommand);
  151. ASSERT(FALSE);
  152. return FALSE;
  153. }
  154. nElementIndex++;
  155. }
  156. return TRUE;
  157. }
  158. BOOL CElementChemistryDB::DeleteElementChemistryById(const long a_nFieldId, const long a_nXrayId)
  159. {
  160. if (!m_listPosXrayInfo.empty())
  161. {
  162. 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); });
  163. }
  164. auto tableInfoPtr = GetTableInfo();
  165. ASSERT(tableInfoPtr);
  166. if (!tableInfoPtr)
  167. {
  168. return FALSE;
  169. }
  170. auto datastorePtr = GetDatastore();
  171. ASSERT(datastorePtr);
  172. if (!datastorePtr)
  173. {
  174. return FALSE;
  175. }
  176. CString sTableName = tableInfoPtr->GetTableName();
  177. if (!datastorePtr->IsTableExists(sTableName))
  178. {
  179. LogTrace(__FILE__, __LINE__, _T("Table %s not exist"), sTableName);
  180. return TRUE;
  181. }
  182. CString sXrayIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN);
  183. CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN);
  184. CString sSQLCommand;
  185. sSQLCommand.Format(_T("DELETE FROM \'%s\' WHERE %s = %d AND %s = %d AND %s = %d AND %s = %d;"),
  186. (LPCTSTR)tableInfoPtr->GetTableName(),
  187. (LPCTSTR)sXrayIdColumnName,
  188. a_nXrayId,
  189. (LPCTSTR)sFieldIdColumnName,
  190. a_nFieldId);
  191. return datastorePtr->RunCommand(sSQLCommand);
  192. }
  193. CDBTableBasePtr CElementChemistryDB::GetTableInfo()
  194. {
  195. return m_tableInfo;
  196. }
  197. BOOL CElementChemistryDB::Init(const BOOL a_bClean /*= FALSE*/)
  198. {
  199. return myDB->Init();
  200. }
  201. BOOL CElementChemistryDB::CreateTable(const BOOL a_bForce /*= FALSE*/)
  202. {
  203. return myDB->CreateTable(a_bForce);
  204. }
  205. BOOL CElementChemistryDB::DeleteTable()
  206. {
  207. return myDB->DeleteTable();
  208. }
  209. BOOL CElementChemistryDB::RemoveAllRows()
  210. {
  211. return myDB->RemoveAllRows();
  212. }
  213. BOOL CElementChemistryDB::IsDBExist()
  214. {
  215. return myDB->IsDBExist();
  216. }
  217. OTSSQLITE::CDBStoreBasePtr CElementChemistryDB::GetDatastore()
  218. {
  219. return myDB->GetDatastore();
  220. }
  221. OTSSQLITE::CDBQueryBasePtr CElementChemistryDB::GetTableQuery(LPCTSTR a_sOrderColumnName /*= nullptr*/)
  222. {
  223. return myDB->GetTableQuery(a_sOrderColumnName);
  224. }
  225. std::map<std::vector<int>, CPosXrayPtr> CElementChemistryDB::ReadPosXrayInfo(OTSSQLITE::CDBQueryBasePtr a_query)
  226. {
  227. std::map<std::vector<int>, CPosXrayPtr> mapXrayInfo;
  228. CDBQueryBasePtr query;
  229. if (a_query == nullptr)
  230. {
  231. auto tableInfoPtr = GetTableInfo();
  232. ASSERT(tableInfoPtr);
  233. if (!tableInfoPtr)
  234. {
  235. return mapXrayInfo;
  236. }
  237. query = GetTableQuery();
  238. ASSERT(query);
  239. if (!query)
  240. {
  241. return mapXrayInfo;
  242. }
  243. }
  244. else
  245. {
  246. query = a_query;
  247. }
  248. while (!query->IsEOF())
  249. {
  250. CPosXrayPtr xrayInfo;
  251. int nCol;
  252. int nXrayIdNow;
  253. int nFieldIdNow;
  254. nCol = (int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN;
  255. nXrayIdNow = query->GetColIntValue(nCol, -1);
  256. nCol = (int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN;
  257. nFieldIdNow = query->GetColIntValue(nCol, -1);
  258. std::vector <int> fldvec;
  259. fldvec.push_back(nFieldIdNow);
  260. fldvec.push_back(nXrayIdNow);
  261. if (mapXrayInfo.find(fldvec) != mapXrayInfo.end())
  262. {
  263. xrayInfo = mapXrayInfo[fldvec];
  264. }
  265. else
  266. {
  267. CPosXrayPtr xrayPointPtr(new CPosXray());
  268. mapXrayInfo[fldvec] = xrayPointPtr;
  269. xrayPointPtr->SetIndex(nXrayIdNow);
  270. xrayPointPtr->SetScanFieldId(nFieldIdNow);
  271. xrayInfo = xrayPointPtr;
  272. }
  273. nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN;
  274. int nElementIndex = query->GetColIntValue(nCol, -1);
  275. nCol = (int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN;
  276. int nElementTotal = query->GetColIntValue(nCol, -1);
  277. xrayInfo->SetElementNum(nElementTotal);
  278. CElementChemistryPtr pElementChemistry = CElementChemistryPtr(new CElementChemistry());
  279. nCol = (int)CElementChemistryTable::ColumnID::S_NAME - (int)CElementChemistryTable::ColumnID::MIN;
  280. pElementChemistry->SetName(query->GetColStringValue(nCol, _T("")));
  281. nCol = (int)CElementChemistryTable::ColumnID::F_PERCENTAGE - (int)CElementChemistryTable::ColumnID::MIN;
  282. pElementChemistry->SetPercentage(query->GetColFloatValue(nCol, -1));
  283. xrayInfo->AddElementQuantifyData(pElementChemistry);
  284. query->NextRow();
  285. }
  286. return mapXrayInfo;
  287. }
  288. CDBQueryBasePtr CElementChemistryDB::GetQueryById(const long a_nXrayId,const long a_nFieldId, const long a_nElementId, const long a_nElementSize)
  289. {
  290. CDBQueryBasePtr query;
  291. auto datastorePtr = GetDatastore();
  292. ASSERT(datastorePtr);
  293. if (!datastorePtr)
  294. {
  295. return query;
  296. }
  297. auto tableInfoPtr = GetTableInfo();
  298. ASSERT(tableInfoPtr);
  299. if (!tableInfoPtr)
  300. {
  301. return query;
  302. }
  303. CString sXrayIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_XRAY_INDEX - (int)CElementChemistryTable::ColumnID::MIN);
  304. CString sFieldIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_FIELD_ID - (int)CElementChemistryTable::ColumnID::MIN);
  305. CString sElementIdColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_ELEMENT_ID - (int)CElementChemistryTable::ColumnID::MIN);
  306. CString sElementNumColumnName = tableInfoPtr->GetColumnName((int)CElementChemistryTable::ColumnID::N_ELEMENT_TOTAL - (int)CElementChemistryTable::ColumnID::MIN);
  307. CString sSQLCommand;
  308. sSQLCommand.Format(_T("SELECT * FROM \'%s\' WHERE %s = %d AND %s = %d AND %s = %d AND %s = %d;"),
  309. (LPCTSTR)tableInfoPtr->GetTableName(),
  310. (LPCTSTR)sFieldIdColumnName,
  311. a_nFieldId,
  312. (LPCTSTR)sXrayIdColumnName,
  313. a_nXrayId,
  314. (LPCTSTR)sElementIdColumnName,
  315. a_nElementId,
  316. (LPCTSTR)sElementNumColumnName,
  317. a_nElementSize);
  318. query = datastorePtr->QueryByCommand(sSQLCommand);
  319. ASSERT(query);
  320. if (!query || !query->IsValid())
  321. {
  322. LogErrorTrace(__FILE__, __LINE__, _T("Invalid quary command (%s)."), (LPCTSTR)sSQLCommand);
  323. ASSERT(FALSE);
  324. return (CDBQueryBasePtr());
  325. }
  326. // do the table related valid checking
  327. if (query->GetColCount() != GetTableInfo()->GetColumnCount())
  328. {
  329. LogErrorTrace(__FILE__, __LINE__, _T("query col num value is %d, but not %d"), query->GetColCount(), GetTableInfo()->GetColumnCount());
  330. ASSERT(FALSE);
  331. return (CDBQueryBasePtr());
  332. }
  333. return query;
  334. }
  335. }