#include "StdAfx.h" #include "DataBaseService.h" #include #include #include "../Util/Util.h" #include "../SmartEvaluationLogic/ScanDll.h" CDataBaseService::CDataBaseService(void) { sqlite3_config(SQLITE_CONFIG_SERIALIZED); InitializeCriticalSection(&db_lock); } void CDataBaseService::SetDataBasePath(const wstring& _indexdbpath, const wstring& _batchdbpath, const wstring& _sharefilename, const wstring& _sharefilepath, const wstring& _workpath) { m_strIndexDBPath = _indexdbpath; m_strBatchDBPath = _batchdbpath; m_strShareFileName = _sharefilename; m_strShareFilePath = _sharefilepath; m_strWorkPath = _workpath; } CDataBaseService::~CDataBaseService(void) { } bool CDataBaseService::OpenIndexDB() { LOCK_GUARD _lock(db_lock); try { m_indexdb.open(UnicodeToUtf8(m_strIndexDBPath.c_str()).c_str()); m_indexdb.execDML("pragma journal_mode = MEMORY"); } catch (...) { return false; } return true; } bool CDataBaseService::OpenBatchDB() { LOCK_GUARD _lock(db_lock); try { m_batchdb.open(UnicodeToUtf8(m_strBatchDBPath.c_str()).c_str()); m_batchdb.execDML("pragma journal_mode = MEMORY"); } catch (...) { return false; } return true; } bool CDataBaseService::InsertPapers(vector& list_papers) { LOCK_GUARD _lock(db_lock); if (!OpenBatchDB()) { return false; } try { m_batchdb.execDML("begin transaction"); for (size_t i = 0; i < list_papers.size() / 2; i++) { string _page1 = list_papers[i * 2]; string _page2 = list_papers[i * 2 + 1]; CppSQLite3Statement stmt = m_batchdb.compileStatement("INSERT INTO papers (page0, page1, state) VALUES (:page0, :page1, :state)"); stmt.bind(":page0", _page1.c_str()); stmt.bind(":page1", _page2.c_str()); stmt.bind(":state", 0); stmt.execDML(); stmt.finalize(); } m_batchdb.execDML("commit transaction"); m_batchdb.close(); } catch (...) { m_batchdb.execDML("rollback transaction"); OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception")); m_batchdb.close(); return false; } return true; } bool CDataBaseService::InsertBatchInfo() { LOCK_GUARD _lock(db_lock); if (!OpenBatchDB()) { return false; } CString strDir(m_strWorkPath.c_str()); strDir.Replace(L"\\", L"/"); CppSQLite3Statement stmt = m_batchdb.compileStatement("INSERT INTO batchinfo (work_dir) VALUES (:work_dir)"); stmt.bind(":work_dir", UnicodeToUtf8(strDir).c_str()); stmt.execDML(); stmt.finalize(); m_batchdb.close(); return true; } bool CDataBaseService::InsertBatchFail(int state, int zipfile_cnt) { LOCK_GUARD _lock(db_lock); if (!OpenIndexDB()) { return false; } try { m_indexdb.execDML("begin transaction"); CString strBatch(m_strBatchDBPath.c_str()); strBatch.Replace(L"\\", L"/"); CString strDir(m_strWorkPath.c_str()); strDir.Replace(L"\\", L"/"); 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)"); stmt.bind(":state", state); stmt.bind(":create_time", (sqlite_int64)std::time(0)); stmt.bind(":batchdb_path", UnicodeToUtf8(strBatch).c_str()); stmt.bind(":total_cnt", 0); stmt.bind(":success_cnt", 0); stmt.bind(":zipfile_cnt", zipfile_cnt); stmt.bind(":work_dir", UnicodeToUtf8(strDir).c_str()); stmt.bind(":zipfile_name", UnicodeToUtf8(m_strShareFileName.c_str()).c_str()); stmt.execDML(); stmt.finalize(); m_indexdb.execDML("commit transaction"); m_indexdb.close(); } catch (...) { m_indexdb.execDML("rollback transaction"); OutputDebugString(_T("IResultHandler.SavePaper.catch CppSQLite3Exception")); m_indexdb.close(); return false; } return true; } bool CDataBaseService::InsertIndex(int total_cnt, int& last_id) { LOCK_GUARD _lock(db_lock); if (!OpenIndexDB()) { return false; } CString strBatch(m_strBatchDBPath.c_str()); strBatch.Replace(L"\\", L"/"); CString strDir(m_strWorkPath.c_str()); strDir.Replace(L"\\", L"/"); 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)"); stmt.bind(":state", 0); stmt.bind(":create_time", (sqlite_int64)std::time(0)); stmt.bind(":batchdb_path", UnicodeToUtf8(strBatch).c_str()); stmt.bind(":total_cnt", total_cnt); stmt.bind(":success_cnt", 0); stmt.bind(":work_dir", UnicodeToUtf8(strDir).c_str()); stmt.bind(":zipfile_cnt", total_cnt * 2); stmt.bind(":zipfile_name", UnicodeToUtf8(m_strShareFileName.c_str()).c_str()); stmt.execDML(); stmt.finalize(); last_id = m_indexdb.execScalar("SELECT id FROM batchs ORDER BY ID DESC"); m_indexdb.close(); return true; } void CDataBaseService::LoadUnhandleBatch(const wstring& _indexdbpath, vector& vct) { LOCK_GUARD _lock(db_lock); try { m_indexdb.open(UnicodeToUtf8(_indexdbpath.c_str()).c_str()); m_indexdb.execDML("pragma journal_mode = MEMORY"); CppSQLite3Query q = m_indexdb.execQuery("select * from batchs where state == 0"); while (!q.eof()) { vct.push_back(q.getIntField("id")); q.nextRow(); } q.finalize(); m_indexdb.close(); } catch (...) { } } void CDataBaseService::GetBatchCount(const wstring& _indexdbpath, int id, int& cnt) { LOCK_GUARD _lock(db_lock); try { m_indexdb.open(UnicodeToUtf8(_indexdbpath.c_str()).c_str()); m_indexdb.execDML("pragma journal_mode = MEMORY"); char buf[256] = { 0 }; sprintf(buf, "select * from batchs where id == %d", id); CppSQLite3Query q = m_indexdb.execQuery(buf); if (!q.eof()) { cnt = q.getIntField("total_cnt"); } q.finalize(); m_indexdb.close(); } catch (...) { } } void CDataBaseService::UpdateBatchSchemaErro(const wstring& _indexdbpath, int batch_id) { LOCK_GUARD _lock(db_lock); try { m_indexdb.open(UnicodeToUtf8(_indexdbpath.c_str()).c_str()); m_indexdb.execDML("pragma journal_mode = MEMORY"); CppSQLite3Statement stmt = m_indexdb.compileStatement("update batchs set state=:state where id = :id"); stmt.bind(":state", batch_exc_invalid_qrcode); stmt.bind(":id", batch_id); stmt.execDML(); stmt.finalize(); m_indexdb.close(); } catch (...) { } }