1
0
mirror of synced 2024-12-13 14:56:01 +03:00

[DDC-2668] Fix trim leading zero string

This commit is contained in:
Fabio B. Silva 2013-09-16 21:36:19 -04:00
parent fe78977973
commit 689da2f36b
2 changed files with 90 additions and 31 deletions

View File

@ -20,6 +20,8 @@
namespace Doctrine\ORM\Query\AST\Functions; namespace Doctrine\ORM\Query\AST\Functions;
use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
/** /**
@ -36,71 +38,60 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
class TrimFunction extends FunctionNode class TrimFunction extends FunctionNode
{ {
/** /**
* @var bool * @var boolean
*/ */
public $leading; public $leading;
/** /**
* @var bool * @var boolean
*/ */
public $trailing; public $trailing;
/** /**
* @var bool * @var boolean
*/ */
public $both; public $both;
/** /**
* @var bool * @var boolean
*/ */
public $trimChar = false; public $trimChar = false;
/**
* @var \Doctrine\ORM\Query\AST\Node
*/
public $stringPrimary; public $stringPrimary;
/** /**
* @override * {@inheritdoc}
*/ */
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) public function getSql(SqlWalker $sqlWalker)
{ {
$pos = AbstractPlatform::TRIM_UNSPECIFIED; $stringPrimary = $sqlWalker->walkStringPrimary($this->stringPrimary);
if ($this->leading) { $platform = $sqlWalker->getConnection()->getDatabasePlatform();
$pos = AbstractPlatform::TRIM_LEADING; $trimMode = $this->getTrimMode();
} else if ($this->trailing) { $trimChar = ($this->trimChar !== false)
$pos = AbstractPlatform::TRIM_TRAILING; ? $sqlWalker->getConnection()->quote($this->trimChar)
} else if ($this->both) { : false;
$pos = AbstractPlatform::TRIM_BOTH;
}
return $sqlWalker->getConnection()->getDatabasePlatform()->getTrimExpression( return $platform->getTrimExpression($stringPrimary, $trimMode, $trimChar);
$sqlWalker->walkStringPrimary($this->stringPrimary),
$pos,
($this->trimChar != false) ? $sqlWalker->getConnection()->quote($this->trimChar) : false
);
} }
/** /**
* @override * {@inheritdoc}
*/ */
public function parse(\Doctrine\ORM\Query\Parser $parser) public function parse(Parser $parser)
{ {
$lexer = $parser->getLexer(); $lexer = $parser->getLexer();
$parser->match(Lexer::T_IDENTIFIER); $parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS); $parser->match(Lexer::T_OPEN_PARENTHESIS);
if (strcasecmp('leading', $lexer->lookahead['value']) === 0) { $this->parseTrimMode($parser);
$parser->match(Lexer::T_LEADING);
$this->leading = true;
} else if (strcasecmp('trailing', $lexer->lookahead['value']) === 0) {
$parser->match(Lexer::T_TRAILING);
$this->trailing = true;
} else if (strcasecmp('both', $lexer->lookahead['value']) === 0) {
$parser->match(Lexer::T_BOTH);
$this->both = true;
}
if ($lexer->isNextToken(Lexer::T_STRING)) { if ($lexer->isNextToken(Lexer::T_STRING)) {
$parser->match(Lexer::T_STRING); $parser->match(Lexer::T_STRING);
$this->trimChar = $lexer->token['value']; $this->trimChar = $lexer->token['value'];
} }
@ -112,4 +103,61 @@ class TrimFunction extends FunctionNode
$parser->match(Lexer::T_CLOSE_PARENTHESIS); $parser->match(Lexer::T_CLOSE_PARENTHESIS);
} }
/**
* @param \Doctrine\ORM\Query\Parser $parser
*
* @return integer
*/
private function getTrimMode()
{
if ($this->leading) {
return AbstractPlatform::TRIM_LEADING;
}
if ($this->trailing) {
return AbstractPlatform::TRIM_TRAILING;
}
if ($this->both) {
return AbstractPlatform::TRIM_BOTH;
}
return AbstractPlatform::TRIM_UNSPECIFIED;
}
/**
* @param \Doctrine\ORM\Query\Parser $parser
*
* @return void
*/
private function parseTrimMode(Parser $parser)
{
$lexer = $parser->getLexer();
$value = $lexer->lookahead['value'];
if (strcasecmp('leading', $value) === 0) {
$parser->match(Lexer::T_LEADING);
$this->leading = true;
return;
}
if (strcasecmp('trailing', $value) === 0) {
$parser->match(Lexer::T_TRAILING);
$this->trailing = true;
return;
}
if (strcasecmp('both', $value) === 0) {
$parser->match(Lexer::T_BOTH);
$this->both = true;
return;
}
}
} }

View File

@ -394,6 +394,17 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
); );
} }
/**
* @group DDC-2668
*/
public function testSupportsTrimLeadingZeroString()
{
$this->assertSqlGeneration(
"SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(TRAILING '0' FROM u.name) != ''",
"SELECT c0_.name AS name0 FROM cms_users c0_ WHERE TRIM(TRAILING '0' FROM c0_.name) <> ''"
);
}
// Ticket 894 // Ticket 894
public function testSupportsBetweenClauseWithPositionalParameters() public function testSupportsBetweenClauseWithPositionalParameters()
{ {