Skip to content
Open
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
18 changes: 14 additions & 4 deletions src/Query/Mysql/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@ class Format extends FunctionNode

public $patternExpression = null;

public $localeExpression = null;

public function parse(\Doctrine\ORM\Query\Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->numberExpression = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->patternExpression = $parser->SimpleArithmeticExpression();

if (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) {
$parser->match(Lexer::T_COMMA);
$this->localeExpression = $parser->SimpleArithmeticExpression();
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker): string
{
return 'FORMAT(' .
$this->numberExpression->dispatch($sqlWalker) . ', ' .
$this->patternExpression->dispatch($sqlWalker) .
')';
return sprintf(
'FORMAT(%s, %s, %s)',
$this->numberExpression->dispatch($sqlWalker),
$this->patternExpression->dispatch($sqlWalker),
$this->localeExpression !== null ? $this->localeExpression->dispatch($sqlWalker) : 'NULL'
);
}
}
10 changes: 9 additions & 1 deletion tests/Query/Mysql/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ public function testFormat()
{
$this->assertDqlProducesSql(
"SELECT FORMAT(1000.00, 2) from DoctrineExtensions\Tests\Entities\Blank b",
'SELECT FORMAT(1000.00, 2) AS sclr_0 FROM Blank b0_'
'SELECT FORMAT(1000.00, 2, NULL) AS sclr_0 FROM Blank b0_'
);
}

public function testFormatWithLocalParameter()
{
$this->assertDqlProducesSql(
"SELECT FORMAT(1000.00, 2, 'de_DE') from DoctrineExtensions\Tests\Entities\Blank b",
"SELECT FORMAT(1000.00, 2, 'de_DE') AS sclr_0 FROM Blank b0_"
);
}
}