1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Query builder API. |
4
|
|
|
* |
5
|
|
|
* @author RN Kushwaha <[email protected]> |
6
|
|
|
* @since v0.0.1 <Date: 12th April, 2019> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Dolphin\Mapper; |
10
|
|
|
|
11
|
|
|
use Dolphin\Connections\Connection; |
12
|
|
|
use Dolphin\Builders\QueryBuilder; |
13
|
|
|
use Dolphin\Builders\WhereQueryBuilder; |
14
|
|
|
use Dolphin\Builders\InsertQueryBuilder; |
15
|
|
|
use Dolphin\Parsers\WhereQueryParser; |
16
|
|
|
use Dolphin\Utils\Utils; |
17
|
|
|
use \Exception; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This class provides some nice features to interact with the Database |
21
|
|
|
* Elegant Query builder |
22
|
|
|
* Method Chaining |
23
|
|
|
* Prepared Statement using named parameter like status = :status |
24
|
|
|
* Raw Query Option |
25
|
|
|
* Join Clause |
26
|
|
|
* Where Clause |
27
|
|
|
* WhereRaw Clause |
28
|
|
|
* orWhere Clause [TODO] |
29
|
|
|
* WhereIn Clause |
30
|
|
|
* WhereNotIn Clause |
31
|
|
|
* WhereNull Clause |
32
|
|
|
* WhereNotNull Clause |
33
|
|
|
* GroupBy Clause |
34
|
|
|
* Having Clause |
35
|
|
|
* OrderBy Clause. |
36
|
|
|
* |
37
|
|
|
* Aggregations like |
38
|
|
|
* Count() |
39
|
|
|
* Max() [TODO] |
40
|
|
|
* Min() [TODO] |
41
|
|
|
* First() |
42
|
|
|
* Last() [TODO] |
43
|
|
|
* Avg() [TODO] |
44
|
|
|
* fetchColumn [TODO] |
45
|
|
|
* union() [TODO] |
46
|
|
|
* delete() |
47
|
|
|
* update() |
48
|
|
|
* insert() |
49
|
|
|
* truncate() |
50
|
|
|
* havingRaw() [TODO] |
51
|
|
|
* exists() [TODO] |
52
|
|
|
*/ |
53
|
|
|
class Dolphin |
54
|
|
|
{ |
55
|
|
|
protected $fields = array(); |
56
|
|
|
public $table; |
57
|
|
|
public $className; |
58
|
|
|
protected $groupBy; |
59
|
|
|
protected $orderBy; |
60
|
|
|
protected $having; |
61
|
|
|
protected $join = array(); |
62
|
|
|
protected $leftJoin = array(); |
63
|
|
|
protected $rightJoin = array(); |
64
|
|
|
protected $crossJoin = array(); |
65
|
|
|
protected $where = array(); |
66
|
|
|
protected $whereRaw = array(); |
67
|
|
|
protected $whereIn = array(); |
68
|
|
|
protected $whereNotIn = array(); |
69
|
|
|
protected $whereNull = array(); |
70
|
|
|
protected $whereNotNull = array(); |
71
|
|
|
protected $limit; |
72
|
|
|
protected $offset; |
73
|
|
|
protected $results; |
74
|
|
|
|
75
|
|
|
private function getFields(array $args, bool $quote = true){ |
76
|
|
|
$fldAr = array(); |
77
|
|
|
$qb = new QueryBuilder(); |
78
|
|
|
|
79
|
|
|
foreach ($args as $arg) { |
80
|
|
|
foreach (explode(',', $arg) as $ar) { |
81
|
|
|
$fldAr[] = ($quote === true) ? $qb->quote(trim($ar)) : trim($ar); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $fldAr; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function select() |
89
|
|
|
{ |
90
|
|
|
$args = func_get_args(); |
91
|
|
|
$fldAr = $this->getFields($args, true); |
92
|
|
|
$this->fields = array_merge($this->fields, $fldAr); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function selectRaw() |
98
|
|
|
{ |
99
|
|
|
$args = func_get_args(); |
100
|
|
|
$fldAr = $this->getFields($args, false); |
101
|
|
|
$this->fields = array_merge($this->fields, $fldAr); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function join($join, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
107
|
|
|
{ |
108
|
|
|
$this->join = array_merge($this->join, [[$join, $mixedParam, $param3, $param4, $mixedParam2]]); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function leftJoin($leftJoin, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
114
|
|
|
{ |
115
|
|
|
$this->leftJoin = array_merge($this->leftJoin, [[$leftJoin, $mixedParam, $param3, $param4, $mixedParam2]]); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function rightJoin($rightJoin, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
121
|
|
|
{ |
122
|
|
|
$this->rightJoin = array_merge($this->rightJoin, [[$rightJoin, $mixedParam, $param3, $param4, $mixedParam2]]); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function crossJoin($crossJoin, $params = null) |
128
|
|
|
{ |
129
|
|
|
$this->crossJoin = array_merge($this->crossJoin, [[$crossJoin, $params]]); |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @throws Exception |
136
|
|
|
*/ |
137
|
|
|
public function where() |
138
|
|
|
{ |
139
|
|
|
$args = func_get_args(); |
140
|
|
|
if(func_num_args()===2){ |
141
|
|
|
$this->where = array_merge($this->where, [[$args[0], '=', $args[1]]]); |
142
|
|
|
return $this; |
143
|
|
|
} elseif(func_num_args()===3){ |
144
|
|
|
$this->where = array_merge($this->where, [[$args[0], $args[1], $args[2]]]); |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
throw new Exception('Where parameter contains invalid number of parameters', 1); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function whereIn($whereIn, $params = array()) |
152
|
|
|
{ |
153
|
|
|
$this->whereIn = array_merge($this->whereIn, [[$whereIn, $params]]); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function whereNotIn($whereNotIn, $params = array()) |
159
|
|
|
{ |
160
|
|
|
$this->whereNotIn = array_merge($this->whereNotIn, [[$whereNotIn, $params]]); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function whereNull($whereNull) |
166
|
|
|
{ |
167
|
|
|
$this->whereNull = array_merge($this->whereNull, [$whereNull]); |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function whereNotNull($whereNotNull) |
173
|
|
|
{ |
174
|
|
|
$this->whereNotNull = array_merge($this->whereNotNull, [$whereNotNull]); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function whereRaw($whereConditions) |
180
|
|
|
{ |
181
|
|
|
$this->whereRaw = array_merge($this->whereRaw, [$whereConditions]); |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function offset($offset) |
187
|
|
|
{ |
188
|
|
|
$this->offset = $offset; |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function limit($limit) |
194
|
|
|
{ |
195
|
|
|
$this->limit = $limit; |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function orderBy($orderBy) |
201
|
|
|
{ |
202
|
|
|
$this->orderBy = $orderBy; |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function groupBy($groupBy) |
208
|
|
|
{ |
209
|
|
|
$this->groupBy = $groupBy; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function having($having) |
215
|
|
|
{ |
216
|
|
|
$this->having = $having; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Builds Query added by method chaining. |
223
|
|
|
* It has the main logic of ORM |
224
|
|
|
*/ |
225
|
|
|
protected function buildQuery() |
226
|
|
|
{ |
227
|
|
|
$qb = new QueryBuilder(); |
228
|
|
|
|
229
|
|
|
$query = $qb->buildQuery([ |
230
|
|
|
'table' => $this->table, |
231
|
|
|
'fields' => $this->fields, |
232
|
|
|
'join' => $this->join, |
233
|
|
|
'leftJoin' => $this->leftJoin, |
234
|
|
|
'rightJoin' => $this->rightJoin, |
235
|
|
|
'crossJoin' => $this->crossJoin, |
236
|
|
|
'where' => $this->where, |
237
|
|
|
'whereRaw' => $this->whereRaw, |
238
|
|
|
'whereIn' => $this->whereIn, |
239
|
|
|
'whereNotIn' => $this->whereNotIn, |
240
|
|
|
'whereNull' => $this->whereNull, |
241
|
|
|
'whereNotNull' => $this->whereNotNull, |
242
|
|
|
'groupBy' => $this->groupBy, |
243
|
|
|
'having' => $this->having, |
244
|
|
|
'orderBy' => $this->orderBy, |
245
|
|
|
'limit' => $this->limit, |
246
|
|
|
'offset' => $this->offset |
247
|
|
|
]); |
248
|
|
|
|
249
|
|
|
return join(' ', $query); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
protected function reset() |
253
|
|
|
{ |
254
|
|
|
$this->fields = array(); |
255
|
|
|
$this->table = null; |
256
|
|
|
$this->className = null; |
257
|
|
|
$this->groupBy = null; |
258
|
|
|
$this->orderBy = null; |
259
|
|
|
$this->having = null; |
260
|
|
|
$this->join = array(); |
261
|
|
|
$this->leftJoin = array(); |
262
|
|
|
$this->rightJoin = array(); |
263
|
|
|
$this->crossJoin = array(); |
264
|
|
|
$this->where = array(); |
265
|
|
|
$this->whereRaw = array(); |
266
|
|
|
$this->whereIn = array(); |
267
|
|
|
$this->whereNotIn = array(); |
268
|
|
|
$this->whereNull = array(); |
269
|
|
|
$this->whereNotNull = array(); |
270
|
|
|
$this->limit = null; |
271
|
|
|
$this->offset = null; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function prepare($query, $fetchRows = 'all') |
275
|
|
|
{ |
276
|
|
|
$qb = new QueryBuilder(); |
277
|
|
|
$wqp = new WhereQueryParser(); |
278
|
|
|
$util = new Utils(); |
279
|
|
|
$rows = null; |
280
|
|
|
|
281
|
|
|
try { |
282
|
|
|
$ar = $wqp->parseWhereQuery($this->where); |
283
|
|
|
$stmt = Connection::get()->prepare($qb->queryPrefix($query)); |
284
|
|
|
$stmt->execute($ar); |
285
|
|
|
|
286
|
|
|
if ($fetchRows == 'first') { |
287
|
|
|
$this->results = $stmt->fetch(\PDO::FETCH_OBJ); |
288
|
|
|
} else{ |
289
|
|
|
$this->results = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if(count($this->results) ){ |
293
|
|
|
// now turn this stdClass object to the object type of calling model |
294
|
|
|
$rows = $util->turnObjects($this->className, $this->results); |
295
|
|
|
} |
296
|
|
|
// Reset class variables |
297
|
|
|
$this->reset(); |
298
|
|
|
|
299
|
|
|
return $rows; |
300
|
|
|
} catch (\PDOException $ex) { |
301
|
|
|
throw new \PDOException($ex->getMessage(), 1); |
302
|
|
|
} catch (Exception $e) { |
303
|
|
|
throw new Exception($e->getMessage(), 1); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function query($query, $fetchRows = 'all') |
308
|
|
|
{ |
309
|
|
|
$qb = new QueryBuilder(); |
310
|
|
|
|
311
|
|
|
try { |
312
|
|
|
$obj = Connection::get()->query($qb->queryPrefix($query), \PDO::FETCH_OBJ); |
313
|
|
|
|
314
|
|
|
if ($fetchRows == 'count') { |
315
|
|
|
$data = $obj->fetchColumn(); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
// Reset class variables |
319
|
|
|
$this->reset(); |
320
|
|
|
|
321
|
|
|
return isset($data) ? $data : $obj; |
322
|
|
|
} catch (\PDOException $ex) { |
323
|
|
|
throw new \PDOException($ex->getMessage(), 1); |
324
|
|
|
} catch (Exception $e) { |
325
|
|
|
throw new Exception($e->getMessage(), 1); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function get() |
330
|
|
|
{ |
331
|
|
|
return $this->prepare($this->buildQuery()); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function first() |
335
|
|
|
{ |
336
|
|
|
$query = $this->buildQuery(); |
337
|
|
|
|
338
|
|
|
if (!strripos($query, 'LIMIT 1')) { |
339
|
|
|
$query .= ' LIMIT 1'; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
return $this->prepare($query, 'first'); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function all() |
346
|
|
|
{ |
347
|
|
|
$query = $this->buildQuery(); |
348
|
|
|
|
349
|
|
|
return $this->prepare($query); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* It fetches the row by primary key |
354
|
|
|
* |
355
|
|
|
* @since v0.0.5 |
356
|
|
|
*/ |
357
|
|
|
public function find($id) |
358
|
|
|
{ |
359
|
|
|
$this->where('id', $id); |
360
|
|
|
|
361
|
|
|
return $this->first(); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* It fetches the row by primary key |
366
|
|
|
* |
367
|
|
|
* @param int $id |
368
|
|
|
* @return object $row |
369
|
|
|
* @throws Exception |
370
|
|
|
* @since v0.0.5 |
371
|
|
|
*/ |
372
|
|
|
public function findOrFail($id) |
373
|
|
|
{ |
374
|
|
|
$this->where('id', $id); |
375
|
|
|
|
376
|
|
|
$row = $this->first(); |
377
|
|
|
|
378
|
|
|
if($row == null ){ |
379
|
|
|
throw new Exception("The record does not exists!"); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
return $row; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
public function count() |
386
|
|
|
{ |
387
|
|
|
$this->fields = null; |
388
|
|
|
$query = $this->buildQuery(); |
389
|
|
|
$query = str_replace('SELECT * ', 'SELECT COUNT(*) as count ', $query); |
390
|
|
|
|
391
|
|
|
return $this->query($query, 'count'); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* It truncates the table |
396
|
|
|
* |
397
|
|
|
* @return boolean |
398
|
|
|
* @throws Exception |
399
|
|
|
* @since v0.0.5 |
400
|
|
|
*/ |
401
|
|
|
public function truncate() |
402
|
|
|
{ |
403
|
|
|
$qb = new QueryBuilder(); |
404
|
|
|
$query = "TRUNCATE ".$this->table; |
405
|
|
|
|
406
|
|
|
try{ |
407
|
|
|
Connection::get()->query($qb->queryPrefix($query)); |
408
|
|
|
} catch(Exception $e){ |
409
|
|
|
throw new Exception($e->getMessage()); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
return true; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* It inserts the new rows |
417
|
|
|
* |
418
|
|
|
* @param array $rows |
419
|
|
|
* @return integer $lastInsertedId |
420
|
|
|
* @throws Exception |
421
|
|
|
* @since v0.0.5 |
422
|
|
|
*/ |
423
|
|
|
public function insert($rows) |
424
|
|
|
{ |
425
|
|
|
$iqb = new InsertQueryBuilder(); |
426
|
|
|
return $iqb->insert($rows, $this->table); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* It updates the rows |
431
|
|
|
* |
432
|
|
|
* @param array $row |
433
|
|
|
* @return boolean |
434
|
|
|
* @throws Exception |
435
|
|
|
* @since v0.0.5 |
436
|
|
|
*/ |
437
|
|
|
public function update($row) |
438
|
|
|
{ |
439
|
|
|
$qb = new QueryBuilder(); |
440
|
|
|
$wqb = new WhereQueryBuilder(); |
441
|
|
|
$query = "UPDATE ".$this->table." SET "; |
442
|
|
|
$ar = array(); |
443
|
|
|
|
444
|
|
|
foreach($row as $key => $val){ |
445
|
|
|
$ar[':'.$key] = $val; |
446
|
|
|
$query.= $qb->quote($key)." =:".$key.","; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
$query = rtrim($query, ","); |
450
|
|
|
|
451
|
|
|
try{ |
452
|
|
|
$whereQuery = $wqb->buildAllWhereQuery( |
453
|
|
|
$this->where, |
454
|
|
|
$this->whereRaw, |
455
|
|
|
$this->whereIn, |
456
|
|
|
$this->whereNotIn, |
457
|
|
|
$this->whereNull, |
458
|
|
|
$this->whereNotNull |
459
|
|
|
); |
460
|
|
|
$query.= " ".join(" ", $whereQuery); |
461
|
|
|
$stmt = Connection::get()->prepare($qb->queryPrefix($query)); |
462
|
|
|
$stmt->execute($ar); |
463
|
|
|
$this->reset(); |
464
|
|
|
} catch(Exception $e){ |
465
|
|
|
throw new Exception($e->getMessage()); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
return true; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* It deleted the rows matched by where clause |
473
|
|
|
* |
474
|
|
|
* @return boolean |
475
|
|
|
* @throws Exception |
476
|
|
|
* @since v0.0.5 |
477
|
|
|
*/ |
478
|
|
|
public function delete() |
479
|
|
|
{ |
480
|
|
|
$qb = new QueryBuilder(); |
481
|
|
|
$wqb = new WhereQueryBuilder(); |
482
|
|
|
$query = "DELETE FROM ".$this->table; |
483
|
|
|
|
484
|
|
|
try{ |
485
|
|
|
$whereQuery = $wqb->buildAllWhereQuery( |
486
|
|
|
$this->where, |
487
|
|
|
$this->whereRaw, |
488
|
|
|
$this->whereIn, |
489
|
|
|
$this->whereNotIn, |
490
|
|
|
$this->whereNull, |
491
|
|
|
$this->whereNotNull |
492
|
|
|
); |
493
|
|
|
$query.= " ".join(" ", $whereQuery); |
494
|
|
|
Connection::get()->query($qb->queryPrefix($query)); |
495
|
|
|
$this->reset(); |
496
|
|
|
} catch(Exception $e){ |
497
|
|
|
throw new Exception($e->getMessage()); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
return true; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
} |
504
|
|
|
|