99 lines
3.3 KiB
C
99 lines
3.3 KiB
C
|
/*************************************************************************
|
|||
|
> File Name : CMysqlPool.h
|
|||
|
> Author : liuzhao
|
|||
|
> EMail : liuzhao@comleader.com.cn
|
|||
|
> Created Time : Fri 17 Jul 2020 09:40:09 AM CST
|
|||
|
************************************************************************/
|
|||
|
|
|||
|
#ifndef MYSQLPOOL_H
|
|||
|
#define MYSQLPOOL_H
|
|||
|
|
|||
|
#include<iostream>
|
|||
|
#include<mysql/mysql.h>
|
|||
|
#include<queue>
|
|||
|
#include<map>
|
|||
|
#include<vector>
|
|||
|
#include<utility>
|
|||
|
#include<string>
|
|||
|
#include<mutex>
|
|||
|
#include<thread>
|
|||
|
|
|||
|
#include "CJudgeUtils.h"
|
|||
|
|
|||
|
struct MysqlResults
|
|||
|
{
|
|||
|
int field_count;
|
|||
|
int record_count;
|
|||
|
std::vector<std::vector<std::string>> value;
|
|||
|
std::vector<std::string> field_name;
|
|||
|
|
|||
|
MysqlResults() : field_count(0), record_count(0) {}
|
|||
|
void Print()
|
|||
|
{
|
|||
|
std::cout << "[field_count:" << field_count << "]" << std::endl;
|
|||
|
std::cout << "[record_count:" << record_count << "]" << std::endl;
|
|||
|
int i = 0;
|
|||
|
std::cout << "[field]" << "\t";
|
|||
|
for (auto v : field_name)
|
|||
|
{
|
|||
|
std::cout << v << "\t";
|
|||
|
}
|
|||
|
std::cout << std::endl;
|
|||
|
|
|||
|
for (auto v : value)
|
|||
|
{
|
|||
|
int j = 0;
|
|||
|
std::cout << "[row:" << i << "]" << "\t";
|
|||
|
for (auto x : v)
|
|||
|
{
|
|||
|
std::cout << x << "\t";
|
|||
|
j++;
|
|||
|
}
|
|||
|
std::cout << std::endl;
|
|||
|
i ++;
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
class MysqlPool : public CSingleton<MysqlPool>
|
|||
|
{
|
|||
|
public:
|
|||
|
MysqlPool();
|
|||
|
virtual ~MysqlPool();
|
|||
|
|
|||
|
public:
|
|||
|
void executeSql(MysqlResults& results, const char* sql);//sql语句的执行函数
|
|||
|
void setParameter( const char* _mysqlhost,
|
|||
|
const char* _mysqluser,
|
|||
|
const char* _mysqlpwd,
|
|||
|
const char* _databasename,
|
|||
|
unsigned int _port = 0,
|
|||
|
const char* _socket = NULL,
|
|||
|
unsigned long _client_flag = 0,
|
|||
|
unsigned int MAX_CONNECT = 50 ); //设置数据库参数
|
|||
|
private:
|
|||
|
MYSQL* createOneConnect(); //创建一个新的连接对象
|
|||
|
MYSQL* getOneConnect(); //获取一个连接对象
|
|||
|
void close(MYSQL* conn); //关闭连接对象
|
|||
|
bool isEmpty(); //连接池队列池是否为空
|
|||
|
MYSQL* poolFront(); //连接池队列的队头
|
|||
|
unsigned int poolSize(); //获取连接池的大小
|
|||
|
void poolPop(); //弹出连接池队列的队头
|
|||
|
private:
|
|||
|
std::queue<MYSQL*> mysqlpool; //连接池队列
|
|||
|
const char* _mysqlhost; //mysql主机地址
|
|||
|
const char* _mysqluser; //mysql用户名
|
|||
|
const char* _mysqlpwd; //mysql密码
|
|||
|
const char* _databasename; //要使用的mysql数据库名字
|
|||
|
unsigned int _port; //mysql端口
|
|||
|
const char* _socket; //可以设置成Socket or Pipeline,通常设置为NULL
|
|||
|
unsigned long _client_flag; //设置为0
|
|||
|
unsigned int MAX_CONNECT; //同时允许最大连接对象数量
|
|||
|
unsigned int connect_count; //目前连接池的连接对象数量
|
|||
|
static std::mutex objectlock; //对象锁
|
|||
|
static std::mutex poollock; //连接池锁
|
|||
|
static MysqlPool* mysqlpool_object; //类的对象
|
|||
|
};
|
|||
|
|
|||
|
#endif
|