PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Query/Sqlite/Round.php

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