1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174 |
- #include "pch.h"
- #include "Database.h"
- #define CHECKDB if (m_pConn == NULL || !m_bConnected) return FALSE
- #define SAFESTR(str) ( str == NULL ? _T("") : str)
- char* CDatabase::m_spImage = NULL;
- CDatabase::CDatabase():m_pConn(NULL), m_pError(NULL), m_bConnected(FALSE)
- {
- m_pConn = mysql_init(NULL);
- }
- CDatabase::~CDatabase()
- {
- mysql_close(m_pConn);
- mysql_library_end();
- }
- BOOL CDatabase::SetBinaryField(std::string strCondition, void* pDataIn, int nDataLen)
- {
- CHECKDB;
- BOOL bResult = FALSE;
- MYSQL_STMT* stmt = NULL;
- stmt = mysql_stmt_init(m_pConn);
- if (!stmt)
- return FALSE;
- if (!mysql_stmt_prepare(stmt, strCondition.c_str(), strCondition.length()))
- {
- MYSQL_BIND bind;
- memset(&bind, 0, sizeof(MYSQL_BIND));
- bind.buffer_type = MYSQL_TYPE_BLOB;
- bind.buffer_length = nDataLen;
- bind.buffer = pDataIn;
- if (!mysql_stmt_bind_param(stmt, (MYSQL_BIND*)&bind))
- {
- if (!mysql_stmt_execute(stmt))
- {
- bResult = TRUE;
- }
- }
- }
- mysql_stmt_close(stmt);
- return bResult;
- }
- BOOL CDatabase::GetBinaryField(std::string strCondition, void** lpDataOut, unsigned long& nDataLen)
- {
- CHECKDB;
- BOOL bResult = FALSE;
- MYSQL_STMT* stmt = NULL;
- stmt = mysql_stmt_init(m_pConn);
- if (!stmt)
- return FALSE;
- MYSQL_RES* prepare_meta_result;
- if (!mysql_stmt_prepare(stmt, strCondition.c_str(), strCondition.length()))
- {
- if ( !mysql_stmt_execute(stmt) )
- {
- /* Fetch result set meta information */
- prepare_meta_result = mysql_stmt_result_metadata(stmt);
- if (prepare_meta_result)
- {
- /* Get total columns in the query */
- //int column_count = mysql_num_fields(prepare_meta_result);
- my_bool is_null;
- my_bool error;
- MYSQL_BIND bind[1];
- memset(bind, 0, sizeof(bind));
- bind[0].buffer_type = MYSQL_TYPE_BLOB;
- bind[0].buffer = 0;
- bind[0].buffer_length = 0;
- bind[0].is_null = &is_null;
- bind[0].length = &nDataLen; // 返回的缓存长度;
- bind[0].error = &error;
- /* Bind the result buffers */
- if (!mysql_stmt_bind_result(stmt, bind))
- {
- // 由于没有绑定缓冲区指针,第一次返回缓冲区大小;
- if (mysql_stmt_fetch(stmt))
- {
- if (nDataLen > 1)
- {
- *lpDataOut = new char[nDataLen];
- memset(*lpDataOut, 0, nDataLen);
- bind[0].buffer = *lpDataOut;
- bind[0].buffer_length = nDataLen;
- mysql_stmt_fetch_column(stmt, bind, 0, 0);
- bResult = TRUE;
- }
- }
- }
- /* Free the prepared result metadata */
- mysql_free_result(prepare_meta_result);
- }
- }
- }
- /* Close the statement */
- mysql_stmt_close(stmt);
- return bResult;
- }
- BOOL CDatabase::Init(std::string host, std::string user, std::string password, std::string db)
- {
- if (!m_pConn)
- return FALSE;
- m_pError = mysql_error(m_pConn);
- if (!mysql_real_connect(m_pConn, host.c_str(), user.c_str(), password.c_str(), db.c_str(), 0, NULL, 0)) {
- #ifdef _DEBUG
- _dprintf(_T("mysql_real_connect error=%s\n"), m_pError ? m_pError: "");
- #endif
- return FALSE;
- }
- return m_bConnected = TRUE;
- }
- BOOL CDatabase::ExcuteSQL(std::string sql)
- {
- if (m_pConn == NULL || !m_bConnected)
- return FALSE;
- if (0 != mysql_query(m_pConn, sql.c_str())) {
- #ifdef _DEBUG
- _dprintf(_T("ExcuteSQL error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::InserSoc(const STSOC& soc)
- {
- CHECKDB;
- #if 0
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_SOC, soc.name.c_str(), soc.provider.c_str(), soc.note.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- #else
- MYSQL_STMT* stmt;
- MYSQL_BIND bind[3];
- TCHAR szQuery[MAX_PATH] = { 0 };
- strcpy_s(szQuery, _T("INSERT INTO soc(name, provider, note) VALUES(?,?,?);"));
- BOOL bResult = FALSE;
- //初始化stmt
- stmt = mysql_stmt_init(m_pConn);
- if (!stmt)
- return FALSE;
- //预处理语句
- if (mysql_stmt_prepare(stmt, szQuery, strlen(szQuery)))
- goto over;
-
- //初始化参数
- bind[0].buffer_type = MYSQL_TYPE_VARCHAR;
- bind[0].buffer = (void*)soc.name.c_str();
- bind[0].buffer_length = soc.name.size();
- bind[0].is_null = 0;
- bind[1].buffer_type = MYSQL_TYPE_VARCHAR;
- bind[1].buffer = (void*)soc.provider.c_str();
- bind[1].buffer_length = soc.provider.size();
- bind[1].is_null = 0;
- bind[2].buffer_type = MYSQL_TYPE_VARCHAR;
- bind[2].buffer = (void*)soc.note.c_str();
- bind[2].buffer_length = soc.note.size();
- bind[2].is_null = 0;
- //绑定参数123
- if (mysql_stmt_bind_param(stmt, bind))
- goto over;
- //执行预处理mysql语句
- if (!mysql_stmt_execute(stmt))
- bResult = TRUE;
- over:
- mysql_stmt_close(stmt);
- #ifdef _DEBUG
- if ( !bResult )
- _dprintf(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
- #endif
- return bResult;
- #endif
- }
- BOOL CDatabase::InserSoc(std::string name, std::string provider, std::string note)
- {
- CHECKDB;
- #if 0
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_SOC, name.c_str(), provider.c_str(), note.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- #else
- //https://dev.mysql.com/doc/c-api/5.6/en/mysql-stmt-execute.html
- MYSQL_STMT* stmt;
- MYSQL_BIND bind[3];
- TCHAR szQuery[MAX_PATH] = { 0 };
- strcpy_s(szQuery, _T("INSERT INTO soc(name, provider, note) VALUES(?,?,?);"));
- //初始化stmt
- BOOL bResult = FALSE;
- stmt = mysql_stmt_init(m_pConn);
- if (!stmt)
- return FALSE;
- //预处理语句
- if (mysql_stmt_prepare(stmt, szQuery, strlen(szQuery)))
- goto over;
- // 字段数量是否匹配;
- int param_count = mysql_stmt_param_count(stmt);
- if (param_count != 3)
- goto over;
- //初始化参数
- memset(bind, 0, sizeof(bind));
- bind[0].buffer_type = MYSQL_TYPE_VARCHAR;
- bind[0].buffer = (void*)name.c_str();
- bind[0].buffer_length = name.size();
- bind[0].is_null = 0;
- bind[1].buffer_type = MYSQL_TYPE_VARCHAR;
- bind[1].buffer = (void*)provider.c_str();
- bind[1].buffer_length = provider.size();
- bind[1].is_null = 0;
- bind[2].buffer_type = MYSQL_TYPE_VARCHAR;
- bind[2].buffer = (void*)note.c_str();
- bind[2].buffer_length = note.size();
- bind[2].is_null = 0;
- //绑定参数123
- if (mysql_stmt_bind_param(stmt, bind))
- goto over;
-
- //执行预处理mysql语句
- if (mysql_stmt_execute(stmt))
- goto over;
- // 影响的行数;
- int affected_rows = mysql_stmt_affected_rows(stmt);
- if (affected_rows == 1)
- bResult = TRUE;
- over:
- #ifdef _DEBUG
- if (!bResult)
- _dprintf(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
- #endif
- mysql_stmt_close(stmt);
- return bResult;
- #endif
- }
- BOOL CDatabase::DeleteSoc(std::string name)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, DEL_SOC, name.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("DeleteSoc error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::UpdateSoc(std::string name, const STSOC& soc)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, MOD_SOC, soc.name.c_str(), soc.provider.c_str(), soc.note.c_str(), name.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("UpdateSoc error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::QuerySoc(std::vector<STSOC>& vtSoc)
- {
- CHECKDB;
- if (0 != mysql_query(m_pConn, QUERY_SOC)) {
- #ifdef _DEBUG
- _dprintf(_T("QuerySoc error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- // 获取表数据;
- MYSQL_RES* pData = mysql_store_result(m_pConn);
- if (pData == NULL) {
- #ifdef _DEBUG
- _dprintf(_T("QuerySoc error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- #ifdef _DEBUG
- // 统计表字段;
- unsigned int nLen = mysql_num_fields(pData);
- // 字段长度是否一致;
- if (nLen != 3) {
- mysql_free_result(pData);
- return FALSE;
- }
- // 打印出字段名称;
- TCHAR szLog[MAX_PATH] = { 0 };
- for (int i = 0; i < nLen; i++) {
- _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
- OutputDebugString(szLog);
- }
- #endif
- // 遍历数据;
- MYSQL_ROW row;
- while ((row = mysql_fetch_row(pData)) != NULL) {
- STSOC soc;
- soc.name = SAFESTR(row[0]);
- soc.provider = SAFESTR(row[1]);
- soc.note = SAFESTR(row[2]);
- vtSoc.push_back(soc);
- }
- // 释放内存;
- mysql_free_result(pData);
- return TRUE;
- }
- BOOL CDatabase::InsertBrand(const STBrand& brand)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _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());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::InsertBrand(std::string name, std::string fav_cfg, std::string local_cfg, std::string apps_cfg, std::string note)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_BRAND, name.c_str(), fav_cfg.c_str(), local_cfg.c_str(), apps_cfg.c_str(), note.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::DeleteBrand(std::string name)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, DEL_BRAND, name.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("DeleteBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::UpdateBrand(std::string name, const STBrand& brand)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _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());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("UpdateBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::QueryBrand(std::vector<STBrand>& vtBrand)
- {
- CHECKDB;
- if (0 != mysql_query(m_pConn, QUERY_BRAND)) {
- #ifdef _DEBUG
- _dprintf(_T("QueryBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- // 获取表数据;
- MYSQL_RES* pData = mysql_store_result(m_pConn);
- if (pData == NULL) {
- #ifdef _DEBUG
- _dprintf(_T("QueryBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- #ifdef _DEBUG
- // 统计表字段;
- unsigned int nLen = mysql_num_fields(pData);
- // 字段长度是否一致;
- if (nLen != 5) {
- mysql_free_result(pData);
- return FALSE;
- }
- // 打印出字段名称;
- TCHAR szLog[MAX_PATH] = { 0 };
- for (int i = 0; i < nLen; i++) {
- _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
- OutputDebugString(szLog);
- }
- #endif
- // 遍历数据;
- MYSQL_ROW row;
- while ((row = mysql_fetch_row(pData)) != NULL) {
- STBrand brand;
- brand.name = SAFESTR(row[0]);
- brand.fav_cfg = SAFESTR(row[1]);
- brand.local_cfg = SAFESTR(row[2]);
- brand.apps_cfg = SAFESTR(row[3]);
- brand.note = SAFESTR(row[4]);
- vtBrand.push_back(brand);
- }
- // 释放内存;
- mysql_free_result(pData);
- return TRUE;
- }
- BOOL CDatabase::ImportBrandLogo(std::string brand, std::string file)
- {
- if (!PathFileExists(file.c_str()))
- {
- _dprintf(_T("%s:logo不存在[%s]"), brand.c_str(), file.c_str());
- return FALSE;
- }
- // 读出图片;
- CFile cfile;
- if (!cfile.Open(file.c_str(), CFile::modeRead, NULL))
- return FALSE;
- ULONGLONG ulLength = cfile.GetLength();
- char* pData = new char[ulLength];
- cfile.Read(pData, ulLength);
- cfile.Close();
- TCHAR szSql[MAX_PATH] = {0};
- _stprintf_s(szSql, _T("update brand set logo = ? where `name` = '%s'"), brand.c_str());
- BOOL bResult = SetBinaryField(szSql, pData, ulLength);
- if (pData)
- delete[]pData;
- pData = NULL;
- return bResult;
- }
- BOOL CDatabase::ExportBrandLogo(std::string brand, std::string file)
- {
- char* pData = NULL;
- unsigned long ulLength = 0;
- TCHAR szSql[MAX_PATH] = {0};
- _stprintf_s(szSql, _T("select logo from brand where `name`='%s';"), brand.c_str());
- BOOL bResult = GetBinaryField(szSql, (void**)&pData, ulLength);
- if (bResult && pData)
- {
- CFile cfile;
- if (cfile.Open(file.c_str(), CFile::modeCreate | CFile::modeWrite))
- {
- cfile.Write(pData, ulLength);
- cfile.Close();
- }
- delete[]pData;
- pData = NULL;
- }
- return bResult;
- }
- BOOL CDatabase::InsertQuarter(const STQuarter& quarter)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_QUARTER, quarter.name.c_str(), quarter.scp.c_str(), quarter.note.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertQuarter error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::InsertQuarter(std::string name, std::string scp, std::string note)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_QUARTER, name.c_str(), scp.c_str(), note.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertQuarter error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::DeleteQuarter(std::string name)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, DEL_QUARTER, name.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("DeleteQuarter error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::UpdateQuarter(std::string name, const STQuarter& quarter)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, MOD_QUARTER,quarter.name.c_str(), quarter.scp.c_str(), quarter.note.c_str(), name.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("UpdateQuarter error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::QueryQuarter(std::vector<STQuarter>& vtQuarter)
- {
- CHECKDB;
- if (0 != mysql_query(m_pConn, QUERY_QUARTER)) {
- #ifdef _DEBUG
- _dprintf(_T("QueryQuarter error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- // 获取表数据;
- MYSQL_RES* pData = mysql_store_result(m_pConn);
- if (pData == NULL) {
- #ifdef _DEBUG
- _dprintf(_T("QueryQuarter error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- #ifdef _DEBUG
- // 统计表字段;
- unsigned int nLen = mysql_num_fields(pData);
- // 字段长度是否一致;
- if (nLen != 3) {
- mysql_free_result(pData);
- return FALSE;
- }
- // 打印出字段名称;
- TCHAR szLog[MAX_PATH] = { 0 };
- for (int i = 0; i < nLen; i++) {
- _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
- OutputDebugString(szLog);
- }
- #endif
- // 遍历数据;
- MYSQL_ROW row;
- while ((row = mysql_fetch_row(pData)) != NULL) {
- STQuarter quarter;
- quarter.name = SAFESTR(row[0]);
- quarter.scp = SAFESTR(row[1]);
- quarter.note = SAFESTR(row[2]);
- vtQuarter.push_back(quarter);
- }
- // 释放内存;
- mysql_free_result(pData);
- return TRUE;
- }
- BOOL CDatabase::InsertQBrand(const STQbrand& QBrand)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_QBRAND, QBrand.soc.c_str(), QBrand.brand.c_str(), QBrand.quarter.c_str(),
- QBrand.whitelist.c_str(), QBrand.version.c_str(),
- QBrand.oemkey.c_str(), QBrand.approved.c_str(),
- QBrand.approveddate.c_str(), QBrand.fingerprint.c_str(),
- QBrand.builddate.c_str(), QBrand.gtvs.c_str(),
- QBrand.ftpdir.c_str(), QBrand.codedir.c_str(),
- QBrand.note.c_str(), QBrand.status.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertQBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::DeleteQBrand(std::string soc, std::string quarter, std::string brand)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, DEL_QBRAND, soc.c_str(), brand.c_str(), quarter.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("DeleteQBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::UpdateQBrand(std::string brand, STQbrand& QBrand)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, MOD_QBRAND, QBrand.soc.c_str(), QBrand.quarter.c_str(),
- QBrand.brand.c_str(), QBrand.whitelist.c_str(),
- QBrand.version.c_str(), QBrand.approved.c_str(),
- QBrand.oemkey.c_str(), QBrand.approveddate.c_str(),
- QBrand.fingerprint.c_str(), QBrand.builddate.c_str(),
- QBrand.gtvs.c_str(), QBrand.note.c_str(),
- QBrand.apps_cfg.c_str(), QBrand.fav_cfg.c_str(),
- QBrand.local_cfg.c_str(), QBrand.ftpdir.c_str(),
- QBrand.codedir.c_str(), QBrand.status.c_str(),
- brand.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("UpdateQBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::UpdateQBrand(std::string soc, std::string quarter, std::string brand, STQbrand& QBrand)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, MOD_QBRAND,
- QBrand.soc.c_str(), QBrand.quarter.c_str(), QBrand.brand.c_str(),
- QBrand.whitelist.c_str(), QBrand.version.c_str(), QBrand.approved.c_str(),
- QBrand.oemkey.c_str(), QBrand.approveddate.c_str(), QBrand.fingerprint.c_str(),
- QBrand.builddate.c_str(), QBrand.gtvs.c_str(), QBrand.note.c_str(),
- QBrand.apps_cfg.c_str(), QBrand.fav_cfg.c_str(), QBrand.local_cfg.c_str(),
- QBrand.ftpdir.c_str(), QBrand.codedir.c_str(), QBrand.status.c_str(),
- brand.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("UpdateQBrand error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::QueryQBrand(std::vector<STQbrand>& vtQBrand)
- {
- CHECKDB;
- if (0 != mysql_query(m_pConn, QUERY_QBRAND)) {
- #ifdef _DEBUG
- _dprintf(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- // 获取表数据;
- MYSQL_RES* pData = mysql_store_result(m_pConn);
- if (pData == NULL) {
- #ifdef _DEBUG
- _dprintf(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- #ifdef _DEBUG
- // 统计表字段;
- unsigned int nLen = mysql_num_fields(pData);
- // 字段长度是否一致;
- if (nLen != 19) {
- mysql_free_result(pData);
- return FALSE;
- }
- // 打印出字段名称;
- TCHAR szLog[MAX_PATH] = { 0 };
- for (int i = 0; i < nLen; i++) {
- _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
- OutputDebugString(szLog);
- }
- #endif
- // 遍历数据;
- MYSQL_ROW row;
- while ((row = mysql_fetch_row(pData)) != NULL) {
- STQbrand QBrand;
- QBrand.soc = SAFESTR(row[0]);
- QBrand.quarter = SAFESTR(row[1]);
- QBrand.brand = SAFESTR(row[2]);
- QBrand.whitelist = SAFESTR(row[3]);
- QBrand.version = SAFESTR(row[4]);
- QBrand.oemkey = SAFESTR(row[5]);
- QBrand.approved = SAFESTR(row[6]);
- QBrand.approveddate = SAFESTR(row[7]);
- QBrand.fingerprint = SAFESTR(row[8]);
- QBrand.builddate = SAFESTR(row[9]);
- QBrand.gtvs = SAFESTR(row[10]);
- QBrand.note = SAFESTR(row[11]);
- QBrand.apps_cfg = SAFESTR(row[12]);
- QBrand.fav_cfg = SAFESTR(row[13]);
- QBrand.local_cfg = SAFESTR(row[14]);
- QBrand.ftpdir = SAFESTR(row[15]);
- QBrand.codedir = SAFESTR(row[16]);
- QBrand.status = SAFESTR(row[17]);
- vtQBrand.push_back(QBrand);
- }
- // 释放内存;
- mysql_free_result(pData);
- return TRUE;
- }
- BOOL CDatabase::ImportQBrandLogo(std::string file, std::string brand, std::string soc, std::string quarter)
- {
- if (!PathFileExists(file.c_str()))
- return FALSE;
- // 读出图片;
- CFile cfile;
- if (!cfile.Open(file.c_str(), CFile::modeRead, NULL))
- return FALSE;
- ULONGLONG ulLength = cfile.GetLength();
- char* pData = new char[ulLength];
- cfile.Read(pData, ulLength);
- cfile.Close();
- TCHAR szSql[MAX_PATH] = { 0 };
- _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());
- BOOL bResult = SetBinaryField(szSql, pData, ulLength);
- if (pData)
- delete[]pData;
- pData = NULL;
- return bResult;
- }
- BOOL CDatabase::ExportQBrandLogo(std::string file, std::string brand, std::string soc, std::string quarter)
- {
- char* pData = NULL;
- unsigned long ulLength = 0;
- TCHAR szSql[MAX_PATH] = { 0 };
- _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());
- BOOL bResult = GetBinaryField(szSql, (void**)&pData, ulLength);
- if (bResult && pData)
- {
- CFile cfile;
- if (cfile.Open(file.c_str(), CFile::modeCreate | CFile::modeWrite))
- {
- cfile.Write(pData, ulLength);
- cfile.Close();
- }
- delete[]pData;
- pData = NULL;
- }
- return bResult;
- }
- BOOL CDatabase::InsertUser(const STUser& user)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_USER, user.user.c_str(), user.password.c_str(), user.permission.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertUser error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::InsertUser(std::string user, std::string password, std::string permission)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_USER, user.c_str(), password.c_str(), permission.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertUser error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::DeleteUser(std::string user)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, DEL_USER, user.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("DeleteUser error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::UpdateUser(std::string user, const STUser& stUser)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, MOD_USER, stUser.user.c_str(), stUser.password.c_str(), stUser.permission.c_str(), user.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("UpdateUser error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::QueryUser(std::vector<STUser>& vtUser)
- {
- CHECKDB;
- if (0 != mysql_query(m_pConn, QUERY_USER)) {
- #ifdef _DEBUG
- _dprintf(_T("QueryUser error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- // 获取表数据;
- MYSQL_RES* pData = mysql_store_result(m_pConn);
- if (pData == NULL) {
- #ifdef _DEBUG
- _dprintf(_T("QueryUser error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- #ifdef _DEBUG
- // 统计表字段;
- unsigned int nLen = mysql_num_fields(pData);
- // 字段长度是否一致;
- if (nLen != 3) {
- mysql_free_result(pData);
- return FALSE;
- }
- // 打印出字段名称;
- TCHAR szLog[MAX_PATH] = { 0 };
- for (int i = 0; i < nLen; i++) {
- _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
- OutputDebugString(szLog);
- }
- #endif
- // 遍历数据;
- MYSQL_ROW row;
- while ((row = mysql_fetch_row(pData)) != NULL) {
- STUser stUser;
- stUser.user = SAFESTR(row[0]);
- stUser.password = SAFESTR(row[1]);
- stUser.permission = SAFESTR(row[2]);
- vtUser.push_back(stUser);
- }
- // 释放内存;
- mysql_free_result(pData);
- return TRUE;
- }
- BOOL CDatabase::InsertServer(const STServer& Server)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _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());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertServer error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::InsertServer(std::string name, std::string ip, std::string type, std::string user, std::string password, std::string note)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_SERVER, name.c_str(), ip.c_str(), type.c_str(), user.c_str(), password.c_str(), note.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertServer error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::DeleteServer(std::string name)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, DEL_SERVER, name.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("DeleteServer error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::UpdateServer(std::string name, const STServer& Server)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _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());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("UpdateServer error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::QueryServer(std::vector<STServer>& vtServer)
- {
- CHECKDB;
- if (0 != mysql_query(m_pConn, QUERY_SERVER)) {
- #ifdef _DEBUG
- _dprintf(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- // 获取表数据;
- MYSQL_RES* pData = mysql_store_result(m_pConn);
- if (pData == NULL) {
- #ifdef _DEBUG
- _dprintf(_T("QueryServer error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- #ifdef _DEBUG
- // 统计表字段;
- unsigned int nLen = mysql_num_fields(pData);
- // 字段长度是否一致;
- if (nLen != 6) {
- mysql_free_result(pData);
- return FALSE;
- }
- // 打印出字段名称;
- TCHAR szLog[MAX_PATH] = { 0 };
- for (int i = 0; i < nLen; i++) {
- _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
- OutputDebugString(szLog);
- }
- #endif
- // 遍历数据;
- MYSQL_ROW row;
- while ((row = mysql_fetch_row(pData)) != NULL) {
- STServer server;
- server.name = SAFESTR(row[0]);
- server.ip = SAFESTR(row[1]);
- server.type = SAFESTR(row[2]);
- server.user = SAFESTR(row[3]);
- server.password = SAFESTR(row[4]);
- server.note = SAFESTR(row[5]);
- vtServer.push_back(server);
- }
- // 释放内存;
- mysql_free_result(pData);
- return TRUE;
- }
- BOOL CDatabase::InsertBranch(const STBranch& branch)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, INSER_BRANCH, branch.name.c_str(), branch.quarter.c_str(),
- branch.address.c_str(), branch.provider.c_str(), branch.chip.c_str(),
- branch.type.c_str(), branch.status.c_str(), branch.note.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("InsertBranch error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::DeleteBranch(std::string branch)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, DEL_BRANCH, branch.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("DeleteBranch error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::UpdateBranch(std::string branch, const STBranch& stBranch)
- {
- CHECKDB;
- TCHAR szSql[MAX_PATH] = { 0 };
- _stprintf_s(szSql, MOD_BRANCH, stBranch.quarter.c_str(), stBranch.address.c_str(),
- stBranch.provider.c_str(), stBranch.chip.c_str(), stBranch.type.c_str(),
- stBranch.status.c_str(), stBranch.note.c_str(),branch.c_str());
- if (0 != mysql_query(m_pConn, szSql)) {
- #ifdef _DEBUG
- _dprintf(_T("UpdateBranch error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- return TRUE;
- }
- BOOL CDatabase::QueryBranch(std::vector<STBranch>& vtBranch)
- {
- CHECKDB;
- if (0 != mysql_query(m_pConn, QUERY_BRANCH)) {
- #ifdef _DEBUG
- _dprintf(_T("QueryBranch error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- // 获取表数据;
- MYSQL_RES* pData = mysql_store_result(m_pConn);
- if (pData == NULL) {
- #ifdef _DEBUG
- _dprintf(_T("QueryBranch error=%s\n"), m_pError ? m_pError : "");
- #endif
- return FALSE;
- }
- #ifdef _DEBUG
- // 统计表字段;
- unsigned int nLen = mysql_num_fields(pData);
- // 字段长度是否一致;
- if (nLen != 8) {
- mysql_free_result(pData);
- return FALSE;
- }
- // 打印出字段名称;
- TCHAR szLog[MAX_PATH] = { 0 };
- for (int i = 0; i < nLen; i++) {
- _stprintf_s(szLog, _T("字段名称:%s\n"), mysql_fetch_field(pData)->name);
- OutputDebugString(szLog);
- }
- #endif
- // 遍历数据;
- MYSQL_ROW row;
- while ((row = mysql_fetch_row(pData)) != NULL) {
- STBranch stBranch;
- stBranch.name = SAFESTR(row[0]);
- stBranch.quarter = SAFESTR(row[1]);
- stBranch.address = SAFESTR(row[2]);
- stBranch.provider = SAFESTR(row[3]);
- stBranch.chip = SAFESTR(row[4]);
- stBranch.type = SAFESTR(row[5]);
- stBranch.status = SAFESTR(row[6]);
- stBranch.note = SAFESTR(row[7]);
- vtBranch.push_back(stBranch);
- }
- // 释放内存;
- mysql_free_result(pData);
- return TRUE;
- }
|