CSqlite3.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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::InitDB()
  39. {
  40. //先处理pos_config表
  41. bool ret = this->InitPosConfig();
  42. if (!ret)
  43. {
  44. return false;
  45. }
  46. //再处理pos_chufang_printer表
  47. ret = this->InitPosChufangPrinter();
  48. if (!ret)
  49. {
  50. return false;
  51. }
  52. //再处理pos_user表
  53. ret = this->InitPosUser();
  54. if (!ret)
  55. {
  56. return false;
  57. }
  58. return true;
  59. }
  60. bool CSqlite3::InitPosConfig()
  61. {
  62. //检查有没有pos_config这个表,如果没有就创建
  63. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_config';";
  64. sqlite3_stmt * stmt = NULL;
  65. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  66. {
  67. if (sqlite3_step(stmt) == SQLITE_ROW)
  68. {
  69. int count = sqlite3_column_int(stmt, 0);
  70. if (count == 0)
  71. {
  72. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  73. sqlite3_finalize(stmt);
  74. stmt = NULL;
  75. sql = "CREATE TABLE pos_config(" \
  76. "name CHAR(100) UNIQUE NOT NULL," \
  77. "value CHAR(2000) NOT NULL);";
  78. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  79. {
  80. //执行该语句
  81. if (sqlite3_step(stmt) != SQLITE_DONE)
  82. {
  83. LOG_INFO("create table fail: " << sqlite3_errmsg(m_db));
  84. sqlite3_finalize(stmt);
  85. return false;
  86. }
  87. //走到这里就是表创建成功了
  88. //LOG_INFO("create table success");
  89. sqlite3_finalize(stmt);
  90. }
  91. else
  92. {
  93. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  94. sqlite3_finalize(stmt);
  95. return false;
  96. }
  97. }
  98. else
  99. {
  100. //说明已经有这个表了,就不用再创建了
  101. sqlite3_finalize(stmt);
  102. }
  103. std::string sql = "SELECT * FROM pos_config;";
  104. sqlite3_stmt * stmt = NULL;
  105. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  106. {
  107. while (sqlite3_step(stmt) == SQLITE_ROW)
  108. {
  109. std::string name = (char*)sqlite3_column_text(stmt, 0);
  110. std::string value = (char*)sqlite3_column_text(stmt, 1);
  111. CSetting::SetParam(name, value, false);
  112. }
  113. sqlite3_finalize(stmt);
  114. }
  115. else
  116. {
  117. //异常情况
  118. sqlite3_finalize(stmt);
  119. return false;
  120. }
  121. }
  122. else
  123. {
  124. //异常情况
  125. sqlite3_finalize(stmt);
  126. return false;
  127. }
  128. }
  129. else
  130. {
  131. //异常情况
  132. sqlite3_finalize(stmt);
  133. return false;
  134. }
  135. return true;
  136. }
  137. bool CSqlite3::InitPosChufangPrinter()
  138. {
  139. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_chufang_printer';";
  140. sqlite3_stmt * stmt = NULL;
  141. //读取厨房打印机的参数
  142. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  143. {
  144. if (sqlite3_step(stmt) == SQLITE_ROW)
  145. {
  146. int count = sqlite3_column_int(stmt, 0);
  147. if (count == 0)
  148. {
  149. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  150. sqlite3_finalize(stmt);
  151. stmt = NULL;
  152. sql = "CREATE TABLE pos_chufang_printer(" \
  153. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  154. "date CHAR(100) NOT NULL," \
  155. "name CHAR(100) NOT NULL," \
  156. "ip CHAR(100) NOT NULL," \
  157. "guige CHAR(100) NOT NULL," \
  158. "fendan CHAR(100) NOT NULL," \
  159. "fenlei CHAR(100) NOT NULL," \
  160. "fenlei_ids CHAR(2000) );";
  161. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  162. {
  163. //执行该语句
  164. if (sqlite3_step(stmt) != SQLITE_DONE)
  165. {
  166. std::string err = sqlite3_errmsg(m_db);
  167. LOG_INFO("create table fail: " << err.c_str());
  168. sqlite3_finalize(stmt);
  169. return false;
  170. }
  171. //走到这里就是表创建成功了
  172. LOG_INFO("create table success");
  173. sqlite3_finalize(stmt);
  174. }
  175. else
  176. {
  177. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  178. sqlite3_finalize(stmt);
  179. return false;
  180. }
  181. }
  182. else
  183. {
  184. //说明已经有这个表了,就不用再创建了
  185. sqlite3_finalize(stmt);
  186. }
  187. //兼容性检查,判断字段是否存在
  188. std::string sql_pandduan = "select COUNT(*) from sqlite_master where type = 'table' and tbl_name = 'pos_chufang_printer' and sql like '%fenlei%';";
  189. stmt = NULL;
  190. if (sqlite3_prepare_v2(m_db, sql_pandduan.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  191. {
  192. if (sqlite3_step(stmt) == SQLITE_ROW)
  193. {
  194. int count = sqlite3_column_int(stmt, 0);
  195. if (count == 0)
  196. {
  197. //说明没找到这个字段,准备添加字段
  198. LOG_INFO("not fount field");
  199. sqlite3_finalize(stmt);
  200. stmt = NULL;
  201. sql = "ALTER TABLE pos_chufang_printer " \
  202. "add fenlei CHAR(100) NOT NULL DEFAULT '0';";
  203. LOG_INFO("sql:" << sql.c_str());
  204. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  205. {
  206. //执行该语句
  207. if (sqlite3_step(stmt) != SQLITE_DONE)
  208. {
  209. std::string err = sqlite3_errmsg(m_db);
  210. LOG_INFO("alter table fail: " << err.c_str());
  211. sqlite3_finalize(stmt);
  212. return false;
  213. }
  214. //走到这里就是表创建成功了
  215. LOG_INFO("alter table success");
  216. sqlite3_finalize(stmt);
  217. }
  218. else
  219. {
  220. LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
  221. sqlite3_finalize(stmt);
  222. return false;
  223. }
  224. }
  225. else
  226. {
  227. LOG_INFO("fount field");
  228. //说明已经有这2个字段了
  229. sqlite3_finalize(stmt);
  230. }
  231. }
  232. }
  233. else
  234. {
  235. //异常情况
  236. sqlite3_finalize(stmt);
  237. return false;
  238. }
  239. sql_pandduan = "select COUNT(*) from sqlite_master where type = 'table' and tbl_name = 'pos_chufang_printer' and sql like '%fenlei_ids%';";
  240. stmt = NULL;
  241. if (sqlite3_prepare_v2(m_db, sql_pandduan.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  242. {
  243. if (sqlite3_step(stmt) == SQLITE_ROW)
  244. {
  245. int count = sqlite3_column_int(stmt, 0);
  246. if (count == 0)
  247. {
  248. //说明没找到这个字段,准备添加字段
  249. LOG_INFO("not fount field");
  250. sqlite3_finalize(stmt);
  251. stmt = NULL;
  252. sql = "ALTER TABLE pos_chufang_printer " \
  253. "add fenlei_ids CHAR(2000) NOT NULL DEFAULT '';";
  254. LOG_INFO("sql:" << sql.c_str());
  255. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  256. {
  257. //执行该语句
  258. if (sqlite3_step(stmt) != SQLITE_DONE)
  259. {
  260. std::string err = sqlite3_errmsg(m_db);
  261. LOG_INFO("alter table fail: " << err.c_str());
  262. sqlite3_finalize(stmt);
  263. return false;
  264. }
  265. //走到这里就是表创建成功了
  266. LOG_INFO("alter table success");
  267. sqlite3_finalize(stmt);
  268. }
  269. else
  270. {
  271. LOG_INFO("alter table prepare fail: " << sqlite3_errmsg(m_db));
  272. sqlite3_finalize(stmt);
  273. return false;;
  274. }
  275. }
  276. else
  277. {
  278. LOG_INFO("fount field");
  279. //说明已经有这2个字段了
  280. sqlite3_finalize(stmt);
  281. }
  282. }
  283. }
  284. else
  285. {
  286. //异常情况
  287. sqlite3_finalize(stmt);
  288. return false;
  289. }
  290. std::string sql = "SELECT * FROM pos_chufang_printer;";
  291. stmt = NULL;
  292. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  293. {
  294. while (sqlite3_step(stmt) == SQLITE_ROW)
  295. {
  296. std::string date = (char*)sqlite3_column_text(stmt, 1);
  297. std::string name = (char*)sqlite3_column_text(stmt, 2);
  298. std::string ip = (char*)sqlite3_column_text(stmt, 3);
  299. std::string guige = (char*)sqlite3_column_text(stmt, 4);
  300. std::string fendan = (char*)sqlite3_column_text(stmt, 5);
  301. std::string fenlei = (char*)sqlite3_column_text(stmt, 6);
  302. std::string fenlei_ids = (char*)sqlite3_column_text(stmt, 7);
  303. //这里仅仅是把数据库内容读到内存,所以之类用false
  304. CSetting::AddChufangPrinter(date, name, ip, guige, fendan, fenlei, fenlei_ids, false);
  305. }
  306. sqlite3_finalize(stmt);
  307. }
  308. else
  309. {
  310. //异常情况
  311. sqlite3_finalize(stmt);
  312. return false;
  313. }
  314. }
  315. }
  316. return true;
  317. }
  318. bool CSqlite3::InitPosUser()
  319. {
  320. std::string sql = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'pos_user';";
  321. sqlite3_stmt * stmt = NULL;
  322. //读取厨房打印机的参数
  323. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  324. {
  325. if (sqlite3_step(stmt) == SQLITE_ROW)
  326. {
  327. int count = sqlite3_column_int(stmt, 0);
  328. if (count == 0)
  329. {
  330. //说明没找到这个表,那么这个时候新建这个表,先释放前面的stmt
  331. sqlite3_finalize(stmt);
  332. stmt = NULL;
  333. sql = "CREATE TABLE pos_user(" \
  334. "id INTEGER PRIMARY KEY AUTOINCREMENT,"\
  335. "username CHAR(100) NOT NULL," \
  336. "password CHAR(100) NOT NULL);";
  337. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  338. {
  339. //执行该语句
  340. if (sqlite3_step(stmt) != SQLITE_DONE)
  341. {
  342. std::string err = sqlite3_errmsg(m_db);
  343. LOG_INFO("create table fail: " << err.c_str());
  344. sqlite3_finalize(stmt);
  345. return false;
  346. }
  347. //走到这里就是表创建成功了
  348. //LOG_INFO("create table success");
  349. sqlite3_finalize(stmt);
  350. }
  351. else
  352. {
  353. LOG_INFO("create table prepare fail: " << sqlite3_errmsg(m_db));
  354. sqlite3_finalize(stmt);
  355. return false;
  356. }
  357. }
  358. else
  359. {
  360. //说明已经有这个表了,就不用再创建了
  361. sqlite3_finalize(stmt);
  362. }
  363. std::string sql = "SELECT * FROM pos_user;";
  364. sqlite3_stmt * stmt = NULL;
  365. if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL) == SQLITE_OK)
  366. {
  367. while (sqlite3_step(stmt) == SQLITE_ROW)
  368. {
  369. std::string username = (char*)sqlite3_column_text(stmt, 1);
  370. std::string password = (char*)sqlite3_column_text(stmt, 2);
  371. //这里仅仅是把数据库内容读到内存,所以之类用false
  372. CSetting::SetUser(username, password);
  373. }
  374. sqlite3_finalize(stmt);
  375. }
  376. else
  377. {
  378. //异常情况
  379. sqlite3_finalize(stmt);
  380. return false;
  381. }
  382. }
  383. }
  384. return true;
  385. }
  386. bool CSqlite3::SaveParams(std::map<std::string, std::string>& params)
  387. {
  388. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  389. std::string sql = "delete from pos_config;";
  390. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  391. for(std::map<std::string, std::string>::iterator it = params.begin(); it != params.end(); it++)
  392. {
  393. std::string name = it->first;
  394. std::string value = it->second;
  395. sql = "INSERT INTO pos_config (name, value) VALUES ('" + name + "','" + value + "');";
  396. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  397. }
  398. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  399. if(result == SQLITE_OK)
  400. {
  401. //LOG_INFO("save params success");
  402. return true;
  403. }
  404. LOG_INFO("save params fail:"<< sqlite3_errmsg(m_db));
  405. return false;
  406. }
  407. bool CSqlite3::SaveChufangPrinter(std::vector<ChufangPrinter>& printers)
  408. {
  409. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  410. std::string sql = "delete from pos_chufang_printer;";
  411. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  412. for (std::vector<ChufangPrinter>::iterator it = printers.begin(); it != printers.end(); it++)
  413. {
  414. std::string date = (*it).date;
  415. std::string name = (*it).name;
  416. std::string ip = (*it).ip;
  417. std::string guige = (*it).guige;
  418. std::string fendan = (*it).fendan;
  419. std::string fenlei = (*it).fenlei;
  420. std::string fenlei_ids = (*it).fenlei_ids;
  421. sql = "INSERT INTO pos_chufang_printer (date, name, ip, guige, fendan, fenlei, fenlei_ids) VALUES ('" + date + "' ,'" + name + "','" + ip + "','" + guige + "','" + fendan + "','" + fenlei + "','" + fenlei_ids + "')";
  422. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  423. }
  424. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  425. if (result == SQLITE_OK)
  426. {
  427. LOG_INFO("save params success");
  428. return true;
  429. }
  430. LOG_INFO("save params fail");
  431. return false;
  432. }
  433. bool CSqlite3::SaveUsers(std::map<string, string> users)
  434. {
  435. int result = sqlite3_exec(m_db, "BEGIN;", 0, 0, 0);
  436. std::string sql = "delete from pos_user;";
  437. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  438. for(std::map<std::string, std::string>::iterator it = users.begin(); it != users.end(); it++)
  439. {
  440. std::string name = it->first;
  441. std::string password = it->second;
  442. sql = "INSERT INTO pos_user (username, password) VALUES ('" + name + "' ,'" + password + "')";
  443. result = sqlite3_exec(m_db, sql.c_str(), 0, 0, 0);
  444. }
  445. result = sqlite3_exec(m_db, "COMMIT;", 0, 0, 0);
  446. if(result == SQLITE_OK)
  447. {
  448. //LOG_INFO("save params success");
  449. return true;
  450. }
  451. //LOG_INFO("save params fail");
  452. return false;
  453. }