-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL.cpp
More file actions
187 lines (150 loc) · 3.05 KB
/
MySQL.cpp
File metadata and controls
187 lines (150 loc) · 3.05 KB
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
#define KEEP_CPPMYSQL_HELPER_MACROS
#include "MySQL.h"
#include "helpers.h"
namespace CppMySQL {
__cold
MySQL::~MySQL(void) noexcept
{
if (conn_)
Close();
}
__cold
int MySQL::Init(void) noexcept
{
conn_ = mysql_init(NULL);
if (!conn_) {
SetErr("Failed to perform mysql_init()");
return -ENOMEM;
}
return 0;
}
__cold
int MySQL::Connect(void) noexcept
{
MYSQL *ret;
ret = mysql_real_connect(conn_, host_, user_, passwd_, dbname_, port_,
NULL, 0);
if (unlikely(!ret)) {
SetMySQLErrFallback("Failed to perform mysql_real_connect()");
return -1;
}
assert(ret == conn_);
return 0;
}
__hot
const char *MySQL::Error(void) noexcept
{
if (err_[0])
return err_;
if (conn_) {
const char *ret = mysql_error(conn_);
if (ret && ret[0])
return ret;
}
return nullptr;
}
__hot
MySQLResult *MySQL::StoreResult(void) noexcept
{
MySQLResult *ret;
MYSQL_RES *res;
res = mysql_store_result(conn_);
if (unlikely(!res)) {
ClearErr();
return nullptr;
}
ret = new(std::nothrow) MySQLResult(this, res);
if (unlikely(!ret)) {
mysql_free_result(res);
SetErr("Cannot allocate memory for MySQLResult");
return nullptr;
}
return ret;
}
__hot
MYSQL_ROW MySQLResult::FetchRow(void) noexcept
{
MYSQL_ROW ret;
ret = mysql_fetch_row(res_);
if (unlikely(!ret)) {
mysql_->ClearErr();
if (unlikely(mysql_->Error()))
return nullptr;
return MYSQL_ROW_END_PTR;
}
return ret;
}
__hot
MySQLStmt *MySQL::Prepare(size_t nr_bind, const char *q, size_t qlen) noexcept
{
MYSQL_BIND *bind = nullptr;
MYSQL_STMT *stmt;
MySQLStmt *ret;
int tmp;
stmt = mysql_stmt_init(conn_);
if (unlikely(!stmt))
return nullptr;
tmp = mysql_stmt_prepare(stmt, q, qlen);
if (unlikely(tmp)) {
const char *err = mysql_stmt_error(stmt);
if (err && *err)
SetErr("%s", err);
goto err;
}
if (nr_bind > 0) {
bind = static_cast<MYSQL_BIND *>(calloc(nr_bind, sizeof(*bind)));
if (unlikely(!bind)) {
SetErr("Cannot allocate memory for MYSQL_BIND");
goto err;
}
}
ret = new(std::nothrow) MySQLStmt(this, stmt, bind);
if (unlikely(!ret)) {
SetErr("Cannot allocate memory for MySQLStmt");
goto err;
}
return ret;
err:
free(bind);
mysql_stmt_close(stmt);
return nullptr;
}
__hot
const char *MySQLStmt::Error(void) noexcept
{
if (err_[0])
return err_;
if (stmt_) {
const char *ret = mysql_stmt_error(stmt_);
if (ret && ret[0])
return ret;
}
return nullptr;
}
MySQLStmtResult *MySQLStmt::StoreResult(void) noexcept
{
MySQLStmtResult *ret = nullptr;
MYSQL_BIND *bind = nullptr;
MYSQL_RES *res;
size_t nfields;
res = mysql_stmt_result_metadata(stmt_);
if (unlikely(!res))
return nullptr;
nfields = mysql_num_fields(res);
bind = static_cast<MYSQL_BIND *>(calloc(nfields, sizeof(*bind)));
if (unlikely(!bind)) {
SetErr("Cannot allocate memory for MYSQL_BIND");
goto err;
}
ret = new(std::nothrow) MySQLStmtResult(stmt_, res, bind);
if (unlikely(!ret)) {
SetErr("Cannot allocate memory for MySQLStmtResult");
goto err;
}
return ret;
err:
free(bind);
mysql_free_result(res);
return nullptr;
}
} /* namespace CppMySQL */