/************************************************************************************************** 版本所有(C), 2021-12-20, 北京普达迪泰 *************************************************************************************************** 文 件 名:DictManager.h 版 本 号: 1.00初版 作 者: HYF 生成日期:2021-12-20 最近修改: 功能描述: 数据字典 函数列表: 修改历史: 1.日 期 : 2021-12-20 作 者 : HYF 修改内容 : 创建文件 **************************************************************************************************/ #include "pch.h" #include "DictManager.h" #include "CppSQLite3.h" #include "DBFactory.h" #include "Dict.h" DictManager::DictManager() { } DictManager::~DictManager() { } /************************************************************************************************** 函 数 名 :Query 功能描述 :查询 输出参数 :无 返 回 值 :CppSQLite3Query **************************************************************************************************/ CppSQLite3Query DictManager::Query() { try { CppSQLite3DB* pDB = DBFactory::GetSQLite(); return pDB->execQuery(_T("SELECT * FROM dict;")); } catch (CppSQLite3Exception& e) { LOG::E(e.errorMessage()); } } CppSQLite3Query DictManager::Query(LPCTSTR szCode) { try { CppSQLite3DB* pDB = DBFactory::GetSQLite(); char szCmd[256]; sprintf_s(szCmd, "SELECT * FROM dict WHERE item_code ='%Q';", szCode); return pDB->execQuery(szCmd); } catch (CppSQLite3Exception& e) { LOG::E(e.errorMessage()); } } /************************************************************************************************** 函 数 名 :IsExist 功能描述 :基于PK查询是否存在 输入参数 :略 输出参数 :略 返 回 值 :TRUE 成功;FALSE 失败 **************************************************************************************************/ CString DictManager::GetDictItemTitle(LPCTSTR szCode, LPCTSTR szDefaultVal) { try { CppSQLite3DB* pDB = DBFactory::GetSQLite(); CppSQLite3Buffer bufSQL; bufSQL.format("SELECT * FROM dict WHERE item_code=%Q;", szCode); CppSQLite3Query q = pDB->execQuery(bufSQL); bufSQL.clear(); if (!q.eof()) { CString szVal = q.getStringField("item_title"); q.finalize(); return szVal; } else { q.finalize(); return CString(szDefaultVal); } } catch (CppSQLite3Exception& e) { LOG::E(e.errorMessage()); return CString(szDefaultVal); } } /************************************************************************************************** 函 数 名 :IsExist 功能描述 :基于PK查询是否存在 输入参数 :略 输出参数 :略 返 回 值 :TRUE 成功;FALSE 失败 **************************************************************************************************/ BOOL DictManager::IsExist(LPCTSTR szCode) { try { CppSQLite3DB* pDB = DBFactory::GetSQLite(); CppSQLite3Buffer bufSQL; bufSQL.format("SELECT * FROM dict WHERE item_code=%Q;", szCode); CppSQLite3Query q = pDB->execQuery(bufSQL); bufSQL.clear(); if (!q.eof()) { q.finalize(); return TRUE; } else { q.finalize(); return FALSE; } } catch (CppSQLite3Exception& e) { LOG::E(e.errorMessage()); return FALSE; } } /************************************************************************************************** 函 数 名 :Add 功能描述 :增加Item 输入参数 :略 输出参数 :无 返 回 值 :TRUE 成功;FALSE 失败 **************************************************************************************************/ BOOL DictManager::Add(const Dict& data) { CString szItemCode = data.GetItemCode(); CString szItemTitle = data.GetItemTitle(); CString szItemRemark = data.GetItemRemark(); try { CppSQLite3DB* pDB = DBFactory::GetSQLite(); CppSQLite3Buffer bufSQL; bufSQL.format("INSERT INTO dict (item_code, item_title, item_remark) VALUES (%Q, %Q, %Q);", szItemCode, szItemTitle, szItemRemark); pDB->execDML("begin transaction;"); pDB->execDML(bufSQL); pDB->execDML("commit transaction;"); bufSQL.clear(); } catch (CppSQLite3Exception& e) { LOG::E(e.errorMessage()); return FALSE; } return TRUE; } /************************************************************************************************** 函 数 名 :Update 功能描述 :更新Item 输入参数 :略 输出参数 :无 返 回 值 :TRUE 成功;FALSE 失败 **************************************************************************************************/ BOOL DictManager::Update(const Dict& data) { CString szItemCode = data.GetItemCode(); CString szItemTitle = data.GetItemTitle(); CString szItemRemark = data.GetItemRemark(); try { CppSQLite3DB* pDB = DBFactory::GetSQLite(); CppSQLite3Buffer bufSQL; bufSQL.format("UPDATE dict SET item_title = %Q, item_remark=%Q WHERE item_code=%Q;", szItemTitle, szItemRemark, szItemCode); pDB->execDML("begin transaction;"); int affected = pDB->execDML(bufSQL); pDB->execDML("commit transaction;"); bufSQL.clear(); } catch (CppSQLite3Exception& e) { LOG::E(e.errorMessage()); return FALSE; } return TRUE; } /************************************************************************************************** 函 数 名 :Delete 功能描述 :删除Item 输入参数 :略 输出参数 :无 返 回 值 :TRUE 成功;FALSE 失败 **************************************************************************************************/ BOOL DictManager::Delete(LPCTSTR szCode) { try { CppSQLite3DB* pDB = DBFactory::GetSQLite(); CppSQLite3Buffer bufSQL; bufSQL.format("DELETE FROM dict WHERE item_code=%Q;", szCode); pDB->execDML("begin transaction;"); pDB->execDML(bufSQL); pDB->execDML("commit transaction;"); bufSQL.clear(); } catch (CppSQLite3Exception& e) { LOG::E(e.errorMessage()); return FALSE; } return TRUE; } /************************************************************************************************** 函 数 名 :ClearAll 功能描述 :清除 输入参数 :无 输出参数 :无 返 回 值 :TRUE 成功;FALSE 失败 **************************************************************************************************/ BOOL DictManager::ClearAll() { try { CppSQLite3DB* pDB = DBFactory::GetSQLite(); CppSQLite3Buffer bufSQL; bufSQL.format("DELETE FROM dict");//删除语句 pDB->execDML("begin transaction;"); pDB->execDML(bufSQL); pDB->execDML("commit transaction;"); bufSQL.clear(); } catch (CppSQLite3Exception& e) { LOG::E(e.errorMessage()); return FALSE; } return TRUE; }