/
OPS_Mysql.cpp
252 lines (216 loc) · 4.47 KB
/
OPS_Mysql.cpp
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
//===============================================
/**
* @file OPS_Mysql.cpp
*
* @author XuZhiXi
* Email 932834199@qq.com or 932834199@163.com
*
* Create datetime: 2012-10-17 08:19:07
* Last modified: 2012-10-25 23:22:00
*
* Description:
*/
//================================================
#include <string>
#include "ky_log.h"
#include "OPS_algorithm.h"
#include "OPS_Mysql.h"
#include "OPS_IDbResult.h"
namespace OPS
{
Mysql::Mysql(unsigned int timeout)
{
my_bool reconnect = 1;
this->db = mysql_init( NULL );
if ( this->db == NULL )
{
KY_LOG_ERROR("mysql#unable to allocate database connection state");
return;
}
this->setOption( MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout ); // 设置 timeout 秒连接超时
this->setOption( MYSQL_OPT_RECONNECT, (char *)&reconnect ); // 设置重连
//this->setOption( MYSQL_READ_DEFAULT_GROUP, "your_prog_name"); // MySQL 将读取my.cnf文件的[client]和[your_prog_name]部分配置
//if ( mysql_set_character_set(this->db, "UTF-8") != 0 ) // 设置字符集
//{
// this->markLastError();
//}
}
Mysql::~Mysql()
{
// 清除结果集
this->dbResult.clearResult();
if ( this->db != NULL )
{
// 关闭数据库连接
mysql_close( this->db );
}
}
bool Mysql::setOption(enum mysql_option option, const char *arg)
{
if ( mysql_options( this->db, option, arg) == 0 )
{
return true;
}
else
{
this->markLastError();
return false;
}
}
bool Mysql::connect(string dbname, string host, unsigned int port, string user, string pwd)
{
if ( mysql_real_connect( this->db, host.c_str(), user.c_str(), pwd.c_str(), dbname.c_str(), port, 0, 0 ) != NULL )
{
this->setDbName( dbname );
return true;
}
else
{
this->markLastError();
return false;
}
}
bool Mysql::execute(const char *sql)
{
if ( IDatabase::getIsShowSql() )
{
this->markSql( sql );
}
if ( mysql_real_query( this->db , sql, strlen(sql) ) == 0 )
{
return true;
}
else
{
this->markLastError();
return false;
}
}
unsigned long Mysql::executeId(const char *sql)
{
if ( this->execute(sql) )
{
return mysql_insert_id( this->db );
}
else
{
return 0;
}
}
bool Mysql::select(const char *sql)
{
if ( this->execute(sql) == false )
{
return false;
}
this->dbResult.clearResult();
return this->saveSelectResult( &(this->dbResult) );
}
IDbResult *Mysql::selectResult(const char *sql)
{
if ( this->execute(sql) == false )
{
return NULL;
}
MysqlResult *dbRes = new MysqlResult();
if ( this->saveSelectResult( dbRes ) == true )
{
return dbRes;
}
else
{
delete dbRes;
return NULL;
}
}
bool Mysql::next()
{
return this->dbResult.next();
}
int Mysql::getInt(const char *fieldName)
{
return this->dbResult.getInt( fieldName );
}
long Mysql::getLong(const char *fieldName)
{
return this->dbResult.getLong( fieldName );
}
string Mysql::getString(const char *fieldName)
{
return this->dbResult.getString( fieldName );
}
unsigned long Mysql::getAffectedRows()
{
return mysql_affected_rows( this->db );
}
bool Mysql::beginTransaction()
{
// mysql_autocommit第二个参数,为0表示不自动提交,为1表示自动提交
if ( mysql_autocommit(this->db, 0) != 0 )
{
KY_LOG_ERROR("beginTransaction set mysql autocommit=0 fail");
return false;
}
return true;
}
bool Mysql::commit()
{
my_bool result;
result = mysql_commit(this->db);
this->endTransaction();
if ( result != 0 )
{
KY_LOG_ERROR("mysql commit transaction fail");
return false;
}
return true;
}
bool Mysql::rollback()
{
my_bool result;
result = mysql_rollback(this->db);
this->endTransaction();
if ( result != 0 )
{
KY_LOG_ERROR("mysql rollback transaction fail");
return false;
}
return true;
}
// protected
bool Mysql::endTransaction()
{
// mysql_autocommit第二个参数,为0表示不自动提交,为1表示自动提交
if ( mysql_autocommit(this->db, 1) != 0 )
{
KY_LOG_ERROR("endTransaction set mysql autocommit=1 fail");
return false;
}
return true;
}
bool Mysql::saveSelectResult(MysqlResult *result)
{
unsigned int i;
unsigned int fieldCount;
MYSQL_FIELD *field;
// 保存结果集
result->res = mysql_store_result( this->db );
if ( result->res == NULL )
{
this->markLastError();
return false;
}
// 建立索引
fieldCount = mysql_num_fields( result->res );
field = mysql_fetch_fields( result->res );
for (i=0; i<fieldCount; i++)
{
result->fieldMap[ field[i].name ] = i;
}
return true;
}
void Mysql::markLastError()
{
KY_LOG_ERROR("mysql#errno: %d errstr: %s", mysql_errno(this->db), mysql_error(this->db));
}
}