-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlify.class.php
More file actions
383 lines (314 loc) · 9.25 KB
/
sqlify.class.php
File metadata and controls
383 lines (314 loc) · 9.25 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
<?php
/**
* Simple SQL condition converter class
*
* @author Gagik Sukiasyan
* @since 22.12.2012
* @version 1.3
* @license BSD http://www.opensource.org/licenses/bsd-license.php
*/
class sqlify {
protected $debug = 0;
/**
* Variable which stores all custom filters defied by $this->add_filterfunction
*/
protected $_queries = array();
public function _test() {
$this->debug = 1;
$f=array(
'product2_txt,product3_txt,product4_txt' => 'Layout',
'aa' => '>AA<',
'aa1' => '! >AA<',
'aa2' => '! AA',
'aa3' => '=AA',
'aa4' => '!=AA',
'aa5' => '<AA',
'aa6' => '> AA',
'aa7' => '>= AA',
'aa8' => '! "AA"',
'aa9' => ' "AA"',
'aa10' => '!>AA',
'bb1' => ' "AA" || <a && >=asd',
);
echo "<h4>TEST 1</h4>";
dump($this->get_where($f));
echo "<h4>TEST 2</h4>";
$this->add_filter('AAA', array('a' => '>dd<'));
$this->add_filter('BBB', array('a' => '>55<', 'b' => 'AA'));
dump($this->get_where('AAA', $f, 'BBB'));
}
/**
* Root function which makes SQL condition from provided string
* For example if provided condition like 'XX || YY' it will convert to:
* `field` LIKE '%XX%' or `field` LIKE '%YY%'
*
* @param string $str : condirion in C style
* @param string $field : SQL field name
* @return string: the converted sql condition
*/
protected function _makesql ($str, $filed) {
$sign = "LIKE";
$matchType = "partial";
$isNegative = "";
$val = "";
if (preg_match("/^\s*(\!?)\s*[>\"](.*)[\"<]\s*$/", $str, $match)) {
// Cases of exact match
list($as, $isNegative, $val) = $match;
$matchType = "exact";
$sign = ($isNegative == '!') ? "<>" : '=';
} elseif (preg_match("/^\s*(\!?)\s*([><=]=?)(.+)$/", $str, $match)) {
$matchType = "exact";
list($as, $isNegative, $sign, $val) = $match;
$sign = ($isNegative == '!' && $sign=="=" ) ? "<>" : $sign;
} elseif (preg_match("/^\s*(\!?)\s*(.+)$/", $str, $match)) {
// Cases of partial match
$matchType = "partial";
list($as, $isNegative,$val) = $match;
$sign = ($isNegative == '!') ? "NOT LIKE" : 'LIKE';
}
$val = trim($val);
if ($matchType == 'partial') {
$val = "%$val%";
}
# This is for cases when value passed with SQL function, most used is COALESCE()
if (strpos($filed, '(') !== false) {
return "$filed $sign '$val'";
}
$filed = preg_replace('/^\s*`|`\s*$/','',$filed);
$res = "";
foreach (explode(",", $filed) as $f ) {
if (strpos($f, '(')===false) {
$ff = explode(".", $f);
$f = '`'.implode("`.`", $ff).'`';
}
if ($this->debug) {
echo "<h3>".$f." (".htmlspecialchars($str).") ==> $matchType $sign '$val' </h3>";
}
if ($res == "") {
$res .= " $f $sign '$val' ";
} else {
$res .= " or $f $sign '$val'";
}
}
return $res;
}
public function exact($n) {return ">$n<";}
private function _convert_macro_conditions ($str, &$field) {
$str2 ="";
while (preg_match('/@(\S+)\s+(((?!\|\||&&).)+)/i', $str, $matches) ) {
$type = trim($matches[1]);
$query = trim($matches[2]);
$value = "";
$str2 = $type;
if (preg_match('/^M(GR)?/i', $type)) {
$value = implode (" || ", array_map(array($this, "exact"), User::get_dir_reports($query)));
} elseif (preg_match('/^T(op)?M(GR)?/i', $type)) {
$value = implode (" || ", array_map(array($this, "exact"), User::get_all_reports($query)));
} elseif (preg_match('/^D(ATE)?/i', $type)) {
$value = date('Y-m-d', strtotime($query, time()));
} elseif (preg_match('/^s(tatus)?/i', $type)) {
if ($query == "open" || $query == "todo") {
$value = '! >Fixed< && ! >Fix Validated< && ! >Closed<';
} elseif ($query == "done") {
$value = '>Fixed< || >Fix Validated< || >Closed<';
} elseif ($query == 'close' || $query == 'closed') {
$value = '>Fix Validated< || >Closed<';
}
} elseif ($type == "is") {
$value = ">$query<";
}
$str = preg_replace('/@'.$type.'\s+((?!\|\||&&).)+/i', $value, $str, 1);
}
while (preg_match('/@(\S+)/i', $str, $matches) ) {
$type = trim($matches[1]);
$value = "";
$str2 = $type;
if ($type == "empty") {
$value = "><";
$field = "COALESCE($field, '')";
// $str2 = $value;
}
$str = preg_replace('/@'.$type.'/i', $value, $str, 1);
}
// echo "<tr><td>".$str2."</td>/<tr>";
return $str;
}
private function _matches ($str, $filed) {
$str = trim($str);
$str = strtolower($str);
$filed = strtolower($filed);
if (preg_match("/^\s*(\!?)\s*>(.*)<\s*$/", $str, $match)) {
list($as, $is_not, $val) = $match;
if ($is_not == "!") {
if($filed != trim($val)) {
return 1;
}
return 0;
}
if ($filed == trim($val)) {
return 1;
}
return 0;
} elseif (preg_match("/^\s*([><]=?)(.+)$/", $str, $match)) {
list($as, $sign, $val) = $match;
return eval ("if (\"$filed\" $sign \"".trim($val)."\") {return 1;} else {return 0;}");
} elseif (preg_match("/^\s*<(.+)$/", $str, $match)) {
list($as, $val) = $match;
return " $filed<'".trim($val)."' ";
} elseif (preg_match("/^\s*!(.+)/", $str, $match)) {
list($as, $val) = $match;
if (! preg_match("/$val/", trim($filed))) {
return 1;
}
return 0;
} else {
if (preg_match("/$str/", trim($filed))) {
return 1;
}
return 0;
}
}
public function convert_condition ($str, $field) {
$str = $this->_convert_macro_conditions($str, $field);
$tmp = preg_split("/(\|\||&&)/", $str, -1, PREG_SPLIT_DELIM_CAPTURE);
$i = 0;
$delim = array ("||", "&&");
$sql = "";
foreach ($tmp as $val) {
if (in_array($val, $delim)) {
$i ++;
continue;
}
if ($i == 0) {
$sql .= $this->_makesql($val, $field);
} else {
$condition = ($tmp[$i-1] == "||") ? "or" : "and";
$sql .= $condition.$this->_makesql($val, $field);
}
$i++;
}
return $sql;
}
public function init () {
$list = func_get_args();
if (count($list) > 0) {
$this->filters = call_user_func_array(array($this, 'get_filters'), $list);
}
return $this->filters;
}
public function is_match ($str, $field) {
$str = $this->_convert_macro_conditions($str, $field);
$tmp = preg_split("/(\|\||&&)/", $str, -1, PREG_SPLIT_DELIM_CAPTURE);
$i = 0;
$delim = array ("||", "&&");
$condination = "";
foreach ($tmp as $val) {
if (in_array($val, $delim)) {
$i ++;
continue;
}
if ($i == 0) {
$condination .= $this->_matches($val, $field);
} else {
$condination .= " ".$tmp[$i-1]." ".$this->_matches($val, $field);
}
$i++;
}
// echo "$str, $field | (".$condination.") => ".eval ("if (".$condination.") {return 1;} else {return 0;}")."<br>";
return eval ("if (".$condination.") {return 1;} else {return 0;}");
}
static public function matches($str, $field) {
$sqlify = new sqlify();
return $sqlify->is_match($str, $field);
}
public function get_where() {
$list = func_get_args();
call_user_func_array(array($this, 'init'), $list);
$where = '';
if (is_array($this->filters) and count($this->filters) >0) {
$filter_array = array();
foreach ($this->filters as $field => $value) {
if (trim($value) != '') {
$value = str_replace("*", "%", $value);
$filter_array[] = " ( ".$this->convert_condition($value, $field)." ) ";
}
}
$w = implode (" and ", $filter_array);
if ($w != '') {
if (preg_match('/^\s*$/i', $where)) {
$where = ' ';
} else {
$where .= ' and ';
}
$where .= " $w ";
}
}
return $where;
}
static public function where() {
$sqlify = new sqlify();
$list = func_get_args();
call_user_func_array(array($sqlify, 'init'), $list);
return $sqlify->get_where();
}
public function get_url() {
//filter value should be:
// field=value1||value2&&value3;;field=value1||value2&&value3;;...
$list = func_get_args();
call_user_func_array(array($this, 'init'), $list);
$urls = array();
$url_params = array();
if (is_array($this->filters) and !empty($this->filters)) {
$filter_array = array();
foreach ($this->filters as $field => $value) {
if (trim($value) != '') {
$urls[] = "%27$field%27:%27".urlencode($value)."%27";
$url_params[] = $field.'='.urlencode($value);
}
}
}
if (isset($this->url_by_params)) {
return implode('&', $url_params);
}
return "filters=".implode(',',$urls);
}
static public function url() {
$sqlify = new sqlify();
$list = func_get_args();
call_user_func_array(array($sqlify, 'init'), $list);
return $sqlify->get_url();
}
static public function url_params() {
$sqlify = new sqlify();
$list = func_get_args();
call_user_func_array(array($sqlify, 'init'), $list);
$sqlify->url_by_params = true;
return $sqlify->get_url();
}
/**
* Meges and returns all custom queries
*/
public function get_filters() {
$list = func_get_args();
$res = array();
foreach($list as $name) {
if (is_array($name)) {
$res = array_merge($res, $name);
} elseif (isset($this->_queries[$name])) {
$res = array_merge($res, $this->_queries[$name]);
}
}
return $res;
}
/**
* Define custom filter which can be used later
*/
public function add_filter() {
$list = func_get_args();
$name = $list[0];
$list = array_slice($list, 1);
$res = call_user_func_array(array($this, 'get_filters'), $list);
$this->_queries[$name] = $res;
}
}
?>