2 declare(strict_types = 1);
18 use Doctrine\DBAL\Platforms\AbstractPlatform;
81 public function orX(...$expressions): CompositeExpression
83 return new CompositeExpression(CompositeExpression::TYPE_OR, $expressions);
95 public function comparison($leftExpression,
string $operator, $rightExpression): string
97 return $leftExpression .
' ' . $operator .
' ' . $rightExpression;
108 public function eq(
string $fieldName, $value): string
110 return $this->
comparison($this->connection->quoteIdentifier($fieldName), static::EQ, $value);
127 public function neq(
string $fieldName, $value): string
129 return $this->
comparison($this->connection->quoteIdentifier($fieldName), static::NEQ, $value);
140 public function lt($fieldName, $value): string
142 return $this->
comparison($this->connection->quoteIdentifier($fieldName), static::LT, $value);
153 public function lte(
string $fieldName, $value): string
155 return $this->
comparison($this->connection->quoteIdentifier($fieldName), static::LTE, $value);
166 public function gt(
string $fieldName, $value): string
168 return $this->
comparison($this->connection->quoteIdentifier($fieldName), static::GT, $value);
179 public function gte(
string $fieldName, $value): string
181 return $this->
comparison($this->connection->quoteIdentifier($fieldName), static::GTE, $value);
191 public function isNull(
string $fieldName): string
193 return $this->connection->quoteIdentifier($fieldName) .
' IS NULL';
203 public function isNotNull(
string $fieldName): string
205 return $this->connection->quoteIdentifier($fieldName) .
' IS NOT NULL';
216 public function like(
string $fieldName, $value): string
218 return $this->
comparison($this->connection->quoteIdentifier($fieldName),
'LIKE', $value);
229 public function notLike(
string $fieldName, $value): string
231 return $this->
comparison($this->connection->quoteIdentifier($fieldName),
'NOT LIKE', $value);
243 public function in(
string $fieldName, $value): string
246 $this->connection->quoteIdentifier($fieldName),
248 '(' . implode(
', ', (array)$value) .
')'
261 public function notIn(
string $fieldName, $value): string
264 $this->connection->quoteIdentifier($fieldName),
266 '(' . implode(
', ', (array)$value) .
')'
280 public function inSet(
string $fieldName,
string $value,
bool $isColumn =
false): string
283 throw new \InvalidArgumentException(
284 'ExpressionBuilder::inSet() can not be used with an empty string value.',
289 if (strpos($value,
',') !==
false) {
290 throw new \InvalidArgumentException(
291 'ExpressionBuilder::inSet() can not be used with values that contain a comma (",").',
296 switch ($this->connection->getDatabasePlatform()->getName()) {
298 case 'pdo_postgresql':
303 'ANY(string_to_array(%s, %s))',
304 $this->connection->quoteIdentifier($fieldName) .
'::text',
310 throw new \RuntimeException(
311 'FIND_IN_SET support for database platform "Oracle" not yet implemented.',
319 $expression = $this->
orX(
320 $this->
eq($fieldName, $value),
321 $this->
like($fieldName, $value .
' + \',%\''),
322 $this->
like($fieldName,
'\'%,\
' + ' . $value),
323 $this->
like($fieldName,
'\'%,\
' + ' . $value .
' + \',%\'')
326 $likeEscapedValue = str_replace(
331 $expression = $this->
orX(
333 $this->
like($fieldName, $this->
literal($likeEscapedValue .
',%')),
334 $this->
like($fieldName, $this->
literal(
'%,' . $likeEscapedValue)),
335 $this->
like($fieldName, $this->
literal(
'%,' . $likeEscapedValue .
',%'))
338 return (
string)$expression;
342 if (strpos($value,
':') === 0 || $value ===
'?') {
343 throw new \InvalidArgumentException(
344 'ExpressionBuilder::inSet() for SQLite can not be used with placeholder arguments.',
348 $comparison = sprintf(
354 $this->connection->quoteIdentifier($fieldName),
365 'cast(' . $value .
' as text)',
376 'FIND_IN_SET(%s, %s)',
378 $this->connection->quoteIdentifier($fieldName)
390 public function bitAnd(
string $fieldName,
int $value): string
392 switch ($this->connection->getDatabasePlatform()->getName()) {
397 $this->connection->quoteIdentifier($fieldName),
402 $this->connection->quoteIdentifier($fieldName),
416 public function min(
string $fieldName,
string $alias =
null): string
428 public function max(
string $fieldName,
string $alias =
null): string
440 public function avg(
string $fieldName,
string $alias =
null): string
452 public function sum(
string $fieldName,
string $alias =
null): string
464 public function count(
string $fieldName,
string $alias =
null): string
466 return $this->
calculation(
'COUNT', $fieldName, $alias);
476 public function length(
string $fieldName,
string $alias =
null): string
478 return $this->
calculation(
'LENGTH', $fieldName, $alias);
489 protected function calculation(
string $aggregateName,
string $fieldName,
string $alias =
null): string
491 $aggregateSQL = sprintf(
494 $this->connection->quoteIdentifier($fieldName)
497 if (!empty($alias)) {
498 $aggregateSQL .=
' AS ' . $this->connection->quoteIdentifier($alias);
501 return $aggregateSQL;
512 public function trim(
string $fieldName,
int $position = AbstractPlatform::TRIM_UNSPECIFIED,
string $char =
null)
514 return $this->connection->getDatabasePlatform()->getTrimExpression(
515 $this->connection->quoteIdentifier($fieldName),
517 ($char ===
null ?
false : $this->literal($char))
529 public function literal($input,
string $type =
null)
531 return $this->connection->quote($input, $type);
542 $quoteChar = $this->connection
543 ->getDatabasePlatform()
544 ->getStringLiteralQuoteCharacter();
546 $isQuoted = strpos($value, $quoteChar) === 0 && strpos(strrev($value), $quoteChar) === 0;
549 return str_replace($quoteChar . $quoteChar, $quoteChar, substr($value, 1, -1));