TrimFunction.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Query\AST\Functions;
  20. use Doctrine\ORM\Query\Lexer;
  21. use Doctrine\DBAL\Platforms\AbstractPlatform;
  22. /**
  23. * "TRIM" "(" [["LEADING" | "TRAILING" | "BOTH"] [char] "FROM"] StringPrimary ")"
  24. *
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @link www.doctrine-project.org
  27. * @since 2.0
  28. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  29. * @author Jonathan Wage <jonwage@gmail.com>
  30. * @author Roman Borschel <roman@code-factory.org>
  31. * @author Benjamin Eberlei <kontakt@beberlei.de>
  32. */
  33. class TrimFunction extends FunctionNode
  34. {
  35. public $leading;
  36. public $trailing;
  37. public $both;
  38. public $trimChar = false;
  39. public $stringPrimary;
  40. /**
  41. * @override
  42. */
  43. public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
  44. {
  45. $pos = AbstractPlatform::TRIM_UNSPECIFIED;
  46. if ($this->leading) {
  47. $pos = AbstractPlatform::TRIM_LEADING;
  48. } else if ($this->trailing) {
  49. $pos = AbstractPlatform::TRIM_TRAILING;
  50. } else if ($this->both) {
  51. $pos = AbstractPlatform::TRIM_BOTH;
  52. }
  53. return $sqlWalker->getConnection()->getDatabasePlatform()->getTrimExpression(
  54. $sqlWalker->walkStringPrimary($this->stringPrimary),
  55. $pos,
  56. ($this->trimChar != false) ? $sqlWalker->getConnection()->quote($this->trimChar) : false
  57. );
  58. }
  59. /**
  60. * @override
  61. */
  62. public function parse(\Doctrine\ORM\Query\Parser $parser)
  63. {
  64. $lexer = $parser->getLexer();
  65. $parser->match(Lexer::T_IDENTIFIER);
  66. $parser->match(Lexer::T_OPEN_PARENTHESIS);
  67. if (strcasecmp('leading', $lexer->lookahead['value']) === 0) {
  68. $parser->match(Lexer::T_LEADING);
  69. $this->leading = true;
  70. } else if (strcasecmp('trailing', $lexer->lookahead['value']) === 0) {
  71. $parser->match(Lexer::T_TRAILING);
  72. $this->trailing = true;
  73. } else if (strcasecmp('both', $lexer->lookahead['value']) === 0) {
  74. $parser->match(Lexer::T_BOTH);
  75. $this->both = true;
  76. }
  77. if ($lexer->isNextToken(Lexer::T_STRING)) {
  78. $parser->match(Lexer::T_STRING);
  79. $this->trimChar = $lexer->token['value'];
  80. }
  81. if ($this->leading || $this->trailing || $this->both || $this->trimChar) {
  82. $parser->match(Lexer::T_FROM);
  83. }
  84. $this->stringPrimary = $parser->StringPrimary();
  85. $parser->match(Lexer::T_CLOSE_PARENTHESIS);
  86. }
  87. }