Database.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. #include "pch.h"
  2. #include "Database.h"
  3. #define CHECKDB if (m_pConn == NULL || !m_bConnected) return FALSE
  4. #define SAFESTR(str) ( str == NULL ? _T("") : str)
  5. char* CDatabase::m_spImage = NULL;
  6. CDatabase::CDatabase():m_pConn(NULL), m_pError(NULL), m_bConnected(FALSE)
  7. {
  8. m_pConn = mysql_init(NULL);
  9. }
  10. CDatabase::~CDatabase()
  11. {
  12. mysql_close(m_pConn);
  13. mysql_library_end();
  14. }
  15. BOOL CDatabase::SetBinaryField(std::string strCondition, void* pDataIn, int nDataLen)
  16. {
  17. CHECKDB;
  18. BOOL bResult = FALSE;
  19. MYSQL_STMT* stmt = NULL;
  20. stmt = mysql_stmt_init(m_pConn);
  21. if (!stmt)
  22. return FALSE;
  23. if (!mysql_stmt_prepare(stmt, strCondition.c_str(), strCondition.length()))
  24. {
  25. MYSQL_BIND bind;
  26. memset(&bind, 0, sizeof(MYSQL_BIND));
  27. bind.buffer_type = MYSQL_TYPE_BLOB;
  28. bind.buffer_length = nDataLen;
  29. bind.buffer = pDataIn;
  30. if (!mysql_stmt_bind_param(stmt, (MYSQL_BIND*)&bind))
  31. {
  32. if (!mysql_stmt_execute(stmt))
  33. {
  34. bResult = TRUE;
  35. }
  36. }
  37. }
  38. mysql_stmt_close(stmt);
  39. return bResult;
  40. }
  41. BOOL CDatabase::GetBinaryField(std::string strCondition, void** lpDataOut, unsigned long& nDataLen)
  42. {
  43. CHECKDB;
  44. BOOL bResult = FALSE;
  45. MYSQL_STMT* stmt = NULL;
  46. stmt = mysql_stmt_init(m_pConn);
  47. if (!stmt)
  48. return FALSE;
  49. MYSQL_RES* prepare_meta_result;
  50. if (!mysql_stmt_prepare(stmt, strCondition.c_str(), strCondition.length()))
  51. {
  52. if ( !mysql_stmt_execute(stmt) )
  53. {
  54. /* Fetch result set meta information */
  55. prepare_meta_result = mysql_stmt_result_metadata(stmt);
  56. if (prepare_meta_result)
  57. {
  58. /* Get total columns in the query */
  59. //int column_count = mysql_num_fields(prepare_meta_result);
  60. my_bool is_null;
  61. my_bool error;
  62. MYSQL_BIND bind[1];
  63. memset(bind, 0, sizeof(bind));
  64. bind[0].buffer_type = MYSQL_TYPE_BLOB;
  65. bind[0].buffer = 0;
  66. bind[0].buffer_length = 0;
  67. bind[0].is_null = &is_null;
  68. bind[0].length = &nDataLen; // 返回的缓存长度;
  69. bind[0].error = &error;
  70. /* Bind the result buffers */
  71. if (!mysql_stmt_bind_result(stmt, bind))
  72. {
  73. // 由于没有绑定缓冲区指针,第一次返回缓冲区大小;
  74. if (mysql_stmt_fetch(stmt))
  75. {
  76. if (nDataLen > 1)
  77. {
  78. *lpDataOut = new char[nDataLen];
  79. memset(*lpDataOut, 0, nDataLen);
  80. bind[0].buffer = *lpDataOut;
  81. bind[0].buffer_length = nDataLen;
  82. mysql_stmt_fetch_column(stmt, bind, 0, 0);
  83. bResult = TRUE;
  84. }
  85. }
  86. }
  87. /* Free the prepared result metadata */
  88. mysql_free_result(prepare_meta_result);
  89. }
  90. }
  91. }
  92. /* Close the statement */
  93. mysql_stmt_close(stmt);
  94. return bResult;
  95. }
  96. BOOL CDatabase::Init(std::string host, std::string user, std::string password, std::string db)
  97. {
  98. if (!m_pConn)
  99. return FALSE;
  100. m_pError = mysql_error(m_pConn);
  101. if (!mysql_real_connect(m_pConn, host.c_str(), user.c_str(), password.c_str(), db.c_str(), 0, NULL, 0)) {
  102. #ifdef _DEBUG
  103. _dprintf(_T("mysql_real_connect error=%s\n"), m_pError ? m_pError: "");
  104. #endif
  105. return FALSE;
  106. }
  107. return m_bConnected = TRUE;
  108. }
  109. BOOL CDatabase::ExcuteSQL(std::string sql)
  110. {
  111. if (m_pConn == NULL || !m_bConnected)
  112. return FALSE;
  113. if (0 != mysql_query(m_pConn, sql.c_str())) {
  114. #ifdef _DEBUG
  115. _dprintf(_T("ExcuteSQL error=%s\n"), m_pError ? m_pError : "");
  116. #endif
  117. return FALSE;
  118. }
  119. return TRUE;
  120. }
  121. BOOL CDatabase::InserSoc(const STSOC& soc)
  122. {
  123. CHECKDB;
  124. #if 0
  125. TCHAR szSql[MAX_PATH] = { 0 };
  126. _stprintf_s(szSql, INSER_SOC, soc.name.c_str(), soc.provider.c_str(), soc.note.c_str());
  127. if (0 != mysql_query(m_pConn, szSql)) {
  128. #ifdef _DEBUG
  129. _dprintf(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  130. #endif
  131. return FALSE;
  132. }
  133. return TRUE;
  134. #else
  135. MYSQL_STMT* stmt;
  136. MYSQL_BIND bind[3];
  137. TCHAR szQuery[MAX_PATH] = { 0 };
  138. strcpy_s(szQuery, _T("INSERT INTO soc(name, provider, note) VALUES(?,?,?);"));
  139. BOOL bResult = FALSE;
  140. //初始化stmt
  141. stmt = mysql_stmt_init(m_pConn);
  142. if (!stmt)
  143. return FALSE;
  144. //预处理语句
  145. if (mysql_stmt_prepare(stmt, szQuery, strlen(szQuery)))
  146. goto over;
  147. //初始化参数
  148. bind[0].buffer_type = MYSQL_TYPE_VARCHAR;
  149. bind[0].buffer = (void*)soc.name.c_str();
  150. bind[0].buffer_length = soc.name.size();
  151. bind[0].is_null = 0;
  152. bind[1].buffer_type = MYSQL_TYPE_VARCHAR;
  153. bind[1].buffer = (void*)soc.provider.c_str();
  154. bind[1].buffer_length = soc.provider.size();
  155. bind[1].is_null = 0;
  156. bind[2].buffer_type = MYSQL_TYPE_VARCHAR;
  157. bind[2].buffer = (void*)soc.note.c_str();
  158. bind[2].buffer_length = soc.note.size();
  159. bind[2].is_null = 0;
  160. //绑定参数123
  161. if (mysql_stmt_bind_param(stmt, bind))
  162. goto over;
  163. //执行预处理mysql语句
  164. if (!mysql_stmt_execute(stmt))
  165. bResult = TRUE;
  166. over:
  167. mysql_stmt_close(stmt);
  168. #ifdef _DEBUG
  169. if ( !bResult )
  170. _dprintf(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  171. #endif
  172. return bResult;
  173. #endif
  174. }
  175. BOOL CDatabase::InserSoc(std::string name, std::string provider, std::string note)
  176. {
  177. CHECKDB;
  178. #if 0
  179. TCHAR szSql[MAX_PATH] = { 0 };
  180. _stprintf_s(szSql, INSER_SOC, name.c_str(), provider.c_str(), note.c_str());
  181. if (0 != mysql_query(m_pConn, szSql)) {
  182. #ifdef _DEBUG
  183. _dprintf(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  184. #endif
  185. return FALSE;
  186. }
  187. return TRUE;
  188. #else
  189. //https://dev.mysql.com/doc/c-api/5.6/en/mysql-stmt-execute.html
  190. MYSQL_STMT* stmt;
  191. MYSQL_BIND bind[3];
  192. TCHAR szQuery[MAX_PATH] = { 0 };
  193. strcpy_s(szQuery, _T("INSERT INTO soc(name, provider, note) VALUES(?,?,?);"));
  194. //初始化stmt
  195. BOOL bResult = FALSE;
  196. stmt = mysql_stmt_init(m_pConn);
  197. if (!stmt)
  198. return FALSE;
  199. //预处理语句
  200. if (mysql_stmt_prepare(stmt, szQuery, strlen(szQuery)))
  201. goto over;
  202. // 字段数量是否匹配;
  203. int param_count = mysql_stmt_param_count(stmt);
  204. if (param_count != 3)
  205. goto over;
  206. //初始化参数
  207. memset(bind, 0, sizeof(bind));
  208. bind[0].buffer_type = MYSQL_TYPE_VARCHAR;
  209. bind[0].buffer = (void*)name.c_str();
  210. bind[0].buffer_length = name.size();
  211. bind[0].is_null = 0;
  212. bind[1].buffer_type = MYSQL_TYPE_VARCHAR;
  213. bind[1].buffer = (void*)provider.c_str();
  214. bind[1].buffer_length = provider.size();
  215. bind[1].is_null = 0;
  216. bind[2].buffer_type = MYSQL_TYPE_VARCHAR;
  217. bind[2].buffer = (void*)note.c_str();
  218. bind[2].buffer_length = note.size();
  219. bind[2].is_null = 0;
  220. //绑定参数123
  221. if (mysql_stmt_bind_param(stmt, bind))
  222. goto over;
  223. //执行预处理mysql语句
  224. if (mysql_stmt_execute(stmt))
  225. goto over;
  226. // 影响的行数;
  227. int affected_rows = mysql_stmt_affected_rows(stmt);
  228. if (affected_rows == 1)
  229. bResult = TRUE;
  230. over:
  231. #ifdef _DEBUG
  232. if (!bResult)
  233. _dprintf(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
  234. #endif
  235. mysql_stmt_close(stmt);
  236. return bResult;
  237. #endif
  238. }
  239. BOOL CDatabase::DeleteSoc(std::string name)
  240. {
  241. CHECKDB;
  242. TCHAR szSql[MAX_PATH] = { 0 };
  243. _stprintf_s(szSql, DEL_SOC, name.c_str());
  244. if (0 != mysql_query(m_pConn, szSql)) {
  245. #ifdef _DEBUG
  246. _dprintf(_T("DeleteSoc error=%s\n"), m_pError ? m_pError : "");
  247. #endif
  248. return FALSE;
  249. }
  250. return TRUE;
  251. }
  252. BOOL CDatabase::UpdateSoc(std::string name, const STSOC& soc)
  253. {
  254. CHECKDB;
  255. TCHAR szSql[MAX_PATH] = { 0 };
  256. _stprintf_s(szSql, MOD_SOC, soc.name.c_str(), soc.provider.c_str(), soc.note.c_str(), name.c_str());
  257. if (0 != mysql_query(m_pConn, szSql)) {
  258. #ifdef _DEBUG
  259. _dprintf(_T("UpdateSoc error=%s\n"), m_pError ? m_pError : "");
  260. #endif
  261. return FALSE;
  262. }
  263. return TRUE;
  264. }
  265. BOOL CDatabase::QuerySoc(std::vector<STSOC>& vtSoc)
  266. {
  267. CHECKDB;
  268. if (0 != mysql_query(m_pConn, QUERY_SOC)) {
  269. #ifdef _DEBUG
  270. _dprintf(_T("QuerySoc error=%s\n"), m_pError ? m_pError : "");
  271. #endif
  272. return FALSE;
  273. }
  274. // 获取表数据;
  275. MYSQL_RES* pData = mysql_store_result(m_pConn);
  276. if (pData == NULL) {
  277. #ifdef _DEBUG
  278. _dprintf(_T("QuerySoc error=%s\n"), m_pError ? m_pError : "");
  279. #endif
  280. return FALSE;
  281. }
  282. #ifdef _DEBUG
  283. // 统计表字段;
  284. unsigned int nLen = mysql_num_fields(pData);
  285. // 字段长度是否一致;
  286. if (nLen != 3) {
  287. mysql_free_result(pData);
  288. return FALSE;
  289. }
  290. // 打印出字段名称;
  291. TCHAR szLog[MAX_PATH] = { 0 };
  292. for (int i = 0; i < nLen; i++) {
  293. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  294. OutputDebugString(szLog);
  295. }
  296. #endif
  297. // 遍历数据;
  298. MYSQL_ROW row;
  299. while ((row = mysql_fetch_row(pData)) != NULL) {
  300. STSOC soc;
  301. soc.name = SAFESTR(row[0]);
  302. soc.provider = SAFESTR(row[1]);
  303. soc.note = SAFESTR(row[2]);
  304. vtSoc.push_back(soc);
  305. }
  306. // 释放内存;
  307. mysql_free_result(pData);
  308. return TRUE;
  309. }
  310. BOOL CDatabase::InsertBrand(const STBrand& brand)
  311. {
  312. CHECKDB;
  313. TCHAR szSql[MAX_PATH] = { 0 };
  314. _stprintf_s(szSql, INSER_BRAND, brand.name.c_str(), brand.fav_cfg.c_str(), brand.local_cfg.c_str(), brand.apps_cfg.c_str(), brand.note.c_str());
  315. if (0 != mysql_query(m_pConn, szSql)) {
  316. #ifdef _DEBUG
  317. _dprintf(_T("InsertBrand error=%s\n"), m_pError ? m_pError : "");
  318. #endif
  319. return FALSE;
  320. }
  321. return TRUE;
  322. }
  323. BOOL CDatabase::InsertBrand(std::string name, std::string fav_cfg, std::string local_cfg, std::string apps_cfg, std::string note)
  324. {
  325. CHECKDB;
  326. TCHAR szSql[MAX_PATH] = { 0 };
  327. _stprintf_s(szSql, INSER_BRAND, name.c_str(), fav_cfg.c_str(), local_cfg.c_str(), apps_cfg.c_str(), note.c_str());
  328. if (0 != mysql_query(m_pConn, szSql)) {
  329. #ifdef _DEBUG
  330. _dprintf(_T("InsertBrand error=%s\n"), m_pError ? m_pError : "");
  331. #endif
  332. return FALSE;
  333. }
  334. return TRUE;
  335. }
  336. BOOL CDatabase::DeleteBrand(std::string name)
  337. {
  338. CHECKDB;
  339. TCHAR szSql[MAX_PATH] = { 0 };
  340. _stprintf_s(szSql, DEL_BRAND, name.c_str());
  341. if (0 != mysql_query(m_pConn, szSql)) {
  342. #ifdef _DEBUG
  343. _dprintf(_T("DeleteBrand error=%s\n"), m_pError ? m_pError : "");
  344. #endif
  345. return FALSE;
  346. }
  347. return TRUE;
  348. }
  349. BOOL CDatabase::UpdateBrand(std::string name, const STBrand& brand)
  350. {
  351. CHECKDB;
  352. TCHAR szSql[MAX_PATH] = { 0 };
  353. _stprintf_s(szSql, MOD_BRAND, brand.name.c_str(), brand.fav_cfg.c_str(), brand.local_cfg.c_str(), brand.apps_cfg.c_str(), brand.note.c_str(), name.c_str());
  354. if (0 != mysql_query(m_pConn, szSql)) {
  355. #ifdef _DEBUG
  356. _dprintf(_T("UpdateBrand error=%s\n"), m_pError ? m_pError : "");
  357. #endif
  358. return FALSE;
  359. }
  360. return TRUE;
  361. }
  362. BOOL CDatabase::QueryBrand(std::vector<STBrand>& vtBrand)
  363. {
  364. CHECKDB;
  365. if (0 != mysql_query(m_pConn, QUERY_BRAND)) {
  366. #ifdef _DEBUG
  367. _dprintf(_T("QueryBrand error=%s\n"), m_pError ? m_pError : "");
  368. #endif
  369. return FALSE;
  370. }
  371. // 获取表数据;
  372. MYSQL_RES* pData = mysql_store_result(m_pConn);
  373. if (pData == NULL) {
  374. #ifdef _DEBUG
  375. _dprintf(_T("QueryBrand error=%s\n"), m_pError ? m_pError : "");
  376. #endif
  377. return FALSE;
  378. }
  379. #ifdef _DEBUG
  380. // 统计表字段;
  381. unsigned int nLen = mysql_num_fields(pData);
  382. // 字段长度是否一致;
  383. if (nLen != 5) {
  384. mysql_free_result(pData);
  385. return FALSE;
  386. }
  387. // 打印出字段名称;
  388. TCHAR szLog[MAX_PATH] = { 0 };
  389. for (int i = 0; i < nLen; i++) {
  390. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  391. OutputDebugString(szLog);
  392. }
  393. #endif
  394. // 遍历数据;
  395. MYSQL_ROW row;
  396. while ((row = mysql_fetch_row(pData)) != NULL) {
  397. STBrand brand;
  398. brand.name = SAFESTR(row[0]);
  399. brand.fav_cfg = SAFESTR(row[1]);
  400. brand.local_cfg = SAFESTR(row[2]);
  401. brand.apps_cfg = SAFESTR(row[3]);
  402. brand.note = SAFESTR(row[4]);
  403. vtBrand.push_back(brand);
  404. }
  405. // 释放内存;
  406. mysql_free_result(pData);
  407. return TRUE;
  408. }
  409. BOOL CDatabase::ImportBrandLogo(std::string brand, std::string file)
  410. {
  411. if (!PathFileExists(file.c_str()))
  412. {
  413. _dprintf(_T("%s:logo不存在[%s]"), brand.c_str(), file.c_str());
  414. return FALSE;
  415. }
  416. // 读出图片;
  417. CFile cfile;
  418. if (!cfile.Open(file.c_str(), CFile::modeRead, NULL))
  419. return FALSE;
  420. ULONGLONG ulLength = cfile.GetLength();
  421. char* pData = new char[ulLength];
  422. cfile.Read(pData, ulLength);
  423. cfile.Close();
  424. TCHAR szSql[MAX_PATH] = {0};
  425. _stprintf_s(szSql, _T("update brand set logo = ? where `name` = '%s'"), brand.c_str());
  426. BOOL bResult = SetBinaryField(szSql, pData, ulLength);
  427. if (pData)
  428. delete[]pData;
  429. pData = NULL;
  430. return bResult;
  431. }
  432. BOOL CDatabase::ExportBrandLogo(std::string brand, std::string file)
  433. {
  434. char* pData = NULL;
  435. unsigned long ulLength = 0;
  436. TCHAR szSql[MAX_PATH] = {0};
  437. _stprintf_s(szSql, _T("select logo from brand where `name`='%s';"), brand.c_str());
  438. BOOL bResult = GetBinaryField(szSql, (void**)&pData, ulLength);
  439. if (bResult && pData)
  440. {
  441. CFile cfile;
  442. if (cfile.Open(file.c_str(), CFile::modeCreate | CFile::modeWrite))
  443. {
  444. cfile.Write(pData, ulLength);
  445. cfile.Close();
  446. }
  447. delete[]pData;
  448. pData = NULL;
  449. }
  450. return bResult;
  451. }
  452. BOOL CDatabase::InsertQuarter(const STQuarter& quarter)
  453. {
  454. CHECKDB;
  455. TCHAR szSql[MAX_PATH] = { 0 };
  456. _stprintf_s(szSql, INSER_QUARTER, quarter.name.c_str(), quarter.scp.c_str(), quarter.note.c_str());
  457. if (0 != mysql_query(m_pConn, szSql)) {
  458. #ifdef _DEBUG
  459. _dprintf(_T("InsertQuarter error=%s\n"), m_pError ? m_pError : "");
  460. #endif
  461. return FALSE;
  462. }
  463. return TRUE;
  464. }
  465. BOOL CDatabase::InsertQuarter(std::string name, std::string scp, std::string note)
  466. {
  467. CHECKDB;
  468. TCHAR szSql[MAX_PATH] = { 0 };
  469. _stprintf_s(szSql, INSER_QUARTER, name.c_str(), scp.c_str(), note.c_str());
  470. if (0 != mysql_query(m_pConn, szSql)) {
  471. #ifdef _DEBUG
  472. _dprintf(_T("InsertQuarter error=%s\n"), m_pError ? m_pError : "");
  473. #endif
  474. return FALSE;
  475. }
  476. return TRUE;
  477. }
  478. BOOL CDatabase::DeleteQuarter(std::string name)
  479. {
  480. CHECKDB;
  481. TCHAR szSql[MAX_PATH] = { 0 };
  482. _stprintf_s(szSql, DEL_QUARTER, name.c_str());
  483. if (0 != mysql_query(m_pConn, szSql)) {
  484. #ifdef _DEBUG
  485. _dprintf(_T("DeleteQuarter error=%s\n"), m_pError ? m_pError : "");
  486. #endif
  487. return FALSE;
  488. }
  489. return TRUE;
  490. }
  491. BOOL CDatabase::UpdateQuarter(std::string name, const STQuarter& quarter)
  492. {
  493. CHECKDB;
  494. TCHAR szSql[MAX_PATH] = { 0 };
  495. _stprintf_s(szSql, MOD_QUARTER,quarter.name.c_str(), quarter.scp.c_str(), quarter.note.c_str(), name.c_str());
  496. if (0 != mysql_query(m_pConn, szSql)) {
  497. #ifdef _DEBUG
  498. _dprintf(_T("UpdateQuarter error=%s\n"), m_pError ? m_pError : "");
  499. #endif
  500. return FALSE;
  501. }
  502. return TRUE;
  503. }
  504. BOOL CDatabase::QueryQuarter(std::vector<STQuarter>& vtQuarter)
  505. {
  506. CHECKDB;
  507. if (0 != mysql_query(m_pConn, QUERY_QUARTER)) {
  508. #ifdef _DEBUG
  509. _dprintf(_T("QueryQuarter error=%s\n"), m_pError ? m_pError : "");
  510. #endif
  511. return FALSE;
  512. }
  513. // 获取表数据;
  514. MYSQL_RES* pData = mysql_store_result(m_pConn);
  515. if (pData == NULL) {
  516. #ifdef _DEBUG
  517. _dprintf(_T("QueryQuarter error=%s\n"), m_pError ? m_pError : "");
  518. #endif
  519. return FALSE;
  520. }
  521. #ifdef _DEBUG
  522. // 统计表字段;
  523. unsigned int nLen = mysql_num_fields(pData);
  524. // 字段长度是否一致;
  525. if (nLen != 3) {
  526. mysql_free_result(pData);
  527. return FALSE;
  528. }
  529. // 打印出字段名称;
  530. TCHAR szLog[MAX_PATH] = { 0 };
  531. for (int i = 0; i < nLen; i++) {
  532. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  533. OutputDebugString(szLog);
  534. }
  535. #endif
  536. // 遍历数据;
  537. MYSQL_ROW row;
  538. while ((row = mysql_fetch_row(pData)) != NULL) {
  539. STQuarter quarter;
  540. quarter.name = SAFESTR(row[0]);
  541. quarter.scp = SAFESTR(row[1]);
  542. quarter.note = SAFESTR(row[2]);
  543. vtQuarter.push_back(quarter);
  544. }
  545. // 释放内存;
  546. mysql_free_result(pData);
  547. return TRUE;
  548. }
  549. BOOL CDatabase::InsertQBrand(const STQbrand& QBrand)
  550. {
  551. CHECKDB;
  552. TCHAR szSql[MAX_PATH] = { 0 };
  553. _stprintf_s(szSql, INSER_QBRAND, QBrand.soc.c_str(), QBrand.brand.c_str(), QBrand.quarter.c_str(),
  554. QBrand.whitelist.c_str(), QBrand.version.c_str(),
  555. QBrand.oemkey.c_str(), QBrand.approved.c_str(),
  556. QBrand.approveddate.c_str(), QBrand.fingerprint.c_str(),
  557. QBrand.builddate.c_str(), QBrand.gtvs.c_str(),
  558. QBrand.ftpdir.c_str(), QBrand.codedir.c_str(),
  559. QBrand.note.c_str(), QBrand.status.c_str());
  560. if (0 != mysql_query(m_pConn, szSql)) {
  561. #ifdef _DEBUG
  562. _dprintf(_T("InsertQBrand error=%s\n"), m_pError ? m_pError : "");
  563. #endif
  564. return FALSE;
  565. }
  566. return TRUE;
  567. }
  568. BOOL CDatabase::DeleteQBrand(std::string soc, std::string quarter, std::string brand)
  569. {
  570. CHECKDB;
  571. TCHAR szSql[MAX_PATH] = { 0 };
  572. _stprintf_s(szSql, DEL_QBRAND, soc.c_str(), brand.c_str(), quarter.c_str());
  573. if (0 != mysql_query(m_pConn, szSql)) {
  574. #ifdef _DEBUG
  575. _dprintf(_T("DeleteQBrand error=%s\n"), m_pError ? m_pError : "");
  576. #endif
  577. return FALSE;
  578. }
  579. return TRUE;
  580. }
  581. BOOL CDatabase::UpdateQBrand(std::string brand, STQbrand& QBrand)
  582. {
  583. CHECKDB;
  584. TCHAR szSql[MAX_PATH] = { 0 };
  585. _stprintf_s(szSql, MOD_QBRAND, QBrand.soc.c_str(), QBrand.quarter.c_str(),
  586. QBrand.brand.c_str(), QBrand.whitelist.c_str(),
  587. QBrand.version.c_str(), QBrand.approved.c_str(),
  588. QBrand.oemkey.c_str(), QBrand.approveddate.c_str(),
  589. QBrand.fingerprint.c_str(), QBrand.builddate.c_str(),
  590. QBrand.gtvs.c_str(), QBrand.note.c_str(),
  591. QBrand.apps_cfg.c_str(), QBrand.fav_cfg.c_str(),
  592. QBrand.local_cfg.c_str(), QBrand.ftpdir.c_str(),
  593. QBrand.codedir.c_str(), QBrand.status.c_str(),
  594. brand.c_str());
  595. if (0 != mysql_query(m_pConn, szSql)) {
  596. #ifdef _DEBUG
  597. _dprintf(_T("UpdateQBrand error=%s\n"), m_pError ? m_pError : "");
  598. #endif
  599. return FALSE;
  600. }
  601. return TRUE;
  602. }
  603. BOOL CDatabase::UpdateQBrand(std::string soc, std::string quarter, std::string brand, STQbrand& QBrand)
  604. {
  605. CHECKDB;
  606. TCHAR szSql[MAX_PATH] = { 0 };
  607. _stprintf_s(szSql, MOD_QBRAND,
  608. QBrand.soc.c_str(), QBrand.quarter.c_str(), QBrand.brand.c_str(),
  609. QBrand.whitelist.c_str(), QBrand.version.c_str(), QBrand.approved.c_str(),
  610. QBrand.oemkey.c_str(), QBrand.approveddate.c_str(), QBrand.fingerprint.c_str(),
  611. QBrand.builddate.c_str(), QBrand.gtvs.c_str(), QBrand.note.c_str(),
  612. QBrand.apps_cfg.c_str(), QBrand.fav_cfg.c_str(), QBrand.local_cfg.c_str(),
  613. QBrand.ftpdir.c_str(), QBrand.codedir.c_str(), QBrand.status.c_str(),
  614. brand.c_str());
  615. if (0 != mysql_query(m_pConn, szSql)) {
  616. #ifdef _DEBUG
  617. _dprintf(_T("UpdateQBrand error=%s\n"), m_pError ? m_pError : "");
  618. #endif
  619. return FALSE;
  620. }
  621. return TRUE;
  622. }
  623. BOOL CDatabase::QueryQBrand(std::vector<STQbrand>& vtQBrand)
  624. {
  625. CHECKDB;
  626. if (0 != mysql_query(m_pConn, QUERY_QBRAND)) {
  627. #ifdef _DEBUG
  628. _dprintf(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
  629. #endif
  630. return FALSE;
  631. }
  632. // 获取表数据;
  633. MYSQL_RES* pData = mysql_store_result(m_pConn);
  634. if (pData == NULL) {
  635. #ifdef _DEBUG
  636. _dprintf(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
  637. #endif
  638. return FALSE;
  639. }
  640. #ifdef _DEBUG
  641. // 统计表字段;
  642. unsigned int nLen = mysql_num_fields(pData);
  643. // 字段长度是否一致;
  644. if (nLen != 19) {
  645. mysql_free_result(pData);
  646. return FALSE;
  647. }
  648. // 打印出字段名称;
  649. TCHAR szLog[MAX_PATH] = { 0 };
  650. for (int i = 0; i < nLen; i++) {
  651. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  652. OutputDebugString(szLog);
  653. }
  654. #endif
  655. // 遍历数据;
  656. MYSQL_ROW row;
  657. while ((row = mysql_fetch_row(pData)) != NULL) {
  658. STQbrand QBrand;
  659. QBrand.soc = SAFESTR(row[0]);
  660. QBrand.quarter = SAFESTR(row[1]);
  661. QBrand.brand = SAFESTR(row[2]);
  662. QBrand.whitelist = SAFESTR(row[3]);
  663. QBrand.version = SAFESTR(row[4]);
  664. QBrand.oemkey = SAFESTR(row[5]);
  665. QBrand.approved = SAFESTR(row[6]);
  666. QBrand.approveddate = SAFESTR(row[7]);
  667. QBrand.fingerprint = SAFESTR(row[8]);
  668. QBrand.builddate = SAFESTR(row[9]);
  669. QBrand.gtvs = SAFESTR(row[10]);
  670. QBrand.note = SAFESTR(row[11]);
  671. QBrand.apps_cfg = SAFESTR(row[12]);
  672. QBrand.fav_cfg = SAFESTR(row[13]);
  673. QBrand.local_cfg = SAFESTR(row[14]);
  674. QBrand.ftpdir = SAFESTR(row[15]);
  675. QBrand.codedir = SAFESTR(row[16]);
  676. QBrand.status = SAFESTR(row[17]);
  677. vtQBrand.push_back(QBrand);
  678. }
  679. // 释放内存;
  680. mysql_free_result(pData);
  681. return TRUE;
  682. }
  683. BOOL CDatabase::ImportQBrandLogo(std::string file, std::string brand, std::string soc, std::string quarter)
  684. {
  685. if (!PathFileExists(file.c_str()))
  686. return FALSE;
  687. // 读出图片;
  688. CFile cfile;
  689. if (!cfile.Open(file.c_str(), CFile::modeRead, NULL))
  690. return FALSE;
  691. ULONGLONG ulLength = cfile.GetLength();
  692. char* pData = new char[ulLength];
  693. cfile.Read(pData, ulLength);
  694. cfile.Close();
  695. TCHAR szSql[MAX_PATH] = { 0 };
  696. _stprintf_s(szSql, _T("update qbrand set logo = ? where `soc` = '%s' and `quarter`='%s' and `brand`='%s';"), soc.c_str(), quarter.c_str(), brand.c_str());
  697. BOOL bResult = SetBinaryField(szSql, pData, ulLength);
  698. if (pData)
  699. delete[]pData;
  700. pData = NULL;
  701. return bResult;
  702. }
  703. BOOL CDatabase::ExportQBrandLogo(std::string file, std::string brand, std::string soc, std::string quarter)
  704. {
  705. char* pData = NULL;
  706. unsigned long ulLength = 0;
  707. TCHAR szSql[MAX_PATH] = { 0 };
  708. _stprintf_s(szSql, _T("select logo from qbrand where `soc` = '%s' and `quarter`='%s' and `brand`='%s';"), soc.c_str(), quarter.c_str(), brand.c_str());
  709. BOOL bResult = GetBinaryField(szSql, (void**)&pData, ulLength);
  710. if (bResult && pData)
  711. {
  712. CFile cfile;
  713. if (cfile.Open(file.c_str(), CFile::modeCreate | CFile::modeWrite))
  714. {
  715. cfile.Write(pData, ulLength);
  716. cfile.Close();
  717. }
  718. delete[]pData;
  719. pData = NULL;
  720. }
  721. return bResult;
  722. }
  723. BOOL CDatabase::InsertUser(const STUser& user)
  724. {
  725. CHECKDB;
  726. TCHAR szSql[MAX_PATH] = { 0 };
  727. _stprintf_s(szSql, INSER_USER, user.user.c_str(), user.password.c_str(), user.permission.c_str());
  728. if (0 != mysql_query(m_pConn, szSql)) {
  729. #ifdef _DEBUG
  730. _dprintf(_T("InsertUser error=%s\n"), m_pError ? m_pError : "");
  731. #endif
  732. return FALSE;
  733. }
  734. return TRUE;
  735. }
  736. BOOL CDatabase::InsertUser(std::string user, std::string password, std::string permission)
  737. {
  738. CHECKDB;
  739. TCHAR szSql[MAX_PATH] = { 0 };
  740. _stprintf_s(szSql, INSER_USER, user.c_str(), password.c_str(), permission.c_str());
  741. if (0 != mysql_query(m_pConn, szSql)) {
  742. #ifdef _DEBUG
  743. _dprintf(_T("InsertUser error=%s\n"), m_pError ? m_pError : "");
  744. #endif
  745. return FALSE;
  746. }
  747. return TRUE;
  748. }
  749. BOOL CDatabase::DeleteUser(std::string user)
  750. {
  751. CHECKDB;
  752. TCHAR szSql[MAX_PATH] = { 0 };
  753. _stprintf_s(szSql, DEL_USER, user.c_str());
  754. if (0 != mysql_query(m_pConn, szSql)) {
  755. #ifdef _DEBUG
  756. _dprintf(_T("DeleteUser error=%s\n"), m_pError ? m_pError : "");
  757. #endif
  758. return FALSE;
  759. }
  760. return TRUE;
  761. }
  762. BOOL CDatabase::UpdateUser(std::string user, const STUser& stUser)
  763. {
  764. CHECKDB;
  765. TCHAR szSql[MAX_PATH] = { 0 };
  766. _stprintf_s(szSql, MOD_USER, stUser.user.c_str(), stUser.password.c_str(), stUser.permission.c_str(), user.c_str());
  767. if (0 != mysql_query(m_pConn, szSql)) {
  768. #ifdef _DEBUG
  769. _dprintf(_T("UpdateUser error=%s\n"), m_pError ? m_pError : "");
  770. #endif
  771. return FALSE;
  772. }
  773. return TRUE;
  774. }
  775. BOOL CDatabase::QueryUser(std::vector<STUser>& vtUser)
  776. {
  777. CHECKDB;
  778. if (0 != mysql_query(m_pConn, QUERY_USER)) {
  779. #ifdef _DEBUG
  780. _dprintf(_T("QueryUser error=%s\n"), m_pError ? m_pError : "");
  781. #endif
  782. return FALSE;
  783. }
  784. // 获取表数据;
  785. MYSQL_RES* pData = mysql_store_result(m_pConn);
  786. if (pData == NULL) {
  787. #ifdef _DEBUG
  788. _dprintf(_T("QueryUser error=%s\n"), m_pError ? m_pError : "");
  789. #endif
  790. return FALSE;
  791. }
  792. #ifdef _DEBUG
  793. // 统计表字段;
  794. unsigned int nLen = mysql_num_fields(pData);
  795. // 字段长度是否一致;
  796. if (nLen != 3) {
  797. mysql_free_result(pData);
  798. return FALSE;
  799. }
  800. // 打印出字段名称;
  801. TCHAR szLog[MAX_PATH] = { 0 };
  802. for (int i = 0; i < nLen; i++) {
  803. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  804. OutputDebugString(szLog);
  805. }
  806. #endif
  807. // 遍历数据;
  808. MYSQL_ROW row;
  809. while ((row = mysql_fetch_row(pData)) != NULL) {
  810. STUser stUser;
  811. stUser.user = SAFESTR(row[0]);
  812. stUser.password = SAFESTR(row[1]);
  813. stUser.permission = SAFESTR(row[2]);
  814. vtUser.push_back(stUser);
  815. }
  816. // 释放内存;
  817. mysql_free_result(pData);
  818. return TRUE;
  819. }
  820. BOOL CDatabase::InsertServer(const STServer& Server)
  821. {
  822. CHECKDB;
  823. TCHAR szSql[MAX_PATH] = { 0 };
  824. _stprintf_s(szSql, INSER_SERVER, Server.name.c_str(), Server.ip.c_str(), Server.type.c_str(), Server.user.c_str(), Server.password.c_str(),Server.note.c_str());
  825. if (0 != mysql_query(m_pConn, szSql)) {
  826. #ifdef _DEBUG
  827. _dprintf(_T("InsertServer error=%s\n"), m_pError ? m_pError : "");
  828. #endif
  829. return FALSE;
  830. }
  831. return TRUE;
  832. }
  833. BOOL CDatabase::InsertServer(std::string name, std::string ip, std::string type, std::string user, std::string password, std::string note)
  834. {
  835. CHECKDB;
  836. TCHAR szSql[MAX_PATH] = { 0 };
  837. _stprintf_s(szSql, INSER_SERVER, name.c_str(), ip.c_str(), type.c_str(), user.c_str(), password.c_str(), note.c_str());
  838. if (0 != mysql_query(m_pConn, szSql)) {
  839. #ifdef _DEBUG
  840. _dprintf(_T("InsertServer error=%s\n"), m_pError ? m_pError : "");
  841. #endif
  842. return FALSE;
  843. }
  844. return TRUE;
  845. }
  846. BOOL CDatabase::DeleteServer(std::string name)
  847. {
  848. CHECKDB;
  849. TCHAR szSql[MAX_PATH] = { 0 };
  850. _stprintf_s(szSql, DEL_SERVER, name.c_str());
  851. if (0 != mysql_query(m_pConn, szSql)) {
  852. #ifdef _DEBUG
  853. _dprintf(_T("DeleteServer error=%s\n"), m_pError ? m_pError : "");
  854. #endif
  855. return FALSE;
  856. }
  857. return TRUE;
  858. }
  859. BOOL CDatabase::UpdateServer(std::string name, const STServer& Server)
  860. {
  861. CHECKDB;
  862. TCHAR szSql[MAX_PATH] = { 0 };
  863. _stprintf_s(szSql, MOD_SERVER, Server.name.c_str(), Server.ip.c_str(), Server.type.c_str(), Server.user.c_str(), Server.password.c_str(), Server.note.c_str(), name.c_str());
  864. if (0 != mysql_query(m_pConn, szSql)) {
  865. #ifdef _DEBUG
  866. _dprintf(_T("UpdateServer error=%s\n"), m_pError ? m_pError : "");
  867. #endif
  868. return FALSE;
  869. }
  870. return TRUE;
  871. }
  872. BOOL CDatabase::QueryServer(std::vector<STServer>& vtServer)
  873. {
  874. CHECKDB;
  875. if (0 != mysql_query(m_pConn, QUERY_SERVER)) {
  876. #ifdef _DEBUG
  877. _dprintf(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
  878. #endif
  879. return FALSE;
  880. }
  881. // 获取表数据;
  882. MYSQL_RES* pData = mysql_store_result(m_pConn);
  883. if (pData == NULL) {
  884. #ifdef _DEBUG
  885. _dprintf(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
  886. #endif
  887. return FALSE;
  888. }
  889. #ifdef _DEBUG
  890. // 统计表字段;
  891. unsigned int nLen = mysql_num_fields(pData);
  892. // 字段长度是否一致;
  893. if (nLen != 6) {
  894. mysql_free_result(pData);
  895. return FALSE;
  896. }
  897. // 打印出字段名称;
  898. TCHAR szLog[MAX_PATH] = { 0 };
  899. for (int i = 0; i < nLen; i++) {
  900. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  901. OutputDebugString(szLog);
  902. }
  903. #endif
  904. // 遍历数据;
  905. MYSQL_ROW row;
  906. while ((row = mysql_fetch_row(pData)) != NULL) {
  907. STServer server;
  908. server.name = SAFESTR(row[0]);
  909. server.ip = SAFESTR(row[1]);
  910. server.type = SAFESTR(row[2]);
  911. server.user = SAFESTR(row[3]);
  912. server.password = SAFESTR(row[4]);
  913. server.note = SAFESTR(row[5]);
  914. vtServer.push_back(server);
  915. }
  916. // 释放内存;
  917. mysql_free_result(pData);
  918. return TRUE;
  919. }
  920. BOOL CDatabase::InsertBranch(const STBranch& branch)
  921. {
  922. CHECKDB;
  923. TCHAR szSql[MAX_PATH] = { 0 };
  924. _stprintf_s(szSql, INSER_BRANCH, branch.name.c_str(), branch.quarter.c_str(),
  925. branch.address.c_str(), branch.provider.c_str(), branch.chip.c_str(),
  926. branch.type.c_str(), branch.status.c_str(), branch.note.c_str());
  927. if (0 != mysql_query(m_pConn, szSql)) {
  928. #ifdef _DEBUG
  929. _dprintf(_T("InsertBranch error=%s\n"), m_pError ? m_pError : "");
  930. #endif
  931. return FALSE;
  932. }
  933. return TRUE;
  934. }
  935. BOOL CDatabase::DeleteBranch(std::string branch)
  936. {
  937. CHECKDB;
  938. TCHAR szSql[MAX_PATH] = { 0 };
  939. _stprintf_s(szSql, DEL_BRANCH, branch.c_str());
  940. if (0 != mysql_query(m_pConn, szSql)) {
  941. #ifdef _DEBUG
  942. _dprintf(_T("DeleteBranch error=%s\n"), m_pError ? m_pError : "");
  943. #endif
  944. return FALSE;
  945. }
  946. return TRUE;
  947. }
  948. BOOL CDatabase::UpdateBranch(std::string branch, const STBranch& stBranch)
  949. {
  950. CHECKDB;
  951. TCHAR szSql[MAX_PATH] = { 0 };
  952. _stprintf_s(szSql, MOD_BRANCH, stBranch.quarter.c_str(), stBranch.address.c_str(),
  953. stBranch.provider.c_str(), stBranch.chip.c_str(), stBranch.type.c_str(),
  954. stBranch.status.c_str(), stBranch.note.c_str(),branch.c_str());
  955. if (0 != mysql_query(m_pConn, szSql)) {
  956. #ifdef _DEBUG
  957. _dprintf(_T("UpdateBranch error=%s\n"), m_pError ? m_pError : "");
  958. #endif
  959. return FALSE;
  960. }
  961. return TRUE;
  962. }
  963. BOOL CDatabase::QueryBranch(std::vector<STBranch>& vtBranch)
  964. {
  965. CHECKDB;
  966. if (0 != mysql_query(m_pConn, QUERY_BRANCH)) {
  967. #ifdef _DEBUG
  968. _dprintf(_T("QueryBranch error=%s\n"), m_pError ? m_pError : "");
  969. #endif
  970. return FALSE;
  971. }
  972. // 获取表数据;
  973. MYSQL_RES* pData = mysql_store_result(m_pConn);
  974. if (pData == NULL) {
  975. #ifdef _DEBUG
  976. _dprintf(_T("QueryBranch error=%s\n"), m_pError ? m_pError : "");
  977. #endif
  978. return FALSE;
  979. }
  980. #ifdef _DEBUG
  981. // 统计表字段;
  982. unsigned int nLen = mysql_num_fields(pData);
  983. // 字段长度是否一致;
  984. if (nLen != 8) {
  985. mysql_free_result(pData);
  986. return FALSE;
  987. }
  988. // 打印出字段名称;
  989. TCHAR szLog[MAX_PATH] = { 0 };
  990. for (int i = 0; i < nLen; i++) {
  991. _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
  992. OutputDebugString(szLog);
  993. }
  994. #endif
  995. // 遍历数据;
  996. MYSQL_ROW row;
  997. while ((row = mysql_fetch_row(pData)) != NULL) {
  998. STBranch stBranch;
  999. stBranch.name = SAFESTR(row[0]);
  1000. stBranch.quarter = SAFESTR(row[1]);
  1001. stBranch.address = SAFESTR(row[2]);
  1002. stBranch.provider = SAFESTR(row[3]);
  1003. stBranch.chip = SAFESTR(row[4]);
  1004. stBranch.type = SAFESTR(row[5]);
  1005. stBranch.status = SAFESTR(row[6]);
  1006. stBranch.note = SAFESTR(row[7]);
  1007. vtBranch.push_back(stBranch);
  1008. }
  1009. // 释放内存;
  1010. mysql_free_result(pData);
  1011. return TRUE;
  1012. }