|
@@ -139,6 +139,7 @@ BOOL CDatabase::ExcuteSQL(std::string sql)
|
|
|
return TRUE;
|
|
|
}
|
|
|
|
|
|
+#if DEPRECATED
|
|
|
BOOL CDatabase::InserProvider(const STProvider& provider)
|
|
|
{
|
|
|
CHECKDB;
|
|
@@ -249,6 +250,7 @@ BOOL CDatabase::QueryProvider(std::vector<STProvider>& vtProvider)
|
|
|
|
|
|
return TRUE;
|
|
|
}
|
|
|
+#endif
|
|
|
|
|
|
BOOL CDatabase::InserSoc(const STSOC& soc)
|
|
|
{
|
|
@@ -269,10 +271,16 @@ BOOL CDatabase::InserSoc(const STSOC& soc)
|
|
|
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;
|
|
|
+
|
|
|
//预处理语句
|
|
|
- mysql_stmt_prepare(stmt, szQuery, strlen(szQuery));
|
|
|
+ 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();
|
|
@@ -290,17 +298,22 @@ BOOL CDatabase::InserSoc(const STSOC& soc)
|
|
|
bind[2].is_null = 0;
|
|
|
|
|
|
//绑定参数123
|
|
|
- mysql_stmt_bind_param(stmt, bind);
|
|
|
+ if (mysql_stmt_bind_param(stmt, bind))
|
|
|
+ goto over;
|
|
|
+
|
|
|
//执行预处理mysql语句
|
|
|
- if (0 != mysql_stmt_execute(stmt))
|
|
|
- {
|
|
|
+ if (!mysql_stmt_execute(stmt))
|
|
|
+ bResult = TRUE;
|
|
|
+
|
|
|
+over:
|
|
|
+ mysql_stmt_close(stmt);
|
|
|
+
|
|
|
#ifdef _DEBUG
|
|
|
+ if ( !bResult )
|
|
|
TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
|
|
|
#endif
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
|
|
|
- return TRUE;
|
|
|
+ return bResult;
|
|
|
#endif
|
|
|
}
|
|
|
|
|
@@ -325,18 +338,19 @@ BOOL CDatabase::InserSoc(std::string name, std::string provider, std::string not
|
|
|
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)))
|
|
|
- return FALSE;
|
|
|
+ goto over;
|
|
|
|
|
|
// 字段数量是否匹配;
|
|
|
int param_count = mysql_stmt_param_count(stmt);
|
|
|
if (param_count != 3)
|
|
|
- return FALSE;
|
|
|
+ goto over;
|
|
|
|
|
|
//初始化参数
|
|
|
memset(bind, 0, sizeof(bind));
|
|
@@ -356,33 +370,27 @@ BOOL CDatabase::InserSoc(std::string name, std::string provider, std::string not
|
|
|
bind[2].is_null = 0;
|
|
|
|
|
|
//绑定参数123
|
|
|
- if (0 != mysql_stmt_bind_param(stmt, bind))
|
|
|
- {
|
|
|
-#ifdef _DEBUG
|
|
|
- TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
|
|
|
-#endif
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
+ if (mysql_stmt_bind_param(stmt, bind))
|
|
|
+ goto over;
|
|
|
|
|
|
//执行预处理mysql语句
|
|
|
- if (0 != mysql_stmt_execute(stmt))
|
|
|
- {
|
|
|
-#ifdef _DEBUG
|
|
|
- TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
|
|
|
-#endif
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
+ if (mysql_stmt_execute(stmt))
|
|
|
+ goto over;
|
|
|
|
|
|
// 影响的行数;
|
|
|
int affected_rows = mysql_stmt_affected_rows(stmt);
|
|
|
- if (affected_rows != 1)
|
|
|
- return FALSE;
|
|
|
+ if (affected_rows == 1)
|
|
|
+ bResult = TRUE;
|
|
|
|
|
|
- // Close the statement;
|
|
|
- if (mysql_stmt_close(stmt))
|
|
|
- return FALSE;
|
|
|
+over:
|
|
|
+ mysql_stmt_close(stmt);
|
|
|
|
|
|
- return TRUE;
|
|
|
+#ifdef _DEBUG
|
|
|
+ if (!bResult)
|
|
|
+ TRACE1(_T("InserSoc error=%s\n"), m_pError ? m_pError : "");
|
|
|
+#endif
|
|
|
+
|
|
|
+ return bResult;
|
|
|
#endif
|
|
|
}
|
|
|
|
|
@@ -821,6 +829,118 @@ BOOL CDatabase::QueryServer(std::vector<STServer>& vtServer)
|
|
|
return TRUE;
|
|
|
}
|
|
|
|
|
|
+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
|
|
|
+ TRACE1(_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
|
|
|
+ TRACE1(_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
|
|
|
+ TRACE1(_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
|
|
|
+ TRACE1(_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
|
|
|
+ TRACE1(_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
|
|
|
+ TRACE1(_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 = row[0];
|
|
|
+ stUser.password = row[1];
|
|
|
+ stUser.permission = row[2];
|
|
|
+ vtUser.push_back(stUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 释放内存;
|
|
|
+ mysql_free_result(pData);
|
|
|
+
|
|
|
+ return TRUE;
|
|
|
+}
|
|
|
+
|
|
|
// 示例;
|
|
|
void test(MYSQL* mysql)
|
|
|
{
|