This repository was archived by the owner on Mar 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase_mysql.cpp
More file actions
459 lines (431 loc) · 14.9 KB
/
database_mysql.cpp
File metadata and controls
459 lines (431 loc) · 14.9 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* File: database_mysql.cpp
* Author: wuhao
*
* Created on 2015年11月19日, 上午10:41
*/
#include "core.h"
#include "database_mysql.h"
#include "database_mysql_where.h"
#include "php/parameter.h"
std::string database_mysql::class_name;
zend_class_entry* database_mysql::class_entry;
std::string database_mysql::MASTER = "master";
std::string database_mysql::SLAVE = "slave";
database_mysql::database_mysql() {
}
void database_mysql::__construct(const php::parameter& param) {
php::property prop(this);
prop.oset("_mysqli", param[0]);
}
database_mysql::~database_mysql() {
}
php::value database_mysql::init(const php::parameter& param) {
if(param.size < 1 || !param[0].is_type(IS_ARRAY)) {
zend_throw_error(NULL, "mysql config of type array is required");
return false;
}
php::value conf = param[0];
if(!conf.__isset("master") && !conf.__isset("slave")) {
zend_throw_error(NULL, "illegal mysql config #1");
return false;
}
php::property prop(database_mysql::class_entry);
prop.sset("_conf", conf);
return true;
}
php::value database_mysql::create(php::value& config) {
std::string host = config["host"],
port = config["port"],
user = config["user"],
pass = config["pass"],
db = config["db"];
if(host.empty() || port.empty() || user.empty()) {
zend_throw_error(NULL, "illegal mysql config #3");
return php::null;
}
std::string s;
s.append("new mysqli(\"")
.append(host)
.append("\",\"")
.append(user)
.append("\",\"")
.append(pass)
.append("\",\"");
if(db.empty()) {
s.append("\",");
}else{
s.append(db)
.append("\",");
}
s.append(port)
.append(")");
php::value mysqli, object;
zend_eval_string(const_cast<char*>(s.c_str()), mysqli.intern(), const_cast<char*>("yafa_database_mysql::create_mysqli"));
php::property prop(mysqli);
if(!prop.oget("connect_error").is_empty()) {
zend_throw_error(nullptr, "failed to create mysqli");
return php::null;
}
object_init_ex(object.intern(), database_mysql::class_entry);
php::_store * p_store = php::get_store(object);
php::parameter param(mysqli.intern(), 1);
p_store->self->__construct(param);
return std::move(object);
}
php::value database_mysql::get_master(const php::parameter& param) {
zend_long idx = 0;
std::string key = "master_";
if(param.size == 1) {
idx = param[0];
}
key.append(std::to_string(idx));
php::property prop(database_mysql::class_entry);
php::value cache = prop.sget("_cache");
php::value db = cache[key];
if(!db.is_type(IS_OBJECT)) {
php::value conf = prop.sget("_conf");
conf = conf["master"][idx];
if(!conf.is_type(IS_ARRAY)) {
zend_throw_error(NULL, "illegal mysql config #2");
return php::null;
}
db = database_mysql::create(conf);
if(db.is_type(IS_OBJECT)) {
cache[key] = db;
}
}
return db;
}
php::value database_mysql::get_slave(const php::parameter& param) {
zend_long idx = 0;
std::string key = "slave_";
if(param.size == 1) {
idx = param[0];
}
key.append(std::to_string(idx));
php::property prop(database_mysql::class_entry);
php::value cache = prop.sget("_cache");
php::value db = cache[key];
if(!db.is_type(IS_OBJECT)) {
php::value conf = prop.sget("_conf");
conf = conf["slave"][idx];
if(!conf.is_type(IS_ARRAY)) {
zend_throw_error(NULL, "illegal mysql config #2");
return php::null;
}
db = database_mysql::create(conf);
cache[key] = db;
}
return db;
}
php::value database_mysql::format(const php::parameter& param) {
if(param.size < 1 || !param[0].is_type(IS_STRING)) {
zend_throw_error(NULL, "format string is required");
return php::null;
}
if(param.size == 1) return param[0];
std::string before = param[0];
std::string after;
size_t i = 0, m = 1;
php::property prop(this);
php::value _mysqli = prop.oget("_mysqli");
while(i < before.length() && m <= param.size) {
if(before[i]!='?') {
after.push_back(before[i]);
}else{
if(m == param.size) {
zend_throw_error(nullptr, "format arguments is not enough");
return after;
}
if(param[m].is_type(IS_LONG)) {
after.append(std::to_string((zend_long)param[m]));
}else{
after.push_back('\'');
after.append((std::string)php::call_method_1(&_mysqli, "escape_string", param[m]));
after.push_back('\'');
}
++m;
}
++i;
}
if(m < param.size) {
zend_throw_error(nullptr, "too many format arguments");
return after;
}
return after;
}
php::value database_mysql::format_query(const php::parameter& param) {
php::value sql = format(param);
php::property prop(this);
php::value _mysqli = prop.oget("_mysqli");
last_query = (std::string)sql;
return php::call_method_1(&_mysqli, "query", sql);
}
php::value database_mysql::insert(const php::parameter& param) {
std::string table = param[0];
php::value data = param[1];
if(table.empty() || data.length() == 0) {
zend_throw_error(nullptr, "table name and data is required");
return php::null;
}
std::string sql = "INSERT INTO `" + table + "`(";
php::property prop(this);
php::value _mysqli = prop.oget("_mysqli");
if(data.__isset(0)) {
insert_keys(_mysqli, sql, data[0]);
sql.append(") VALUES");
size_t length = data.length();
for(auto i=data.begin(); i!=data.end(); ++i) {
insert_vals(_mysqli, sql, php::value(i->val, false));
if(--length != 0) {
sql.push_back(',');
}
}
}else{
insert_keys(_mysqli, sql, data);
sql.append(") VALUES");
insert_vals(_mysqli, sql, data);
}
last_query = sql;
return php::call_method_1(&_mysqli, "query", sql);
}
void database_mysql::insert_keys(php::value& _mysqli, std::string& sql, const php::value& item) {
size_t length = item.length();
for(auto i=item.begin(); i!=item.end(); ++i) {
sql.push_back('`');
sql.append(std::string(ZSTR_VAL(i->key), ZSTR_LEN(i->key)));
sql.push_back('`');
if(--length != 0) {
sql.push_back(',');
}
}
}
void database_mysql::insert_vals(php::value& _mysqli, std::string& sql, const php::value& item) {
size_t length = item.length();
sql.push_back('(');
for(auto i=item.begin(); i!=item.end(); ++i) {
sql.push_back('\'');
php::value val(i->val, false);
if(val.is_type(IS_ARRAY)) {
val = php::call_method_2(nullptr, "json_encode", php::value(i->val), php::value(1<<8)); // PHP_JSON_UNESCAPED_UNICODE
}
sql.append((std::string)php::call_method_1(&_mysqli, "escape_string", val));
sql.push_back('\'');
--length;
if(length != 0) {
sql.push_back(',');
}
}
sql.push_back(')');
}
php::value database_mysql::remove(const php::parameter& param) {
std::string table = param[0];
php::value data = param[1];
if(table.empty() || data.length() == 0) {
zend_throw_error(nullptr, "table name and conditions is required");
return php::null;
}
std::string sql = "DELETE FROM `"+table+"`";
php::property prop(this);
php::value _mysqli = prop.oget("_mysqli");
database_mysql_where where(_mysqli, sql);
where.build(data);
last_query = sql;
return php::call_method_1(&_mysqli, "query", sql);
}
php::value database_mysql::update(const php::parameter& param) {
std::string table = param[0];
php::value data = param[1];
if(table.empty() || !data.is_type(IS_ARRAY) || data.length() == 0) {
zend_throw_error(nullptr, "table name and data is required");
return php::null;
}
std::string sql = "UPDATE `" + table + "` SET ";
unsigned long len = data.length();
php::property prop(this);
php::value _mysqli = prop.oget("_mysqli");
for(auto i=data.begin(); i!=data.end(); ++i) {
sql.push_back('`');
sql.append(std::string(ZSTR_VAL(i->key), ZSTR_LEN(i->key)));
sql.push_back('`');
sql.push_back('=');
sql.push_back('\'');
php::value val = i->val;
if(val.is_type(IS_ARRAY)) {
val = php::call_method_2(nullptr, "json_encode", php::value(i->val), php::value(1<<8)); // PHP_JSON_UNESCAPED_UNICODE
}
sql.append((std::string)php::call_method_1(&_mysqli, "escape_string", val));
sql.push_back('\'');
if(--len != 0) {
sql.push_back(',');
}
}
if(param.size >= 3) {
php::value cond = param[2];
database_mysql_where where(_mysqli, sql);
where.build(cond);
}else{
zend_error(E_NOTICE, "database update without conditions");
}
last_query = sql;
return php::call_method_1(&_mysqli, "query", sql);
}
php::value database_mysql::select(const php::parameter& param) {
std::string table = param[0];
php::value column = param[1];
if(table.empty() || !column.is_type(IS_STRING) && !column.is_type(IS_ARRAY) || column.length() < 1) {
zend_throw_error(nullptr, "table name and columns is required");
return php::null;
}
// 以下参数可选
php::value cond = param[2];
php::value group = param[3];
php::value order = param[4];
php::value limit = param[5];
php::property prop(this);
php::value _mysqli = prop.oget("_mysqli");
std::string sql = "SELECT ";
if(column.is_type(IS_STRING)) {
sql.append((std::string)column);
}else if(column.is_type(IS_ARRAY)) {
int len = column.length();
for(auto i=column.begin(); i!=column.end(); ++i) {
sql.push_back('`');
sql.append((std::string)php::value(i->val));
sql.push_back('`');
if(--len !=0) {
sql.push_back(',');
}
}
}
sql.append(" FROM `");
sql.append(table);
sql.push_back('`');
if(!cond.is_empty()) {
database_mysql_where where(_mysqli, sql);
where.build(cond);
}
if(!group.is_empty()) {
sql.append(" GROUP BY ");
if(group.is_type(IS_STRING)) {
sql.append((std::string)group);
}else if(group.is_type(IS_ARRAY)) {
int len = group.length();
for(auto i=group.begin(); i!=group.end(); ++i) {
sql.push_back('`');
sql.append((std::string)php::value(i->val));
sql.push_back('`');
if(--len !=0) {
sql.push_back(',');
}
}
}
}
build_order(sql, order);
if(!limit.is_empty()) {
sql.append(" LIMIT ");
if(limit.is_type(IS_STRING)) {
sql.append((std::string)limit);
}else if(limit.is_type(IS_LONG)) {
sql.append(std::to_string((zend_long)limit));
}else if(limit.is_type(IS_ARRAY)) {
int len = limit.length();
for(auto i=limit.begin(); i!=limit.end(); ++i) {
zend_long ln = php::value(i->val, true); // copy 一份并作类型转换
sql.append(std::to_string(ln));
if(--len !=0) {
sql.push_back(',');
}
}
}
}
last_query = sql;
return php::call_method_1(&_mysqli, "query", sql);
}
void database_mysql::build_order(std::string& sql, php::value& order) {
if(order.is_empty()) {
return;
}
sql.append(" ORDER BY ");
if(order.is_type(IS_STRING)) {
sql.append((std::string)order);
return;
}else if(!order.is_type(IS_ARRAY)) {
zend_throw_error(nullptr, "order must be array with either bool or \"ASC\"/\"DESC\" value]");
return;
}
int len = order.length();
for(auto i=order.begin(); i!=order.end(); ++i) {
sql.push_back('`');
sql.append(std::string(ZSTR_VAL(i->key), ZSTR_LEN(i->key)));
sql.push_back('`');
php::value osc = php::value(i->val);
if(osc.is_empty()) { // bool 变量转换
sql.append(" DESC");
}else if(osc.is_type(IS_STRING)) {
sql.push_back(' ');
sql.append((std::string)osc);
}else{
sql.append(" ASC");
}
if(--len !=0) {
sql.push_back(',');
}
}
}
php::value database_mysql::one(const php::parameter& param) {
std::string table = param[0];
php::value cond = param[1];
php::value order = param[2];
if(table.empty()) {
zend_throw_error(nullptr, "table name is required");
return php::null;
}
std::string sql = "SELECT * FROM `"+table+"`";
php::property prop(this);
php::value _mysqli = prop.oget("_mysqli");
if(!cond.is_empty()) {
database_mysql_where where(_mysqli, sql);
where.build(cond);
}
build_order(sql, order);
sql.append(" LIMIT 1");
last_query = sql;
php::value rs = php::call_method_1(&_mysqli, "query", sql);
if(rs.is_type(IS_OBJECT)) {
return php::call_method_0(&rs, "fetch_assoc");
}else{
return php::null;
}
}
// 将其他所有调用转接到 mysqli
php::value database_mysql::__call(const std::string& name, const php::parameter& param) {
std::vector<zval> params;
for(size_t i=0;i<param.size;++i) {
params.push_back(*param.get(i));
}
php::property prop(this);
php::value _mysqli = prop.oget("_mysqli");
if(name == "query") {
convert_to_string(¶ms[0])
last_query.assign(Z_STRVAL(params[0]),Z_STRLEN(params[0]));
}
return php::call_method(&_mysqli, name, params);
}
bool database_mysql::__isset(const std::string& name) {
php::property self(this);
php::value _mysqli = self.oget("_mysqli");
php::property prop(_mysqli);
bool isset = prop.__isset(name);
return prop.__isset(name);
}
php::value database_mysql::__get(const std::string& name) {
if(name == "last_query") {
return last_query;
}
php::property self(this);
php::value _mysqli = self.oget("_mysqli");
php::property prop(_mysqli);
return prop.oget(name);
}