forked from beberlei/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ExtractFunction for PostgreSQL (beberlei#331)
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Query\Postgresql; | ||
|
||
use Doctrine\ORM\Query\AST\ASTException; | ||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\AST\PathExpression; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\QueryException; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
|
||
class ExtractFunction extends FunctionNode | ||
{ | ||
/** @var string */ | ||
private $field; | ||
|
||
/** @var PathExpression */ | ||
private $value; | ||
|
||
/** | ||
* @param SqlWalker $sqlWalker | ||
* | ||
* @throws ASTException | ||
* @return string | ||
*/ | ||
public function getSql(SqlWalker $sqlWalker): string | ||
{ | ||
return sprintf( | ||
'EXTRACT(%s FROM %s)', | ||
$this->field, | ||
$this->value->dispatch($sqlWalker) | ||
); | ||
} | ||
|
||
/** | ||
* @param Parser $parser | ||
* | ||
* @throws QueryException | ||
*/ | ||
public function parse(Parser $parser): void | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
|
||
$parser->match(Lexer::T_IDENTIFIER); | ||
$this->field = $parser->getLexer()->token['value']; | ||
|
||
$parser->match(Lexer::T_FROM); | ||
|
||
$this->value = $parser->ScalarExpression(); | ||
|
||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace DoctrineExtensions\Tests\Query\Postgresql; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use DoctrineExtensions\Tests\Query\PostgresqlTestCase; | ||
|
||
class ExtractFunctionTest extends PostgresqlTestCase | ||
{ | ||
public function testExtract(): void | ||
{ | ||
$queryBuilder = new QueryBuilder($this->entityManager); | ||
$queryBuilder | ||
->select('extract(EPOCH FROM dt.created)') | ||
->from('DoctrineExtensions\Tests\Entities\Date', 'dt'); | ||
|
||
$expected = 'SELECT EXTRACT(EPOCH FROM d0_.created) AS sclr_0 FROM Date d0_'; | ||
|
||
$this->assertEquals($expected, $queryBuilder->getQuery()->getSQL()); | ||
} | ||
} |