1
|
|
|
<?php |
2
|
|
|
namespace Fwlib\Db; |
3
|
|
|
|
4
|
|
|
use Fwlib\Bridge\Adodb; |
5
|
|
|
use Fwlib\Util\UtilContainerAwareTrait; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* SQL Generator |
9
|
|
|
* |
10
|
|
|
* Covered SQL clause: |
11
|
|
|
* { |
12
|
|
|
* DELETE, |
13
|
|
|
* FROM, |
14
|
|
|
* GROUPBY, |
15
|
|
|
* HAVING, |
16
|
|
|
* INSERT, |
17
|
|
|
* LIMIT, |
18
|
|
|
* ORDERBY, |
19
|
|
|
* SELECT, |
20
|
|
|
* SET, |
21
|
|
|
* UPDATE, |
22
|
|
|
* VALUES, |
23
|
|
|
* WHERE, |
24
|
|
|
* } |
25
|
|
|
* |
26
|
|
|
* When combine SQL parts, add space before clause keywords except DELETE, |
27
|
|
|
* SELECT, INSERT, UPDATE. |
28
|
|
|
* |
29
|
|
|
* Notice: call genClause() method directly works, but beware the result may |
30
|
|
|
* from mixed config from several set() before, use clear() to clear them, or |
31
|
|
|
* use getClause() method to avoid this. |
32
|
|
|
* |
33
|
|
|
* @copyright Copyright 2003-2015 Fwolf |
34
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
35
|
|
|
*/ |
36
|
|
|
class SqlGenerator |
37
|
|
|
{ |
38
|
|
|
use UtilContainerAwareTrait; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Db connection |
43
|
|
|
* |
44
|
|
|
* @var object |
45
|
|
|
*/ |
46
|
|
|
protected $db; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Param array index by SQL part |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $paramPart = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Generated SQL string part |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $sqlPart = []; |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Constructor |
65
|
|
|
* |
66
|
|
|
* @param Adodb &$db Db object |
67
|
|
|
*/ |
68
|
|
|
public function __construct(&$db) |
69
|
|
|
{ |
70
|
|
|
if (!empty($db)) { |
71
|
|
|
$this->db = &$db; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Clear all or some parts set param |
78
|
|
|
* |
79
|
|
|
* @param string $part |
80
|
|
|
*/ |
81
|
|
|
public function clear($part = '') |
82
|
|
|
{ |
83
|
|
|
// order by => ORDERBY |
84
|
|
|
$part = str_replace(' ', '', (strtoupper($part))); |
85
|
|
|
|
86
|
|
|
// Reset-able part |
87
|
|
|
$arPart = [ |
|
|
|
|
88
|
|
|
'DELETE', |
89
|
|
|
'FROM', |
90
|
|
|
'GROUPBY', |
91
|
|
|
'HAVING', |
92
|
|
|
'INSERT', |
93
|
|
|
'LIMIT', |
94
|
|
|
'ORDERBY', |
95
|
|
|
'SELECT', |
96
|
|
|
'SET', |
97
|
|
|
'UPDATE', |
98
|
|
|
'VALUES', |
99
|
|
|
'WHERE', |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
if (empty($part) || 'all' == $part) { |
103
|
|
|
$this->paramPart = []; |
104
|
|
|
$this->sqlPart = []; |
|
|
|
|
105
|
|
|
} else { |
106
|
|
|
// Reset part split by comma |
107
|
|
|
$arToClear = explode(',', $part); |
108
|
|
|
foreach ($arToClear as $s) { |
109
|
|
|
unset($this->paramPart[$s]); |
110
|
|
|
unset($this->sqlPart[$s]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Generate DELETE sql |
118
|
|
|
* |
119
|
|
|
* @param array $part |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function genDelete($part = null) |
123
|
|
|
{ |
124
|
|
|
$arrayUtil = $this->getUtilContainer()->getArray(); |
125
|
|
|
|
126
|
|
|
if (!empty($part) && is_array($part)) { |
127
|
|
|
// Using preferred parts in $part only |
128
|
|
|
$ar = &$part; |
129
|
|
|
} else { |
130
|
|
|
// Using all parts, by below sequence |
131
|
|
|
$ar = ['DELETE', 'WHERE', 'ORDERBY', 'LIMIT']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$sql = ''; |
135
|
|
|
foreach ($ar as $v) { |
136
|
|
|
$sql .= $arrayUtil->getIdx($this->sqlPart, strtoupper($v), ''); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $sql; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Generate INSERT sql |
145
|
|
|
* |
146
|
|
|
* @param array $part |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function genInsert($part = []) |
150
|
|
|
{ |
151
|
|
|
$arrayUtil = $this->getUtilContainer()->getArray(); |
152
|
|
|
|
153
|
|
|
if (!empty($part) && is_array($part)) { |
154
|
|
|
// Using preferred parts in $part only |
155
|
|
|
$ar = &$part; |
156
|
|
|
} else { |
157
|
|
|
// Using all parts, by below sequence |
158
|
|
|
$ar = ['INSERT', 'VALUES']; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$sql = ''; |
162
|
|
|
foreach ($ar as $v) { |
163
|
|
|
$sql .= $arrayUtil->getIdx($this->sqlPart, strtoupper($v), ''); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $sql; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Generate SELECT sql |
172
|
|
|
* |
173
|
|
|
* @param array $part |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
View Code Duplication |
public function genSelect($part = []) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
$arrayUtil = $this->getUtilContainer()->getArray(); |
179
|
|
|
|
180
|
|
|
if (!empty($part) && is_array($part)) { |
181
|
|
|
// Using preferred parts in $part only |
182
|
|
|
$ar = &$part; |
183
|
|
|
} else { |
184
|
|
|
// Using all parts, by below sequence |
185
|
|
|
$ar = [ |
186
|
|
|
'SELECT', 'FROM', 'WHERE', 'GROUPBY', 'HAVING', |
187
|
|
|
'ORDERBY', 'LIMIT' |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$sql = ''; |
192
|
|
|
foreach ($ar as $v) { |
193
|
|
|
$sql .= $arrayUtil->getIdx($this->sqlPart, strtoupper($v), ''); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $sql; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Generate SQL part, convert param array to string with separator |
202
|
|
|
* |
203
|
|
|
* @param mixed $param |
204
|
|
|
* @param string $separator Should have space included |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
protected function genSqlArray($param, $separator = ', ') |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$sql = ''; |
210
|
|
|
if (!empty($param) && is_array($param)) { |
211
|
|
|
// Key of param array is not used |
212
|
|
|
foreach ($param as $v) { |
213
|
|
|
$sql .= "$separator$v"; |
214
|
|
|
} |
215
|
|
|
} else { |
216
|
|
|
$sql .= "$separator$param"; |
217
|
|
|
} |
218
|
|
|
// Remove heading separator and space |
219
|
|
|
$sql = substr($sql, strlen($separator)); |
220
|
|
|
|
221
|
|
|
return $sql; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Generate SQL part, which param is array and need use AS in it |
227
|
|
|
* |
228
|
|
|
* @param mixed $param Param content, array or string, |
229
|
|
|
* {k: v} means 'k AS v', |
230
|
|
|
* [v] means 'v AS v', |
231
|
|
|
* remember to check $reverse param below. |
232
|
|
|
* @param boolean $useAs Sybase table alias can't use AS |
233
|
|
|
* @param boolean $quote AS column alias, need to be quoted(true), |
234
|
|
|
* AS table alias, need not to be quoted(false). |
235
|
|
|
* @param boolean $reverse Default true: {k: v} means 'v AS k'. |
236
|
|
|
* Because array key must be unique, and a |
237
|
|
|
* table can have many alias, so use unique |
238
|
|
|
* alias as key in param array. Also this |
239
|
|
|
* will make define code pretty and short, |
240
|
|
|
* especially when mixed items with/without |
241
|
|
|
* alias. Eg: {tbl1, a: tbl2} |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
protected function genSqlArrayAs( |
|
|
|
|
245
|
|
|
$param, |
246
|
|
|
$useAs = true, |
247
|
|
|
$quote = false, |
248
|
|
|
$reverse = true |
249
|
|
|
) { |
250
|
|
|
$sql = ''; |
251
|
|
|
if (!empty($param) && is_array($param)) { |
252
|
|
|
foreach ($param as $k => $v) { |
253
|
|
|
// If there are space in $v, it need to be quoted, so always |
254
|
|
|
// quote it except number (here will not have float value). |
255
|
|
|
if (is_int($k)) { |
256
|
|
|
$sql .= ", $v"; |
257
|
|
|
} else { |
258
|
|
|
// table AS a |
259
|
|
|
// table AS 'a' |
260
|
|
|
$split = ($quote) ? "'" : ''; |
261
|
|
|
$as = ($useAs) ? 'AS ' : ''; |
262
|
|
|
|
263
|
|
|
// Reverse as is only useful for particular db type |
264
|
|
|
// @codeCoverageIgnoreStart |
265
|
|
|
if ($reverse) { |
266
|
|
|
$sql .= ", $v $as$split{$k}$split"; |
267
|
|
|
} else { |
268
|
|
|
$sql .= ", $k $as$split{$v}$split"; |
269
|
|
|
} |
270
|
|
|
// @codeCoverageIgnoreEnd |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
} else { |
274
|
|
|
$sql .= ", $param"; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
$sql = substr($sql, 2); |
279
|
|
|
|
280
|
|
|
return $sql; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Generate SQL part, SET sub-parse of UPDATE |
286
|
|
|
* |
287
|
|
|
* @param array $param Array only, string will use original value. |
288
|
|
|
* Array($k=>$v) means 'SET $k = $v, ' in sql, |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
View Code Duplication |
protected function genSqlArraySet($param) |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
$sql = ''; |
294
|
|
|
if (!empty($param) && is_array($param)) { |
295
|
|
|
foreach ($param as $k => $v) { |
296
|
|
|
$sql .= ", $k = " . $this->genSqlQuote($this->paramPart['UPDATE'], $k, $v); |
297
|
|
|
} |
298
|
|
|
$sql = ' SET ' . substr($sql, 2); |
299
|
|
|
} else { |
300
|
|
|
// String param, add 'SET ' if user forgot |
301
|
|
|
if ('SET ' != substr(strtoupper(trim($param)), 0, 4)) { |
302
|
|
|
$sql .= ' SET '; |
303
|
|
|
} |
304
|
|
|
$sql .= $param; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $sql; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Generate SQL part, VALUES sub-parse of INSERT |
313
|
|
|
* |
314
|
|
|
* @param array $param Array only, string will use original value. |
315
|
|
|
* Array($k=>$v) means '($k) VALUES ($v)' in sql. |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
View Code Duplication |
protected function genSqlArrayValues($param) |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
$sql = ''; |
321
|
|
|
if (!empty($param) && is_array($param)) { |
322
|
|
|
$sql1 = ''; |
323
|
|
|
$sql2 = ''; |
324
|
|
|
foreach ($param as $k => $v) { |
325
|
|
|
$sql1 .= ', ' . $k; |
326
|
|
|
$sql2 .= ', ' |
327
|
|
|
. $this->genSqlQuote($this->paramPart['INSERT'], $k, $v); |
328
|
|
|
} |
329
|
|
|
$sql1 = substr($sql1, 2); |
330
|
|
|
$sql2 = substr($sql2, 2); |
331
|
|
|
$sql .= '(' . $sql1 . ') VALUES (' . $sql2 . ')'; |
332
|
|
|
} else { |
333
|
|
|
$sql = $param; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $sql; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Smart quote value in sql, by check columns type |
342
|
|
|
* |
343
|
|
|
* @param string $tbl |
344
|
|
|
* @param string $col |
345
|
|
|
* @param mixed $val |
346
|
|
|
* @return string |
347
|
|
|
*/ |
348
|
|
|
protected function genSqlQuote($tbl, $col, $val) |
349
|
|
|
{ |
350
|
|
|
return $this->db->quoteValue($tbl, $col, $val); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Generate UPDATE sql |
356
|
|
|
* @param array $part |
357
|
|
|
* @return string |
358
|
|
|
*/ |
359
|
|
View Code Duplication |
public function genUpdate($part = []) |
|
|
|
|
360
|
|
|
{ |
361
|
|
|
$arrayUtil = $this->getUtilContainer()->getArray(); |
362
|
|
|
|
363
|
|
|
if (!empty($part) && is_array($part)) { |
364
|
|
|
// Using preferred parts in $part only |
365
|
|
|
$ar = &$part; |
366
|
|
|
} else { |
367
|
|
|
// Using all parts, by below sequence |
368
|
|
|
$ar = ['UPDATE', 'SET', 'WHERE', 'ORDERBY', 'LIMIT']; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
$sql = ''; |
372
|
|
|
foreach ($ar as $v) { |
373
|
|
|
$sql .= $arrayUtil->getIdx($this->sqlPart, strtoupper($v), ''); |
|
|
|
|
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
return $sql; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Get result SQL statement |
382
|
|
|
* |
383
|
|
|
* Notice: If $config include SELECT, UPDATE, INSERT, DELETE |
384
|
|
|
* simultaneously, system will select the first occurs by raw order. |
385
|
|
|
* |
386
|
|
|
* @param array $config {SELECT: , FROM: ...} |
387
|
|
|
* If omit, use $this->paramPart |
388
|
|
|
* @param string $part SELECT/UPDATE ... etc |
389
|
|
|
* @return string |
390
|
|
|
*/ |
391
|
|
|
public function get($config = [], $part = '') |
392
|
|
|
{ |
393
|
|
|
$part = strtoupper($part); |
394
|
|
|
$this->set($config); |
395
|
|
|
|
396
|
|
|
// Got real action |
397
|
|
|
if (empty($part)) { |
398
|
|
|
foreach ($this->paramPart as $key => $val) { |
399
|
|
|
// SELECT/UPDATE/INSERT/DELETE ? Use the 1st occurred one |
400
|
|
View Code Duplication |
if (in_array( |
|
|
|
|
401
|
|
|
$key, |
402
|
|
|
['SELECT', 'UPDATE', 'INSERT', 'DELETE'] |
403
|
|
|
)) { |
404
|
|
|
$part = $key; |
405
|
|
|
break; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
// No part to do |
411
|
|
|
if (empty($part) || !isset($this->paramPart[$part])) { |
412
|
|
|
return ''; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
// Call separate func to generate sql |
416
|
|
|
$part = ucfirst(strtolower($part)); |
417
|
|
|
$sql = $this->{'gen' . $part}(array_keys($config)); |
418
|
|
|
|
419
|
|
|
return $sql; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Get DELETE sql only |
425
|
|
|
* |
426
|
|
|
* @param array $config |
427
|
|
|
* @return string |
428
|
|
|
*/ |
429
|
|
|
public function getDelete($config = []) |
430
|
|
|
{ |
431
|
|
|
return $this->get($config, 'DELETE'); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Get INSERT sql only |
437
|
|
|
* |
438
|
|
|
* @param array $config |
439
|
|
|
* @return string |
440
|
|
|
*/ |
441
|
|
|
public function getInsert($config = []) |
442
|
|
|
{ |
443
|
|
|
return $this->get($config, 'INSERT'); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Get SQL statement for PREPARE usage |
449
|
|
|
* |
450
|
|
|
* Used need replace actual value with Adodb::param(col), to generate sql |
451
|
|
|
* use placeholder (? or :name by db type), this method will auto remove |
452
|
|
|
* quote. |
453
|
|
|
* |
454
|
|
|
* When execute a prepared SQL, db system will auto add quote, but it |
455
|
|
|
* depends on type of value, NOT type of db column. |
456
|
|
|
* |
457
|
|
|
* Known replaceQuote list: |
458
|
|
|
* mssql: double single-quote |
459
|
|
|
* mysql: backslash-quote |
460
|
|
|
* |
461
|
|
|
* @param array $param |
462
|
|
|
* @return string |
463
|
|
|
*/ |
464
|
|
View Code Duplication |
public function getPrepared($param = []) |
|
|
|
|
465
|
|
|
{ |
466
|
|
|
$sql = $this->get($param); |
467
|
|
|
|
468
|
|
|
// @codeCoverageIgnoreStart |
469
|
|
|
// Remove duplicate ' in sql add by SqlGenerator, |
470
|
|
|
if ("''" == $this->db->replaceQuote) { |
471
|
|
|
$quote = "'"; |
472
|
|
|
} else { |
473
|
|
|
$quote = $this->db->replaceQuote; |
474
|
|
|
} |
475
|
|
|
// @codeCoverageIgnoreEnd |
476
|
|
|
|
477
|
|
|
// Remove quote |
478
|
|
|
$sql = preg_replace( |
479
|
|
|
"/([\s,\(]){$quote}([\?\:\w\-_]+){$quote}([\s,\)])/i", |
480
|
|
|
"$1$2$3", |
481
|
|
|
$sql |
482
|
|
|
); |
483
|
|
|
|
484
|
|
|
return $sql; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Get SELECT sql only |
490
|
|
|
* |
491
|
|
|
* @param array $config |
492
|
|
|
* @return string |
493
|
|
|
*/ |
494
|
|
|
public function getSelect($config = []) |
495
|
|
|
{ |
496
|
|
|
return $this->get($config, 'SELECT'); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Get UPDATE sql only |
502
|
|
|
* |
503
|
|
|
* @param array $config |
504
|
|
|
* @return string |
505
|
|
|
*/ |
506
|
|
|
public function getUpdate($config = []) |
507
|
|
|
{ |
508
|
|
|
return $this->get($config, 'UPDATE'); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Set param |
514
|
|
|
* |
515
|
|
|
* Un-recognized clause is ignored. |
516
|
|
|
* |
517
|
|
|
* @param array &$config |
518
|
|
|
* @return string |
519
|
|
|
*/ |
520
|
|
|
public function set(&$config) |
521
|
|
|
{ |
522
|
|
|
if (empty($config) || !is_array($config)) { |
523
|
|
|
return ''; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
// Global clause order, will sort by this. |
527
|
|
|
$clauseOrder = [ |
528
|
|
|
'SELECT', 'DELETE', 'INSERT', 'UPDATE', |
529
|
|
|
'VALUES', 'FROM', 'SET', |
530
|
|
|
'WHERE', 'GROUPBY', 'HAVING', 'ORDERBY', 'LIMIT' |
531
|
|
|
]; |
532
|
|
|
|
533
|
|
|
// Re-order sql part |
534
|
|
|
$ar = []; |
535
|
|
|
$config = array_change_key_case($config, CASE_UPPER); |
536
|
|
|
foreach ($clauseOrder as $clause) { |
537
|
|
|
if (isset($config[$clause])) { |
538
|
|
|
$ar[$clause] = &$config[$clause]; |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
// Write data back to config |
542
|
|
|
$config = $ar; |
543
|
|
|
|
544
|
|
|
foreach ($config as $part => $param) { |
545
|
|
|
// Write config to param array |
546
|
|
|
$part = ucfirst(strtolower($part)); |
547
|
|
|
$this->{"Set$part"}($param); |
548
|
|
|
} |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Set and treat DELETE param |
554
|
|
|
* |
555
|
|
|
* @param mixed $param |
556
|
|
|
* @return string |
557
|
|
|
*/ |
558
|
|
View Code Duplication |
public function setDelete($param) |
|
|
|
|
559
|
|
|
{ |
560
|
|
|
$this->paramPart['DELETE'] = $param; |
561
|
|
|
|
562
|
|
|
$this->sqlPart['DELETE'] = 'DELETE FROM ' . $param; |
563
|
|
|
|
564
|
|
|
return $this->sqlPart['DELETE']; |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Set and treat FROM param |
570
|
|
|
* |
571
|
|
|
* @param mixed $param |
572
|
|
|
* @return string |
573
|
|
|
*/ |
574
|
|
View Code Duplication |
public function setFrom($param) |
|
|
|
|
575
|
|
|
{ |
576
|
|
|
$this->paramPart['FROM'] = $param; |
577
|
|
|
|
578
|
|
|
// In 'FROM tbl AS alias' clause, no space allowed in alias, so need |
579
|
|
|
// not add quote to it. |
580
|
|
|
$this->sqlPart['FROM'] = ' FROM ' |
581
|
|
|
. $this->genSqlArrayAs($param, false, false, true); |
582
|
|
|
|
583
|
|
|
return $this->sqlPart['FROM']; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Set and treat GROUP BY param |
589
|
|
|
* |
590
|
|
|
* @param mixed $param |
591
|
|
|
* @return string |
592
|
|
|
*/ |
593
|
|
View Code Duplication |
public function setGroupby($param) |
|
|
|
|
594
|
|
|
{ |
595
|
|
|
$this->paramPart['GROUPBY'] = $param; |
596
|
|
|
|
597
|
|
|
$this->sqlPart['GROUPBY'] = ' GROUP BY ' . $this->genSqlArray($param); |
598
|
|
|
|
599
|
|
|
return $this->sqlPart['GROUPBY']; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* Set and treat HAVING param |
605
|
|
|
* |
606
|
|
|
* @param mixed $param |
607
|
|
|
* @return string |
608
|
|
|
*/ |
609
|
|
View Code Duplication |
public function setHaving($param) |
|
|
|
|
610
|
|
|
{ |
611
|
|
|
$this->paramPart['HAVING'] = $param; |
612
|
|
|
|
613
|
|
|
// Add '(' to defend sql injection |
614
|
|
|
$this->sqlPart['HAVING'] = ' HAVING (' |
615
|
|
|
. $this->genSqlArray($param, ') AND (') . ')'; |
616
|
|
|
|
617
|
|
|
return $this->sqlPart['HAVING']; |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Set and treat INSERT param |
623
|
|
|
* |
624
|
|
|
* @param mixed $param |
625
|
|
|
* @return string |
626
|
|
|
*/ |
627
|
|
View Code Duplication |
public function setInsert($param) |
|
|
|
|
628
|
|
|
{ |
629
|
|
|
$this->paramPart['INSERT'] = $param; |
630
|
|
|
|
631
|
|
|
$this->sqlPart['INSERT'] = 'INSERT INTO ' . $param; |
632
|
|
|
|
633
|
|
|
// Retrieve table schema, so VALUES can determine how to quote |
634
|
|
|
$this->db->getMetaColumn($param); |
635
|
|
|
|
636
|
|
|
return $this->sqlPart['INSERT']; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Set and treat LIMIT param |
642
|
|
|
* |
643
|
|
|
* @param mixed $param |
644
|
|
|
* @return string |
645
|
|
|
*/ |
646
|
|
|
public function setLimit($param) |
647
|
|
|
{ |
648
|
|
|
// @codeCoverageIgnoreStart |
649
|
|
|
if ($this->db->isDbSybase()) { |
650
|
|
|
// Sybase does not support LIMIT clause |
651
|
|
|
$this->paramPart['LIMIT'] = ''; |
652
|
|
|
$this->sqlPart['LIMIT'] = ''; |
653
|
|
|
|
654
|
|
|
} else { |
655
|
|
|
$this->paramPart['LIMIT'] = $param; |
656
|
|
|
$this->sqlPart['LIMIT'] = ' LIMIT ' . $this->genSqlArray($param); |
657
|
|
|
} |
658
|
|
|
// @codeCoverageIgnoreEnd |
659
|
|
|
|
660
|
|
|
return $this->sqlPart['LIMIT']; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Set and treat ORDER BY param |
666
|
|
|
* |
667
|
|
|
* @param mixed $param |
668
|
|
|
* @return string |
669
|
|
|
*/ |
670
|
|
View Code Duplication |
public function setOrderby($param) |
|
|
|
|
671
|
|
|
{ |
672
|
|
|
$this->paramPart['ORDERBY'] = $param; |
673
|
|
|
|
674
|
|
|
$this->sqlPart['ORDERBY'] = ' ORDER BY ' . $this->genSqlArray($param); |
675
|
|
|
|
676
|
|
|
return $this->sqlPart['ORDERBY']; |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
|
680
|
|
|
/** |
681
|
|
|
* Set and treat SELECT param |
682
|
|
|
* |
683
|
|
|
* @param mixed $param |
684
|
|
|
* @return string |
685
|
|
|
*/ |
686
|
|
View Code Duplication |
public function setSelect($param) |
|
|
|
|
687
|
|
|
{ |
688
|
|
|
$this->paramPart['SELECT'] = $param; |
689
|
|
|
|
690
|
|
|
$this->sqlPart['SELECT'] = 'SELECT ' . $this->genSqlArrayAs($param, true, true, true); |
691
|
|
|
|
692
|
|
|
return $this->sqlPart['SELECT']; |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* Set and treat SET param |
698
|
|
|
* |
699
|
|
|
* @param mixed $param |
700
|
|
|
* @return string |
701
|
|
|
*/ |
702
|
|
View Code Duplication |
public function setSet($param) |
|
|
|
|
703
|
|
|
{ |
704
|
|
|
$this->paramPart['SET'] = $param; |
705
|
|
|
|
706
|
|
|
// For UPDATE only, INSERT uses VALUES |
707
|
|
|
// User give param array(col => value) |
708
|
|
|
$this->sqlPart['SET'] = $this->genSqlArraySet($param); |
709
|
|
|
|
710
|
|
|
return $this->sqlPart['SET']; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
|
714
|
|
|
/** |
715
|
|
|
* Set and treat UPDATE param |
716
|
|
|
* |
717
|
|
|
* @param mixed $param |
718
|
|
|
* @return string |
719
|
|
|
*/ |
720
|
|
View Code Duplication |
public function setUpdate($param) |
|
|
|
|
721
|
|
|
{ |
722
|
|
|
$this->paramPart['UPDATE'] = $param; |
723
|
|
|
|
724
|
|
|
$this->sqlPart['UPDATE'] = 'UPDATE ' . $param; |
725
|
|
|
|
726
|
|
|
// Retrieve table schema, so SET can determine how to quote |
727
|
|
|
$this->db->getMetaColumn($param); |
728
|
|
|
|
729
|
|
|
return $this->sqlPart['UPDATE']; |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Set and treat VALUES param |
735
|
|
|
* |
736
|
|
|
* @param mixed $param |
737
|
|
|
* @return string |
738
|
|
|
*/ |
739
|
|
|
public function setValues($param) |
740
|
|
|
{ |
741
|
|
|
$this->paramPart['VALUES'] = $param; |
742
|
|
|
|
743
|
|
|
// For INSERT only, UPDATE uses SET |
744
|
|
|
// User give param array(col => value) |
745
|
|
|
$this->sqlPart['VALUES'] = $this->genSqlArrayValues($param); |
746
|
|
|
|
747
|
|
|
return $this->sqlPart['VALUES']; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
|
751
|
|
|
/** |
752
|
|
|
* Set and treat WHERE param |
753
|
|
|
* |
754
|
|
|
* @param mixed $param |
755
|
|
|
* @return string |
756
|
|
|
*/ |
757
|
|
View Code Duplication |
public function setWhere($param) |
|
|
|
|
758
|
|
|
{ |
759
|
|
|
$this->paramPart['WHERE'] = $param; |
760
|
|
|
|
761
|
|
|
// Add '(' to defend sql injection |
762
|
|
|
$this->sqlPart['WHERE'] = ' WHERE (' |
763
|
|
|
. $this->genSqlArray($param, ') AND (') . ')'; |
764
|
|
|
|
765
|
|
|
return $this->sqlPart['WHERE']; |
766
|
|
|
} |
767
|
|
|
} |
768
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.