Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept more than 2 parameters in CONCAT function #583

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Doctrine\ORM\Query\Lexer;

/**
* "CONCAT" "(" StringPrimary "," StringPrimary ")"
* "CONCAT" "(" StringPrimary "," StringPrimary {"," StringPrimary }* ")"
*
*
* @link www.doctrine-project.org
Expand All @@ -35,18 +35,25 @@
class ConcatFunction extends FunctionNode
{
public $firstStringPrimary;

public $secondStringPrimary;


public $concatExpressions = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the public properties is a BC break for any code dealing with the AST


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all trailing whitespaces. Empty line should be really empty

/**
* @override
*/
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
$platform = $sqlWalker->getConnection()->getDatabasePlatform();
return $platform->getConcatExpression(
$sqlWalker->walkStringPrimary($this->firstStringPrimary),
$sqlWalker->walkStringPrimary($this->secondStringPrimary)
);

$args = array();

foreach ($this->concatExpressions as $expression) {
$args[] = $sqlWalker->walkStringPrimary($expression);
}

return call_user_func_array(array($platform,'getConcatExpression'), $args);
}

/**
Expand All @@ -56,10 +63,19 @@ public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

$this->firstStringPrimary = $parser->StringPrimary();
$this->concatExpressions[] = $this->firstStringPrimary;

$parser->match(Lexer::T_COMMA);
$this->secondStringPrimary = $parser->StringPrimary();

$this->secondStringPrimary = $parser->StringPrimary();
$this->concatExpressions[] = $this->secondStringPrimary;

while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not require to have at least 2 arguments. It is wrong IMO.

$parser->match(Lexer::T_COMMA);
$this->concatExpressions[] = $parser->StringPrimary();
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
Expand Down
43 changes: 43 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,49 @@ public function testCaseThenFunction()
'SELECT CASE WHEN LENGTH(c0_.name) > ? THEN SUBSTRING(c0_.name FROM 0 FOR ?) ELSE TRIM(c0_.name) END AS sclr0 FROM cms_users c0_'
);
}

/**
* @group DDC-2268
*/

public function testSupportsMoreThanTwoParametersInConcatFunction()
{
$connMock = $this->_em->getConnection();
$orgPlatform = $connMock->getDatabasePlatform();

$connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform);
$this->assertSqlGeneration(
"SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, u.status, 's') = ?1",
"SELECT c0_.id AS id0 FROM cms_users c0_ WHERE CONCAT(c0_.name, c0_.status, 's') = ?"
);
$this->assertSqlGeneration(
"SELECT CONCAT(u.id, u.name, u.status) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
"SELECT CONCAT(c0_.id, c0_.name, c0_.status) AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
);

$connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
$this->assertSqlGeneration(
"SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, u.status, 's') = ?1",
"SELECT c0_.id AS id0 FROM cms_users c0_ WHERE c0_.name || c0_.status || 's' = ?"
);
$this->assertSqlGeneration(
"SELECT CONCAT(u.id, u.name, u.status) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
"SELECT c0_.id || c0_.name || c0_.status AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
);


$connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\SQLServerPlatform());
$this->assertSqlGeneration(
"SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, u.status, 's') = ?1",
"SELECT c0_.id AS id0 FROM cms_users c0_ WITH (NOLOCK) WHERE (c0_.name + c0_.status + 's') = ?"
);
$this->assertSqlGeneration(
"SELECT CONCAT(u.id, u.name, u.status) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
"SELECT (c0_.id + c0_.name + c0_.status) AS sclr0 FROM cms_users c0_ WITH (NOLOCK) WHERE c0_.id = ?"
);

$connMock->setDatabasePlatform($orgPlatform);
}
}

class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode
Expand Down