/
Query.php
1225 lines (1046 loc) · 28.4 KB
/
Query.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
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* DataTables PHP libraries.
*
* PHP libraries for DataTables and DataTables Editor, utilising PHP 5.3+.
*
* @author SpryMedia
* @copyright 2012 SpryMedia ( http://sprymedia.co.uk )
* @license http://editor.datatables.net/license DataTables Editor
* @link http://editor.datatables.net
*/
namespace DataTables\Database;
if (!defined('DATATABLES')) exit();
use
DataTables,
DataTables\Database,
DataTables\Database\Query,
DataTables\Database\Result;
//
// This is a stub class that a driver must extend and complete
//
/**
* Perform an individual query on the database.
*
* The typical pattern for using this class is through the {@see
* \DataTables\Database->query()} method (and it's 'select', etc short-cuts).
* Typically it would not be initialised directly.
*
* Note that this is a stub class that a driver will extend and complete as
* required for individual database types. Individual drivers could add
* additional methods, but this is discouraged to ensure that the API is the
* same for all database types.
*/
abstract class Query {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constructor
*/
/**
* Query instance constructor.
*
* Note that typically instances of this class will be automatically created
* through the {@see \DataTables\Database->query()} method.
* @param Database $db Database instance
* @param string $type Query type - 'select', 'insert', 'update' or 'delete'
* @param string|string[] $table Tables to operate on - see {@see Query->table()}.
*/
public function __construct( $dbHost, $type, $table=null )
{
$this->_dbHost = $dbHost;
$this->_type = $type;
$this->table( $table );
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Private properties
*/
/**
* @var string Driver to use
* @internal
*/
protected $_type = "";
/**
* @var array
* @internal
*/
protected $_table = array();
/**
* @var array
* @internal
*/
protected $_field = array();
/**
* @var array
* @internal
*/
protected $_bindings = array();
/**
* @var array
* @internal
*/
protected $_where = array();
/**
* @var array
* @internal
*/
protected $_join = array();
/**
* @var array
* @internal
*/
protected $_order = array();
/**
* @var array
* @internal
*/
protected $_noBind = array();
/**
* @var int
* @internal
*/
protected $_limit = null;
/**
* @var int
* @internal
*/
protected $_group_by = null;
/**
* @var int
* @internal
*/
protected $_offset = null;
/**
* @var string
* @internal
*/
protected $_distinct = false;
/**
* @var string
* @internal
*/
protected $_identifier_limiter = array( '`', '`' );
/**
* @var string
* @internal
*/
protected $_field_quote = '\'';
/**
* @var array
* @internal
*/
protected $_pkey = null;
protected $_supportsAsAlias = true;
protected $_whereInCnt = 1;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Static methods
*/
/**
* Commit a transaction.
* @param \PDO $dbh The Database handle (typically a PDO object, but not always).
*/
public static function commit ( $dbh )
{
$dbh->commit();
}
/**
* Database connection - override by the database driver.
* @param string|array $user User name or all parameters in an array
* @param string $pass Password
* @param string $host Host name
* @param string $db Database name
* @return Query
*/
abstract public static function connect ( $user, $pass='', $host='', $port='', $db='', $dsn='' );
/**
* Start a database transaction
* @param \PDO $dbh The Database handle (typically a PDO object, but not always).
*/
public static function transaction ( $dbh )
{
$dbh->beginTransaction();
}
/**
* Rollback the database state to the start of the transaction.
* @param \PDO $dbh The Database handle (typically a PDO object, but not always).
*/
public static function rollback ( $dbh )
{
$dbh->rollBack();
}
/**
* Common helper for the drivers to handle a PDO DSN postfix
* @param string $dsn DSN postfix to use
* @return Query
* @internal
*/
static function dsnPostfix ( $dsn )
{
if ( ! $dsn ) {
return '';
}
// Add a DSN field separator if not given
if ( strpos( $dsn, ';' ) !== 0 ) {
return ';'.$dsn;
}
return $dsn;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
*/
/**
* Safely bind an input value to a parameter. This is evaluated when the
* query is executed. This allows user input to be safely executed without
* risk of an SQL injection attack.
*
* @param string $name Parameter name. This should include a leading colon
* @param string $value Value to bind
* @param mixed $type Data type. See the PHP PDO documentation:
* http://php.net/manual/en/pdo.constants.php
* @return Query
*/
public function bind ( $name, $value, $type=null )
{
$this->_bindings[] = array(
"name" => $this->_safe_bind( $name ),
"value" => $value,
"type" => $type
);
return $this;
}
/**
* Get the Database host for this query instance
* @return DataTable Database class instance
*/
public function database ()
{
return $this->_dbHost;
}
/**
* Set a distinct flag for a `select` query. Note that this has no effect on
* any of the other query types.
* @param boolean $dis Optional
* @return Query
*/
public function distinct ( $dis )
{
$this->_distinct = $dis;
return $this;
}
/**
* Execute the query.
* @param string $sql SQL string to execute (only if _type is 'raw').
* @return Result
*/
public function exec ( $sql=null )
{
$type = strtolower( $this->_type );
if ( $type === 'select' ) {
return $this->_select();
}
else if ( $type === 'insert' ) {
return $this->_insert();
}
else if ( $type === 'update' ) {
return $this->_update();
}
else if ( $type === 'delete' ) {
return $this->_delete();
}
else if ( $type === 'count' ) {
return $this->_count();
}
else if ( $type === 'raw' ) {
return $this->_raw( $sql );
}
throw new \Exception("Unknown database command or not supported: ".$type, 1);
}
/**
* Get fields.
* @param string|string[] $get,... Fields to get - can be specified as
* individual fields or an array of fields.
* @return self
*/
public function get ( $get )
{
if ( $get === null ) {
return $this;
}
$args = func_get_args();
for ( $i=0 ; $i<count($args) ; $i++ ) {
// If argument is an array then we loop over and add each using a
// recursive call
if ( is_array( $args[$i] ) ) {
for ( $j=0 ; $j<count($args[$i]) ; $j++ ) {
$this->get( $args[$i][$j] );
}
}
else if ( strpos($args[$i], ',') !== false && strpos($args[$i], '(') === false) {
// Comma delimited set of fields - legacy. Recommended that fields be split into
// an array on input
$a = explode(',', $args[$i]);
for ( $j=0 ; $j<count($a) ; $j++ ) {
$this->get( $a[$j] );
}
}
else {
$this->_field[] = trim( $args[$i] );
}
}
return $this;
}
/**
* Perform a JOIN operation
* @param string $table Table name to do the JOIN on
* @param string $condition JOIN condition
* @param string $type JOIN type
* @return self
*/
public function join ( $table, $condition, $type='', $bind=true )
{
// Tidy and check we know what the join type is
if ($type !== '') {
$type = strtoupper(trim($type));
if ( ! in_array($type, array('LEFT', 'RIGHT', 'INNER', 'OUTER', 'LEFT OUTER', 'RIGHT OUTER'))) {
$type = '';
}
}
// Protect the identifiers
if ($bind && preg_match('/([\w\.]+)([\W\s]+)(.+)/', $condition, $match))
{
$match[1] = $this->_protect_identifiers( $match[1] );
$match[3] = $this->_protect_identifiers( $match[3] );
$condition = $match[1].$match[2].$match[3];
}
$this->_join[] = $type .' JOIN '. $this->_protect_identifiers($table) .' ON '. $condition .' ';
return $this;
}
/**
* Limit the result set to a certain size.
* @param int $lim The number of records to limit the result to.
* @return self
*/
public function limit ( $lim )
{
$this->_limit = $lim;
return $this;
}
/**
* Group the results by the values in a field
* @param string The field of which the values are to be grouped
* @return self
*/
public function group_by ( $group_by )
{
$this->_group_by = $group_by;
return $this;
}
/**
* Get / set the primary key column name(s) so they can be easily returned
* after an insert.
* @param string[] $pkey Primary keys
* @return Query|string[]
*/
public function pkey ( $pkey=null )
{
if ( $pkey === null ) {
return $this->_pkey;
}
$this->_pkey = $pkey;
return $this;
}
/**
* Set table(s) to perform the query on.
* @param string|string[] $table,... Table(s) to use - can be specified as
* individual names, an array of names, a string of comma separated
* names or any combination of those.
* @return self
*/
public function table ( $table )
{
if ( $table === null ) {
return $this;
}
if ( is_array($table) ) {
// Array so loop internally
for ( $i=0 ; $i<count($table) ; $i++ ) {
$this->table( $table[$i] );
}
}
else {
// String based, explode for multiple tables
$tables = explode(",", $table);
for ( $i=0 ; $i<count($tables) ; $i++ ) {
$this->_table[] = $this->_protect_identifiers( trim($tables[$i]) );
}
}
return $this;
}
/**
* Offset the return set by a given number of records (useful for paging).
* @param int $off The number of records to offset the result by.
* @return self
*/
public function offset ( $off )
{
$this->_offset = $off;
return $this;
}
/**
* Order by
* @param string|string[] $order Columns and direction to order by - can
* be specified as individual names, an array of names, a string of comma
* separated names or any combination of those.
* @return self
*/
public function order ( $order )
{
if ( $order === null ) {
return $this;
}
if ( !is_array($order) ) {
$order = preg_split('/\,(?![^\(]*\))/',$order);
}
for ( $i=0 ; $i<count($order) ; $i++ ) {
// Simplify the white-space
$order[$i] = trim( preg_replace('/[\t ]+/', ' ', $order[$i]) );
// Find the identifier so we don't escape that
if ( strpos($order[$i], ' ') !== false ) {
$direction = strstr($order[$i], ' ');
$identifier = substr($order[$i], 0, - strlen($direction));
}
else {
$direction = '';
$identifier = $order[$i];
}
$this->_order[] = $this->_protect_identifiers( $identifier ).' '.$direction;
}
return $this;
}
/**
* Set fields to a given value.
*
* Can be used in two different ways, as set( field, value ) or as an array of
* fields to set: set( array( 'fieldName' => 'value', ...) );
* @param string|string[] $set Can be given as a single string, when then $val
* must be set, or as an array of key/value pairs to be set.
* @param string $val When $set is given as a simple string, $set is the field
* name and this is the field's value.
* @param boolean $bind Should the value be bound or not
* @return self
*/
public function set ( $set, $val=null, $bind=true )
{
if ( $set === null ) {
return $this;
}
if ( !is_array($set) ) {
$set = array( $set => $val );
}
foreach ($set as $key => $value) {
$this->_field[] = $key;
if ( $bind ) {
$this->bind( ':'.$key, $value );
}
else {
$this->_noBind[$key] = $value;
}
}
return $this;
}
/**
* Where query - multiple conditions are bound as ANDs.
*
* Can be used in two different ways, as where( field, value ) or as an array of
* conditions to use: where( array('fieldName', ...), array('value', ...) );
* @param string|string[]|callable $key Single field name, or an array of field names.
* If given as a function (i.e. a closure), the function is called, passing the
* query itself in as the only parameter, so the function can add extra conditions
* with parentheses around the additional parameters.
* @param string|string[] $value Single field value, or an array of
* values. Can be null to search for `IS NULL` or `IS NOT NULL` (depending
* on the value of `$op` which should be `=` or `!=`.
* @param string $op Condition operator: <, >, = etc
* @param boolean $bind Escape the value (true, default) or not (false).
* @return self
*
* @example
* The following will produce
* `'WHERE name='allan' AND ( location='Scotland' OR location='Canada' )`:
*
* ```php
* $query
* ->where( 'name', 'allan' )
* ->where( function ($q) {
* $q->where( 'location', 'Scotland' );
* $q->or_where( 'location', 'Canada' );
* } );
* ```
*/
public function where ( $key, $value=null, $op="=", $bind=true )
{
if ( $key === null ) {
return $this;
}
else if ( is_callable($key) && is_object($key) ) { // is a closure
$this->_where_group( true, 'AND' );
$key( $this );
$this->_where_group( false, 'OR' );
}
else if ( !is_array($key) && is_array($value) ) {
for ( $i=0 ; $i<count($value) ; $i++ ) {
$this->where( $key, $value[$i], $op, $bind );
}
}
else {
$this->_where( $key, $value, 'AND ', $op, $bind );
}
return $this;
}
/**
* Add addition where conditions to the query with an AND operator. An alias
* of `where` for naming consistency.
*
* Can be used in two different ways, as where( field, value ) or as an array of
* conditions to use: where( array('fieldName', ...), array('value', ...) );
* @param string|string[]|callable $key Single field name, or an array of field names.
* If given as a function (i.e. a closure), the function is called, passing the
* query itself in as the only parameter, so the function can add extra conditions
* with parentheses around the additional parameters.
* @param string|string[] $value Single field value, or an array of
* values. Can be null to search for `IS NULL` or `IS NOT NULL` (depending
* on the value of `$op` which should be `=` or `!=`.
* @param string $op Condition operator: <, >, = etc
* @param boolean $bind Escape the value (true, default) or not (false).
* @return self
*/
public function and_where ( $key, $value=null, $op="=", $bind=true )
{
return $this->where( $key, $value, $op, $bind );
}
/**
* Add addition where conditions to the query with an OR operator.
*
* Can be used in two different ways, as where( field, value ) or as an array of
* conditions to use: where( array('fieldName', ...), array('value', ...) );
* @param string|string[]|callable $key Single field name, or an array of field names.
* If given as a function (i.e. a closure), the function is called, passing the
* query itself in as the only parameter, so the function can add extra conditions
* with parentheses around the additional parameters.
* @param string|string[] $value Single field value, or an array of
* values. Can be null to search for `IS NULL` or `IS NOT NULL` (depending
* on the value of `$op` which should be `=` or `!=`.
* @param string $op Condition operator: <, >, = etc
* @param boolean $bind Escape the value (true, default) or not (false).
* @return self
*/
public function or_where ( $key, $value=null, $op="=", $bind=true )
{
if ( $key === null ) {
return $this;
}
else if ( is_callable($key) && is_object($key) ) {
$this->_where_group( true, 'OR' );
$key( $this );
$this->_where_group( false, 'OR' );
}
else {
if ( !is_array($key) && is_array($value) ) {
for ( $i=0 ; $i<count($value) ; $i++ ) {
$this->or_where( $key, $value[$i], $op, $bind );
}
return $this;
}
$this->_where( $key, $value, 'OR ', $op, $bind );
}
return $this;
}
/**
* Provide grouping for WHERE conditions. Use it with a callback function to
* automatically group any conditions applied inside the method.
*
* For legacy reasons this method also provides the ability to explicitly
* define if a grouping bracket should be opened or closed in the query.
* This method is not prefer.
*
* @param boolean|callable $inOut If callable it will create the group
* automatically and pass the query into the called function. For
* legacy operations use `true` to open brackets, `false` to close.
* @param string $op Conditional operator to use to join to the
* preceding condition. Default `AND`.
* @return self
*
* @example
* ```php
* $query->where_group( function ($q) {
* $q->where( 'location', 'Edinburgh' );
* $q->where( 'position', 'Manager' );
* } );
* ```
*/
public function where_group ( $inOut, $op='AND' )
{
if ( is_callable($inOut) && is_object($inOut) ) {
$this->_where_group( true, $op );
$inOut( $this );
$this->_where_group( false, $op );
}
else {
$this->_where_group( $inOut, $op );
}
return $this;
}
/**
* Provide a method that can be used to perform a `WHERE ... IN (...)` query
* with bound values and parameters.
*
* Note this is only suitable for local values, not a sub-query. For that use
* `->where()` with an unbound value.
*
* @param string Field name
* @param array Values
* @param string Conditional operator to use to join to the
* preceding condition. Default `AND`.
* @return self
*/
public function where_in ( $field, $arr, $operator="AND" )
{
if ( count($arr) === 0 ) {
return $this;
}
$binders = array();
$prefix = ':wherein';
// Need to build an array of the binders (having bound the values) so
// the query can be constructed
for ( $i=0, $ien=count($arr) ; $i<$ien ; $i++ ) {
$binder = $prefix.$this->_whereInCnt;
$this->bind( $binder, $arr[$i] );
$binders[] = $binder;
$this->_whereInCnt++;
}
$this->_where[] = array(
'operator' => $operator,
'group' => null,
'field' => $this->_protect_identifiers($field),
'query' => $this->_protect_identifiers($field) .' IN ('.implode(', ', $binders).')'
);
return $this;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Protected methods
*/
/**
* Create a comma separated field list
* @param bool $addAlias Flag to add an alias
* @return string
* @internal
*/
protected function _build_field( $addAlias=false )
{
$a = array();
$asAlias = $this->_supportsAsAlias ?
' as ' :
' ';
for ( $i=0 ; $i<count($this->_field) ; $i++ ) {
$field = $this->_field[$i];
// Keep the name when referring to a table
if ( $addAlias && $field !== '*' && strpos($field, '(') === false ) {
$split = preg_split( '/ as (?![^\(]*\))/i', $field );
if ( count($split) > 1 ) {
$a[] = $this->_protect_identifiers( $split[0] ).$asAlias.
$this->_field_quote. $split[1] .$this->_field_quote;
}
else {
$a[] = $this->_protect_identifiers( $field ).$asAlias.
$this->_field_quote. $field .$this->_field_quote;
}
}
else if ( $addAlias && strpos($field, '(') !== false && ! strpos($field, ' as ') ) {
$a[] = $this->_protect_identifiers( $field ).$asAlias.
$this->_field_quote. $field .$this->_field_quote;
}
else {
$a[] = $this->_protect_identifiers( $field );
}
}
return ' '.implode(', ', $a).' ';
}
/**
* Create a JOIN statement list
* @return string
* @internal
*/
protected function _build_join()
{
return implode(' ', $this->_join);
}
/**
* Create the LIMIT / OFFSET string
*
* MySQL and Postgres style - anything else can have the driver override
* @return string
* @internal
*/
protected function _build_limit()
{
$out = '';
if ( $this->_limit ) {
$out .= ' LIMIT '.$this->_limit;
}
if ( $this->_offset ) {
$out .= ' OFFSET '.$this->_offset;
}
return $out;
}
/**
* Create the GROUP BY string
*
* @return string
* @internal
*/
protected function _build_group_by()
{
$out = '';
if ( $this->_group_by) {
$out .= ' GROUP BY '.$this->_group_by;
}
return $out;
}
/**
* Create the ORDER BY string
* @return string
* @internal
*/
protected function _build_order()
{
if ( count( $this->_order ) > 0 ) {
return ' ORDER BY '.implode(', ', $this->_order).' ';
}
return '';
}
/**
* Create a set list
* @return string
* @internal
*/
protected function _build_set()
{
$a = array();
for ( $i=0 ; $i<count($this->_field) ; $i++ ) {
$field = $this->_field[$i];
if ( isset( $this->_noBind[ $field ] ) ) {
$a[] = $this->_protect_identifiers( $field ) .' = '. $this->_noBind[ $field ];
}
else {
$a[] = $this->_protect_identifiers( $field ) .' = :'. $this->_safe_bind( $field );
}
}
return ' '.implode(', ', $a).' ';
}
/**
* Create the TABLE list
* @return string
* @internal
*/
protected function _build_table()
{
if ( $this->_type === 'insert' ) {
// insert, update and delete statements don't need or want aliases in the table name
$a = array();
for ( $i=0, $ien=count($this->_table) ; $i<$ien ; $i++ ) {
$table = str_ireplace( ' as ', ' ', $this->_table[$i] );
$tableParts = explode( ' ', $table );
$a[] = $tableParts[0];
}
return ' '.implode(', ', $a).' ';
}
return ' '.implode(', ', $this->_table).' ';
}
/**
* Create a bind field value list
* @return string
* @internal
*/
protected function _build_value()
{
$a = array();
for ( $i=0, $ien=count($this->_field) ; $i<$ien ; $i++ ) {
$a[] = ' :'.$this->_safe_bind( $this->_field[$i] );
}
return ' '.implode(', ', $a).' ';
}
/**
* Create the WHERE statement
* @return string
* @internal
*/
protected function _build_where()
{
if ( count($this->_where) === 0 ) {
return "";
}
$condition = "WHERE ";
for ( $i=0 ; $i<count($this->_where) ; $i++ ) {
if ( $i === 0 ) {
// Nothing (simplifies the logic!)
}
else if ( $this->_where[$i]['group'] === ')' ) {
// If a group has been used but no conditions were added inside
// of, we don't want to end up with `()` in the SQL as that is
// invalid, so add a 1.
if ( $this->_where[$i-1]['group'] === '(' ) {
$condition .= '1=1';
}
// else nothing
}
else if ( $this->_where[$i-1]['group'] === '(' ) {
// Nothing
}
else {
$condition .= $this->_where[$i]['operator'].' ';
}
if ( $this->_where[$i]['group'] !== null ) {
$condition .= $this->_where[$i]['group'];
}
else {
$condition .= $this->_where[$i]['query'] .' ';
}
}
return $condition;
}
/**
* Create a DELETE statement
* @return Result
* @internal
*/
protected function _delete()
{
$this->_prepare(
'DELETE FROM '
.$this->_build_table()
.$this->_build_where()
);
return $this->_exec();
}
/**
* Execute the query. Provided by the driver
* @return Result
* @internal
*/
abstract protected function _exec();
/**
* Create an INSERT statement
* @return Result
* @internal
*/
protected function _insert()
{
$this->_prepare(
'INSERT INTO '
.$this->_build_table().' ('
.$this->_build_field()
.') '
.'VALUES ('
.$this->_build_value()
.')'
);
return $this->_exec();
}