diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index d1c4ac2fe..e670c7fa2 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -321,10 +321,10 @@ abstract class AbstractPlatform * * SQLite only supports the 2 parameter variant of this function * - * @param string $value an sql string literal or column name/alias - * @param integer $position where to start the substring portion - * @param integer $length the substring portion length - * @return string SQL substring function with given parameters + * @param string $value an sql string literal or column name/alias + * @param integer $from where to start the substring portion + * @param integer $len the substring portion length + * @return string */ public function getSubstringExpression($value, $from, $len = null) { diff --git a/lib/Doctrine/ORM/Query/AST/Functions/SubstringFunction.php b/lib/Doctrine/ORM/Query/AST/Functions/SubstringFunction.php index 16fce7a2d..acc8903d8 100644 --- a/lib/Doctrine/ORM/Query/AST/Functions/SubstringFunction.php +++ b/lib/Doctrine/ORM/Query/AST/Functions/SubstringFunction.php @@ -38,21 +38,23 @@ class SubstringFunction extends FunctionNode { public $stringPrimary; public $firstSimpleArithmeticExpression; - public $secondSimpleArithmeticExpression; + public $secondSimpleArithmeticExpression = null; /** * @override */ public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) { - //TODO: Use platform to get SQL - return 'SUBSTRING(' - . $sqlWalker->walkStringPrimary($this->stringPrimary) - . ', ' - . $sqlWalker->walkSimpleArithmeticExpression($this->firstSimpleArithmeticExpression) - . ', ' - . $sqlWalker->walkSimpleArithmeticExpression($this->secondSimpleArithmeticExpression) - . ')'; + $optionalSecondSimpleArithmeticExpression = null; + if ($this->secondSimpleArithmeticExpression !== null) { + $optionalSecondSimpleArithmeticExpression = $sqlWalker->walkSimpleArithmeticExpression($this->secondSimpleArithmeticExpression); + } + + return $sqlWalker->getConnection()->getDatabasePlatform()->getSubstringExpression( + $sqlWalker->walkStringPrimary($this->stringPrimary), + $sqlWalker->walkSimpleArithmeticExpression($this->firstSimpleArithmeticExpression), + $optionalSecondSimpleArithmeticExpression + ); } /** @@ -70,10 +72,12 @@ class SubstringFunction extends FunctionNode $parser->match(Lexer::T_COMMA); $this->firstSimpleArithmeticExpression = $parser->SimpleArithmeticExpression(); + + if ($lexer->isNextToken(Lexer::T_COMMA)) { + $parser->match(Lexer::T_COMMA); - $parser->match(Lexer::T_COMMA); - - $this->secondSimpleArithmeticExpression = $parser->SimpleArithmeticExpression(); + $this->secondSimpleArithmeticExpression = $parser->SimpleArithmeticExpression(); + } $parser->match(Lexer::T_CLOSE_PARENTHESIS); } diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php index 9f8a13d61..d58e29cb5 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php @@ -165,9 +165,10 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase public function testFunctionSubstring() { - $this->markTestSkipped('SUBSTRING does not exist on Oracle and Sqlite.'); + $dql = "SELECT m, SUBSTRING(m.name, 1, 3) AS str1, SUBSTRING(m.name, 5) AS str2 ". + "FROM Doctrine\Tests\Models\Company\CompanyManager m"; - $result = $this->_em->createQuery("SELECT m, SUBSTRING(m.name, 1, 3) AS str1 FROM Doctrine\Tests\Models\Company\CompanyManager m") + $result = $this->_em->createQuery($dql) ->getArrayResult(); $this->assertEquals(4, count($result)); @@ -175,6 +176,10 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals('Ben', $result[1]['str1']); $this->assertEquals('Gui', $result[2]['str1']); $this->assertEquals('Jon', $result[3]['str1']); + $this->assertEquals('n B.', $result[0]['str2']); + $this->assertEquals('amin E.', $result[1]['str2']); + $this->assertEquals('herme B.', $result[2]['str2']); + $this->assertEquals('than W.', $result[3]['str2']); } public function testFunctionTrim()