forked from KrisJordan/recess
/
SqlBuilder.class.php
790 lines (685 loc) · 23.1 KB
/
SqlBuilder.class.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
<?php
Library::import('recess.database.sql.ISqlConditions');
Library::import('recess.database.sql.ISqlSelectOptions');
/**
* SqlBuilder is used to incrementally compose named-parameter PDO Sql strings
* using a simple, chainable method call API. This is a naive wrapper that does
* not gaurantee valid SQL output (i.e. column names using reserved SQL words).
*
* 4 classes of SQL strings can be built: INSERT, UPDATE, DELETE, SELECT.
* This class is intentionally arranged from the low complexity requirements
* of INSERT to the more complex SELECT.
*
* INSERT: table, column/value assignments
* UPDATE/DELETE: where conditions
* SELECT: order, joins, offset, limit, distinct
*
* Example usage:
*
* $sqlBuilder->into('table_name')->assign('column', 'value')->insert() ..
* returns "INSERT INTO table_name (column) VALUES (:column)"
* $sqlBuilder->getPdoArguments() returns array( ':column' => 'value' )
*
* @author Kris Jordan <krisjordan@gmail.com>
* @contributor Luiz Alberto Zaiats
*
* @copyright 2008, 2009 Kris Jordan
* @package Recess PHP Framework
* @license MIT
* @link http://www.recessframework.org/
*/
class SqlBuilder implements ISqlConditions, ISqlSelectOptions {
/* INSERT */
protected $table;
protected $assignments = array();
/**
* Build an INSERT SQL string from SqlBuilder's state.
*
* @return string INSERT string.
*/
public function insert() {
$this->insertSanityCheck();
$sql = 'INSERT INTO ' . self::escapeWithTicks($this->table);
$columns = '';
$values = '';
$first = true;
$table_prefix = $this->tableAsPrefix() . '.';
foreach($this->assignments as $assignment) {
if($first) { $first = false; }
else { $columns .= ', '; $values .= ', '; }
$columns .= self::escapeWithTicks(str_replace($table_prefix, '', $assignment->column));
$values .= $assignment->getQueryParameter();
}
$columns = ' (' . $columns . ')';
$values = '(' . $values . ')';
$sql .= $columns . ' VALUES ' . $values;
return $sql;
}
/**
* Safety check used with insert to ensure only a table and assignments were applied.
*/
protected function insertSanityCheck() {
if( !empty($this->conditions) )
throw new RecessException('Insert does not use conditionals.', get_defined_vars());
if( !empty($this->joins) )
throw new RecessException('Insert does not use joins.', get_defined_vars());
if( !empty($this->orderBy) )
throw new RecessException('Insert does not use order by.', get_defined_vars());
if( !empty($this->groupBy) )
throw new RecessException('Insert does not use group by.', get_defined_vars());
if( isset($this->limit) )
throw new RecessException('Insert does not use limit.', get_defined_vars());
if( isset($this->offset) )
throw new RecessException('Insert does not use offset.', get_defined_vars());
if( isset($this->distinct) )
throw new RecessException('Insert does not use distinct.', get_defined_vars());
}
/**
* Set the table of focus on a sql statement.
*
* @param string $table
* @return SqlBuilder
*/
public function table($table) { $this->table = $table; return $this; }
/**
* Alias for table (insert into)
*
* @param string $table
* @return SqlBuilder
*/
public function into($table) { return $this->table($table); }
/**
* Assign a value to a column. Used with inserts and updates.
*
* @param string $column
* @param mixed $value
* @return SqlBuilder
*/
public function assign($column, $value) {
if(strpos($column, '.') === false) {
if(isset($this->table)) {
$this->assignments[] = new Criterion($this->tableAsPrefix() . '.' . $column, $value, Criterion::ASSIGNMENT);
} else {
throw new RecessException('Cannot assign without specifying table.', get_defined_vars());
}
} else {
$this->assignments[] = new Criterion($column, $value, Criterion::ASSIGNMENT);
}
return $this;
}
/* UPDATE & DELETE */
protected $conditions = array();
protected $conditionsUsed = array();
protected $useAssignmentsAsConditions = false;
/**
* Build a DELETE SQL string from SqlBuilder's state.
*
* @return string DELETE string
*/
public function delete() {
$this->deleteSanityCheck();
return 'DELETE FROM ' . self::escapeWithTicks($this->table) . $this->whereHelper();
}
/**
* Safety check used with delete.
*/
protected function deleteSanityCheck() {
if( !empty($this->joins) )
throw new RecessException('Delete does not use joins.', get_defined_vars());
if( !empty($this->orderBy) )
throw new RecessException('Delete does not use order by.', get_defined_vars());
if( !empty($this->groupBy) )
throw new RecessException('Delete does not use group by.', get_defined_vars());
if( isset($this->limit) )
throw new RecessException('Delete does not use limit.', get_defined_vars());
if( isset($this->offset) )
throw new RecessException('Delete does not use offset.', get_defined_vars());
if( isset($this->distinct) )
throw new RecessException('Delete does not use distinct.', get_defined_vars());
if( !empty($this->assignments) && !$this->useAssignmentsAsConditions)
throw new RecessException('Delete does not use assignments. To use assignments as conditions add ->useAssignmentsAsConditions() to your method call chain.', get_defined_vars());
}
/**
* Build an UPDATE SQL string from SqlBuilder's state.
*
* @return string
*/
public function update() {
$this->updateSanityCheck();
$sql = 'UPDATE ' . self::escapeWithTicks($this->table) . ' SET ';
$first = true;
$table_prefix = $this->tableAsPrefix() . '.';
foreach($this->assignments as $assignment) {
if($first) { $first = false; }
else { $sql .= ', '; }
$sql .= self::escapeWithTicks(str_replace($table_prefix, '', $assignment->column)) . ' = ' . $assignment->getQueryParameter();;
}
$sql .= $this->whereHelper();
return $sql;
}
/**
* Safety check used with update.
*/
protected function updateSanityCheck() {
if( !empty($this->joins) )
throw new RecessException('Update does not use joins.', get_defined_vars());
if( !empty($this->orderBy) )
throw new RecessException('Update (in Recess) does not use order by.', get_defined_vars());
if( !empty($this->groupBy) )
throw new RecessException('Update (in Recess) does not use group by.', get_defined_vars());
if( isset($this->limit) )
throw new RecessException('Update (in Recess) does not use limit.', get_defined_vars());
if( isset($this->offset) )
throw new RecessException('Update (in Recess) does not use offset.', get_defined_vars());
if( isset($this->distinct) )
throw new RecessException('Update does not use distinct.', get_defined_vars());
}
/**
* Return the collection of PDO named parameters and values to be
* applied to a parameterized PDO statement.
*
* @return array
*/
public function getPdoArguments() {
if($this->useAssignmentsAsConditions)
return array_merge($this->conditions, $this->cleansedAssignmentsAsConditions());
else
return array_merge($this->conditions, $this->assignments);
}
/**
* Method for when using assignments as conditions. This purges
* assignments which have null values.
*
* @return array
*/
protected function cleansedAssignmentsAsConditions() {
$assignments = array();
$count = count($this->assignments);
for($i = 0; $i < $count; $i++) {
if(isset($this->assignments[$i]->value))
$assignments[] = $this->assignments[$i];
}
return $assignments;
}
/**
* Alias to specify which table is being used.
*
* @param string $table
* @return SqlBuilder
*/
public function from($table) { return $this->table($table); }
/**
* Handy shortcut which allows assignments to be used as conditions
* in a select statement.
*
* @param boolean $bool
* @return SqlBuilder
*/
public function useAssignmentsAsConditions($bool) { $this->useAssignmentsAsConditions = $bool; return $this; }
/* ISqlConditions */
/**
* Equality expression for WHERE clause of update, delete, or select statements.
*
* @param string $column
* @param mixed $value
* @return SqlBuilder
*/
public function equal($column, $value) { return $this->addCondition($column, $value, Criterion::EQUAL_TO); }
/**
* Inequality than expression for WHERE clause of update, delete, or select statements.
*
* @param string $column
* @param mixed $value
* @return SqlBuilder
*/
public function notEqual($column, $value) { return $this->addCondition($column, $value, Criterion::NOT_EQUAL_TO); }
/**
* Shortcut alias for SqlBuilder->lessThan($column,$big)->greaterThan($column,$small)
*
* @param string $column
* @param numeric $small Greater than this number.
* @param numeric $big Less than this number.
* @return SqlBuilder
*/
public function between ($column, $small, $big) { $this->greaterThan($column, $small); return $this->lessThan($column, $big); }
/**
* Greater than expression for WHERE clause of update, delete, or select statements.
*
* @param string $column
* @param numeric $value
* @return SqlBuilder
*/
public function greaterThan($column, $value) { return $this->addCondition($column, $value, Criterion::GREATER_THAN); }
/**
* Greater than or equal to expression for WHERE clause of update, delete, or select statements.
*
* @param string $column
* @param numeric $value
* @return SqlBuilder
*/
public function greaterThanOrEqualTo($column, $value) { return $this->addCondition($column, $value, Criterion::GREATER_THAN_EQUAL_TO); }
/**
* Less than expression for WHERE clause of update, delete, or select statements.
*
* @param string $column
* @param numeric $value
* @return SqlBuilder
*/
public function lessThan($column, $value) { return $this->addCondition($column, $value, Criterion::LESS_THAN); }
/**
* Less than or equal to expression for WHERE clause of update, delete, or select statements.
*
* @param string $column
* @param numeric $value
* @return SqlBuilder
*/
public function lessThanOrEqualTo($column, $value) { return $this->addCondition($column, $value, Criterion::LESS_THAN_EQUAL_TO); }
/**
* LIKE expression for WHERE clause of update, delete, or select statements, does not include wildcards.
*
* @param string $column
* @param string $value
* @return SqlBuilder
*/
public function like($column, $value) { return $this->addCondition($column, $value, Criterion::LIKE); }
/**
* NOT LIKE expression for WHERE clause of update, delete, or select statements, does not include wildcards.
*
* @param string $column
* @param string $value
* @return SqlBuilder
*/
public function notLike($column, $value) { return $this->addCondition($column, $value, Criterion::NOT_LIKE); }
/**
* IS NULL expression for WHERE clause of update, delete, or select statements
*
* @param string $column
* @param string $value
* @return SqlBuilder
*/
public function isNull($column) { return $this->addCondition($column, null, Criterion::IS_NULL); }
/**
* IS NOT NULL expression for WHERE clause of update, delete, or select statements
*
* @param string $column
* @param string $value
* @return SqlBuilder
*/
public function isNotNull($column) { return $this->addCondition($column, null, Criterion::IS_NOT_NULL); }
/**
* Add a condition to the SqlBuilder statement. Additional logic here to prepend
* a table name and also keep track of which columns have already been assigned conditions
* to ensure we do not use two identical named parameters in PDO.
*
* @param string $column
* @param mixed $value
* @param string $operator
* @return SqlBuilder
*/
protected function addCondition($column, $value, $operator) {
if(strpos($column, '.') === false && strpos($column, '(') === false && !in_array($column, array_keys($this->selectAs))) {
if(isset($this->table)) {
$column = $this->tableAsPrefix() . '.' . $column;
} else {
throw new RecessException('Cannot use "' . $operator . '" operator without specifying table for column "' . $column . '".', get_defined_vars());
}
}
if(isset($this->conditionsUsed[$column])) {
$this->conditionsUsed[$column]++;
$pdoLabel = $column . '_' . $this->conditionsUsed[$column];
} else {
$this->conditionsUsed[$column] = 1;
$pdoLabel = null;
}
$this->conditions[] = new Criterion($column, $value, $operator, $pdoLabel);
return $this;
}
/* SELECT */
protected $select = '*';
protected $selectAs = array();
protected $joins = array();
protected $limit;
protected $offset;
protected $distinct;
protected $orderBy = array();
protected $groupBy = array();
protected $usingAliases = false;
/**
* Build a SELECT SQL string from SqlBuilder's state.
*
* @return string
*/
public function select() {
$this->selectSanityCheck();
$sql = 'SELECT ' . $this->distinct . self::escapeWithTicks($this->select);
foreach($this->selectAs as $selectAs) {
$sql .= ', ' . $selectAs;
}
$sql .= ' FROM ' . self::escapeWithTicks($this->table);
$sql .= $this->joinHelper();
$sql .= $this->whereHelper();
$sql .= $this->orderByHelper();
$sql .= $this->groupByHelper();
$sql .= $this->rangeHelper();
return $sql;
}
/**
* Safety check used when creating a SELECT statement.
*/
protected function selectSanityCheck() {
if( (!empty($this->where) || !empty($this->orderBy)) && !isset($this->table))
throw new RecessException('Must have from if using where.', get_defined_vars());
if( isset($this->offset) && !isset($this->limit))
throw new RecessException('Must define limit if using offset.', get_defined_vars());
if($this->select == '*' && !isset($this->table))
throw new RecessException('No table has been selected.', get_defined_vars());
}
/* ISqlSelectOptions */
/**
* LIMIT results to some number of records.
*
* @param integer $size
* @return SqlBuilder
*/
public function limit($size) { $this->limit = $size; return $this; }
/**
* When used in conjunction with limit($size), offset specifies which row the results will begin at.
*
* @param integer $offset
* @return SqlBuilder
*/
public function offset($offset) { $this->offset = $offset; return $this; }
/**
* Shortcut alias to ->limit($finish - $start)->offset($start);
*
* @param integer $start
* @param integer $finish
* @return SqlBuilder
*/
public function range($start, $finish) { $this->offset = $start; $this->limit = $finish - $start; return $this; }
/**
* Add an ORDER BY expression to sql string. Example: ->orderBy('name ASC')
*
* @param string $clause
* @return SqlBuilder
*/
public function orderBy($clause) {
if(($spacePos = strpos($clause,' ')) !== false) {
$name = substr($clause,0,$spacePos);
} else {
$name = $clause;
}
if(isset($this->table) && strpos($clause,'.') === false && strpos($name,'(') === false && !array_key_exists($name, $this->selectAs)) {
$this->orderBy[] = $this->tableAsPrefix() . '.' . $clause;
} else {
$this->orderBy[] = $clause;
}
return $this;
}
/**
* Add an GROUP BY expression to sql string. Example: ->groupBy('name')
*
* @param string $clause
* @return SqlBuilder
*/
public function groupBy($clause) {
if(($spacePos = strpos($clause,' ')) !== false) {
$name = substr($clause,0,$spacePos);
} else {
$name = $clause;
}
if(isset($this->table) && strpos($clause,'.') === false && strpos($name,'(') === false && !array_key_exists($name, $this->selectAs)) {
$this->groupBy[] = $this->tableAsPrefix() . '.' . $clause;
} else {
$this->groupBy[] = $clause;
}
return $this;
}
/**
* Helper method which returns the current table even when it
* is aliased due to joins between the same table.
*
* @return string The current table which can be used as a prefix.
*/
protected function tableAsPrefix() {
if($this->usingAliases) {
$spacePos = strrpos($this->table, ' ');
if($spacePos !== false) {
return substr($this->table, $spacePos + 1);
}
}
return $this->table;
}
/**
* Left outer join expression for SELECT SQL statement.
*
* @param string $table
* @param string $tablePrimaryKey
* @param string $fromTableForeignKey
* @return SqlBuilder
*/
public function leftOuterJoin($table, $tablePrimaryKey, $fromTableForeignKey) {
return $this->join(Join::LEFT, Join::OUTER, $table, $tablePrimaryKey, $fromTableForeignKey);
}
/**
* Inner join expression for SELECT SQL statement.
*
* @param string $table
* @param string $tablePrimaryKey
* @param string $fromTableForeignKey
* @return SqlBuilder
*/
public function innerJoin($table, $tablePrimaryKey, $fromTableForeignKey) {
return $this->join('', Join::INNER, $table, $tablePrimaryKey, $fromTableForeignKey);
}
/**
* Generic join expression to be added to a SELECT SQL statement.
*
* @param string $leftOrRight
* @param string $innerOrOuter
* @param string $table
* @param string $tablePrimaryKey
* @param string $fromTableForeignKey
* @return SqlBuilder
*/
protected function join($leftOrRight, $innerOrOuter, $table, $tablePrimaryKey, $fromTableForeignKey) {
if($this->table == $table) {
$oldTable = $this->table;
$parts = explode('__', $this->table);
$partsCount = count($parts);
if($partsCount > 0 && is_int($parts[$partsCount-1])) {
$number = $parts[$partsCount - 1] + 1;
} else {
$number = 2;
}
$tableAlias = $this->table . '__' . $number;
$this->table = self::escapeWithTicks($this->table) . ' AS ' . self::escapeWithTicks($tableAlias);
$this->usingAliases = true;
$tablePrimaryKey = str_replace($oldTable,$tableAlias,$tablePrimaryKey);
}
$this->select = $this->tableAsPrefix() . '.*';
$this->joins[] = new Join($leftOrRight, $innerOrOuter, $table, $tablePrimaryKey, $fromTableForeignKey);
return $this;
}
/**
* Add additional field to select statement which is aliased using the AS parameter.
* ->selectAs("ABS(location - 5)", 'distance') translates to => SELECT ABS(location-5) AS distance
*
* @param string $select
* @param string $as
* @return SqlBuilder
*/
public function selectAs($select, $as) {
$this->selectAs[$as] = $select . ' as ' . $as;
return $this;
}
/**
* Add a DISTINCT clause to SELECT SQL.
*
* @return SqlBuilder
*/
public function distinct() { $this->distinct = ' DISTINCT '; return $this; }
/* HELPER METHODS */
protected function whereHelper() {
$sql = '';
$first = true;
if(!empty($this->conditions)) {
foreach($this->conditions as $clause) {
if(!$first) { $sql .= ' AND '; } else { $first = false; } // TODO: Figure out how we'll do ORing
$sql .= self::escapeWithTicks($clause->column) . $clause->operator . $clause->getQueryParameter();
}
}
if($this->useAssignmentsAsConditions && !empty($this->assignments)) {
$assignments = $this->cleansedAssignmentsAsConditions();
foreach($assignments as $clause) {
if(!$first) { $sql .= ' AND '; } else { $first = false; } // TODO: Figure out how we'll do ORing
$sql .= self::escapeWithTicks($clause->column) . ' = ' . $clause->getQueryParameter();
}
}
if($sql != '') {
$sql = ' WHERE ' . $sql;
}
return $sql;
}
protected function joinHelper() {
$sql = '';
if(!empty($this->joins)) {
$joins = array_reverse($this->joins, true);
foreach($joins as $join) {
$joinStatement = ' ';
if(isset($join->natural) && $join->natural != '') {
$joinStatement .= $join->natural . ' ';
}
if(isset($join->leftRightOrFull) && $join->leftRightOrFull != '') {
$joinStatement .= $join->leftRightOrFull . ' ';
}
if(isset($join->innerOuterOrCross) && $join->innerOuterOrCross != '') {
$joinStatement .= $join->innerOuterOrCross . ' ';
}
$onStatement = ' ON ' . self::escapeWithTicks($join->tablePrimaryKey) . ' = ' . self::escapeWithTicks($join->fromTableForeignKey);
$joinStatement .= 'JOIN ' . self::escapeWithTicks($join->table) . $onStatement;
$sql .= $joinStatement;
}
}
return $sql;
}
protected static function escapeWithTicks($string) {
if($string == '*' || strpos($string, '`') !== false) {
return $string;
}
if(strpos($string,Library::dotSeparator) !== false) { // Todo: Replace with Regexp
$parts = explode(Library::dotSeparator, $string);
if(isset($parts[1]) && $parts[1] == '*') {
return '`' . $parts[0] . '`.*';
} else {
return '`' . implode('`.`', $parts) . '`';
}
} else {
return '`' . $string . '`';
}
}
protected function orderByHelper() {
$sql = '';
if(!empty($this->orderBy)){
$sql = ' ORDER BY ';
$first = true;
foreach($this->orderBy as $order){
if(!$first) { $sql .= ', '; } else { $first = false; }
$sql .= $order;
}
}
return $sql;
}
protected function groupByHelper() {
$sql = '';
if(!empty($this->groupBy)){
$sql = ' GROUP BY ';
$first = true;
foreach($this->groupBy as $order){
if(!$first) { $sql .= ', '; } else { $first = false; }
$sql .= $order;
}
}
return $sql;
}
protected function rangeHelper() {
$sql = '';
if(isset($this->limit)){ $sql .= ' LIMIT ' . $this->limit; }
if(isset($this->offset)){ $sql .= ' OFFSET ' . $this->offset; }
return $sql;
}
public function getCriteria() {
return array_merge($this->conditions, $this->assignments);
}
public function getTable() {
return $this->table;
}
}
class Criterion {
public $column;
public $pdoLabel;
public $value;
public $operator;
const GREATER_THAN = ' > ';
const GREATER_THAN_EQUAL_TO = ' >= ';
const LESS_THAN = ' < ';
const LESS_THAN_EQUAL_TO = ' <= ';
const EQUAL_TO = ' = ';
const NOT_EQUAL_TO = ' != ';
const LIKE = ' LIKE ';
const NOT_LIKE = ' NOT LIKE ';
const IS_NULL = ' IS NULL';
const IS_NOT_NULL = ' IS NOT NULL';
const COLON = ':';
const ASSIGNMENT = '=';
const ASSIGNMENT_PREFIX = 'assgn_';
const UNDERSCORE = '_';
public function __construct($column, $value, $operator, $pdoLabel = null){
$this->column = $column;
$this->value = $value;
$this->operator = $operator;
if(!isset($pdoLabel)) {
$this->pdoLabel = preg_replace('/[ \-.,\(\)`]/', '_', $column);
} else {
$this->pdoLabel = preg_replace('/[ \-.,\(\)`]/', '_', $pdoLabel);
}
}
public function getQueryParameter() {
// Begin workaround for PDO's poor numeric binding
if(is_numeric($this->value)) {
return $this->value;
}
// End workaround
if($this->operator == self::ASSIGNMENT) {
return self::COLON . self::ASSIGNMENT_PREFIX . $this->pdoLabel;
} elseif($this->operator == self::IS_NULL || $this->operator == self::IS_NOT_NULL) {
return '';
} else {
return self::COLON . $this->pdoLabel;
}
}
}
class Join {
const NATURAL = 'NATURAL';
const LEFT = 'LEFT';
const RIGHT = 'RIGHT';
const FULL = 'FULL';
const INNER = 'INNER';
const OUTER = 'OUTER';
const CROSS = 'CROSS';
public $natural;
public $leftRightOrFull;
public $innerOuterOrCross = 'OUTER';
public $table;
public $tablePrimaryKey;
public $fromTableForeignKey;
public function __construct($leftRightOrFull, $innerOuterOrCross, $table, $tablePrimaryKey, $fromTableForeignKey, $natural = ''){
$this->natural = $natural;
$this->leftRightOrFull = $leftRightOrFull;
$this->innerOuterOrCross = $innerOuterOrCross;
$this->table = $table;
$this->tablePrimaryKey = $tablePrimaryKey;
$this->fromTableForeignKey = $fromTableForeignKey;
}
}
?>