-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathadditional.php
773 lines (666 loc) · 19.7 KB
/
additional.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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 enc=utf8: */
/**
* @author Alexey Zakhlestin
* @package mysql-query-builder
**/
/*
MySQL Query Builder
Copyright © 2005-2009 Alexey Zakhlestin <indeyets@gmail.com>
Copyright © 2005-2006 Konstantin Sedov <kostya.online@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Defines set of methods, which must be implemented by any "CONDITION" object
*
* @package mysql-query-builder
*/
interface MQB_Condition
{
/**
* used for generation of "prepared" SQL-queries. Supposed to be used recursively, and add parameters to the end of $parameters stack
*
* @param array $parameters
* @return string
*/
public function getSql(array &$parameters);
}
/**
* Defines set of methods, which must be implemented by any object, which is used as a Field of result-set row
*
* @package mysql-query-builder
*/
interface MQB_Field
{
/**
* used for generation of "prepared" SQL-queries. Supposed to be used recursively, and add parameters to the end of $parameters stack
*
* @param array $parameters
* @return string
*/
public function getSql(array &$parameters);
/**
* returns "alias" name of field
*
* @return string
* @author Jimi Dini
*/
public function getAlias();
}
/**
* Represents the table-entity of SQL-query
*
* @package mysql-query-builder
*/
class QBTable
{
private $table_name = null;
private $db_name = null;
/**
* Designated constructor of table-object.
*
* @param string $table_name
* @param string $db_name
*/
public function __construct($table_name, $db_name = null)
{
$this->table_name = $table_name;
$this->db_name = $db_name;
}
/**
* accessor, which returns sql-friendly (escaped) string-representation of table
*
* @return string
*/
public function __toString()
{
$res = '';
if (null !== $this->db_name) {
$res .= '`'.$this->db_name.'`.';
}
$res .= '`'.$this->table_name.'`';
return $res;
}
/**
* accessor, which returns raw table-name (without database-name)
*
* @return string
*/
public function getTable()
{
return $this->table_name;
}
}
/**
* generic Operator class. You can't instantiate this directly
*
* @package mysql-query-builder
*/
class Operator implements MQB_Condition
{
private $content = array();
protected $startSql;
protected $implodeSql;
protected $endSql;
protected function __construct(array $content)
{
$this->setContent($content);
}
/**
* Specifies array of MQB_Conditions, which should be used as the content of Operator
*
* @param array $content
* @return void
* @throws InvalidArgumentException
*/
public function setContent(array $content)
{
foreach ($content as $c) {
if (!is_object($c) or !($c instanceof MQB_Condition)) {
throw new InvalidArgumentException("Operators should be given valid Operators or Conditions as parameters");
}
}
$this->content = $content;
}
/**
* accessor, which returns internal content-array
*
* @return array
*/
public function getContent()
{
return $this->content;
}
public function getSql(array &$parameters)
{
$sqlparts = array();
foreach ($this->content as $c) {
$sqlparts[] = $c->getSql($parameters);
}
$parts = implode($this->implodeSql, $sqlparts);
if (empty($parts))
return '';
return $this->startSql.$parts.$this->endSql;
}
}
/**
* Class, which implements SQLs "NOT()" operator
*
* @package mysql-query-builder
*/
class NotOp extends Operator
{
private $my_content = null;
/**
* designated constructor. takes either MQB_Condition or array consisting of the single MQB_Condition
*
* @param mixed $content
* @throws InvalidArgumentException
*/
public function __construct($content)
{
if (is_array($content)) {
// compatibility with "legacy" API
if (count($content) != 1)
throw new InvalidArgumentException("NotOp takes an array of exactly one Condition or Operator");
$content = $content[0];
}
parent::__construct(array($content));
}
public function getSql(array &$parameters)
{
$content = $this->getContent();
return 'NOT ('.$content[0]->getSql($parameters).')';
}
}
/**
* Class, which implements SQLs "AND" operator
*
* @package mysql-query-builder
*/
class AndOp extends Operator
{
/**
* Designated constructor.
* Takes either single parameter — array of MQB_Conditions or several parameters-MQB_Conditions
*
* @param string|array $content,...
*/
public function __construct($content)
{
if (func_num_args() > 1)
parent::__construct(func_get_args());
else
parent::__construct($content);
$this->startSql = "(";
$this->implodeSql = " AND ";
$this->endSql = ")";
}
public function getSql(array &$parameters)
{
$content = $this->getContent();
// shortcut
if (count($content) == 1)
return $content[0]->getSql($parameters);
return parent::getSql($parameters);
}
}
/**
* Class, which implements SQLs "OR" operator
*
* @package mysql-query-builder
*/
class OrOp extends Operator
{
/**
* Designated constructor.
* Takes either single parameter — array of MQB_Conditions or several parameters-MQB_Conditions
*
* @param string|array $content,...
*/
public function __construct($content)
{
if (func_num_args() > 1)
parent::__construct(func_get_args());
else
parent::__construct($content);
$this->startSql = "(";
$this->implodeSql = " OR ";
$this->endSql = ")";
}
public function getSql(array &$parameters)
{
$content = $this->getContent();
// shortcut
if (count($content) == 1)
return $content[0]->getSql($parameters);
return parent::getSql($parameters);
}
}
/**
* Class, which implements SQLs "XOR()" operator
*
* @package mysql-query-builder
*/
class XorOp extends Operator
{
/**
* Designated constructor.
* Takes either single parameter — array of MQB_Conditions or several parameters-MQB_Conditions
*
* @param string|array $content,...
*/
public function __construct($content)
{
if (func_num_args() > 1)
parent::__construct(func_get_args());
else
parent::__construct($content);
$this->startSql = "(";
$this->implodeSql = " XOR ";
$this->endSql = ")";
}
public function getSql(array &$parameters)
{
$content = $this->getContent();
// shortcut
if (count($content) == 1)
return $content[0]->getSql($parameters);
return parent::getSql($parameters);
}
}
/**
* Class, which implements generic Condition. Actually, almost any condition can be implemented using it
*
* @package mysql-query-builder
*/
class Condition implements MQB_Condition
{
private $content = array();
private $validConditions = array("=", "<>", "<", ">", ">=", "<=", "like", "is null", "find_in_set", "and", "or", "xor", "in");
private $validSingulars = array("is null");
/**
* Designated constructor.
* First parameter should be one of the allowed comparator-strings
* Second parameter should be some MQB_Field-compliant object
* Third parameter is the value, which is compared against second-parameter. It should be either scalar-value, or another MQB_Field
*
* @param string $comparison
* @param MQB_Field $left
* @param mixed $right
* @throws RangeException, InvalidArgumentException
*/
public function __construct($comparison, MQB_Field $left, $right = null)
{
$comparison = strtolower($comparison);
if (!in_array($comparison, $this->validConditions))
throw new RangeException('invalid comparator-function');
if ($comparison == 'in') {
if (is_array($right)) {
// IN (1,2,3,4)
foreach ($right as $value) {
if (!is_numeric($value)) {
throw new InvalidArgumentException('Right-op has to be array consisting of NUMERIC VALUES, if comparison is "in"');
}
}
} else {
// IN (SELECT …)
if (!is_object($right) or !($right instanceof SelectQuery)) {
throw new InvalidArgumentException('Right-op has to be object of class SelectQuery, if comparison is "in"');
}
if ($right->countSelects() != 1) {
throw new InvalidArgumentException('Right-op has to be query with one field, if comparison is "in"');
}
}
} elseif (!in_array($comparison, $this->validSingulars)) {
if (is_scalar($right)) {
$right = new Parameter($right);
} elseif (null !== $right and !($right instanceof Parameter) and !($right instanceof MQB_Field)) {
throw new InvalidArgumentException('Right-op has to be Parameter or MQB_Field. Got '.get_class($right).' instead');
}
}
$this->content = array($comparison, $left, $right);
}
public function getSql(array &$parameters)
{
$comparison = $this->content[0];
$leftpart = $this->content[1]->getSql($parameters);
if ($comparison == 'is null' or ($comparison == '=' and null === $this->content[2])) {
return $leftpart." IS NULL";
} elseif ($comparison == '<>' and null === $this->content[2]) {
return $leftpart." IS NOT NULL";
} elseif ($comparison == 'in') {
$right = $this->content[2];
if (is_array($right)) {
return $leftpart." IN (".implode(', ', $right).")";
} elseif ($right instanceof SelectQuery) {
return $leftpart.' IN ('.$right->sql($parameters).')';
}
} else {
$rightpart = $this->content[2]->getSql($parameters);
if ($comparison == "find_in_set")
return $comparison."(".$rightpart.",".$leftpart.")";
return $leftpart." ".$comparison." ".$rightpart;
}
}
/**
* accessor which returns comparator
*
* @return string
*/
public function getComparison()
{
return $this->content[0];
}
/**
* accessor which returns left-parameter of comparison
*
* @return MQB_Field
*/
public function getLeft()
{
return $this->content[1];
}
/**
* accessor which returns right-parameter of comparison
*
* @return mixed
*/
public function getRight()
{
return $this->content[2];
}
}
/**
* Representation of the Field of N-th Table in Query
*
* @package mysql-query-builder
*/
class Field implements MQB_Field
{
private $name;
private $table;
private $alias;
/**
* Designated constructor. Used to create representation of 'tN.field as alias' construction, which can be referred from various parts of query
*
* @param string $name
* @param integer $table
* @param string $alias
* @throws RangeException
*/
public function __construct($name, $table = 0, $alias = null)
{
if (!$name)
throw new RangeException('Name of the field is not specified');
if (!is_string($name) or !is_numeric($table))
throw new InvalidArgumentException();
$this->table = $table;
$this->name = $name;
$this->alias = $alias;
}
public function getSql(array &$parameters, $full = false)
{
if (true === $full or null === $this->alias) {
$res = '`t'.$this->table."`.`".$this->name.'`';
if (null !== $this->alias) {
$res .= ' AS `'.$this->alias.'`';
}
} else {
$res = '`'.$this->alias.'`';
}
return $res;
}
/**
* accessor for internal "number of table in query" property
*
* @return integer
*/
public function getTable()
{
return $this->table;
}
/**
* accessor for internal "name of the field" property
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* accessor for internal "number of alias" property. returns NULL, if alias is not set
*
* @return string|null
*/
public function getAlias()
{
if (null === $this->alias)
return null;
return '`'.$this->alias.'`';
}
}
/**
* Class, which represents 'table.*' concept
*
* @package mysql-query-builder
*/
class AllFields
{
private $table;
/**
* Designated constructor. Takes "number of table in query" as the parameter
*
* @param integer $table
*/
public function __construct($table = 0)
{
$this->table = $table;
}
public function getSql(array &$parameters)
{
return '`t'.$this->table."`.*";
}
/**
* accessor for internal "number of table in query" property
*
* @return integer
*/
public function getTable()
{
return $this->table;
}
}
/**
* Class, which represents SQL-Functions used as columns in query
*
* @package mysql-query-builder
*/
class SqlFunction implements MQB_Field
{
private $name;
private $values;
private $alias;
private $validNames = array('substring', 'year', 'month', 'day', 'date', 'length');
/**
* Designated constructor, which generates representation of '$name($value1, $value2, ... $valueN) as $alias' sql-construct
* $values can either be literal, MQB_Field or array of literals and MQB_Fields
*
* @param string $name
* @param mixed $values
* @param string $alias
* @throws InvalidArgumentException
*/
public function __construct($name, $values, $alias = null)
{
if (!is_string($name) or !in_array($name, $this->validNames))
throw new InvalidArgumentException('Invalid sql-function: '.$name);
if (!is_array($values))
$values = array($values);
foreach ($values as $v) {
if (is_object($v) and !($v instanceof MQB_Field))
throw new InvalidArgumentException("Something wrong passed as a parameter");
}
$this->name = $name;
$this->values = $values;
$this->alias = $alias;
}
public function getSql(array &$parameters)
{
$result = strtoupper($this->name)."(";
$first = true;
foreach ($this->values as $v) {
if ($first) {
$first = false;
} else {
$result .= ', ';
}
if (is_object($v)) {
$result .= $v->getSql($parameters);
} else {
$result .= $v;
}
}
$result .= ')';
if (null !== $this->alias) {
$result .= ' AS '.$this->getAlias();
}
return $result;
}
/**
* accessor for internal "number of alias" property. returns NULL, if alias is not set
*
* @return string|null
*/
public function getAlias()
{
if (null === $this->alias)
return null;
return '`'.$this->alias.'`';
}
/**
* accessor for internal "name of the function" property
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
/**
* Class, which represents SQLs aggregate-functions used as columns in query
*
* @package mysql-query-builder
*/
class Aggregate implements MQB_Field
{
private $aggregate;
private $distinct;
private $name;
private $table;
private $alias;
private $validAggregates = array("sum", "count", "min", "max", "avg");
private $field = null;
/**
* Creates representation of SQLs aggregate-function.
* $field can be null, if $aggregate is 'count' — this would result in COUNT(*) query.
* If $distinct is set to true, then something like the following will appear: COUNT(DISTINCT `foo`) AS `alias`
*
* @param string $aggregate
* @param string $field
* @param bool $distinct
* @param string $alias
* @throws RangeException, InvalidArgumentException
*/
public function __construct($aggregate, $field = null, $distinct=false, $alias=null)
{
$aggregate = strtolower($aggregate);
if (!in_array($aggregate, $this->validAggregates))
throw new RangeException('Invalid aggregate function: '.$aggregate);
if (($field instanceof MQB_Field) or (null === $field and $aggregate == 'count')) {
$this->aggregate = $aggregate;
$this->distinct = ($distinct === true);
$this->alias = $alias;
$this->field = $field;
} else {
throw new InvalidArgumentException('field should be MQB_Field');
}
}
public function getSql(array &$parameters, $full = false)
{
if (true === $full or null === $this->alias) {
if (null === $this->field) {
$field_sql = '*';
} else {
$field_sql = $this->field->getSql($parameters);
}
if ($this->distinct) {
$field_sql = 'DISTINCT '.$field_sql;
}
$field_sql = strtoupper($this->aggregate).'('.$field_sql.')';
if (null !== $this->alias) {
$field_sql .= ' AS `'.$this->alias.'`';
}
} else {
$field_sql = '`'.$this->alias.'`';
}
return $field_sql;
}
/**
* accessor for internal "number of alias" property. returns NULL, if alias is not set
*
* @return string|null
*/
public function getAlias()
{
if (null === $this->alias)
return null;
return '`'.$this->alias.'`';
}
}
/**
* Class, which represents literal parameter in SQL-queries.
* value is never directly used in query, instead, it is put array, which is later used for executing prepared SQL-statement
*
* @package mysql-query-builder
*/
class Parameter
{
private $content;
/**
* Creates representation of literal-value, passed as the single parameter
*
* @param string|integer|bool|null $content
*/
public function __construct($content)
{
$this->content = $content;
}
public function getSql(array &$parameters)
{
$number = count($parameters) + 1;
$parameters[":p".$number] = $this->content;
return ":p".$number;
}
/**
* accessor, which returns value of parameter
*
* @return mixed
*/
public function getParameters()
{
return $this->content;
}
}