|
@@ -0,0 +1,278 @@
|
|
|
+#include "pch.h"
|
|
|
+#include "Database.h"
|
|
|
+
|
|
|
+#define CHECKDB if (m_pConn == NULL || !m_bConnected) return FALSE
|
|
|
+
|
|
|
+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::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
|
|
|
+ TRACE1(_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
|
|
|
+ TRACE1(_T("ExcuteSQL error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+BOOL CDatabase::InserProvider(const STProvider& provider)
|
|
|
+{
|
|
|
+ CHECKDB;
|
|
|
+ TCHAR szSql[MAX_PATH] = {0};
|
|
|
+ _stprintf_s(szSql, INSER_PROVIDER, provider.name.c_str(), provider.note.c_str());
|
|
|
+ if (0 != mysql_query(m_pConn, szSql)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("InserProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+BOOL CDatabase::InserProvider(std::string name, std::string note)
|
|
|
+{
|
|
|
+ CHECKDB;
|
|
|
+ TCHAR szSql[MAX_PATH] = { 0 };
|
|
|
+ _stprintf_s(szSql, INSER_PROVIDER, name.c_str(), note.c_str());
|
|
|
+ if (0 != mysql_query(m_pConn, szSql)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("InserProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+BOOL CDatabase::DeleteProvider(std::string name)
|
|
|
+{
|
|
|
+ CHECKDB;
|
|
|
+ TCHAR szSql[MAX_PATH] = { 0 };
|
|
|
+ _stprintf_s(szSql, DEL_PROVIDER, name.c_str());
|
|
|
+ if (0 != mysql_query(m_pConn, szSql)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("DeleteProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+BOOL CDatabase::UpdateProvider(std::string name, const STProvider& provider)
|
|
|
+{
|
|
|
+ CHECKDB;
|
|
|
+ TCHAR szSql[MAX_PATH] = { 0 };
|
|
|
+ _stprintf_s(szSql, MOD_PROVIDER, provider.name.c_str(), provider.note.c_str(), name.c_str());
|
|
|
+ if (0 != mysql_query(m_pConn, szSql)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("DeleteProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+BOOL CDatabase::QueryProvider(std::vector<STProvider>& vtProvider)
|
|
|
+{
|
|
|
+ CHECKDB;
|
|
|
+ if (0 != mysql_query(m_pConn, QUERY_PROVIDER)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("QueryProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取表数据;
|
|
|
+ MYSQL_RES* pData = mysql_store_result(m_pConn);
|
|
|
+ if ( pData == NULL ) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("QueryProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+#ifdef _DEBUG
|
|
|
+ // 统计表字段;
|
|
|
+ unsigned int nLen = mysql_num_fields(pData);
|
|
|
+ // 字段长度是否一致;
|
|
|
+ if (nLen != 2) {
|
|
|
+ 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 ) {
|
|
|
+ STProvider provider;
|
|
|
+ provider.name = row[0];
|
|
|
+ provider.note = row[1];
|
|
|
+ vtProvider.push_back(provider);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 释放内存;
|
|
|
+ mysql_free_result(pData);
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+BOOL CDatabase::InsertBrand(const STBranch& brand)
|
|
|
+{
|
|
|
+ CHECKDB;
|
|
|
+ TCHAR szSql[MAX_PATH] = { 0 };
|
|
|
+ _stprintf_s(szSql, INSER_BRAND, brand.name.c_str(), brand.note.c_str());
|
|
|
+ if (0 != mysql_query(m_pConn, szSql)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("InserProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+BOOL CDatabase::InsertBrand(std::string name, std::string note)
|
|
|
+{
|
|
|
+ CHECKDB;
|
|
|
+ TCHAR szSql[MAX_PATH] = { 0 };
|
|
|
+ _stprintf_s(szSql, INSER_BRAND, name.c_str(), note.c_str());
|
|
|
+ if (0 != mysql_query(m_pConn, szSql)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("InserProvider 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
|
|
|
+ TRACE1(_T("DeleteProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+BOOL CDatabase::UpdateBrand(std::string name, const STBranch& brand)
|
|
|
+{
|
|
|
+ CHECKDB;
|
|
|
+ TCHAR szSql[MAX_PATH] = { 0 };
|
|
|
+ _stprintf_s(szSql, MOD_BRAND, name.c_str(), brand.name.c_str(), brand.note.c_str());
|
|
|
+ if (0 != mysql_query(m_pConn, szSql)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("DeleteProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+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
|
|
|
+ TRACE1(_T("InserProvider 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
|
|
|
+ TRACE1(_T("InserProvider 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
|
|
|
+ TRACE1(_T("DeleteProvider 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, name.c_str(), quarter.name.c_str(), quarter.scp.c_str(), quarter.note.c_str());
|
|
|
+ if (0 != mysql_query(m_pConn, szSql)) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ TRACE1(_T("DeleteProvider error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|