forked from terrywh/yafa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_mysql_where.cpp
149 lines (142 loc) · 4.75 KB
/
database_mysql_where.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: database_mysql_where.cpp
* Author: wuhao
*
* Created on 2015年11月22日, 下午2:11
*/
#include "core.h"
#include "database_mysql_where.h"
database_mysql_where::database_mysql_where(php::value& mysqli, std::string& sql):_mysqli(mysqli), _sql(sql) {
}
database_mysql_where::~database_mysql_where() {
}
void database_mysql_where::build(php::value& c) {
_sql.append(" WHERE ");
if(c.is_type(IS_STRING)) {
_sql.append((std::string)c);
return;
}else if(!c.is_type(IS_ARRAY)) {
zend_throw_error(nullptr, "conditions must be string or array");
return;
}
size_t len = c.length();
for(auto i=c.begin(); i!= c.end(); ++i) {
std::string key(ZSTR_VAL(i->key), ZSTR_LEN(i->key));
php::value val(i->val, false);
append_item(key, val);
if(--len != 0) { // 最顶层默认按照 AND 处理
_sql.append(" AND ");
}
}
}
void database_mysql_where::append_item(const std::string& key, php::value& val) {
if(key[key.length()-1] == ']') {
size_t pos = key.find_first_of('[');
std::string op = key.substr(pos+1, key.length()-pos-2);
_sql.push_back('`');
_sql.append(key.substr(0, pos));
_sql.push_back('`');
if(op == "!") {
if(val.is_type(IS_NULL)) {
_sql.append(" IS NOT ");
}else if(val.is_type(IS_ARRAY)) {
_sql.append(" NOT IN ");
}else{
_sql.append("!=");
}
}else if(op == ">" || op == "<" || op == ">=" || op == "<=") {
_sql.append(op);
}else if(op == "<>") {
_sql.append(" BETWEEN ");
php::value v0 = val[0];
php::value v1 = val[1];
append_val(v0);
_sql.append(" AND ");
append_val(v1);
return;
}else if(op == "><") {
_sql.append(" NOT BETWEEN ");
php::value v0 = val[0];
php::value v1 = val[1];
append_val(v0);
_sql.append(" AND ");
append_val(v1);
return;
}else if(op == "~") {
_sql.append(" LIKE ");
}else if(op == "!~") {
_sql.append(" NOT LIKE ");
}else{
zend_throw_error(nullptr,"operation \"%s\" not suppored", op.c_str());
return;
}
append_val(val);
}else if(key == "AND") {
_sql.push_back('(');
unsigned long length = val.length();
for(auto p=val.begin(); p!= val.end(); ++p) {
std::string key( ZSTR_VAL(p->key), ZSTR_LEN(p->key) );
php::value val(p->val, false);
append_item(key, val);
if(--length != 0) {
_sql.append(" AND ");
}
}
_sql.push_back(')');
}else if(key == "OR") {
_sql.push_back('(');
unsigned long length = val.length();
for(auto p=val.begin(); p!= val.end(); ++p) {
std::string key( ZSTR_VAL(p->key), ZSTR_LEN(p->key) );
php::value val(p->val, false);
append_item(key, val);
if(--length != 0) {
_sql.append(" OR ");
}
}
_sql.push_back(')');
}else{ // `key`
_sql.push_back('`');
_sql.append(key);
_sql.push_back('`');
if(val.is_type(IS_NULL)) { // IS NULL
_sql.append(" IS ");
}else if(val.is_type(IS_ARRAY)) {
_sql.append(" IN ");
}else{
_sql.push_back('=');
}
append_val(val);
}
}
void database_mysql_where::append_val(php::value& val) {
if(val.is_type(IS_NULL)) { // IS NULL
_sql.append("NULL");
}else if(val.is_type(IS_ARRAY)) { // ('1','2','3')
_sql.push_back('(');
unsigned int length = val.length();
for(auto i=val.begin(); i!= val.end(); ++i) {
php::value val(i->val, false);
append_val(val);
if(--length != 0) {
_sql.push_back(',');
}
}
_sql.push_back(')');
}else if(val.is_type(IS_TRUE)) {
_sql.append("true");
}else if(val.is_type(IS_FALSE)) {
_sql.append("false");
}else if(val.is_type(IS_LONG)) {
_sql.append(std::to_string((zend_long)val));
}else{ // 'val'
_sql.push_back('\'');
_sql.append((std::string)php::call_method_1(&_mysqli, "escape_string", val));
_sql.push_back('\'');
}
}