-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.php
More file actions
908 lines (827 loc) · 22.5 KB
/
Query.php
File metadata and controls
908 lines (827 loc) · 22.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Database;
use Hubzero\Database\Syntax\Mysql;
use Hubzero\Database\Syntax\Sqlite;
use App;
/**
* Database query class
*
* @uses \Hubzero\Database\Row for results returned from queries
*/
class Query
{
/**
* The actual database connection object
*
* @var object
**/
private $connection = null;
/**
* The query syntax
*
* @var object
**/
protected $syntax = null;
/**
* The query results cache
*
* This is a key value dictionary of query md5 hash and query results.
*
* @var array
**/
private static $cache = array();
/**
* The database query type constants
**/
const ROW = 'loadObject';
const ROWS = 'loadObjectList';
const COLUMN = 'loadColumn';
/**
* The elements of a basic select statement
*
* @var array
**/
private $select = array(
'select',
'from',
'join',
'where',
'group',
'having',
'order',
'limit'
);
/**
* The elements of a basic insert statement
*
* @var array
**/
private $insert = array(
'insert',
'values'
);
/**
* The elements of a basic update statement
*
* @var array
**/
private $update = array(
'update',
'set',
'where'
);
/**
* The elements of a basic delete statement
*
* @var array
**/
private $delete = array(
'delete',
'where'
);
/**
* The query type to be performed
*
* This is a silly way of tracking what type of query we think
* we're going to execute. This is used by the execute method.
*
* @var string
**/
protected $type = null;
/**
* Constructs a new query instance
*
* @param object $connect The database connection to use in the query builder
* @return void
* @since 2.0.0
**/
public function __construct($connection = null)
{
$this->connection = $connection ?: App::get('db');
$this->reset();
}
/**
* Clones the query object, including its individual syntax elements
*
* We want to duplicate our syntax elements, as well as the overall query object,
* hence the need for this. Otherwise, PHP would only provide references to the
* syntax elements, which is counter productive in this instance.
*
* @return void
* @since 2.0.0
**/
public function __clone()
{
$this->syntax = clone $this->syntax;
}
/**
* Purges the query cache
*
* @return void
* @since 2.0.0
**/
public static function purgeCache()
{
self::$cache = array();
}
/**
* Empties a query clause of current values
*
* @param string $clause [select, update, insert, delete, from, join, set, values, where, group, having, order]
* @return $this
* @since 2.2.15
**/
public function clear($clause = '')
{
if (!$clause)
{
$this->reset();
}
else
{
$clause = 'reset' . ucfirst(strtolower($clause));
$this->syntax->$clause();
}
return $this;
}
/**
* Empties a query of current select values
*
* @return $this
* @since 2.2.2
**/
public function deselect()
{
$this->syntax->resetSelect();
return $this;
}
/**
* Applies a select field to the pending query
*
* @param string $column The column to select
* @param string $as What to call the return val
* @param bool $count Whether or not to count column
* @return $this
* @since 2.0.0
**/
public function select($column, $as = null, $count = false)
{
$this->syntax->setSelect($column, $as, $count);
$this->type = 'select';
return $this;
}
/**
* Applies an insert statement to the pending query
*
* @param string $table The table into which we will be inserting
* @param bool $ignore Whether or not to ignore errors produced related to things like duplicate keys
* @return $this
* @since 2.0.0
**/
public function insert($table, $ignore = false)
{
$this->syntax->setInsert($table, $ignore);
$this->type = 'insert';
return $this;
}
/**
* Applies an update statement to the pending query
*
* @param string $table The table whose fields will be updated
* @return $this
* @since 2.0.0
**/
public function update($table)
{
$this->syntax->setUpdate($table);
$this->type = 'update';
return $this;
}
/**
* Applies a delete statement to the pending query
*
* @param string $table The table whose row will be deleted
* @return $this
* @since 2.0.0
**/
public function delete($table)
{
$this->syntax->setDelete($table);
$this->type = 'delete';
return $this;
}
/**
* Defines the table from which data should be retrieved
*
* @param string $table The table of interest
* @param string $as What to call the table
* @return $this
**/
public function from($table, $as = null)
{
$this->syntax->setFrom($table, $as);
return $this;
}
/**
* Defines a table join to be performed for the query
*
* @param string $table The table join
* @param string $leftKey The left side of the join condition
* @param string $rightKey The right side of the join condition
* @param string $type The join type to perform
* @return $this
**/
public function join($table, $leftKey, $rightKey, $type = 'inner')
{
$this->syntax->setJoin($table, $leftKey, $rightKey, $type);
return $this;
}
/**
* Defines a table join to be performed for the query using a raw expression
*
* @param string $table The table join
* @param string $raw The join clause (anything after the ON keyword)
* @param string $type The join type to perform
* @return $this
**/
public function joinRaw($table, $raw, $type = 'inner')
{
$this->syntax->setRawJoin($table, $raw, $type);
return $this;
}
/**
* Defines a table INNER join to be performed for the query
*
* @param string $table The table join
* @param string $leftKey The left side of the join condition
* @param string $rightKey The right side of the join condition
* @return $this
**/
public function innerJoin($table, $leftKey, $rightKey)
{
$this->syntax->setJoin($table, $leftKey, $rightKey, 'inner');
return $this;
}
/**
* Defines a table FULL OUTER join to be performed for the query
*
* @param string $table The table join
* @param string $leftKey The left side of the join condition
* @param string $rightKey The right side of the join condition
* @return $this
**/
public function fullJoin($table, $leftKey, $rightKey)
{
$this->syntax->setJoin($table, $leftKey, $rightKey, 'full');
return $this;
}
/**
* Defines a table LEFT join to be performed for the query
*
* @param string $table The table join
* @param string $leftKey The left side of the join condition
* @param string $rightKey The right side of the join condition
* @return $this
**/
public function leftJoin($table, $leftKey, $rightKey)
{
$this->syntax->setJoin($table, $leftKey, $rightKey, 'left');
return $this;
}
/**
* Defines a table RIGHT join to be performed for the query
*
* @param string $table The table join
* @param string $leftKey The left side of the join condition
* @param string $rightKey The right side of the join condition
* @return $this
**/
public function rightJoin($table, $leftKey, $rightKey)
{
$this->syntax->setJoin($table, $leftKey, $rightKey, 'right');
return $this;
}
/**
* Applies a where clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param string $operator The operation that will compare column to value
* @param string $value The value to which the column will be evaluated
* @param string $logical The operator between multiple clauses
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function where($column, $operator, $value, $logical = 'and', $depth = 0)
{
$this->syntax->setWhere($column, $operator, $value, $logical, $depth);
return $this;
}
/**
* Applies a where clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param string $operator The operation that will compare column to value
* @param string $value The value to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function orWhere($column, $operator, $value, $depth = 0)
{
$this->where($column, $operator, $value, 'or', $depth);
return $this;
}
/**
* Applies a simple where equals clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param string $value The value to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function whereEquals($column, $value, $depth = 0)
{
$this->where($column, '=', $value, 'and', $depth);
return $this;
}
/**
* Applies a simple where equals clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param string $value The value to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function orWhereEquals($column, $value, $depth = 0)
{
$this->where($column, '=', $value, 'or', $depth);
return $this;
}
/**
* Applies a simple where in clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param array $value The values to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function whereIn($column, $values, $depth = 0)
{
$this->where($column, 'IN', $values, 'and', $depth);
return $this;
}
/**
* Applies a simple where in clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param array $value The values to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function orWhereIn($column, $values, $depth = 0)
{
$this->where($column, 'IN', $values, 'or', $depth);
return $this;
}
/**
* Applies a simple where not in clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param array $value The values to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function whereNotIn($column, $values, $depth = 0)
{
$this->where($column, 'NOT IN', $values, 'and', $depth);
return $this;
}
/**
* Applies a simple where not in clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param array $value The values to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function orWhereNotIn($column, $values, $depth = 0)
{
$this->where($column, 'NOT IN', $values, 'or', $depth);
return $this;
}
/**
* Applies a simple where like clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param string $value The value to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.1.0
**/
public function whereLike($column, $value, $depth = 0)
{
$this->where($column, 'LIKE', "%{$value}%", 'and', $depth);
return $this;
}
/**
* Applies a simple where like clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param string $value The value to which the column will be evaluated
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.1.0
**/
public function orWhereLike($column, $value, $depth = 0)
{
$this->where($column, 'LIKE', "%{$value}%", 'or', $depth);
return $this;
}
/**
* Applies an AND where is null clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.2.15
**/
public function whereIsNull($column, $depth = 0)
{
$this->where($column, 'IS', null, 'and', $depth);
return $this;
}
/**
* Applies a OR where is null clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.2.15
**/
public function orWhereIsNull($column, $depth = 0)
{
$this->where($column, 'IS', null, 'or', $depth);
return $this;
}
/**
* Applies an AND where is not null clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.2.15
**/
public function whereIsNotNull($column, $depth = 0)
{
$this->where($column, 'IS NOT', null, 'and', $depth);
return $this;
}
/**
* Applies a OR where is not null clause to the pending query
*
* @param string $column The column to which the clause will apply
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.2.15
**/
public function orWhereIsNotNull($column, $depth = 0)
{
$this->where($column, 'IS NOT', null, 'or', $depth);
return $this;
}
/**
* Applies a raw where clause to the pending query
*
* @param string $string The raw where clause
* @param array $bindings Any bindings to apply to the where clause
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function whereRaw($string, $bindings = [], $depth = 0)
{
$this->syntax->setRawWhere($string, $bindings, 'and', $depth);
return $this;
}
/**
* Applies a raw where clause to the pending query
*
* @param string $string The raw where clause
* @param array $bindings Any bindings to apply to the where clause
* @param int $depth The depth level of the clause, for sub clauses
* @return $this
* @since 2.0.0
**/
public function orWhereRaw($string, $bindings = [], $depth = 0)
{
$this->syntax->setRawWhere($string, $bindings, 'or', $depth);
return $this;
}
/**
* Resets the depth of a nested statement back down to a given level
*
* @param int $depth The depth to set to
* @return $this
* @since 2.1.0
**/
public function resetDepth($depth = 0)
{
$this->syntax->resetDepth($depth);
return $this;
}
/**
* Applies 'order by' clause
*
* @param string $column The column to which the order by will apply
* @param string $dir The direction in which the results will be ordered
* @return $this
* @since 2.0.0
**/
public function order($column, $dir)
{
$this->syntax->setOrder($column, $dir);
return $this;
}
/**
* Removes 'order by' clause
*
* @return $this
* @since 2.0.0
**/
public function unorder()
{
$this->syntax->resetOrder();
return $this;
}
/**
* Sets query offset to start at a certain position
*
* @param int $start Position to start from
* @return $this
* @since 2.0.0
**/
public function start($start)
{
$this->syntax->setStart((int)$start);
return $this;
}
/**
* Limits query results returned to a certain number
*
* @param int $limit Number of results to return on next query
* @return $this
* @since 2.0.0
**/
public function limit($limit)
{
$this->syntax->setLimit((int)$limit);
return $this;
}
/**
* Sets the values to be inserted into the database
*
* @param array $data The data to be inserted
* @return $this
* @since 2.0.0
**/
public function values($data)
{
$this->syntax->setValues($data);
return $this;
}
/**
* Sets the values to be modified in the database
*
* @param array $data The data to be modified
* @return $this
* @since 2.0.0
**/
public function set($data)
{
$this->syntax->setSet($data);
return $this;
}
/**
* Sets the group by element on the query
*
* @param string $column The column on which to apply the group by
* @return $this
* @since 2.0.0
**/
public function group($column)
{
$this->syntax->setGroup($column);
return $this;
}
/**
* Sets the having element on the query
*
* @param string $column The column to which the clause will apply
* @param string $operator The operation that will compare column to value
* @param string $value The value to which the column will be evaluated
* @return $this
* @since 2.0.0
**/
public function having($column, $operator, $value)
{
$this->syntax->setHaving($column, $operator, $value);
return $this;
}
/**
* Retrieves all applicable data
*
* @FIXME: this could result in slightly odd behavior if you call the same query
* twice, but for some reason want differing structures of the returned data.
*
* @param string $structure The structure of the item(s) returned (if applicable)
* @param bool $noCache Whether or not to check cache for results
* @return $this
* @since 2.0.0
**/
public function fetch($structure = 'rows', $noCache = false)
{
// Build and hash query
$query = $this->buildQuery();
$key = hash('md5', $structure . $query . serialize($this->syntax->getBindings()));
// Check cache for results first
if ($noCache || !isset(self::$cache[$key]))
{
self::$cache[$key] = $this->query($query, $structure);
}
// Clear elements
$this->reset();
return self::$cache[$key];
}
/**
* Inserts a new row using data provided into given table
*
* @param string $table The table name into which the data should be inserted
* @param array $data An associative array of data to insert
* @param bool $ignore Whether or not to perform an insert ignore
* @return bool|int
* @since 2.0.0
**/
public function push($table, $data, $ignore = false)
{
// Add insert statement
$this->insert($table, $ignore)
->values($data);
$result = $this->execute();
// Return the inserted data
return !$result ?: $this->connection->insertid();
}
/**
* Updates an existing item in the database using the provided data
*
* @param string $table The table to update
* @param string $pkField The table field serving as primary key
* @param mixed $pkValue The primary key value
* @param array $data The data to update in the database
* @return bool
* @since 2.0.0
**/
public function alter($table, $pkField, $pkValue, $data)
{
// Add insert statement
$this->update($table)
->set($data);
// Where primary key is...
$this->whereEquals($pkField, $pkValue);
// Return the result of the query
return $this->execute();
}
/**
* Removes a record by its primary key
*
* @param string $table The table to update
* @param string $pkField The table field serving as primary key
* @param mixed $pkValue The primary key value
* @return bool
* @since 2.0.0
**/
public function remove($table, $pkField, $pkValue)
{
// Make sure we have an id (i.e. don't delete everything in the table!)
if (is_null($pkValue) || empty($pkValue))
{
return false;
}
// Add delete statement
$this->delete($table)
->whereEquals($pkField, $pkValue);
// Return result of the query
return $this->execute();
}
/**
* Builds and executes the current query based on the elements present
*
* This is a fairly 'dumb' function, in that it just looks for whichever type was
* most recently set by one of the primary functions (select, insert, update, delete).
* Fetch should still be used for select queries as it offers result caching.
*
* @FIXME: maybe this should be combined with fetch?
*
* @return mixed
* @since 2.0.0
**/
public function execute()
{
$result = $this->query($this->buildQuery($this->type));
// Clear elements
$this->reset();
// Return result of the query
return $result;
}
/**
* Performs the actual query and returns the results
*
* @param string $query The query to perform
* @param string $structure The structure of the item(s) returned (if applicable)
* @return mixed
* @since 2.0.0
**/
public function query($query, $structure = null)
{
// Check the type of query to decide what to return
list($type) = explode(' ', $query, 2);
$type = strtolower($type);
// Default structure if needed
if ($type == 'select' && is_null($structure))
{
$structure = 'rows';
}
$this->connection->prepare($query)->bind($this->syntax->getBindings());
$result = (isset($structure))
? $this->connection->{constant('self::' . strtoupper($structure))}()
: $this->connection->query();
return $result;
}
/**
* Retrieves the current query as a string (without executing it)
*
* @return string
* @since 2.0.0
**/
public function toString()
{
return $this->connection
->prepare($this->buildQuery($this->type))
->bind($this->syntax->getBindings())
->toString();
}
/**
* Retrieves the current query as a string (without executing it)
*
* @return string
* @since 2.1.9
**/
public function __toString()
{
return $this->toString();
}
/**
* Builds query based on the current query elements established
*
* @param string $type The type of query to build
* @return string
* @since 2.0.0
**/
private function buildQuery($type = 'select')
{
$pieces = array();
// Loop through query elements
foreach ($this->$type as $piece)
{
// If we have one of these elements, get its string value
if ($element = $this->syntax->build($piece))
{
$pieces[] = $element;
}
}
return implode("\n", $pieces);
}
/**
* Resets the query elements
*
* @return void
* @since 2.0.0
**/
private function reset()
{
// Reset the syntax element
$syntax = '\\Hubzero\\Database\\Syntax\\' . ucfirst($this->connection->getSyntax());
$this->syntax = new $syntax($this->connection);
}
}