PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Query/Mysql/Round.php

https://github.com/beberlei/DoctrineExtensions
PHP | 43 lines | 32 code | 9 blank | 2 comment | 2 complexity | 53a5925ed038fe24f1fc6340d09cb00b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace DoctrineExtensions\Query\Mysql;
  3. use Doctrine\ORM\Query\AST\Functions\FunctionNode;
  4. use Doctrine\ORM\Query\Lexer;
  5. class Round extends FunctionNode
  6. {
  7. private $firstExpression = null;
  8. private $secondExpression = null;
  9. public function parse(\Doctrine\ORM\Query\Parser $parser)
  10. {
  11. $lexer = $parser->getLexer();
  12. $parser->match(Lexer::T_IDENTIFIER);
  13. $parser->match(Lexer::T_OPEN_PARENTHESIS);
  14. $this->firstExpression = $parser->SimpleArithmeticExpression();
  15. // parse second parameter if available
  16. if (Lexer::T_COMMA === $lexer->lookahead['type']) {
  17. $parser->match(Lexer::T_COMMA);
  18. $this->secondExpression = $parser->ArithmeticPrimary();
  19. }
  20. $parser->match(Lexer::T_CLOSE_PARENTHESIS);
  21. }
  22. public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
  23. {
  24. // use second parameter if parsed
  25. if (null !== $this->secondExpression) {
  26. return 'ROUND('
  27. . $this->firstExpression->dispatch($sqlWalker)
  28. . ', '
  29. . $this->secondExpression->dispatch($sqlWalker)
  30. . ')';
  31. }
  32. return 'ROUND(' . $this->firstExpression->dispatch($sqlWalker) . ')';
  33. }
  34. }