DataImpl.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "DataImpl.h"
  2. #include <string>
  3. using namespace std;
  4. CDataImpl::CDataImpl(void):m_psqlite3(NULL),m_pszErrmsg(NULL)
  5. {
  6. }
  7. CDataImpl::~CDataImpl(void)
  8. {
  9. }
  10. BOOL CDataImpl::Open()
  11. {
  12. Close();
  13. CHAR szpath[MAX_PATH] = {0};
  14. sprintf(szpath, "E:\\repos_other\\satd.db");
  15. string strPath = szpath;
  16. INT nResult = sqlite3_open(strPath.c_str(), &m_psqlite3);
  17. if ( nResult != SQLITE_OK )
  18. return FALSE;
  19. return TRUE;
  20. }
  21. void CDataImpl::Close()
  22. {
  23. if ( m_psqlite3 )
  24. sqlite3_close(m_psqlite3);
  25. m_psqlite3 = NULL;
  26. }
  27. BOOL CDataImpl::ExecteSQL(IN LPCSTR lpSQL)
  28. {
  29. if ( lpSQL == NULL || lpSQL[0] == '\0' )
  30. {
  31. #ifdef _DEBUG
  32. cout << "ExecteSQL:执行语句空!" << endl;
  33. #endif
  34. return FALSE;
  35. }
  36. if(m_psqlite3 == NULL)
  37. return FALSE;
  38. char* psqlite_error = NULL;
  39. int sqlite_error = sqlite3_exec(m_psqlite3, lpSQL, NULL, 0, &psqlite_error);
  40. if(SQLITE_OK != sqlite_error)
  41. {
  42. #ifdef _DEBUG
  43. cout << "ExecteSQL:" << psqlite_error << endl;
  44. #endif
  45. return FALSE;
  46. }
  47. return TRUE;
  48. }
  49. INT CDataImpl::QueryComplieServer(satdDatabase::listServer & list)
  50. {
  51. if (m_psqlite3 == NULL)
  52. return -1;
  53. INT nRow = 0;
  54. INT nCol = 0;
  55. char** pazResult = NULL;
  56. int sqlite_error = sqlite3_get_table(m_psqlite3, "select server, user, password from complie_server", &pazResult, &nRow, &nCol, &m_pszErrmsg);
  57. if (sqlite_error != SQLITE_OK)
  58. {
  59. #ifdef _DEBUG
  60. cout << "QueryComplieServer:" << m_pszErrmsg << endl;
  61. #endif
  62. return -1;
  63. }
  64. for (int i = 1; i <= nRow; i++)
  65. {
  66. satdDatabase::complieServer *pObj = list.add_servers();
  67. #ifndef USE_UTF8
  68. pObj->set_server(pazResult[i*nCol + 0]);
  69. pObj->set_user(pazResult[i*nCol + 1]);
  70. pObj->set_password(pazResult[i*nCol + 2]);
  71. #else
  72. // 由Native for SQLite3插入的数据,都是utf8格式;
  73. pObj->set_server(pazResult[i*nCol + 0]);
  74. pObj->set_user(pazResult[i*nCol + 1]);
  75. pObj->set_password(pazResult[i*nCol + 2]);
  76. #endif
  77. }
  78. sqlite3_free_table(pazResult);
  79. return nRow;
  80. }
  81. INT CDataImpl::QueryAutoComplieModel(satdDatabase::listModel & list)
  82. {
  83. if (m_psqlite3 == NULL)
  84. return -1;
  85. INT nRow = 0;
  86. INT nCol = 0;
  87. char** pazResult = NULL;
  88. string strQuery = "select name, soft_suffix, flags,content from model";
  89. sqlite3_stmt *pStmt = NULL;
  90. int sqlite_error = sqlite3_prepare_v2(m_psqlite3, strQuery.c_str(), strQuery.size(), &pStmt, NULL);
  91. if (sqlite_error != SQLITE_OK)
  92. {
  93. OutputDebugString(sqlite3_errmsg(m_psqlite3));
  94. return -1;
  95. }
  96. int nTotal = 0;
  97. int nStrLen = 0;
  98. const int intSize = sizeof(int);
  99. string strInfo;
  100. for (;;)
  101. {
  102. sqlite_error = sqlite3_step(pStmt);
  103. if (sqlite_error == SQLITE_ROW)
  104. {
  105. satdDatabase::model *pObj = list.add_models();
  106. pObj->set_name((const char*)sqlite3_column_text(pStmt, 0));
  107. pObj->set_softsuffix((const char*)sqlite3_column_text(pStmt, 1));
  108. const void *pdat = (const char*)sqlite3_column_blob(pStmt, 2);
  109. nTotal = sqlite3_column_bytes(pStmt, 2);
  110. pObj->mutable_flags()->ParseFromArray(pdat, nTotal);
  111. pdat = sqlite3_column_blob(pStmt, 3);
  112. nTotal = sqlite3_column_bytes(pStmt, 3);
  113. pObj->mutable_content()->ParseFromArray(pdat, nTotal);
  114. }
  115. else if (sqlite_error == SQLITE_DONE)
  116. {
  117. break;
  118. }
  119. else
  120. {
  121. break;
  122. }
  123. }
  124. if (pStmt)
  125. sqlite3_finalize(pStmt);
  126. return nRow;
  127. }
  128. BOOL CDataImpl::InsertComplieServer(IN satdDatabase::complieServer & server)
  129. {
  130. if (m_psqlite3 == NULL)
  131. return FALSE;
  132. string strInsert = "INSERT INTO complie_server(server, user, password) VALUES ('";
  133. #ifndef USE_UTF8
  134. strInsert.append(server.server());
  135. strInsert.append("','");
  136. strInsert.append(server.user());
  137. strInsert.append("','");
  138. strInsert.append(server.password());
  139. #else
  140. string str;
  141. strInsert.append(server.server());
  142. strInsert.append("','");
  143. strInsert.append(server.user());
  144. strInsert.append("','");
  145. strInsert.append(server.password());
  146. #endif
  147. strInsert.append("');");
  148. char* psqlite_error = NULL;
  149. int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
  150. if (SQLITE_OK != sqlite_error)
  151. {
  152. //Global::WriteTextLog(_T("InsertContactsType:%s"), psqlite_error);
  153. return FALSE;
  154. }
  155. return TRUE;
  156. }
  157. BOOL CDataImpl::InsertAutoComplieModel(IN satdDatabase::model & model)
  158. {
  159. if (m_psqlite3 == NULL)
  160. return FALSE;
  161. sqlite3_stmt *pStmt = NULL;
  162. int nRet = 0;
  163. string strSql = "insert into model(name,soft_suffix,flags,content) values(?,?,?,?)";
  164. nRet = sqlite3_prepare_v2(m_psqlite3, strSql.c_str(), strSql.size(), &pStmt, NULL);
  165. if (nRet != SQLITE_OK)
  166. {
  167. #ifdef _DEBUG
  168. OutputDebugString(sqlite3_errmsg(m_psqlite3));
  169. #endif
  170. return FALSE;
  171. }
  172. unsigned char *pfData = NULL;
  173. unsigned char *pmData = NULL;
  174. // 插入时,索引是从1开始。查询时,索引是从0开始;
  175. nRet = sqlite3_bind_text(pStmt, 1, model.name().c_str(), model.name().size(), NULL);
  176. if (nRet != SQLITE_OK)
  177. {
  178. #ifdef _DEBUG
  179. OutputDebugString(sqlite3_errmsg(m_psqlite3));
  180. #endif
  181. goto clear;
  182. return FALSE;
  183. }
  184. nRet = sqlite3_bind_text(pStmt, 2, model.softsuffix().c_str(), model.softsuffix().size(), NULL);
  185. if (nRet != SQLITE_OK)
  186. {
  187. #ifdef _DEBUG
  188. OutputDebugString(sqlite3_errmsg(m_psqlite3));
  189. #endif
  190. goto clear;
  191. return FALSE;
  192. }
  193. satdDatabase::listEndFlag *pflags = model.mutable_flags();
  194. if (pflags != NULL)
  195. {
  196. int nSize = pflags->ByteSize();
  197. pfData = new unsigned char[nSize];
  198. memset(pfData, 0, nSize);
  199. pflags->SerializeToArray(pfData, nSize);
  200. nRet = sqlite3_bind_blob(pStmt, 3, pfData, nSize, SQLITE_STATIC);
  201. if (nRet != SQLITE_OK)
  202. {
  203. goto clear;
  204. return FALSE;
  205. }
  206. }
  207. satdDatabase::modelContent *pMc = model.mutable_content();
  208. if (pMc)
  209. {
  210. int nSize = pMc->ByteSize();
  211. pmData = new unsigned char[nSize];
  212. memset(pmData, 0, nSize);
  213. pMc->SerializeToArray(pmData, nSize);
  214. nRet = sqlite3_bind_blob(pStmt, 4, pmData, nSize, SQLITE_STATIC);
  215. if (nRet != SQLITE_OK)
  216. {
  217. goto clear;
  218. return FALSE;
  219. }
  220. }
  221. nRet = sqlite3_step(pStmt);
  222. if (nRet != SQLITE_DONE)
  223. {
  224. goto clear;
  225. return FALSE;
  226. }
  227. clear:
  228. if (pStmt)
  229. sqlite3_finalize(pStmt);
  230. if (pfData)
  231. delete[]pfData;
  232. if (pmData)
  233. delete[]pmData;
  234. return nRet == SQLITE_DONE;
  235. }
  236. BOOL CDataImpl::UpdateComplieServer(IN satdDatabase::complieServer & server)
  237. {
  238. if (m_psqlite3 == NULL)
  239. return FALSE;
  240. string strInsert = "UPDATE complie_server SET server = '";
  241. #ifndef USE_UTF8
  242. strInsert.append(server.server());
  243. strInsert.append("', user = '");
  244. strInsert.append(server.user());
  245. strInsert.append("', password = '");
  246. strInsert.append(server.password());
  247. strInsert.append("' WHERE server = ");
  248. strInsert.append(server.server());
  249. strInsert.append(";");
  250. #else
  251. string str;
  252. strInsert.append(server.server());
  253. strInsert.append("', user = '");
  254. strInsert.append(server.user());
  255. strInsert.append("', password = '");
  256. strInsert.append(server.password());
  257. strInsert.append("' WHERE server = ");
  258. strInsert.append(server.server());
  259. strInsert.append(";");
  260. #endif
  261. char* psqlite_error = NULL;
  262. int sqlite_error = sqlite3_exec(m_psqlite3, strInsert.c_str(), NULL, 0, &psqlite_error);
  263. if (SQLITE_OK != sqlite_error)
  264. {
  265. //Global::WriteTextLog(_T("UpdateContactsType:%s"), psqlite_error);
  266. return FALSE;
  267. }
  268. return TRUE;
  269. }
  270. BOOL CDataImpl::UpdateAutoCompliteModel(IN satdDatabase::model & model)
  271. {
  272. if (m_psqlite3 == NULL)
  273. return FALSE;
  274. sqlite3_stmt *pStmt = NULL;
  275. int nRet = 0;
  276. char szSql[500] = {0};
  277. sprintf(szSql, "update model set soft_suffix=?, flags=?, content=? where name = '%s';", model.name().c_str());
  278. nRet = sqlite3_prepare_v2(m_psqlite3, szSql, strlen(szSql), &pStmt, NULL);
  279. if (nRet != SQLITE_OK)
  280. {
  281. #ifdef _DEBUG
  282. OutputDebugString(sqlite3_errmsg(m_psqlite3));
  283. #endif
  284. return FALSE;
  285. }
  286. char *pData1 = NULL, *pData2 = NULL;
  287. nRet = sqlite3_bind_text(pStmt, 1, model.softsuffix().c_str(), model.softsuffix().size(), NULL);
  288. if (nRet != SQLITE_OK)
  289. {
  290. #ifdef _DEBUG
  291. OutputDebugString(sqlite3_errmsg(m_psqlite3));
  292. #endif
  293. goto clear;
  294. return FALSE;
  295. }
  296. satdDatabase::listEndFlag *pflags = model.mutable_flags();
  297. if (pflags != NULL)
  298. {
  299. int nSize = pflags->ByteSize();
  300. pData1 = new char[nSize];
  301. memset(pData1, 0, nSize);
  302. pflags->SerializeToArray(pData1, nSize);
  303. nRet = sqlite3_bind_blob(pStmt, 2, pData1, nSize, NULL);
  304. if (nRet != SQLITE_OK)
  305. {
  306. #ifdef _DEBUG
  307. OutputDebugString(sqlite3_errmsg(m_psqlite3));
  308. #endif
  309. goto clear;
  310. return FALSE;
  311. }
  312. }
  313. satdDatabase::modelContent *pMc = model.mutable_content();
  314. if (pMc)
  315. {
  316. int nSize = pMc->ByteSize();
  317. pData2 = new char[nSize];
  318. memset(pData2, 0, nSize);
  319. pMc->SerializeToArray(pData2, nSize);
  320. nRet = sqlite3_bind_blob(pStmt, 3, pData2, nSize, NULL);
  321. if (nRet != SQLITE_OK)
  322. {
  323. #ifdef _DEBUG
  324. OutputDebugString(sqlite3_errmsg(m_psqlite3));
  325. #endif
  326. goto clear;
  327. return FALSE;
  328. }
  329. }
  330. if (nRet != SQLITE_OK)
  331. {
  332. goto clear;
  333. return FALSE;
  334. }
  335. nRet = sqlite3_step(pStmt);
  336. if (nRet != SQLITE_DONE)
  337. {
  338. goto clear;
  339. return FALSE;
  340. }
  341. clear:
  342. if (pStmt)
  343. sqlite3_finalize(pStmt);
  344. if (pData1) delete[]pData1;
  345. if (pData2) delete[]pData2;
  346. return nRet == SQLITE_OK;
  347. }