1
0
mirror of synced 2024-12-13 06:46:03 +03:00

[2.0] DDC-320 - Fixed Substring across all platforms using AbstractPlatform::getSubstringExpression() instead of the "hand-built" one.

This commit is contained in:
beberlei 2010-02-11 20:19:05 +00:00
parent d197c814f3
commit 8357289050
3 changed files with 27 additions and 18 deletions

View File

@ -321,10 +321,10 @@ abstract class AbstractPlatform
* *
* SQLite only supports the 2 parameter variant of this function * SQLite only supports the 2 parameter variant of this function
* *
* @param string $value an sql string literal or column name/alias * @param string $value an sql string literal or column name/alias
* @param integer $position where to start the substring portion * @param integer $from where to start the substring portion
* @param integer $length the substring portion length * @param integer $len the substring portion length
* @return string SQL substring function with given parameters * @return string
*/ */
public function getSubstringExpression($value, $from, $len = null) public function getSubstringExpression($value, $from, $len = null)
{ {

View File

@ -38,21 +38,23 @@ class SubstringFunction extends FunctionNode
{ {
public $stringPrimary; public $stringPrimary;
public $firstSimpleArithmeticExpression; public $firstSimpleArithmeticExpression;
public $secondSimpleArithmeticExpression; public $secondSimpleArithmeticExpression = null;
/** /**
* @override * @override
*/ */
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{ {
//TODO: Use platform to get SQL $optionalSecondSimpleArithmeticExpression = null;
return 'SUBSTRING(' if ($this->secondSimpleArithmeticExpression !== null) {
. $sqlWalker->walkStringPrimary($this->stringPrimary) $optionalSecondSimpleArithmeticExpression = $sqlWalker->walkSimpleArithmeticExpression($this->secondSimpleArithmeticExpression);
. ', ' }
. $sqlWalker->walkSimpleArithmeticExpression($this->firstSimpleArithmeticExpression)
. ', ' return $sqlWalker->getConnection()->getDatabasePlatform()->getSubstringExpression(
. $sqlWalker->walkSimpleArithmeticExpression($this->secondSimpleArithmeticExpression) $sqlWalker->walkStringPrimary($this->stringPrimary),
. ')'; $sqlWalker->walkSimpleArithmeticExpression($this->firstSimpleArithmeticExpression),
$optionalSecondSimpleArithmeticExpression
);
} }
/** /**
@ -70,10 +72,12 @@ class SubstringFunction extends FunctionNode
$parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_COMMA);
$this->firstSimpleArithmeticExpression = $parser->SimpleArithmeticExpression(); $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); $parser->match(Lexer::T_CLOSE_PARENTHESIS);
} }

View File

@ -165,9 +165,10 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase
public function testFunctionSubstring() 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(); ->getArrayResult();
$this->assertEquals(4, count($result)); $this->assertEquals(4, count($result));
@ -175,6 +176,10 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals('Ben', $result[1]['str1']); $this->assertEquals('Ben', $result[1]['str1']);
$this->assertEquals('Gui', $result[2]['str1']); $this->assertEquals('Gui', $result[2]['str1']);
$this->assertEquals('Jon', $result[3]['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() public function testFunctionTrim()