db.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "pch.h"
  2. #include "db.h"
  3. #include "Global.h"
  4. #include "CharEncoding.h"
  5. // 上报表;
  6. #define _CREATE_REPORT_TABLE_ \
  7. "CREATE TABLE report \
  8. (url TEXT NOT NULL,\
  9. content TEXT NOT NULL, \
  10. gener_date DATETIME NOT NULL DEFAULT current_timestamp,\
  11. report_date DATETIME NOT NULL DEFAULT '');"
  12. #define _SELECT_REPORT_TABLE_ "SELECT url, content, report_status FROM report"
  13. #define _INSERT_REPORT_TABLE_ "INSERT INTO report(url, content, report_status) VALUES (%s, %s, %s);"
  14. // 普通日志表;
  15. #define _CREATE_LOG_TABLE_ \
  16. "CREATE TABLE report \
  17. (type TEXT(16) NOT NULL,\
  18. content TEXT NOT NULL, \
  19. gener_date DATETIME NOT NULL DEFAULT current_timestamp,\
  20. report_date DATETIME NOT NULL DEFAULT '');"
  21. #define _SELECT_LOG_TABLE_ "SELECT url, content, report_status FROM log"
  22. #define _INSERT_LOG_TABLE_ "INSERT INTO log(url, content, report_status) VALUES (%s, %s, %s);"
  23. #define FREE_MSG if ( psqlite_error ) sqlite3_free(psqlite_error),psqlite_error = NULL
  24. CDataImpl::CDataImpl(void):m_psqlite3(NULL)
  25. {
  26. //sqlite3_threadsafe();
  27. }
  28. CDataImpl::~CDataImpl(void)
  29. {
  30. Close();
  31. }
  32. BOOL CDataImpl::Open(std::string com)
  33. {
  34. if ( com.size() == 0 )
  35. return FALSE;
  36. com.replace(0, 3, "port");
  37. Close();
  38. CHAR szpath[MAX_PATH] = {0};
  39. _stprintf_s(szpath, _T("%sdb\\%s.db"), Global::g_szCurModuleDir, com.c_str());
  40. std::string strPath;
  41. if ( !CharEncoding::ASCII2UTF8(szpath,strPath))
  42. {
  43. return FALSE;
  44. }
  45. INT nResult = sqlite3_open(strPath.c_str(), &m_psqlite3);
  46. if ( nResult != SQLITE_OK )
  47. return FALSE;
  48. // 创建表;
  49. char* psqlite_error = NULL;
  50. if ( !QueryTable("report"))
  51. {
  52. sqlite3_exec(m_psqlite3, _CREATE_REPORT_TABLE_, NULL, NULL, &psqlite_error);
  53. FREE_MSG;
  54. }
  55. return TRUE;
  56. }
  57. void CDataImpl::Close()
  58. {
  59. if ( m_psqlite3 )
  60. sqlite3_close(m_psqlite3);
  61. m_psqlite3 = NULL;
  62. }
  63. BOOL CDataImpl::ExecteSQL(IN LPCSTR lpSQL)
  64. {
  65. if ( lpSQL == NULL || lpSQL[0] == '\0' )
  66. {
  67. //Global::WriteTextLog(_T("ExecteSQL:执行语句空!"));
  68. return FALSE;
  69. }
  70. if(m_psqlite3 == NULL)
  71. return FALSE;
  72. char* psqlite_error = NULL;
  73. int sqlite_error = sqlite3_exec(m_psqlite3, lpSQL, NULL, 0, &psqlite_error);
  74. if(SQLITE_OK != sqlite_error)
  75. {
  76. //Global::WriteTextLog(_T("ExecteSQL:%s"), psqlite_error);
  77. FREE_MSG;
  78. return FALSE;
  79. }
  80. return TRUE;
  81. }
  82. BOOL CDataImpl::QueryTable(std::string table)
  83. {
  84. if ( table.size() == 0 )
  85. {
  86. //Global::WriteTextLog(_T("ExecteSQL:执行语句空!"));
  87. return FALSE;
  88. }
  89. if(m_psqlite3 == NULL)
  90. return FALSE;
  91. INT nRow = 0;
  92. INT nCol = 0;
  93. char** pazResult = NULL;
  94. char* psqlite_error = NULL;
  95. char szSql[MAX_PATH] = {0};
  96. _stprintf_s(szSql, _T("select * from sqlite_master where type = 'table' and name = '%s'"), table.c_str());
  97. int sqlite_error = sqlite3_get_table(m_psqlite3, szSql, &pazResult, &nRow, &nCol, &psqlite_error);
  98. if ( sqlite_error != SQLITE_OK)
  99. {
  100. //Global::WriteTextLog(_T("QueryContactsType:%s"), m_pszErrmsg);
  101. FREE_MSG;
  102. return FALSE;
  103. }
  104. sqlite3_free_table(pazResult);
  105. return nRow != 0;
  106. }
  107. // 已上报的;
  108. INT CDataImpl::QueryReportInfo(std::vector<STReport> &vtdata)
  109. {
  110. if ( m_psqlite3 == NULL )
  111. return -1;
  112. INT nRow = 0;
  113. INT nCol = 0;
  114. char** pazResult = NULL;
  115. char* psqlite_error = NULL;
  116. std::string strSql = "SELECT url, content FROM report WHERE report_date <> ''";
  117. int sqlite_error = sqlite3_get_table(m_psqlite3, strSql.c_str(), &pazResult, &nRow, &nCol, &psqlite_error);
  118. if ( sqlite_error != SQLITE_OK)
  119. {
  120. //Global::WriteTextLog(_T("QueryContactsInfo:%s"), m_pszErrmsg);
  121. FREE_MSG;
  122. return -1;
  123. }
  124. vtdata.resize(nRow);
  125. std::vector<STReport>::iterator it = vtdata.begin();
  126. for(int i = 1; i <= nRow; i++, it++)
  127. {
  128. it->url = pazResult[i*nCol+0];
  129. it->content = pazResult[i*nCol+1];
  130. }
  131. sqlite3_free_table(pazResult);
  132. return nRow;
  133. }
  134. // 未上报的;
  135. INT CDataImpl::QueryUnReportInfo(std::vector<STReport>& vtdata)
  136. {
  137. if (m_psqlite3 == NULL)
  138. return -1;
  139. INT nRow = 0;
  140. INT nCol = 0;
  141. char** pazResult = NULL;
  142. char* psqlite_error = NULL;
  143. std::string strSql = "SELECT url, content FROM report WHERE report_date = ''";
  144. int sqlite_error = sqlite3_get_table(m_psqlite3, strSql.c_str(), &pazResult, &nRow, &nCol, &psqlite_error);
  145. if (sqlite_error != SQLITE_OK)
  146. {
  147. //Global::WriteTextLog(_T("QueryContactsInfo:%s"), m_pszErrmsg);
  148. FREE_MSG;
  149. return -1;
  150. }
  151. vtdata.resize(nRow);
  152. std::vector<STReport>::iterator it = vtdata.begin();
  153. for (int i = 1; i <= nRow; i++, it++)
  154. {
  155. it->url = pazResult[i * nCol + 0];
  156. it->content = pazResult[i * nCol + 1];
  157. }
  158. sqlite3_free_table(pazResult);
  159. return nRow;
  160. }
  161. // INT CDataImpl::InsertReportInfo(STReport& data)
  162. // {
  163. // if(m_psqlite3 == NULL)
  164. // return -1;
  165. //
  166. // std::string strInsert = "INSERT INTO report(url, content)VALUES ('";
  167. // strInsert.append(data.url);
  168. // strInsert.append("','");
  169. // strInsert.append(data.content);
  170. // strInsert.append("');");
  171. //
  172. // char* psqlite_error = NULL;
  173. // int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
  174. // if(SQLITE_OK != sqlite_error)
  175. // {
  176. // //Global::WriteTextLog(_T("InsertContactsType:%s"), psqlite_error);
  177. // FREE_MSG;
  178. // }
  179. //
  180. // return sqlite_error;
  181. // }
  182. INT CDataImpl::InsertReportInfo(std::string url, std::string content, bool report_status)
  183. {
  184. if (m_psqlite3 == NULL)
  185. return -1;
  186. std::string strInsert = "INSERT INTO report(url, content, report_date)VALUES ('";
  187. strInsert.append(url);
  188. strInsert.append("','");
  189. strInsert.append(content);
  190. strInsert.append("',");
  191. if (report_status == true)
  192. {//上报成功;
  193. strInsert.append("current_timestamp");
  194. strInsert.append(");");
  195. }
  196. else
  197. {
  198. strInsert.append("''");
  199. strInsert.append("');");
  200. }
  201. char* psqlite_error = NULL;
  202. int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
  203. if (SQLITE_OK != sqlite_error)
  204. {
  205. //Global::WriteTextLog(_T("InsertContactsType:%s"), psqlite_error);
  206. FREE_MSG;
  207. }
  208. return sqlite_error;
  209. }
  210. // INT CDataImpl::InsertReportInfo(std::string content)
  211. // {
  212. // if (m_psqlite3 == NULL)
  213. // return -1;
  214. //
  215. // std::string strInsert = "INSERT INTO report(url, content)VALUES ('";
  216. // strInsert.append("https://cn.ota.qhmoka.com/ota-services/report/reportToolsLog.do");
  217. // strInsert.append("','");
  218. // strInsert.append(content);
  219. // strInsert.append("');");
  220. // char* psqlite_error = NULL;
  221. // int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
  222. // if (SQLITE_OK != sqlite_error)
  223. // {
  224. // //Global::WriteTextLog(_T("InsertContactsType:%s"), psqlite_error);
  225. // FREE_MSG;
  226. // }
  227. //
  228. // return sqlite_error;
  229. // }
  230. INT CDataImpl::InsertReportInfo(std::string report_type, std::map<std::string, std::string> report_data)
  231. {
  232. if (m_psqlite3 == NULL)
  233. return -1;
  234. // Json数据;
  235. std::string content;
  236. cJSON* pJson = cJSON_CreateObject();
  237. if (!pJson)
  238. return -1;
  239. cJSON_AddStringToObject(pJson, "reportType", report_type.c_str());
  240. cJSON* pObj = cJSON_AddObjectToObject(pJson, _T("reportData"));
  241. for (auto it : report_data)
  242. {
  243. cJSON_AddStringToObject(pObj, it.first.c_str(), it.second.c_str());
  244. }
  245. char* pJsonText = cJSON_Print(pJson);
  246. if (!pJsonText)
  247. {
  248. cJSON_Delete(pJson);
  249. return -1;
  250. }
  251. content = pJsonText;
  252. if (pJsonText)
  253. free(pJsonText);
  254. cJSON_Delete(pJson);
  255. std::string strInsert = "INSERT INTO report(url, content)VALUES ('";
  256. strInsert.append("https://cn.ota.qhmoka.com/ota-services/report/reportToolsLog");
  257. strInsert.append("','");
  258. strInsert.append(content);
  259. strInsert.append("');");
  260. char* psqlite_error = NULL;
  261. int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
  262. if (SQLITE_OK != sqlite_error)
  263. {
  264. //Global::WriteTextLog(_T("InsertContactsType:%s"), psqlite_error);
  265. FREE_MSG;
  266. }
  267. return sqlite_error;
  268. }
  269. INT CDataImpl::RemoveReportInfo()
  270. {
  271. if (m_psqlite3 == NULL)
  272. return -1;
  273. char** pazResult = NULL;
  274. char* psqlite_error = NULL;
  275. CTime ct = CTime::GetCurrentTime() - CTimeSpan(30, 0, 0, 0);
  276. std::string strSql = "delete from report where report_date <> '' and report_date < '";
  277. strSql.append(ct.Format("%Y-%m-%d %H:%M:%S").GetString());
  278. strSql.append("';");
  279. // 删除嵌套的content数据;
  280. strSql.append("delete from report where length(content) > 8192 and url = 'https://cn.ota.qhmoka.com/ota-services/report/reportToolsLog';");
  281. int sqlite_error = sqlite3_exec(m_psqlite3, strSql.c_str(),0, 0, &psqlite_error);
  282. if (sqlite_error != SQLITE_OK)
  283. {
  284. //Global::WriteTextLog(_T("QueryContactsInfo:%s"), m_pszErrmsg);
  285. FREE_MSG;
  286. return -1;
  287. }
  288. return 0;
  289. }
  290. BOOL CDataImpl::UpdateKeyReportStatus(STReport& data)
  291. {
  292. if(m_psqlite3 == NULL)
  293. return FALSE;
  294. std::string strInsert = "UPDATE report SET report_date= ";
  295. strInsert.append("current_timestamp");
  296. strInsert.append(" WHERE url ='");
  297. strInsert.append(data.url);
  298. strInsert.append("' and content = '");
  299. strInsert.append(data.content);
  300. strInsert.append("';");
  301. char* psqlite_error = NULL;
  302. int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
  303. if(SQLITE_OK != sqlite_error)
  304. {
  305. //Global::WriteTextLog(_T("UpdateContactsInfo:%s"), psqlite_error);
  306. FREE_MSG;
  307. return FALSE;
  308. }
  309. return TRUE;
  310. }