You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.5 KiB
C++
100 lines
3.5 KiB
C++
/****************************************************************************
|
|
** File name : HTDatabase.h
|
|
** Description : define database api
|
|
** Create date : 2018.09.01
|
|
** Auther by : Liuyx
|
|
** Version info : V1.0.01
|
|
** Copyright (C) 2002-2018 xi'an huatek, Inc Co., Ltd
|
|
** Update record:
|
|
** DATE AUTHER DESC
|
|
** -------------------------------------------------------------------------
|
|
** 2018.09.01 Liuyx first build
|
|
****************************************************************************/
|
|
#ifndef __HT_DATABASE_H
|
|
#define __HT_DATABASE_H
|
|
|
|
#include "HTPublic.h"
|
|
#include "mysql.h"
|
|
|
|
#ifdef _WIN32
|
|
//#pragma comment(lib,"ociliba.lib")
|
|
#pragma comment(lib,"libmysql.lib")
|
|
#endif
|
|
|
|
typedef std::list<MYSQL *> CONNECTION_HANDLE_LIST;
|
|
typedef std::list<MYSQL *>::iterator ITER_CONNECTION_HANDLE_LIST;
|
|
|
|
#define CONNECTION_NUM 10 //同时打开的连接数
|
|
|
|
class CDBMySQL
|
|
{
|
|
public:
|
|
CDBMySQL();
|
|
CDBMySQL(const char *host, const char *user, const char *password, const char *db, unsigned int port=3306);
|
|
virtual ~CDBMySQL();
|
|
|
|
public:
|
|
|
|
bool dbConnectPool(); //连接数据库
|
|
void dbFreeConnectPool() ; //释放链接资源
|
|
|
|
bool dbAutoCommit(MYSQL *pMysql, bool bStart); // 开始事务处理
|
|
bool dbCommit(MYSQL *pMysql);
|
|
bool InsertRecordBitch(const char pszSql[][256], int count);
|
|
bool AddInsertRecord(MYSQL *pHandle, const char *szSql);
|
|
|
|
MYSQL_RES* SelectRecord(const char *szSql); //选择记录,返回结果集
|
|
bool SelectDB(const char *szDB); //选择数据库
|
|
bool UpdateRecord(const char *szSql); //更新记录
|
|
bool InsertRecord(const char *szSql); //插入记录
|
|
bool DelRecord(const char *szSql); //删除记录
|
|
|
|
bool IsEnd(MYSQL_RES *myquery); //是否最后
|
|
void SeekData(MYSQL_RES *myquery, int offset); //查找指定数据
|
|
void FreeRecord(MYSQL_RES *myquery); //释放结果集
|
|
unsigned int GetFieldNum(MYSQL_RES *myquery); //得到字段数
|
|
MYSQL_ROW GetRecord(MYSQL_RES *myquery); //得到结果(一个记录)
|
|
my_ulonglong GetRowNum(MYSQL_RES *myquery); //得到记录数
|
|
char* getLastError(MYSQL* pMySql); //输出错误信息
|
|
|
|
char* GetState(); //服务器状态
|
|
char* GetServerInfo(); //服务器信息
|
|
int GetProtocolInfo(); //协议信息
|
|
char* GetHostInfo(); //主机信息
|
|
char* GetClientInfo(); //客户机信息
|
|
char* GetFieldName(MYSQL_RES *myquery, int FieldNum); //字段名
|
|
|
|
bool LockTable(const char *TableName, const char *Priority); //对特定表加锁
|
|
bool UnlockTable(); //解锁
|
|
bool SetCharset();
|
|
//int CreateDB(char *db); //创建数据库,返回错误信息
|
|
//int DropDB(char *db); //删除数据库,返回错误信息
|
|
|
|
MYSQL* GetIdleMySql(); //提取一个空闲句柄供使用
|
|
void SetIdleMysql(MYSQL* pMySql); //从使用队列中释放一个使用完毕的句柄,插入到空闲队列
|
|
static CDBMySQL* Instance();
|
|
|
|
public:
|
|
//MYSQL m_mysql; //数据库连接句柄
|
|
MYSQL_ROW m_row; //记录集(单行)
|
|
MYSQL_FIELD *m_field; //字段信息(结构体)
|
|
|
|
//创建两个队列
|
|
CONNECTION_HANDLE_LIST m_lsBusyList; //正在使用的连接句柄
|
|
CONNECTION_HANDLE_LIST m_lsIdleList; //未使用的连接句柄
|
|
|
|
mutex m_csList;
|
|
|
|
private:
|
|
static CDBMySQL* p_dbHandle ;
|
|
|
|
char m_host[20]; //主机
|
|
char m_user[20]; //用户名
|
|
char m_password[20]; //密码
|
|
char m_db[20]; //数据库名
|
|
unsigned int m_port; //端口
|
|
unsigned int m_min; // 链接数
|
|
};
|
|
|
|
#endif // __HT_DATABASE_H
|