Coverage for Doctrine_Query_Where

Back to coverage report

1 <?php
2 /*
3  *  $Id: Where.php 3884 2008-02-22 18:26:35Z jwage $
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the LGPL. For more information, see
19  * <http://www.phpdoctrine.org>.
20  */
21 Doctrine::autoload('Doctrine_Query_Condition');
22 /**
23  * Doctrine_Query_Where
24  *
25  * @package     Doctrine
26  * @subpackage  Query
27  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28  * @link        www.phpdoctrine.org
29  * @since       1.0
30  * @version     $Revision: 3884 $
31  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32  */
33 class Doctrine_Query_Where extends Doctrine_Query_Condition
34 {
35     public function load($where) 
36     {
37         $where = $this->_tokenizer->bracketTrim(trim($where));
38         $conn  = $this->query->getConnection();
39         $terms = $this->_tokenizer->sqlExplode($where);  
40
41         if (count($terms) > 1) {
42             if (substr($where, 0, 6) == 'EXISTS') {
43                 return $this->parseExists($where, true);
44             } elseif (substr($where, 0, 10) == 'NOT EXISTS') {
45                 return $this->parseExists($where, false);
46             }
47         }
48
49         if (count($terms) < 3) {
50             $terms = $this->_tokenizer->sqlExplode($where, array('=', '<', '<>', '>', '!='));
51         }
52
53         if (count($terms) > 1) {
54             $first = array_shift($terms);
55             $value = array_pop($terms);
56             $operator = trim(substr($where, strlen($first), -strlen($value)));
57             $table = null;
58             $field = null;
59
60             if (strpos($first, "'") === false && strpos($first, '(') === false) {
61                 // normal field reference found
62                 $a = explode('.', $first);
63         
64                 $field = array_pop($a);
65                 $reference = implode('.', $a);
66                 
67                 if (empty($reference)) {
68                     $map = $this->query->getRootDeclaration();  
69                     
70                     $alias = $this->query->getTableAlias($this->query->getRootAlias());
71                     $table = $map['table'];
72                 } else {
73                     $map = $this->query->load($reference, false);
74     
75                     $alias = $this->query->getTableAlias($reference);
76                     $table = $map['table'];
77                 }
78             }
79             $first = $this->query->parseClause($first);
80             
81             $sql = $first . ' ' . $operator . ' ' . $this->parseValue($value, $table, $field);
82         
83             return $sql;  
84         } else {
85
86         }
87     }
88
89     public function parseValue($value, Doctrine_Table $table = null, $field = null)
90     {
91         $conn = $this->query->getConnection();
92
93         if (substr($value, 0, 1) == '(') {
94             // trim brackets
95             $trimmed = $this->_tokenizer->bracketTrim($value);
96
97             if (substr($trimmed, 0, 4) == 'FROM' ||
98                 substr($trimmed, 0, 6) == 'SELECT') {
99
100                 // subquery found
101                 $q     = new Doctrine_Query();
102                 $value = '(' . $this->query->createSubquery()->parseQuery($trimmed, false)->getQuery() . ')';
103
104             } elseif (substr($trimmed, 0, 4) == 'SQL:') {
105                 $value = '(' . substr($trimmed, 4) . ')';
106             } else {
107                 // simple in expression found
108                 $e = $this->_tokenizer->sqlExplode($trimmed, ',');
109
110                 $value = array();
111
112                 $index = false;
113
114                 foreach ($e as $part) {
115                     if (isset($table) && isset($field)) {
116                         $index = $table->enumIndex($field, trim($part, "'"));
117
118                         if (false !== $index && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
119                             $index = $conn->quote($index, 'text');
120                         }
121                     }
122
123                     if ($index !== false) {
124                         $value[] = $index;
125                     } else {
126                         $value[] = $this->parseLiteralValue($part);
127                     }
128                 }
129
130                 $value = '(' . implode(', ', $value) . ')';
131             }
132         } else if (substr($value, 0, 1) == ':' || $value === '?') {
133             // placeholder found
134             if (isset($table) && isset($field) && $table->getTypeOf($field) == 'enum') {
135                 $this->query->addEnumParam($value, $table, $field);
136             } else {
137                 $this->query->addEnumParam($value, null, null);
138             }
139         } else {
140             $enumIndex = false;
141             if (isset($table) && isset($field)) {
142                 // check if value is enumerated value
143                 $enumIndex = $table->enumIndex($field, trim($value, "'"));
144
145                 if (false !== $enumIndex && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
146                     $enumIndex = $conn->quote($enumIndex, 'text');
147                 }
148             }
149
150             if ($enumIndex !== false) {
151                 $value = $enumIndex;
152             } else {
153                 $value = $this->parseLiteralValue($value);
154             }
155         }
156         return $value;
157     }
158
159     /**
160      * parses an EXISTS expression
161      *
162      * @param string $where         query where part to be parsed
163      * @param boolean $negation     whether or not to use the NOT keyword
164      * @return string
165      */
166     public function parseExists($where, $negation)
167     {
168         $operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';
169
170         $pos = strpos($where, '(');
171
172         if ($pos == false) {
173             throw new Doctrine_Query_Exception('Unknown expression, expected a subquery with () -marks');
174         }
175
176         $sub = $this->_tokenizer->bracketTrim(substr($where, $pos));
177
178         return $operator . ' (' . $this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
179     }
180 }