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