CSqlite3.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "../pch/pch.h"
  2. #include "CSqlite3.h"
  3. #include "CSetting.h"
  4. CSqlite3::CSqlite3()
  5. {
  6. wstring folderPath = CSystem::GetProgramDir() + L"\\db";
  7. if(!CSystem::IsDirExist(folderPath))
  8. {
  9. LOG_INFO("folderPath:" << folderPath.c_str() << ",没有找到对应的目录,即将创建");
  10. bool flag = CreateDirectory(folderPath.c_str(), NULL);
  11. if(flag == false)
  12. {
  13. LOG_INFO("新建 db 目录失败!");
  14. }
  15. LOG_INFO("新建 db 目录成功!");
  16. }
  17. //如果没有这个文件,这里会创建这个文件
  18. wstring path = CSystem::GetProgramDir() + L"\\db\\pos.db";
  19. string s_path = CLewaimaiString::UnicodeToUTF8(path);
  20. m_rc = sqlite3_open(s_path.c_str(), &m_db);
  21. if(m_rc)
  22. {
  23. LOG_INFO("Can't open database: " << sqlite3_errmsg(m_db));
  24. return;
  25. }
  26. else
  27. {
  28. LOG_INFO("Opened database successfully");
  29. }
  30. }
  31. CSqlite3::~CSqlite3()
  32. {
  33. if(m_db != NULL)
  34. {
  35. sqlite3_close(m_db);
  36. }
  37. }
  38. bool CSqlite3::InitConfig()
  39. {
  40. //检查有没有pos_config这个表,如果没有就创建
  41. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_config';";
  42. sqlite3_stmt* stmt = NULL;
  43. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  44. {
  45. if(sqlite3_step(stmt) == SQLITE_ROW)
  46. {
  47. int count = sqlite3_column_int(stmt, 0);
  48. if(count == 0)
  49. {
  50. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  51. sqlite3_finalize(stmt);
  52. stmt = NULL;
  53. sql = "CREATE TABLE pos_config(" \
  54. "name CHAR(100) UNIQUE NOT NULL," \
  55. "value CHAR(2000) NOT NULL);";
  56. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  57. {
  58. //执行该语句
  59. if(sqlite3_step(stmt) != SQLITE_DONE)
  60. {
  61. LOG_INFO("create table fail: " << sqlite3_errmsg(m_db));
  62. sqlite3_finalize(stmt);
  63. return false;
  64. }
  65. //走到这里就是表创建成功了
  66. LOG_INFO("create table success");
  67. sqlite3_finalize(stmt);
  68. }
  69. else
  70. {
  71. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  72. sqlite3_finalize(stmt);
  73. return false;
  74. }
  75. }
  76. else
  77. {
  78. //说明已经有这个表了,就不用再创建了
  79. sqlite3_finalize(stmt);
  80. }
  81. std::string sql = "SELECT * FROM pos_config;";
  82. sqlite3_stmt* stmt = NULL;
  83. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  84. {
  85. while(sqlite3_step(stmt) == SQLITE_ROW)
  86. {
  87. std::string name = (char*)sqlite3_column_text(stmt, 0);
  88. std::string value = (char*)sqlite3_column_text(stmt, 1);
  89. CSetting::SetParam(name, value, false);
  90. }
  91. sqlite3_finalize(stmt);
  92. }
  93. else
  94. {
  95. //异常情况
  96. sqlite3_finalize(stmt);
  97. return false;
  98. }
  99. }
  100. else
  101. {
  102. //异常情况
  103. sqlite3_finalize(stmt);
  104. return false;
  105. }
  106. }
  107. else
  108. {
  109. //异常情况
  110. sqlite3_finalize(stmt);
  111. return false;
  112. }
  113. sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
  114. //读取厨房打印机的参数
  115. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  116. {
  117. if(sqlite3_step(stmt) == SQLITE_ROW)
  118. {
  119. int count = sqlite3_column_int(stmt, 0);
  120. if(count == 0)
  121. {
  122. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  123. sqlite3_finalize(stmt);
  124. stmt = NULL;
  125. sql = "CREATE TABLE pos_chufang_printer(" \
  126. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  127. "date CHAR(100) NOT NULL," \
  128. "name CHAR(100) NOT NULL," \
  129. "ip CHAR(100) NOT NULL," \
  130. "guige CHAR(100) NOT NULL," \
  131. "fendan CHAR(100) NOT NULL," \
  132. "fenlei CHAR(100) NOT NULL," \
  133. "fenlei_ids CHAR(2000) );";
  134. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  135. {
  136. //执行该语句
  137. if(sqlite3_step(stmt) != SQLITE_DONE)
  138. {
  139. std::string err = sqlite3_errmsg(m_db);
  140. LOG_INFO("create table fail: " << err.c_str());
  141. sqlite3_finalize(stmt);
  142. return false;
  143. }
  144. //走到这里就是表创建成功了
  145. LOG_INFO("create table success");
  146. sqlite3_finalize(stmt);
  147. }
  148. else
  149. {
  150. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  151. sqlite3_finalize(stmt);
  152. return false;
  153. }
  154. }
  155. else
  156. {
  157. //说明已经有这个表了,就不用再创建了
  158. sqlite3_finalize(stmt);
  159. }
  160. std::string sql = "SELECT * FROM pos_chufang_printer;";
  161. sqlite3_stmt* stmt = NULL;
  162. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  163. {
  164. while(sqlite3_step(stmt) == SQLITE_ROW)
  165. {
  166. std::string date = (char*)sqlite3_column_text(stmt, 1);
  167. std::string name = (char*)sqlite3_column_text(stmt, 2);
  168. std::string ip = (char*)sqlite3_column_text(stmt, 3);
  169. std::string guige = (char*)sqlite3_column_text(stmt, 4);
  170. std::string fendan = (char*)sqlite3_column_text(stmt, 5);
  171. std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
  172. std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
  173. //这里仅仅是把数据库内容读到内存,所以之类用false
  174. CSetting::AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
  175. }
  176. sqlite3_finalize(stmt);
  177. }
  178. else
  179. {
  180. //异常情况
  181. sqlite3_finalize(stmt);
  182. return false;
  183. }
  184. }
  185. }
  186. sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
  187. //读取厨房打印机的参数
  188. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  189. {
  190. if(sqlite3_step(stmt) == SQLITE_ROW)
  191. {
  192. int count = sqlite3_column_int(stmt, 0);
  193. if(count == 0)
  194. {
  195. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  196. sqlite3_finalize(stmt);
  197. stmt = NULL;
  198. sql = "CREATE TABLE pos_user(" \
  199. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  200. "username CHAR(100) NOT NULL," \
  201. "password CHAR(100) NOT NULL);";
  202. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  203. {
  204. //执行该语句
  205. if(sqlite3_step(stmt) != SQLITE_DONE)
  206. {
  207. std::string err = sqlite3_errmsg(m_db);
  208. LOG_INFO("create table fail: " << err.c_str());
  209. sqlite3_finalize(stmt);
  210. return false;
  211. }
  212. //走到这里就是表创建成功了
  213. LOG_INFO("create table success");
  214. sqlite3_finalize(stmt);
  215. }
  216. else
  217. {
  218. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  219. sqlite3_finalize(stmt);
  220. return false;
  221. }
  222. }
  223. else
  224. {
  225. //说明已经有这个表了,就不用再创建了
  226. sqlite3_finalize(stmt);
  227. }
  228. std::string sql = "SELECT * FROM pos_user;";
  229. sqlite3_stmt* stmt = NULL;
  230. if(sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  231. {
  232. while(sqlite3_step(stmt) == SQLITE_ROW)
  233. {
  234. std::string username = (char*)sqlite3_column_text(stmt, 1);
  235. std::string password = (char*)sqlite3_column_text(stmt, 2);
  236. //这里仅仅是把数据库内容读到内存,所以之类用false
  237. CSetting::SetUser(username, password);
  238. }
  239. sqlite3_finalize(stmt);
  240. }
  241. else
  242. {
  243. //异常情况
  244. sqlite3_finalize(stmt);
  245. return false;
  246. }
  247. }
  248. }
  249. return true;
  250. }
  251. bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
  252. {
  253. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  254. std::string sql = "delete from pos_config;";
  255. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  256. for(std::map<std::string, std::string>::iterator it = params.begin(); it != params.end(); it++)
  257. {
  258. std::string name = it->first;
  259. std::string value = it->second;
  260. sql = "INSERT INTO pos_config (name, value) VALUES ('" + name + "','" + value + "');";
  261. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  262. }
  263. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  264. if(result == SQLITE_OK)
  265. {
  266. LOG_INFO("save params success");
  267. return true;
  268. }
  269. LOG_INFO("save params fail");
  270. return false;
  271. }
  272. bool CSqlite3::SaveChufangPrinter(std::vector<ChufangPrinter>& printers)
  273. {
  274. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  275. std::string sql = "delete from pos_chufang_printer;";
  276. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  277. for(std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
  278. {
  279. std::string date = (*it).date;
  280. std::string name = (*it).name;
  281. std::string ip = (*it).ip;
  282. std::string guige = (*it).guige;
  283. std::string fendan = (*it).fendan;
  284. std::string fenlei = (*it).fenlei;
  285. std::string fenlei_ids = (*it).fenlei_ids;
  286. sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
  287. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  288. }
  289. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  290. if(result == SQLITE_OK)
  291. {
  292. LOG_INFO("save params success");
  293. return true;
  294. }
  295. LOG_INFO("save params fail");
  296. return false;
  297. }
  298. bool CSqlite3::SaveUsers(std::map<string, string> users)
  299. {
  300. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  301. std::string sql = "delete from pos_user;";
  302. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  303. for(std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
  304. {
  305. std::string name = it->first;
  306. std::string password = it->second;
  307. sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
  308. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  309. }
  310. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  311. if(result == SQLITE_OK)
  312. {
  313. LOG_INFO("save params success");
  314. return true;
  315. }
  316. LOG_INFO("save params fail");
  317. return false;
  318. }