DataBaseService.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "StdAfx.h"
  2. #include "DataBaseService.h"
  3. #include <fstream>
  4. #include <iostream>
  5. #include "../Util/Util.h"
  6. #include "../SmartEvaluationLogic/ScanDll.h"
  7. CDataBaseService::CDataBaseService(void)
  8. {
  9. sqlite3_config(SQLITE_CONFIG_SERIALIZED);
  10. InitializeCriticalSection(&db_lock);
  11. }
  12. void CDataBaseService::SetDataBasePath(const wstring& _indexdbpath, const wstring& _batchdbpath,
  13. const wstring& _sharefilename, const wstring& _sharefilepath, const wstring& _workpath)
  14. {
  15. m_strIndexDBPath = _indexdbpath;
  16. m_strBatchDBPath = _batchdbpath;
  17. m_strShareFileName = _sharefilename;
  18. m_strShareFilePath = _sharefilepath;
  19. m_strWorkPath = _workpath;
  20. }
  21. CDataBaseService::~CDataBaseService(void)
  22. {
  23. }
  24. bool CDataBaseService::OpenIndexDB()
  25. {
  26. LOCK_GUARD _lock(db_lock);
  27. try
  28. {
  29. m_indexdb.open(UnicodeToUtf8(m_strIndexDBPath.c_str()).c_str());
  30. m_indexdb.execDML("pragma journal_mode = MEMORY");
  31. }
  32. catch (...)
  33. {
  34. return false;
  35. }
  36. return true;
  37. }
  38. bool CDataBaseService::OpenBatchDB()
  39. {
  40. LOCK_GUARD _lock(db_lock);
  41. try
  42. {
  43. m_batchdb.open(UnicodeToUtf8(m_strBatchDBPath.c_str()).c_str());
  44. m_batchdb.execDML("pragma journal_mode = MEMORY");
  45. }
  46. catch (...)
  47. {
  48. return false;
  49. }
  50. return true;
  51. }
  52. bool CDataBaseService::InsertPapers(vector<string>& list_papers)
  53. {
  54. LOCK_GUARD _lock(db_lock);
  55. if (!OpenBatchDB())
  56. {
  57. return false;
  58. }
  59. try
  60. {
  61. m_batchdb.execDML("begin transaction");
  62. for (size_t i = 0; i < list_papers.size() / 2; i++)
  63. {
  64. string _page1 = list_papers[i * 2];
  65. string _page2 = list_papers[i * 2 + 1];
  66. CppSQLite3Statement stmt = m_batchdb.compileStatement("INSERT INTO papers (page0, page1, state) VALUES (:page0, :page1, :state)");
  67. stmt.bind(":page0", _page1.c_str());
  68. stmt.bind(":page1", _page2.c_str());
  69. stmt.bind(":state", 0);
  70. stmt.execDML();
  71. stmt.finalize();
  72. }
  73. m_batchdb.execDML("commit transaction");
  74. m_batchdb.close();
  75. }
  76. catch (...)
  77. {
  78. m_batchdb.execDML("rollback transaction");
  79. OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception"));
  80. m_batchdb.close();
  81. return false;
  82. }
  83. return true;
  84. }
  85. bool CDataBaseService::InsertBatchInfo()
  86. {
  87. LOCK_GUARD _lock(db_lock);
  88. if (!OpenBatchDB())
  89. {
  90. return false;
  91. }
  92. CString strDir(m_strWorkPath.c_str());
  93. strDir.Replace(L"\\", L"/");
  94. CppSQLite3Statement stmt = m_batchdb.compileStatement("INSERT INTO batchinfo (work_dir) VALUES (:work_dir)");
  95. stmt.bind(":work_dir", UnicodeToUtf8(strDir).c_str());
  96. stmt.execDML();
  97. stmt.finalize();
  98. m_batchdb.close();
  99. return true;
  100. }
  101. bool CDataBaseService::InsertBatchFail(int state, int zipfile_cnt)
  102. {
  103. LOCK_GUARD _lock(db_lock);
  104. if (!OpenIndexDB())
  105. {
  106. return false;
  107. }
  108. try
  109. {
  110. m_indexdb.execDML("begin transaction");
  111. CString strBatch(m_strBatchDBPath.c_str());
  112. strBatch.Replace(L"\\", L"/");
  113. CString strDir(m_strWorkPath.c_str());
  114. strDir.Replace(L"\\", L"/");
  115. CppSQLite3Statement stmt = m_indexdb.compileStatement("INSERT INTO batchs (state, create_time, batchdb_path, total_cnt, success_cnt, work_dir, zipfile_name, zipfile_cnt) \
  116. VALUES (:state, :create_time, :batchdb_path, :total_cnt, :success_cnt, :work_dir, :zipfile_name, :zipfile_cnt)");
  117. stmt.bind(":state", state);
  118. stmt.bind(":create_time", (sqlite_int64)std::time(0));
  119. stmt.bind(":batchdb_path", UnicodeToUtf8(strBatch).c_str());
  120. stmt.bind(":total_cnt", 0);
  121. stmt.bind(":success_cnt", 0);
  122. stmt.bind(":zipfile_cnt", zipfile_cnt);
  123. stmt.bind(":work_dir", UnicodeToUtf8(strDir).c_str());
  124. stmt.bind(":zipfile_name", UnicodeToUtf8(m_strShareFileName.c_str()).c_str());
  125. stmt.execDML();
  126. stmt.finalize();
  127. m_indexdb.execDML("commit transaction");
  128. m_indexdb.close();
  129. }
  130. catch (...)
  131. {
  132. m_indexdb.execDML("rollback transaction");
  133. OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception"));
  134. m_indexdb.close();
  135. return false;
  136. }
  137. return true;
  138. }
  139. bool CDataBaseService::InsertIndex(int total_cnt, int& last_id)
  140. {
  141. LOCK_GUARD _lock(db_lock);
  142. if (!OpenIndexDB())
  143. {
  144. return false;
  145. }
  146. CString strBatch(m_strBatchDBPath.c_str());
  147. strBatch.Replace(L"\\", L"/");
  148. CString strDir(m_strWorkPath.c_str());
  149. strDir.Replace(L"\\", L"/");
  150. CppSQLite3Statement stmt = m_indexdb.compileStatement("INSERT INTO batchs (state, create_time, batchdb_path, total_cnt, success_cnt, work_dir, zipfile_name, zipfile_cnt) VALUES (:state, :create_time, :batchdb_path, :total_cnt, :success_cnt, :work_dir, :zipfile_name, :zipfile_cnt)");
  151. stmt.bind(":state", 0);
  152. stmt.bind(":create_time", (sqlite_int64)std::time(0));
  153. stmt.bind(":batchdb_path", UnicodeToUtf8(strBatch).c_str());
  154. stmt.bind(":total_cnt", total_cnt);
  155. stmt.bind(":success_cnt", 0);
  156. stmt.bind(":work_dir", UnicodeToUtf8(strDir).c_str());
  157. stmt.bind(":zipfile_cnt", total_cnt * 2);
  158. stmt.bind(":zipfile_name", UnicodeToUtf8(m_strShareFileName.c_str()).c_str());
  159. stmt.execDML();
  160. stmt.finalize();
  161. last_id = m_indexdb.execScalar("SELECT id FROM batchs ORDER BY ID DESC");
  162. m_indexdb.close();
  163. return true;
  164. }
  165. void CDataBaseService::LoadUnhandleBatch(const wstring& _indexdbpath, vector<int>& vct)
  166. {
  167. LOCK_GUARD _lock(db_lock);
  168. try
  169. {
  170. m_indexdb.open(UnicodeToUtf8(_indexdbpath.c_str()).c_str());
  171. m_indexdb.execDML("pragma journal_mode = MEMORY");
  172. CppSQLite3Query q = m_indexdb.execQuery("select * from batchs where state == 0");
  173. while (!q.eof())
  174. {
  175. vct.push_back(q.getIntField("id"));
  176. q.nextRow();
  177. }
  178. q.finalize();
  179. m_indexdb.close();
  180. }
  181. catch (...)
  182. {
  183. }
  184. }
  185. void CDataBaseService::GetBatchCount(const wstring& _indexdbpath, int id, int& cnt)
  186. {
  187. LOCK_GUARD _lock(db_lock);
  188. try
  189. {
  190. m_indexdb.open(UnicodeToUtf8(_indexdbpath.c_str()).c_str());
  191. m_indexdb.execDML("pragma journal_mode = MEMORY");
  192. char buf[256] = { 0 };
  193. sprintf(buf, "select * from batchs where id == %d", id);
  194. CppSQLite3Query q = m_indexdb.execQuery(buf);
  195. if (!q.eof())
  196. {
  197. cnt = q.getIntField("total_cnt");
  198. }
  199. q.finalize();
  200. m_indexdb.close();
  201. }
  202. catch (...)
  203. {
  204. }
  205. }
  206. void CDataBaseService::UpdateBatchSchemaErro(const wstring& _indexdbpath, int batch_id)
  207. {
  208. LOCK_GUARD _lock(db_lock);
  209. try
  210. {
  211. m_indexdb.open(UnicodeToUtf8(_indexdbpath.c_str()).c_str());
  212. m_indexdb.execDML("pragma journal_mode = MEMORY");
  213. CppSQLite3Statement stmt = m_indexdb.compileStatement("update batchs set state=:state where id = :id");
  214. stmt.bind(":state", batch_exc_invalid_qrcode);
  215. stmt.bind(":id", batch_id);
  216. stmt.execDML();
  217. stmt.finalize();
  218. m_indexdb.close();
  219. }
  220. catch (...)
  221. {
  222. }
  223. }