-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql_builder.php
508 lines (417 loc) · 12.6 KB
/
sql_builder.php
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
/**
* SQLBuilder Class
*
* @category SQL Query Statements Auto Generation
* @package SQLBuilder
* @author Emmanuel O. Sobande <eosobande@gmail.com>
* @copyright Copyright (c) 2016
* @link http://github.com/eosobande/pdo-database-class
* @version 1.0
*/
class SQLBuilder {
/**
* SQL KEYWORDS
* @var array
*/
private $keywords = [
'group_by' => ' GROUP BY ',
'order_by' => ' ORDER BY ',
'limit' => ' LIMIT ',
'offset' => ' OFFSET '
];
/**
* Table Join Types
* @var array
*/
private $join_types = ['LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'];
/**
* SQL Query string to be executed
* @var string
*/
protected $sql;
/**
* Variable that holds the where statement
* @var array
*/
private $where;
/**
* Variable that holds the having statement
* @var array
*/
private $having;
/**
* Union
* @var array
*/
private $union;
/**
* Var holds the typr of query to be executed
* @var string
*/
private $query_type;
/**
* Variable which allows query values to be quoted automatically
* @var boolean
*/
protected $auto_quote_values = TRUE;
/**
* Get $sql
* @return string
*/
public function get_sql() {
return $this->sql;
}
/**
* Unset $sql
* @return void
*/
protected function unset_sql() {
$this->sql = NULL;
}
/**
* Method sets @var SQLBuilder::auto_quote_values
*
* @param boolean $query_type
* @return void
*/
public function auto_quote_values($bool) {
$this->auto_quote_values = (bool) $bool;
}
/**
* Method sets @var $query_type
*
* @param string $query_type
* @return void
*/
private function set_query_type($query_type) {
$this->query_type = $query_type;
}
/**
* Run raw SQL Query
*
* @param string $sql @var holds SQL statement
*
* @return int
*/
public function raw_query($sql) {
$this->set_query_type('RAW');
if (is_object($sql) && get_class($sql) === __CLASS__) {
$this->sql .= $sql->get_sql();
} else {
$this->sql .= $sql;
}
}
/**
* Instantiate object for sub queries
*
* @return SQLBuilder Objeect
*/
public function sub_query() {
return new self;
}
/**
* COMPILE the TABLES for the statement
*
* @param array|string $table_names @var that holds the name(s) of the table(s)
*
* @return void
*/
private function table_clause(&$table_names) {
if (is_array($table_names)) {
// i.e selecting from multiple tables
foreach ($table_names as $table) {
if (is_array($table)) {
// alias
$clause = !empty($table['as']) ? "{$table[0]} AS {$table['as']}" : $table[0];
// the table has join
// [ 0=>table name, join => join type, on => on statement ]
if (!empty($table['join']) && in_array(strtoupper($table['join']), $this->join_types) && !empty($table['on'])) {
$clause = strtoupper($table['join']). ' JOIN ' . $clause . ' ON ' . $table['on'];
}
$joins[] = $clause;
} else {
$joins[] = $table;
}
}
}
// convert array to text seperated by commas if $joins is not empty
$this->sql .= empty($joins) ? $table_names : implode(' ', $joins);
}
/**
* Create a VIEW
*
* @param string $view_name @var contains name of the VIEW
* @param boolean $replace @var determining whether to use 'OR REPLACE' keyword
*
* @return void
*/
public function create_view($view_name, $replace=FALSE) {
$this->sql .= "CREATE " . ($replace ? 'OR REPLACE ' : NULL) . "VIEW $view_name AS ";
}
/**
* DROP a Database|Table|View
*
* @param string $name @var that holds the name of the DATABASE|TABLE|VIEW
* @param string $type @var containing what type of object to DROP
*
* @return void
*/
public function drop($name, $type='table') {
if (in_array(strtolower($type), ['database', 'table', 'view'])) {
$this->sql .= "DROP " . strtoupper($type) . " $name";
} else {
throw new Exception('invalid type passed as parameter 2', 1);
}
}
/**
* Build SQL INSERT Query
*
* @param boolean $ignore @var to determine whether to ignore on duplicate
* @param array|string $table_name @var that holds the table to insert into
* @param string $columns @var of column(s) to insert values into
* @param array|string $values @var of value(s) to insert into the column(s)
*
* @return void
*/
public function insert($table_name, $values, $columns=NULL, $ignore=FALSE) {
$this->set_query_type('INSERT');
$this->sql .= 'INSERT ' . ($ignore ? 'IGNORE ' : NULL) . "INTO $table_name";
$this->sql .= $this->add_parenthesis(is_array($columns) ? implode(',', $columns) : $columns);
$this->sql .= ' VALUES ' . $this->add_parenthesis($this->quote_values($values));
}
/**
* Build SQL SELECT Query
*
* @param boolean $distinct @var to determine whether to select only unique fields
* @param array|string $columns @var of column(s) to be selected
* @param array|string $table_names @var that holds the table(s) to select data from
* @param array $keywords @var containing SQL KEYWORDS ( [group, having, order, limitL, offset] )
*
* @return void
*/
public function select($table_names, $columns='*', $keywords=[], $distinct=FALSE) {
if (!$this->union) {
$this->unset_sql();
}
$this->set_query_type('SELECT');
$this->sql .= 'SELECT ' . ($distinct ? 'DISTINCT ' : NULL);
$this->sql .= (is_array($columns) ? implode(',', $columns) : $columns) . ' FROM ';
$this->table_clause($table_names);
$this->build_rest_of_sql($keywords);
if ($this->union) {
$this->union = NULL;
}
}
/**
* Build SQL UPDATE Query
*
* @param array|string $table_names @var that holds the table(s) to update
* @param array|string $set @var containing the SET clause
* @param int $limit @var of maximum number of columns to update
*
* @return void
*/
public function update($table_names, $set, $limit=NULL) {
$this->set_query_type('UPDATE');
$this->sql .= 'UPDATE ';
$this->table_clause($table_names);
$this->sql .= ' SET ' . $this->set_clause($set);
$this->build_rest_of_sql(['limit'=>$limit]);
}
/**
* Build SET clause
*
* @param array|string $set
*
* @return string
*/
private function set_clause(&$set) {
if (is_array($set)) {
// loop through the array keys
foreach ($set as $column => $value) {
// quote the values
$set[$column] = "$column=" . $this->quote_values($value);
}
// return as text seperated by commas
return implode(',', $set);
}
// return as it is, (string)
return trim($set);
}
/**
* Build SQL DELETE Query
*
* @param string $table_name @var that holds the table to DELETE FROM
* @param int $limit @var of maximum number of columns to delete
*
* @return void
*/
public function delete($table_name, $limit=NULL) {
$this->set_query_type('DELETE');
$this->sql .= 'DELETE FROM ' . $table_name;
$this->build_rest_of_sql(['limit'=>$limit]);
}
/**
* TRUNCATE a table
*
* @param string $view_name @var that holds the name of the table
*
* @return void
*/
public function truncate($table_name) {
$this->delete($table_name);
}
/**
* Method bulds the remaining parts of the SQL Query
*
* @param array $keywords i.e GROUP BY|ORDER BY|LIMIT|OFFSET
*
* @return void
*/
private function build_rest_of_sql($keywords) {
$this->build_clause('WHERE', $this->where);
$this->build_keywords('group_by', $keywords);
$this->build_clause('HAVING', $this->having);
$this->build_keywords('order_by', $keywords);
$this->build_keywords('limit', $keywords);
$this->build_keywords('offset', $keywords);
}
/**
* SET the KEYWORDS for an SQL query
*
* @param string $key
* @param array $data
*
* @return void
*/
private function build_keywords($key, &$data) {
if (!empty($data[$key])) {
$this->sql .= $this->keywords[$key] . ( is_array($data[$key]) ? implode(', ', $data[$key]) : $data[$key] );
}
}
/**
* Method sets the WHERE variable to the argumentsb passed
*
* @param array|string
*
* @return void
*/
public function where() {
$this->where = func_get_args();
}
/**
* Method sets the HAVING variable to the argumentsb passed
*
* @param array|string
*
* @return void
*/
public function having() {
$this->having = func_get_args();
}
/**
* Union
* @return void
*/
public function union($union=FALSE) {
if (in_array($union, [NULL, FALSE])) {
$this->sql .= $this->union = ' UNION ';
} elseif (in_array($union, [TRUE, 'all'])) {
$this->sql .= $this->union = ' UNION ALL ';
} else {
throw new Exception(__METHOD__."expects parameter 1 to be boolean or 'all'", 1);
}
return $this;
}
/**
* Build WHERE|HAVING clause
*
* @param string $clause
* @param array $data
*
* @return void
*/
private function build_clause($clause, &$data) {
if (is_null($data)) {
return;
}
foreach ($data as $key => $param) {
// HAVING $data = [ 0=>column_name|aggregate_function(column_name), 1=>value, 2=> operator, 3=>and/or operator ]
// WHERE $data = [ 0=>column_name, 1=>value, 2=>operator, 3=>and/or operator ]
if (is_array($param)) {
if ($key > 0) {
if (empty($param[3])) {
$data[$key] = 'AND';
} elseif (in_array(strtoupper($param[3]), ['AND', 'OR'])) {
$data[$key] = strtoupper($param[3]);
} else {
throw new Exception(__METHOD__." expects array key 3 of each parameter to be 'AND', 'OR' or NULL", 1);
}
} else {
$data[$key] = ' ' . $clause;
}
$operator = empty($param[2]) ? '=' : trim(strtoupper($param[2]));
$data[$key] .= ' ' . $param[0] . " $operator ";
switch ($operator) {
case 'BETWEEN':
case 'NOT BETWEEN':
$data[$key] .= implode(' AND ', $this->quote_values($param[1], 'array'));
break;
case 'IN':
case 'NOT IN':
$data[$key] .= $this->add_parenthesis($this->quote_values($param[1]));
break;
case 'IS NULL':
break;
default:
$data[$key] .= $this->quote_values($param[1]);
break;
}
} elseif (is_string($param)) {
$this->where[$key] = ' WHERE ' . $param;
}
}
$this->sql .= implode(' ', $data);
$data = NULL;
}
/**
* Enclose @var in parenthesis
*
* @param string|int $value
* @return string
*/
private function add_parenthesis($value) {
return $value ? "($value)" : NULL;
}
/**
* Surround @var values in single quotes ''
*
* @param array|string $values
* @param string $return_type
*
* @return string|array
*/
private function quote_values($values, $return_type='string') {
if (is_object($values) && get_class($values) === __CLASS__) {
return $this->add_parenthesis($values->get_sql());
} elseif (!is_array($values)) {
$values = [$values];
}
foreach ($values as $key => $value) {
if (is_object($value) && get_class($value) === __CLASS__) {
$values[$key] = $value->get_sql();
} elseif ($this->auto_quote_values) {
$values[$key] = $value == '?' || is_int($value) ? $value : "'$value'";
}
}
switch ($return_type) {
case 'array':
return $values;
break;
default:
return implode(',', $values);
break;
}
}
}