Commit 63449646 authored by Bernhard Schussek's avatar Bernhard Schussek

Merge branch 'hotfix/concat' into 'master'

Overrode the CONCAT() function to support automatic string casting

See merge request !9
parents 5f1184b8 9626ffe6
<?php
/*
* This file is part of the CWD Data Doctrine ORM Bundle
*
* (c) cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cwd\DataDoctrineORMBundle\Func\Postgresql\String;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* Doctrine's native implementation uses PostgreSQL's "||" operator, which
* does not automatically cast non-string types (e.g. INT) to string.
*
* Use CONCAT() instead which casts automatically.
*/
class Concat extends FunctionNode
{
private $expressions = [];
/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->expressions[] = $parser->StringPrimary();
$parser->match(Lexer::T_COMMA);
$this->expressions[] = $parser->StringPrimary();
while (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) {
$parser->match(Lexer::T_COMMA);
$this->expressions[] = $parser->StringPrimary();
}
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
/**
* {@inheritdoc}
*/
public function getSql(SqlWalker $sqlWalker)
{
return sprintf(
'CONCAT(%s)',
implode(', ', array_map(function ($expr) use ($sqlWalker) {
return $expr->dispatch($sqlWalker);
}, $this->expressions))
);
}
}
......@@ -44,6 +44,9 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
/**
* @var string[]
*
* @deprecated We're always casting to text now. Setting this field is not
* required anymore.
*/
private $explicitCasts;
......@@ -78,13 +81,13 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
sprintf(', \'%s\', ', $this->separator),
array_map(
function ($sortField) use ($alias) {
return $this->generateFieldName($sortField, $alias);
return $this->addAlias($sortField, $alias);
},
$this->fieldNames
)
)
)
: $this->generateFieldName($this->fieldNames[0], $alias);
: sprintf('CAST(%s AS TEXT)', $this->addAlias($this->fieldNames[0], $alias));
$qb->addSelect(sprintf('%s as offset', $offsetField));
}
......@@ -200,14 +203,6 @@ class CompositeFieldsOffsetStrategy implements OffsetStrategy
return $this->defaultDirections;
}
private function generateFieldName($fieldName, $alias)
{
return in_array($fieldName, $this->explicitCasts, true)
? sprintf('CAST(%s AS TEXT)', $this->addAlias($fieldName, $alias))
: $this->addAlias($fieldName, $alias)
;
}
private function addAlias(string $fieldName, string $alias): string
{
if (false !== ($pos = strpos($fieldName, '.'))) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment