|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * expression-types.php. |
| 5 | + * |
| 6 | + * |
| 7 | + * Copyright (c) 2010-2013, Justin Swanhart |
| 8 | + * with contributions by André Rothe <[email protected], [email protected]> |
| 9 | + * and David Négrier <[email protected]> |
| 10 | + * |
| 11 | + * All rights reserved. |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without modification, |
| 14 | + * are permitted provided that the following conditions are met: |
| 15 | + * |
| 16 | + * * Redistributions of source code must retain the above copyright notice, |
| 17 | + * this list of conditions and the following disclaimer. |
| 18 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 19 | + * this list of conditions and the following disclaimer in the documentation |
| 20 | + * and/or other materials provided with the distribution. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 23 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 24 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 25 | + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 | + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 31 | + * DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +namespace SQLParser\Node; |
| 35 | + |
| 36 | +use Doctrine\DBAL\Connection; |
| 37 | +use Mouf\MoufManager; |
| 38 | +use Mouf\MoufInstanceDescriptor; |
| 39 | +use SQLParser\Node\Traverser\VisitorInterface; |
| 40 | + |
| 41 | +/** |
| 42 | + * This class represents a constant of a LIMIT in an SQL expression. |
| 43 | + * |
| 44 | + * @author David MAECHLER <[email protected]> |
| 45 | + */ |
| 46 | +class LimitNode implements NodeInterface |
| 47 | +{ |
| 48 | + private $value; |
| 49 | + |
| 50 | + public function getValue() |
| 51 | + { |
| 52 | + return $this->value; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Sets the value. |
| 57 | + * |
| 58 | + * @Important |
| 59 | + * |
| 60 | + * @param string $value |
| 61 | + */ |
| 62 | + public function setValue($value) |
| 63 | + { |
| 64 | + $this->value = $value; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns a Mouf instance descriptor describing this object. |
| 69 | + * |
| 70 | + * @param MoufManager $moufManager |
| 71 | + * |
| 72 | + * @return MoufInstanceDescriptor |
| 73 | + */ |
| 74 | + public function toInstanceDescriptor(MoufManager $moufManager) |
| 75 | + { |
| 76 | + $instanceDescriptor = $moufManager->createInstance(get_called_class()); |
| 77 | + $instanceDescriptor->getProperty('value')->setValue(NodeFactory::nodeToInstanceDescriptor($this->value, $moufManager)); |
| 78 | + |
| 79 | + return $instanceDescriptor; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Renders the object as a SQL string. |
| 84 | + * |
| 85 | + * @param array $parameters |
| 86 | + * @param Connection $dbConnection |
| 87 | + * @param int|number $indent |
| 88 | + * @param int $conditionsMode |
| 89 | + * @return string |
| 90 | + * @throws \Exception |
| 91 | + */ |
| 92 | + public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
| 93 | + { |
| 94 | + if($this->value === null) { |
| 95 | + throw new \Exception('A limit parameter must be an integer'); |
| 96 | + } |
| 97 | + |
| 98 | + if(is_numeric($this->value)) { |
| 99 | + return (int) $this->value; |
| 100 | + } else if ($dbConnection != null) { |
| 101 | + return $dbConnection->quote($this->value); |
| 102 | + } else { |
| 103 | + return addslashes($this->value); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Walks the tree of nodes, calling the visitor passed in parameter. |
| 109 | + * |
| 110 | + * @param VisitorInterface $visitor |
| 111 | + * @return NodeInterface|null|string Can return null if nothing is to be done or a node that should replace this node, or NodeTraverser::REMOVE_NODE to remove the node |
| 112 | + */ |
| 113 | + public function walk(VisitorInterface $visitor) { |
| 114 | + $node = $this; |
| 115 | + $result = $visitor->enterNode($node); |
| 116 | + if ($result instanceof NodeInterface) { |
| 117 | + $node = $result; |
| 118 | + } |
| 119 | + return $visitor->leaveNode($node); |
| 120 | + } |
| 121 | +} |
0 commit comments