-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconnectpool.h
312 lines (228 loc) · 5.78 KB
/
connectpool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#ifndef _CONNECTPOOL_H_
#define _CONNECTPOOL_H_
//#include "myosmutex.h"
#ifdef WIN32
#include <process.h>
#include <winsock2.h>
#else
#include <sys/errno.h>
#include <pthread.h>
#endif
#include <mysql.h> //文件位于 MySQL 提供的 C API 目录中
#include <mysqld_error.h>
#include <map>
#include <vector>
#include <stdexcept>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
class CSql_error : public std::runtime_error
{
private:
std::string m_err;
public:
explicit CSql_error();
explicit CSql_error(const std::string &whatarg);
explicit CSql_error(const std::string &whatarg, const std::string &err);
virtual ~CSql_error() throw ();
virtual const char * what() const throw ();
};
class CConnect
{
private:
//
protected:
CConnect(const CConnect &);
CConnect &operator=(const CConnect &);
//错误原因
std::string m_err;
public:
//构造对象
CConnect();
//析构对象
virtual ~CConnect();
//连接指定的数据库
virtual bool Connect(const std::string &host,
const std::string &user,
const std::string &password,
const std::string &dbname,
unsigned int port,
const std::string &unix_socket,
const std::string &character) = 0;
//得到连接的语法
virtual const std::string GetConnectSyntax() = 0;
//中断连接
virtual bool Disconnect() = 0;
//得到连接对象
virtual void * GetConnect() = 0;
//virtual MYSQL * GetConnect() = 0;
//错误原因
std::string What(){return m_err;}
};
class CMysqlConnect : public CConnect
{
private:
//一个连接
MYSQL *m_conn;
//MYSQL 对象不能 copy,屏蔽 拷贝构造
CMysqlConnect(const CMysqlConnect &rhs);
//赋值运算也被屏蔽
CMysqlConnect & operator=(const CMysqlConnect &rhs);
protected:
//
public:
//构造对象
CMysqlConnect();
//析构对象
virtual ~CMysqlConnect();
//连接指定的数据库
virtual bool Connect(const std::string &host,
const std::string &user,
const std::string &password,
const std::string &dbname,
unsigned int port,
const std::string &unix_socket,
const std::string &character);
//中断连接
virtual bool Disconnect();
//得到连接
void * GetConnect();
//得到连接的语法
virtual const std::string GetConnectSyntax();
};
class CDataStore
{
private:
//
protected:
//
public:
CDataStore();
virtual ~CDataStore();
virtual bool SetTransAction(CConnect * conn) = 0;
//执行数据定义语言(DDL)类语句
virtual bool Exec(const std::string &ddl) = 0;
//执行数据定义操作(DML)类语句
virtual bool Query(const std::string &dml) = 0;
//事务提交
virtual bool Commit() = 0;
//事务回滚
virtual bool RollBack() = 0;
//得到查询记录数
virtual unsigned long RowCount() = 0;
//错误原因
virtual const std::string What() = 0;
//得到指定行某个字段的字符串类型值
virtual const std::string GetItemString(
unsigned long row,
unsigned int index) = 0;
virtual const std::string GetItemString(
unsigned long row,
const std::string &fieldname) = 0;
//得到指定行某个字段的数值
virtual float GetItemFloat(unsigned long row,
const unsigned int index) = 0;
virtual float GetItemFloat(unsigned long row,
const std::string &fieldname) = 0;
//得到指定行某个字段的整数值
virtual long GetItemLong(unsigned long row,
const unsigned int index) = 0;
virtual long GetItemLong(unsigned long row,
const std::string &fieldname) = 0;
};
class CMysqlStore : public CDataStore
{
private:
//指向 mysql 的连接指针
MYSQL * m_connptr;
//指向 mysql 的查询数据集
MYSQL_RES *m_resultptr;
//操作影响的记录数
unsigned long m_row;
//错误原因
std::string m_err;
//新的自增的序列号
long m_increaseID;
//事务提交模式
bool m_autocommit;
//字段索引和字段类型的对应表
enum filedtype_t
{CHAR = 1,INT = 2,DATETIME = 3,DOUBLE = 4,DEC = 5,UNKNOWN = 6};
struct typeset_t
{
std::string name;
filedtype_t type;
unsigned int length;
//查询列表中的列位置
unsigned int index;
typeset_t();
};
//取得信息状态
bool m_getstatus;
//字段信息表
std::vector<typeset_t> m_fieldtype;
typedef std::vector<std::string> row_t;
std::vector<row_t> m_recordset;
int m_colcount;
//清除临时对象
void Clear();
//找到行
row_t * FindRow(unsigned long findrow);
//找到对应的列序号
unsigned int GetFieldIndex(const std::string &fieldname);
//设置自增序列号
void SetIncreaseID(long id);
const std::string GetItemValue(unsigned long row,
unsigned int index);
const std::string GetItemValue(unsigned long row,
const std::string &fieldname);
protected:
//
public:
CMysqlStore();
virtual ~CMysqlStore();
//设置连接对象
bool SetTransAction(CConnect * conn);
//得到当前执行状态
bool GetStatus();
//错误原因
virtual const std::string What();
//执行数据定义语言(DDL)类语句
virtual bool Exec(const std::string &ddl);
//执行数据操作语言(DML)类语句
virtual bool Query(const std::string &dml);
filedtype_t SetFieldType(enum_field_types fieldtype);
//得到新自增的序列号
long GetIncreaseID();
//事务提交
virtual bool Commit();
//事务回滚
virtual bool RollBack();
//得到查询记录数
virtual unsigned long RowCount();
//得到指定行某个字段的字符串类型值
virtual const std::string GetItemString(unsigned long row,
unsigned int index);
virtual const std::string GetItemString(unsigned long row,
const std::string &fieldname);
//得到指定行某个字段的数值
virtual float GetItemFloat(unsigned long row,
const unsigned int index);
virtual float GetItemFloat(unsigned long row,
const std::string &fieldname);
//得到指定行某个字段的整数值
virtual long GetItemLong(unsigned long row,
const unsigned int index);
virtual long GetItemLong(unsigned long row,
const std::string &fieldname);
MYSQL* GetMySqlConn()
{
return m_connptr;
}
int GetColCount()
{
return m_colcount;
}
};
#endif