Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Persisters / SqlExpressionVisitor.php
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 MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\ORM\Persisters;
21
22 use Doctrine\Common\Collections\Expr\ExpressionVisitor;
23 use Doctrine\Common\Collections\Expr\Comparison;
24 use Doctrine\Common\Collections\Expr\Value;
25 use Doctrine\Common\Collections\Expr\CompositeExpression;
26
27 /**
28  * Visit Expressions and generate SQL WHERE conditions from them.
29  *
30  * @author Benjamin Eberlei <kontakt@beberlei.de>
31  * @since 2.3
32  */
33 class SqlExpressionVisitor extends ExpressionVisitor
34 {
35     /**
36      * @var \Doctrine\ORM\Persisters\BasicEntityPersister
37      */
38     private $persister;
39
40     /**
41      * @param \Doctrine\ORM\Persisters\BasicEntityPersister $persister
42      */
43     public function __construct(BasicEntityPersister $persister)
44     {
45         $this->persister = $persister;
46     }
47
48     /**
49      * Convert a comparison expression into the target query language output
50      *
51      * @param \Doctrine\Common\Collections\Expr\Comparison $comparison
52      *
53      * @return mixed
54      */
55     public function walkComparison(Comparison $comparison)
56     {
57         $field = $comparison->getField();
58         $value = $comparison->getValue()->getValue(); // shortcut for walkValue()
59
60         return $this->persister->getSelectConditionStatementSQL($field, $value, null, $comparison->getOperator());
61     }
62
63     /**
64      * Convert a composite expression into the target query language output
65      *
66      * @param \Doctrine\Common\Collections\Expr\CompositeExpression $expr
67      *
68      * @return mixed
69      */
70     public function walkCompositeExpression(CompositeExpression $expr)
71     {
72         $expressionList = array();
73
74         foreach ($expr->getExpressionList() as $child) {
75             $expressionList[] = $this->dispatch($child);
76         }
77
78         switch($expr->getType()) {
79             case CompositeExpression::TYPE_AND:
80                 return '(' . implode(' AND ', $expressionList) . ')';
81
82             case CompositeExpression::TYPE_OR:
83                 return '(' . implode(' OR ', $expressionList) . ')';
84
85             default:
86                 throw new \RuntimeException("Unknown composite " . $expr->getType());
87         }
88     }
89
90     /**
91      * Convert a value expression into the target query language part.
92      *
93      * @param \Doctrine\Common\Collections\Expr\Value $value
94      *
95      * @return mixed
96      */
97     public function walkValue(Value $value)
98     {
99         return '?';
100     }
101 }
102