forked from old-blueday/sql4array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql4array.class.php
More file actions
373 lines (312 loc) · 10.5 KB
/
sql4array.class.php
File metadata and controls
373 lines (312 loc) · 10.5 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
<?PHP
/*
* Project: Absynthe sql4array
* File: sql4array.class.php5
* Author: Absynthe <[email protected]>
* Webste: http://absynthe.is.free.fr/sql4array/
* Version: alpha 1
* Date: 30/04/2007
* License: LGPL
*/
/*
* Parameters available :
* SELECT, DISTINCT, FROM, WHERE, ORDER BY, LIMIT, OFFSET
*
* Operators available :
* =, <, >, <=, >=, <>, !=, IS, IS IN, IS NOT, IS NOT IN, LIKE, ILIKE, NOT LIKE, NOT ILIKE
*
* Functions available in WHERE parameters :
* LOWER(var), UPPER(var), TRIM(var)
*/
class sql4array
{
/* Init
-------------------------------------------- */
private $query = FALSE;
private $parse_query = FALSE;
private $parse_query_lower = FALSE;
private $parse_select = FALSE;
private $parse_select_as = FALSE;
private $parse_from = FALSE;
private $parse_from_as = FALSE;
private $parse_where = FALSE;
private $distinct_query = FALSE;
private $tables = array();
private $response = array();
/* Query function
-------------------------------------------- */
public function query($query)
{
$this->destroy();
$this->query = $query;
$this->parse_query();
$this->parse_select();
$this->parse_select_as();
$this->parse_from();
$this->parse_from_as();
$this->parse_where();
$this->exec_query();
$this->parse_order();
return $this->return_response();
}
/* Destroy current values
-------------------------------------------- */
private function destroy()
{
$this->query = FALSE;
$this->parse_query = FALSE;
$this->parse_query_lower = FALSE;
$this->parse_select = FALSE;
$this->parse_select_as = FALSE;
$this->parse_from = FALSE;
$this->parse_from_as = FALSE;
$this->parse_where = FALSE;
$this->distinct_query = FALSE;
$this->tables = array();
$this->response = array();
}
/* Parse SQL query
-------------------------------------------- */
private function parse_query()
{
$this->parse_query = preg_replace('#ORDER(\s){2,}BY(\s+)(.*)(\s+)(ASC|DESC)#i', 'ORDER BY \\3 \\5', $this->query);
$this->parse_query = preg_split('#(SELECT|DISTINCT|FROM|JOIN|WHERE|ORDER(\s+)BY|LIMIT|OFFSET)+#i', $this->parse_query, -1, PREG_SPLIT_DELIM_CAPTURE);
$this->parse_query = array_map('trim', $this->parse_query);
$this->parse_query_lower = array_map('strtolower', $this->parse_query);
}
/* Parse SQL select parameters
-------------------------------------------- */
private function parse_select()
{
$key = array_search("distinct", $this->parse_query_lower);
if ($key === FALSE)
$key = array_search("select", $this->parse_query_lower);
else
$this->distinct_query = TRUE;
$string = $this->parse_query[$key+1];
$arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY);
foreach ($arrays as $array)
$this->parse_select[] = $array;
}
/* Parse again SQL select parameters with as keyword
-------------------------------------------- */
private function parse_select_as()
{
foreach ($this->parse_select as $select)
{
if (eregi('as', $select))
{
$arrays = preg_split('#((\s)+AS(\s)+)#i', $select, -1, PREG_SPLIT_NO_EMPTY);
$this->parse_select_as[$arrays[1]] = $arrays[0];
}
else
{
$this->parse_select_as[$select] = $select;
}
}
}
/* Parse SQL from parameters
-------------------------------------------- */
private function parse_from()
{
$key = array_search("from", $this->parse_query_lower);
$string = $this->parse_query[$key+1];
$arrays = preg_split('#((\s)*,(\s)*)#i', $string, -1, PREG_SPLIT_NO_EMPTY);
foreach ($arrays as $array)
$this->parse_from[] = $array;
}
/* Parse again SQL from parameters with as keyword
-------------------------------------------- */
private function parse_from_as()
{
foreach ($this->parse_from as $from)
{
if (eregi('AS', $from))
{
$arrays = preg_split('#((\s)+AS(\s)+)#i', $from, -1, PREG_SPLIT_NO_EMPTY);
$table = $arrays[0];
global $$table;
$this->parse_from_as[$arrays[1]] = $table;
$this->tables[$arrays[1]] = $$table;
}
else
{
$table = $from;
global $$table;
$this->parse_from_as[$from] = $table;
$this->tables[$arrays[1]] = $$table;
}
}
}
/* Parse SQL where parameters
-------------------------------------------- */
private function parse_where()
{
$key = array_search("where", $this->parse_query_lower);
if ($key == FALSE)
return $this->parse_where = "return TRUE;";
$string = $this->parse_query[$key+1];
if (trim($string) == '')
return $this->parse_where = "return TRUE;";
/* SQL Functions
-------------------------------------------- */
$patterns[] = '#LOWER\((.*)\)#ie';
$patterns[] = '#UPPER\((.*)\)#ie';
$patterns[] = '#TRIM\((.*)\)#ie';
$replacements[] = "'strtolower(\\1)'";
$replacements[] = "'strtoupper(\\1)'";
$replacements[] = "'trim(\\1)'";
/* Basics SQL operators
-------------------------------------------- */
$patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+([[:digit:]]+)(\s)*#ie';
$patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(=|IS)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie';
$patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(>|<)(\s)+([[:digit:]]+)(\s)*#ie';
$patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<=|>=)(\s)+([[:digit:]]+)(\s)*#ie';
$patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+([[:digit:]]+)(\s)*#ie';
$patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(<>|IS NOT|!=)(\s)+(\'|\")(.*)(\'|\")(\s)*#ie';
$patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(NOT IN)(\s)+\((.*)\)#ie';
$patterns[] = '#(([a-zA-Z0-9\._]+)(\())?([a-zA-Z0-9\.]+)(\))?(\s)+(IS)?(IN)(\s)+\((.*)\)#ie';
$replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \\9 '";
$replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == \"\\10\" '";
$replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '";
$replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 \\7 \\9 '";
$replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \\9 '";
$replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != \"\\10\" '";
$replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 != ('.\$this->parse_in(\"\\10\").') '";
$replacements[] = "'\\1'.\$this->parse_where_key(\"\\4\").'\\5 == ('.\$this->parse_in(\"\\10\").') '";
/* match SQL operators
-------------------------------------------- */
$ereg = array('%' => '(.*)', '_' => '(.)');
$patterns[] = '#([a-zA-Z0-9\.]+)(\s)+LIKE(\s)*(\'|\")(.*)(\'|\")#ie';
$patterns[] = '#([a-zA-Z0-9\.]+)(\s)+ILIKE(\s)*(\'|\")(.*)(\'|\")#ie';
$patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT LIKE(\s)*(\'|\")(.*)(\'|\")#ie';
$patterns[] = '#([a-zA-Z0-9\.]+)(\s)+NOT ILIKE(\s)*(\'|\")(.*)(\'|\")#ie';
$replacements[] = "'ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'";
$replacements[] = "'eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'";
$replacements[] = "'!ereg(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'";
$replacements[] = "'!eregi(\"'.strtr(\"\\5\", \$ereg).'\", '.\$this->parse_where_key(\"\\1\").')'";
$this->parse_where = "return ".stripslashes(trim(preg_replace($patterns, $replacements, $string))).";";
}
/*
-------------------------------------------- */
private function parse_where_key($key)
{
if (ereg('\.', $key))
{
list($table, $col) = explode('.', $key);
return '$row[$this->parse_select_as['.$col.']]';
}
else
{
return '$row[$this->parse_select_as['.$key.']]';
}
}
/* Format IN parameters for PHP
-------------------------------------------- */
private function parse_in($string)
{
$array = explode(',', $string);
$array = array_map('trim', $array);
return implode(' || ', $array);
}
/* Execute query
-------------------------------------------- */
private function exec_query()
{
$klimit = array_search("limit", $this->parse_query_lower);
$koffset = array_search("offset", $this->parse_query_lower);
if ($klimit !== FALSE)
$limit = (int) $this->parse_query[$klimit+1];
if ($koffset !== FALSE)
$offset = (int) $this->parse_query[$koffset+1];
$irow = 0;
$distinct = array();
foreach ($this->tables as $table)
{
foreach ($table as $row)
{
// Offset
if ($koffset !== FALSE && $irow < $offset)
{
$irow++;
continue;
}
if (eval($this->parse_where))
{
if ($this->parse_select_as[0] == '*')
{
foreach (array_keys($row) as $key)
$temp[$key] = $row[$key];
if ($this->distinct_query && in_array($temp, $distinct))
continue;
else
$this->response[] = $temp;
$distinct[] = $response;
}
else
{
foreach ($this->parse_select_as as $key => $value)
$temp[$key] = $row[$value];
if ($this->distinct_query && in_array($temp, $distinct))
continue;
else
$this->response[] = $temp;
$distinct[] = $temp;
}
// Limit
if ($klimit !== FALSE && count($this->response) == $limit)
break;
}
$irow++;
}
}
}
/* Parse SQL order by parameters
-------------------------------------------- */
private function parse_order()
{
$key = array_search("order by", $this->parse_query_lower);
if ($key === FALSE)
return;
$string = $this->parse_query[$key+2];
$arrays = explode(',', $string);
if (!is_array($arrays))
$arrays[] = $string;
$arrays = array_map('trim', $arrays);
$multisort = "array_multisort(";
foreach ($arrays as $array)
{
list($col, $sort) = preg_split('#((\s)+)#', $array, -1, PREG_SPLIT_NO_EMPTY);
$multisort .= "\$this->split_array(\$this->response, '$col'), SORT_".strtoupper($sort).", SORT_STRING, ";
}
$multisort .= "\$this->response);";
eval($multisort);
}
/* Return response
-------------------------------------------- */
private function return_response()
{
return $this->response;
}
/* Return a column of an array
-------------------------------------------- */
private function split_array($input_array, $column)
{
$output_array = array();
foreach ($input_array as $key => $value)
$output_array[] = $value[$column];
return $output_array;
}
/* Entire array search
-------------------------------------------- */
private function entire_array_search($needle, $array)
{
foreach($array as $key => $value)
if ($value === $needle)
$return[] = $key;
if (!is_array($return))
$return = FALSE;
return $return;
}
}
?>