DictManager.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**************************************************************************************************
  2. 版本所有(C), 2021-12-20, 北京普达迪泰
  3. ***************************************************************************************************
  4. 文 件 名:DictManager.h
  5. 版 本 号: 1.00初版
  6. 作 者: HYF
  7. 生成日期:2021-12-20
  8. 最近修改:
  9. 功能描述: 数据字典
  10. 函数列表:
  11. 修改历史:
  12. 1.日 期 : 2021-12-20
  13. 作 者 : HYF
  14. 修改内容 : 创建文件
  15. **************************************************************************************************/
  16. #include "pch.h"
  17. #include "DictManager.h"
  18. #include "CppSQLite3.h"
  19. #include "DBFactory.h"
  20. #include "Dict.h"
  21. DictManager::DictManager()
  22. {
  23. }
  24. DictManager::~DictManager()
  25. {
  26. }
  27. /**************************************************************************************************
  28. 函 数 名 :Query
  29. 功能描述 :查询
  30. 输出参数 :无
  31. 返 回 值 :CppSQLite3Query
  32. **************************************************************************************************/
  33. CppSQLite3Query DictManager::Query()
  34. {
  35. try
  36. {
  37. CppSQLite3DB* pDB = DBFactory::GetSQLite();
  38. return pDB->execQuery(_T("SELECT * FROM dict;"));
  39. }
  40. catch (CppSQLite3Exception& e)
  41. {
  42. LOG::E(e.errorMessage());
  43. }
  44. }
  45. CppSQLite3Query DictManager::Query(LPCTSTR szCode)
  46. {
  47. try
  48. {
  49. CppSQLite3DB* pDB = DBFactory::GetSQLite();
  50. char szCmd[256];
  51. sprintf_s(szCmd, "SELECT * FROM dict WHERE item_code ='%Q';", szCode);
  52. return pDB->execQuery(szCmd);
  53. }
  54. catch (CppSQLite3Exception& e)
  55. {
  56. LOG::E(e.errorMessage());
  57. }
  58. }
  59. /**************************************************************************************************
  60. 函 数 名 :IsExist
  61. 功能描述 :基于PK查询是否存在
  62. 输入参数 :略
  63. 输出参数 :略
  64. 返 回 值 :TRUE 成功;FALSE 失败
  65. **************************************************************************************************/
  66. CString DictManager::GetDictItemTitle(LPCTSTR szCode, LPCTSTR szDefaultVal)
  67. {
  68. try
  69. {
  70. CppSQLite3DB* pDB = DBFactory::GetSQLite();
  71. CppSQLite3Buffer bufSQL;
  72. bufSQL.format("SELECT * FROM dict WHERE item_code=%Q;", szCode);
  73. CppSQLite3Query q = pDB->execQuery(bufSQL);
  74. bufSQL.clear();
  75. if (!q.eof())
  76. {
  77. CString szVal = q.getStringField("item_title");
  78. q.finalize();
  79. return szVal;
  80. }
  81. else
  82. {
  83. q.finalize();
  84. return CString(szDefaultVal);
  85. }
  86. }
  87. catch (CppSQLite3Exception& e)
  88. {
  89. LOG::E(e.errorMessage());
  90. return CString(szDefaultVal);
  91. }
  92. }
  93. /**************************************************************************************************
  94. 函 数 名 :IsExist
  95. 功能描述 :基于PK查询是否存在
  96. 输入参数 :略
  97. 输出参数 :略
  98. 返 回 值 :TRUE 成功;FALSE 失败
  99. **************************************************************************************************/
  100. BOOL DictManager::IsExist(LPCTSTR szCode)
  101. {
  102. try
  103. {
  104. CppSQLite3DB* pDB = DBFactory::GetSQLite();
  105. CppSQLite3Buffer bufSQL;
  106. bufSQL.format("SELECT * FROM dict WHERE item_code=%Q;", szCode);
  107. CppSQLite3Query q = pDB->execQuery(bufSQL);
  108. bufSQL.clear();
  109. if (!q.eof())
  110. {
  111. q.finalize();
  112. return TRUE;
  113. }
  114. else
  115. {
  116. q.finalize();
  117. return FALSE;
  118. }
  119. }
  120. catch (CppSQLite3Exception& e)
  121. {
  122. LOG::E(e.errorMessage());
  123. return FALSE;
  124. }
  125. }
  126. /**************************************************************************************************
  127. 函 数 名 :Add
  128. 功能描述 :增加Item
  129. 输入参数 :略
  130. 输出参数 :无
  131. 返 回 值 :TRUE 成功;FALSE 失败
  132. **************************************************************************************************/
  133. BOOL DictManager::Add(const Dict& data)
  134. {
  135. CString szItemCode = data.GetItemCode();
  136. CString szItemTitle = data.GetItemTitle();
  137. CString szItemRemark = data.GetItemRemark();
  138. try
  139. {
  140. CppSQLite3DB* pDB = DBFactory::GetSQLite();
  141. CppSQLite3Buffer bufSQL;
  142. bufSQL.format("INSERT INTO dict (item_code, item_title, item_remark) VALUES (%Q, %Q, %Q);",
  143. szItemCode,
  144. szItemTitle,
  145. szItemRemark);
  146. pDB->execDML("begin transaction;");
  147. pDB->execDML(bufSQL);
  148. pDB->execDML("commit transaction;");
  149. bufSQL.clear();
  150. }
  151. catch (CppSQLite3Exception& e)
  152. {
  153. LOG::E(e.errorMessage());
  154. return FALSE;
  155. }
  156. return TRUE;
  157. }
  158. /**************************************************************************************************
  159. 函 数 名 :Update
  160. 功能描述 :更新Item
  161. 输入参数 :略
  162. 输出参数 :无
  163. 返 回 值 :TRUE 成功;FALSE 失败
  164. **************************************************************************************************/
  165. BOOL DictManager::Update(const Dict& data)
  166. {
  167. CString szItemCode = data.GetItemCode();
  168. CString szItemTitle = data.GetItemTitle();
  169. CString szItemRemark = data.GetItemRemark();
  170. try
  171. {
  172. CppSQLite3DB* pDB = DBFactory::GetSQLite();
  173. CppSQLite3Buffer bufSQL;
  174. bufSQL.format("UPDATE dict SET item_title = %Q, item_remark=%Q WHERE item_code=%Q;",
  175. szItemTitle,
  176. szItemRemark,
  177. szItemCode);
  178. pDB->execDML("begin transaction;");
  179. int affected = pDB->execDML(bufSQL);
  180. pDB->execDML("commit transaction;");
  181. bufSQL.clear();
  182. }
  183. catch (CppSQLite3Exception& e)
  184. {
  185. LOG::E(e.errorMessage());
  186. return FALSE;
  187. }
  188. return TRUE;
  189. }
  190. /**************************************************************************************************
  191. 函 数 名 :Delete
  192. 功能描述 :删除Item
  193. 输入参数 :略
  194. 输出参数 :无
  195. 返 回 值 :TRUE 成功;FALSE 失败
  196. **************************************************************************************************/
  197. BOOL DictManager::Delete(LPCTSTR szCode)
  198. {
  199. try
  200. {
  201. CppSQLite3DB* pDB = DBFactory::GetSQLite();
  202. CppSQLite3Buffer bufSQL;
  203. bufSQL.format("DELETE FROM dict WHERE item_code=%Q;", szCode);
  204. pDB->execDML("begin transaction;");
  205. pDB->execDML(bufSQL);
  206. pDB->execDML("commit transaction;");
  207. bufSQL.clear();
  208. }
  209. catch (CppSQLite3Exception& e)
  210. {
  211. LOG::E(e.errorMessage());
  212. return FALSE;
  213. }
  214. return TRUE;
  215. }
  216. /**************************************************************************************************
  217. 函 数 名 :ClearAll
  218. 功能描述 :清除
  219. 输入参数 :无
  220. 输出参数 :无
  221. 返 回 值 :TRUE 成功;FALSE 失败
  222. **************************************************************************************************/
  223. BOOL DictManager::ClearAll()
  224. {
  225. try
  226. {
  227. CppSQLite3DB* pDB = DBFactory::GetSQLite();
  228. CppSQLite3Buffer bufSQL;
  229. bufSQL.format("DELETE FROM dict");//删除语句
  230. pDB->execDML("begin transaction;");
  231. pDB->execDML(bufSQL);
  232. pDB->execDML("commit transaction;");
  233. bufSQL.clear();
  234. }
  235. catch (CppSQLite3Exception& e)
  236. {
  237. LOG::E(e.errorMessage());
  238. return FALSE;
  239. }
  240. return TRUE;
  241. }