#ifndef CCDATABASEAIDE_H_
#define CCDATABASEAIDE_H_
#include "CSYSType.h"
#include <string>
#include <mysql.h>
#include "CGFConfig.h"
#include "CMutex.h"
#define DB_NET_ERROR (1)
#define DB_USER_ERROR (2)
class GFET_EXPORTS CCDBResult
{
private:
MYSQL_RES* m_pRes; //查询结果数据指针
MYSQL_ROW m_sqlRow;//mysql_fetch_row(m_pRes);1行数据结果
MYSQL* m_pMysql; //连接mysql的句柄
DT_DWORD m_rowNum; //字段数
public:
CCDBResult();
void Init(MYSQL* pMySql);
void GetValue(DT_WORD index, std::string& value);
void GetValue(DT_WORD index, DT_WORD& value);//取m_sqlRow[index] 1行数据里的某个字段的数据转换成相映的类型
void GetValue(DT_WORD index, DT_DWORD& value);
void GetValue(DT_WORD index, DT_SHORT& value);
void GetValue(DT_WORD index, DT_CHAR& value);
void GetValue(DT_WORD index, DT_UCHAR& value);
void GetValue(DT_WORD index, DT_INT32& value);
void GetValue(DT_WORD index, DT_FLOAT& value);
void GetValue(DT_WORD index, DT_DOUBLE& value);
void GetValue(DT_WORD index, DT_INT64& value);
void GetValue(DT_WORD index, DT_CHAR* value, DT_DWORD bufLen);
void GetValue(DT_WORD index, DT_BOOL& value);
int GetResult(); //mysql_store_result(m_pMysql); 返回查询结果数据,mysql_num_fields(m_pRes);返回结果字段数
int GetFiledNum();//mysql_num_fields(m_pRes);返回结果字段数
bool MoveNextRow();//mysql_fetch_row(m_pRes);取1行数据结果
//如果存在多个查询结果,mysql_next_result()将读取下一个查询结果,并将状态返回给应用程序。如果前面的查询返回了结果集,必须为其调用mysql_free_result()。
void Release();//mysql_free_result(m_pRes);释放资源,
};
class GFET_EXPORTS CCDataBaseAide
{
class CCDBProcParm
{
public:
std::string m_command; //存储过程命令
void SetProcName(const std::string& spName);//组合存储过程命令为 call spName(
void Reset(); //制空命令
void AddParm(const std::string& parmName, const std::string& parmValue );
void AddParm(const std::string& parmName, DT_DWORD parmValue);
void AddParm(const std::string& parmName, DT_CHAR parmValue);//组合存储过程命令为 call spName( 1234,
void AddParm(const std::string& parmName, DT_INT32 parmValue);
void AddParm(const std::string& parmName, DT_WORD parmValue);
void AddParm(const std::string& parmName, DT_SHORT parmValue);
void AddParm(const std::string& parmName, DT_UCHAR parmValue);
void AddParm(const std::string& parmName, DT_FLOAT parmValue);
void AddParm(const std::string& parmName, DT_DOUBLE parmValue);
void AddParm(const std::string& parmName, DT_CHAR* parmValue);//组合存储过程命令为 call spName( 1234,'abc',
void AddParm(const std::string& parmName, DT_BOOL& parmValue);
void AddParm(const std::string& parmName, DT_INT64& parmValue);
void AddRawParm(const std::string& parmName, const std::string& parmValue);//传什么进来就设置成什么字符
void ExecuteProc(); //组合存储过程命令为 call spName( 1234,'abc' )用空格替换最后一个逗号,添加) 完成调用存储过程语句
};
private:
MYSQL m_mySql; //mysql对象句柄,所有的命令由他去执行
DT_INT32 m_connected; //是否连接
CCDBProcParm m_procParm; //执行存储过程对象
CCDBResult m_result; //结果集
CCMutex m_mutex; //互斥对象
public:
bool Init(); //调用mysql提供的接口初始化m_mySql句柄mysql_init,mysql_options设置使用的字符集,mysql_close销毁mysql句柄,
bool Connect(const std::string& host, const std::string& user, const std::string& key,
const std::string& dbName, DT_DWORD port);//mysql_real_connect连接mysql,mysql_set_character_set设置字符集
void SetProcName(const std::string& spName);
void Reset();
void AddParm(const std::string& parmName, const std::string& parmValue );
void AddParm(const std::string& parmName, DT_DWORD parmValue);
void AddParm(const std::string& parmName, DT_CHAR parmValue);
void AddParm(const std::string& parmName, DT_INT32 parmValue);
void AddParm(const std::string& parmName, DT_WORD parmValue);
void AddParm(const std::string& parmName, DT_SHORT parmValue);
void AddParm(const std::string& parmName, DT_UCHAR parmValue);
void AddParm(const std::string& parmName, DT_FLOAT parmValue);
void AddParm(const std::string& parmName, DT_DOUBLE parmValue);
void AddParm(const std::string& parmName, DT_INT64& value);
void AddParm(const std::string& parmName, DT_CHAR* parmValue);
void AddParm(const std::string& parmName, DT_BOOL& parmValue);
void AddRawParm(const std::string& parmName, const std::string& parmValue);//传什么进来就设置成什么字符
public:
bool ExecuteProc();//mysql_real_query 执行mysql语句
CCDBResult* GetResult() {return &m_result;} //返回结果集对象
DT_DWORD GetErrno(); //mysql_errno(&m_mySql);返回错误代码,参考MySql错误大全
public:
CCDataBaseAide(void);
~CCDataBaseAide(void);
};
#endif
服务端框架之mysql分析
最新推荐文章于 2024-02-21 20:42:18 发布