Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

accept more than 2 parameters in CONCAT function #583

Closed
wants to merge 5 commits into from

5 participants

@broncha

The DBAL Platform supports more then 2 parameters but the ConcatFunction only validates 2 parameters to CONCAT. This commit allows to pass more than 2 parameters to CONCAT. Also this change would require that getConcatExpression accept array as a parameter. I have opened a pull request for that as well.

Here is the pull request : doctrine/dbal#275

I also propose that the function getConcatExpression only accept array of string rather than multiple string arguments.

@doctrinebot
Collaborator

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DDC-2304

lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
@@ -56,10 +59,13 @@ public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
-
- $this->firstStringPrimary = $parser->StringPrimary();
- $parser->match(Lexer::T_COMMA);
- $this->secondStringPrimary = $parser->StringPrimary();
+
+ $this->concatExpressions[] = $parser->StringPrimary();
+
+ while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
+ $parser->match(Lexer::T_COMMA);
+ $this->concatExpressions[] = $parser->StringPrimary();

Wrong indentation by tab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
@@ -22,7 +22,7 @@
use Doctrine\ORM\Query\Lexer;
/**
- * "CONCAT" "(" StringPrimary "," StringPrimary ")"
+ * "CONCAT" "(" StringPrimary "," StringPrimary "," StringPrimary ",...)"
@stof
stof added a note

This is not the right syntax for this in EBNF It should be "CONCAT" "(" StringPrimary "," StringPrimary {"," StringPrimary }* ")"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
((9 lines not shown))
/**
* @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 $e){
@stof
stof added a note

missing space after foreach. And you should name the variable $expression rather than $e to have a meanignful name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
@@ -34,19 +34,22 @@
*/
class ConcatFunction extends FunctionNode
{
- public $firstStringPrimary;
- public $secondStringPrimary;
-
+ public $concatExpressions = array();
+
@stof
stof added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
((9 lines not shown))
/**
* @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 $e){
+ $args[] = $sqlWalker->walkStringPrimary($this->firstStringPrimary);
@stof
stof added a note

This is broken. $this->firstStringPrimary does not exist anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
@@ -34,19 +34,22 @@
*/
class ConcatFunction extends FunctionNode
{
- public $firstStringPrimary;
- public $secondStringPrimary;
-
+ public $concatExpressions = array();
@stof
stof added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
@@ -56,10 +59,13 @@ public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
-
- $this->firstStringPrimary = $parser->StringPrimary();
- $parser->match(Lexer::T_COMMA);
- $this->secondStringPrimary = $parser->StringPrimary();
+
+ $this->concatExpressions[] = $parser->StringPrimary();
+
+ while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
@stof
stof added a note

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof

The new feature should be tested.
And you should run the tests when doing changes (or at least look at the Travis result on the pull request) as they spotted the issue about the wrong property

@broncha

doctrine2/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php is passing given the DBAL Platform has been patched. I doubt if the travis build will pass as the pull request I sent fot DBAL has not been merged

@stof

If this depend on an unmerged PR in DBAL, please add the link in the description of the PR.

lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
((9 lines not shown))
/**
* @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 $platform->getConcatExpression( $args );
@stof
stof added a note

you should remove the extra spaces around $args

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
@@ -56,10 +59,15 @@ public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
-
- $this->firstStringPrimary = $parser->StringPrimary();
- $parser->match(Lexer::T_COMMA);
- $this->secondStringPrimary = $parser->StringPrimary();
+
+ $this->concatExpressions[] = $parser->StringPrimary();
+ $parser->match(Lexer::T_COMMA);
+ $this->concatExpressions[] = $parser->StringPrimary();
+
+ while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
+ $parser->match(Lexer::T_COMMA);
+ $this->concatExpressions[] = $parser->StringPrimary();
@stof
stof added a note

wrong indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof

you still need to add a unit test (in https://github.com/doctrine/doctrine2/blob/master/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php) for the case where you have more than 2 expressions

@broncha

Unit test SelectSqlGenerationTest::testSupportsMoreThanTwoParametersInConcatFunction added and passing..
Indentation looks fine in eclipse, but seems distorted when I push. I dont know whats happening.

@broncha broncha calling getConcatExpression using call_user_func_array as number of a…
…rguments is not known removing dependency to patch DBAL
3a3d053
@broncha

I have pushed a new commit, which does not require doctrine/dbal#275 to be merged.
Travis build should now pass

@beberlei
Owner

Merged in 4841a06

@beberlei beberlei closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 19, 2013
  1. @broncha
Commits on Feb 20, 2013
  1. @broncha
  2. @broncha
Commits on Feb 26, 2013
  1. @broncha

    calling getConcatExpression using call_user_func_array as number of a…

    broncha authored
    …rguments is not known removing dependency to patch DBAL
Commits on Feb 28, 2013
  1. @broncha
This page is out of date. Refresh to see the latest.
View
32 lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
@@ -22,7 +22,7 @@
use Doctrine\ORM\Query\Lexer;
/**
- * "CONCAT" "(" StringPrimary "," StringPrimary ")"
+ * "CONCAT" "(" StringPrimary "," StringPrimary {"," StringPrimary }* ")"
*
*
* @link www.doctrine-project.org
@@ -35,18 +35,25 @@
class ConcatFunction extends FunctionNode
{
public $firstStringPrimary;
+
public $secondStringPrimary;
-
+
+ public $concatExpressions = array();
+
/**
* @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);
}
/**
@@ -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)) {
+ $parser->match(Lexer::T_COMMA);
+ $this->concatExpressions[] = $parser->StringPrimary();
+ }
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
View
43 tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
@@ -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
Something went wrong with that request. Please try again.