Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

DoctrineExpression.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Doctrine;
8
9 use eZ\Publish\Core\Persistence\Database\Expression;
10 use Doctrine\DBAL\Connection;
11 use eZ\Publish\Core\Persistence\Database\QueryException;
12
19 class DoctrineExpression implements Expression
20 {
22 private $connection;
23
25 private $platform;
26
27 public function __construct(Connection $connection)
28 {
29 $this->connection = $connection;
30 $this->platform = $connection->getDatabasePlatform();
31 }
32
52 public function lOr()
53 {
54 $args = func_get_args();
55
56 return $this->combine($args, ' OR ');
57 }
58
67 private function combine(array $args, $by)
68 {
69 $args = $this->arrayFlatten($args);
70
71 if (count($args) < 1) {
72 throw new QueryException(
73 "The expression '{$by}' expected at least 1 argument but none provided."
74 );
75 }
76
77 if (count($args) === 1) {
78 return $args[0];
79 }
80
81 return '( ' . implode($by, $args) . ' )';
82 }
83
103 public function lAnd()
104 {
105 $args = func_get_args();
106
107 return $this->combine($args, ' AND ');
108 }
109
125 public function not($expression)
126 {
127 return "NOT ( {$expression} )";
128 }
129
150 public function add()
151 {
152 $args = func_get_args();
153
154 return $this->basicMath('+', $args);
155 }
156
177 public function sub()
178 {
179 $args = func_get_args();
180
181 return $this->basicMath('-', $args);
182 }
183
204 public function mul()
205 {
206 $args = func_get_args();
207
208 return $this->basicMath('*', $args);
209 }
210
231 public function div()
232 {
233 $args = func_get_args();
234
235 return $this->basicMath('/', $args);
236 }
237
253 public function eq($value1, $value2)
254 {
255 return "{$value1} = {$value2}";
256 }
257
273 public function neq($value1, $value2)
274 {
275 return "{$value1} <> {$value2}";
276 }
277
293 public function gt($value1, $value2)
294 {
295 return "{$value1} > {$value2}";
296 }
297
314 public function gte($value1, $value2)
315 {
316 return "{$value1} >= {$value2}";
317 }
318
334 public function lt($value1, $value2)
335 {
336 return "{$value1} < {$value2}";
337 }
338
355 public function lte($value1, $value2)
356 {
357 return "{$value1} <= {$value2}";
358 }
359
386 public function in($column)
387 {
388 $args = func_get_args();
389
390 if (count($args) < 2) {
391 throw new QueryException('Expected two or more parameters to in()');
392 }
393
394 if (is_array($args[1])) {
395 $values = array_values($args[1]);
396 } else {
397 $values = array_slice($args, 1);
398 }
399
400 // Special handling of sub selects to avoid double braces
401 if (count($values) === 1 && $values[0] instanceof SubselectDoctrineQuery) {
402 return "{$column} IN " . $values[0]->getQuery();
403 }
404
405 if (count($values) == 0) {
406 throw new QueryException('At least one element is required as value.');
407 }
408
409 foreach ($values as $key => $value) {
410 switch (true) {
411 case $value instanceof SubselectDoctrineQuery:
412 $values[$key] = $value->getQuery();
413 break;
414 case is_int($value):
415 case is_float($value):
416 $values[$key] = (string)$value;
417 break;
418 default:
419 $values[$key] = $this->connection->quote($value);
420 }
421 }
422
423 return "{$column} IN ( " . implode(', ', $values) . ' )';
424 }
425
440 public function isNull($expression)
441 {
442 return "{$expression} IS NULL";
443 }
444
468 public function between($expression, $value1, $value2)
469 {
470 return "{$expression} BETWEEN {$value1} AND {$value2}";
471 }
472
483 public function like($expression, $pattern)
484 {
485 return "{$expression} LIKE {$pattern}";
486 }
487
495 public function avg($column)
496 {
497 return "AVG( {$column} )";
498 }
499
510 public function count($column)
511 {
512 return "COUNT( {$column} )";
513 }
514
522 public function max($column)
523 {
524 return "MAX( {$column} )";
525 }
526
534 public function min($column)
535 {
536 return "MIN( {$column} )";
537 }
538
546 public function sum($column)
547 {
548 return "SUM( {$column} )";
549 }
550
558 public function length($column)
559 {
560 return $this->platform->getLengthExpression($column);
561 }
562
571 public function round($column, $decimals)
572 {
573 return $this->platform->getRoundExpression($column, $decimals);
574 }
575
585 public function mod($expression1, $expression2)
586 {
587 return $this->platform->getModExpression($expression1, $expression2);
588 }
589
596 public function now()
597 {
598 return $this->platform->getNowExpression();
599 }
600
612 public function subString($value, $from, $len = null)
613 {
614 return $this->platform->getSubstringExpression($value, $from, $len);
615 }
616
625 public function concat()
626 {
627 $args = func_get_args();
628
629 return call_user_func_array([$this->platform, 'getConcatExpression'], $args);
630 }
631
640 public function position($substr, $value)
641 {
642 return $this->platform->getLocateExpression($value, $substr);
643 }
644
652 public function lower($value)
653 {
654 return $this->platform->getLowerExpression($value);
655 }
656
664 public function upper($value)
665 {
666 return $this->platform->getUpperExpression($value);
667 }
668
677 public function bitAnd($value1, $value2)
678 {
679 return $this->platform->getBitAndComparisonExpression($value1, $value2);
680 }
681
690 public function bitOr($value1, $value2)
691 {
692 return $this->platform->getBitOrComparisonExpression($value1, $value2);
693 }
694
719 public function searchedCase()
720 {
721 $args = func_get_args();
722
723 if (count($args) === 0) {
724 throw new QueryException('Expected at least one parameter in searchedCase()');
725 }
726
727 $expr = ' CASE';
728 foreach ($args as $arg) {
729 if (is_array($arg) && count($arg) == 2) {
730 $column1 = $arg[0];
731 $column2 = $arg[1];
732 $expr .= " WHEN {$column1} THEN {$column2}";
733 } elseif (is_scalar($arg)) {
734 $column = $arg;
735 $expr .= " ELSE {$column}";
736 }
737 }
738 $expr .= ' END ';
739
740 return $expr;
741 }
742
758 private function basicMath($type)
759 {
760 $args = func_get_args();
761 $elements = $this->arrayFlatten(array_slice($args, 1));
762 if (count($elements) < 1) {
763 throw new QueryException(
764 "The operation '{$type}' expected at least 1 argument but none provided."
765 );
766 }
767 if (count($elements) == 1) {
768 return $elements[0];
769 } else {
770 return '( ' . implode(" $type ", $elements) . ' )';
771 }
772 }
773
781 private function arrayFlatten(array $array)
782 {
783 $flat = [];
784 foreach ($array as $arg) {
785 switch (gettype($arg)) {
786 case 'array':
787 $flat = array_merge($flat, $arg);
788 break;
789
790 default:
791 $flat[] = $arg;
792 break;
793 }
794 }
795
796 return $flat;
797 }
798 }
eZ\Publish\Core\Persistence\Database\QueryException
Class QueryException.
Definition: QueryException.php:18
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression
Class DoctrineExpression.
Definition: DoctrineExpression.php:20
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\lt
lt($value1, $value2)
Returns the SQL to check if one value is less than another value.
Definition: DoctrineExpression.php:334
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\div
div()
Returns the SQL to divide values or expressions by eachother.
Definition: DoctrineExpression.php:231
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\mul
mul()
Returns the SQL to multiply values or expressions by eachother.
Definition: DoctrineExpression.php:204
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\arrayFlatten
arrayFlatten(array $array)
Returns all the elements in $array as one large single dimensional array.
Definition: DoctrineExpression.php:781
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\sum
sum($column)
Returns the total sum of a column.
Definition: DoctrineExpression.php:546
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\subString
subString($value, $from, $len=null)
Returns part of a string.
Definition: DoctrineExpression.php:612
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\avg
avg($column)
Returns the average value of a column.
Definition: DoctrineExpression.php:495
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\basicMath
basicMath($type)
Returns the SQL to perform the same mathematical operation over an array of values or expressions.
Definition: DoctrineExpression.php:758
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\not
not($expression)
Returns the SQL for a logical not, negating the $expression.
Definition: DoctrineExpression.php:125
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\in
in($column)
Returns the SQL to check if a value is one in a set of given values.
Definition: DoctrineExpression.php:386
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\bitAnd
bitAnd($value1, $value2)
Returns the SQL that performs the bitwise AND on two values.
Definition: DoctrineExpression.php:677
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\sub
sub()
Returns the SQL to subtract values or expressions from eachother.
Definition: DoctrineExpression.php:177
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\upper
upper($value)
Returns the SQL to change all characters to uppercase.
Definition: DoctrineExpression.php:664
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\combine
combine(array $args, $by)
Combine an array of expression by OR/AND.
Definition: DoctrineExpression.php:67
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\count
count($column)
Returns the number of rows (without a NULL value) of a column.
Definition: DoctrineExpression.php:510
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\add
add()
Returns the SQL to add values or expressions together.
Definition: DoctrineExpression.php:150
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\now
now()
Returns the current system date and time in the database internal format.
Definition: DoctrineExpression.php:596
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\eq
eq($value1, $value2)
Returns the SQL to check if two values are equal.
Definition: DoctrineExpression.php:253
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\lower
lower($value)
Returns the SQL to change all characters to lowercase.
Definition: DoctrineExpression.php:652
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\isNull
isNull($expression)
Returns SQL that checks if a expression is null.
Definition: DoctrineExpression.php:440
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\neq
neq($value1, $value2)
Returns the SQL to check if two values are unequal.
Definition: DoctrineExpression.php:273
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\position
position($substr, $value)
Returns the SQL to locate the position of the first occurrence of a substring.
Definition: DoctrineExpression.php:640
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\$platform
$platform
Definition: DoctrineExpression.php:25
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\between
between($expression, $value1, $value2)
Returns SQL that checks if an expression evaluates to a value between two values.
Definition: DoctrineExpression.php:468
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\gt
gt($value1, $value2)
Returns the SQL to check if one value is greater than another value.
Definition: DoctrineExpression.php:293
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\max
max($column)
Returns the highest value of a column.
Definition: DoctrineExpression.php:522
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\mod
mod($expression1, $expression2)
Returns the remainder of the division operation $expression1 / $expression2.
Definition: DoctrineExpression.php:585
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\lAnd
lAnd()
Returns the SQL to bind logical expressions together using a logical and.
Definition: DoctrineExpression.php:103
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\round
round($column, $decimals)
Rounds a numeric field to the number of decimals specified.
Definition: DoctrineExpression.php:571
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\__construct
__construct(Connection $connection)
Definition: DoctrineExpression.php:27
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\lte
lte($value1, $value2)
Returns the SQL to check if one value is less than or equal to another value.
Definition: DoctrineExpression.php:355
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\gte
gte($value1, $value2)
Returns the SQL to check if one value is greater than or equal to another value.
Definition: DoctrineExpression.php:314
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\length
length($column)
Returns the length of text field $column.
Definition: DoctrineExpression.php:558
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\lOr
lOr()
Returns the SQL to bind logical expressions together using a logical or.
Definition: DoctrineExpression.php:52
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\like
like($expression, $pattern)
Match a partial string in a column.
Definition: DoctrineExpression.php:483
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\min
min($column)
Returns the lowest value of a column.
Definition: DoctrineExpression.php:534
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\concat
concat()
Returns a series of strings concatinated.
Definition: DoctrineExpression.php:625
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\searchedCase
searchedCase()
Returns a searched CASE statement.
Definition: DoctrineExpression.php:719
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\$connection
$connection
Definition: DoctrineExpression.php:22
eZ\Publish\Core\Persistence\Doctrine\DoctrineExpression\bitOr
bitOr($value1, $value2)
Returns the SQL that performs the bitwise OR on two values.
Definition: DoctrineExpression.php:690
eZ\Publish\Core\Persistence\Doctrine\SubselectDoctrineQuery
Class SubselectDoctrineQuery.
Definition: SubselectDoctrineQuery.php:18
eZ\Publish\Core\Persistence\Doctrine\SubselectDoctrineQuery\getQuery
getQuery()
Returns the SQL string for the subselect.
Definition: SubselectDoctrineQuery.php:151
eZ\Publish\Core\Persistence\Database\Expression
Interface for generation of all the expressions for database abstractions.
Definition: Expression.php:16
eZ\Publish\Core\Persistence\Doctrine
Definition: AbstractDoctrineQuery.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.