# Source for file Where.php

Documentation is available at Where.php

1. <?php
2. /*
3. * $Id: Where.php 2224 2007-08-13 21:23:56Z zYne $
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.com>.
20. */
21. Doctrine::autoload('Doctrine_Query_Condition');
22. /**
23. * Doctrine_Query_Where
24. *
25. * @package Doctrine
26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @category Object Relational Mapping
28. * @link www.phpdoctrine.com
29. * @since 1.0
30. * @version $Revision: 2224 $
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 = Doctrine_Tokenizer::bracketTrim(trim($where));
38. $conn = $this->query->getConnection();
39. $terms = Doctrine_Tokenizer::sqlExplode($where);
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. }
49. if (count($terms) < 3) {
50. $terms = Doctrine_Tokenizer::sqlExplode($where, array('=', '<', '<>', '>', '!='));
51. }
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;
60. if (strpos($first, "'") === false && strpos($first, '(') === false) {
61. // normal field reference found
62. $a = explode('.', $first);
64. $field = array_pop($a);
65. $reference = implode('.', $a);
67. if (empty($reference)) {
68. $map = $this->query->getRootDeclaration();
70. $alias = $this->query->getTableAlias($this->query->getRootAlias());
71. $table = $map['table'];
72. } else {
73. $map = $this->query->load($reference, false);
75. $alias = $this->query->getTableAlias($reference);
76. $table = $map['table'];
77. }
78. if ($this->query->getType() === Doctrine_Query::SELECT) {
79. $first = $conn->quoteIdentifier($alias)
80. . '.'
81. . $conn->quoteIdentifier($table->getColumnName($field));
82. } else {
83. $first = $conn->quoteIdentifier($table->getColumnName($field));
84. }
85. } else {
86. $first = $this->query->parseClause($first);
87. }
88. $sql = $first . ' ' . $operator . ' ' . $this->parseValue($value, $table, $field);
90. return $sql;
91. } else {
93. }
94. }
96. public function parseValue($value, Doctrine_Table $table = null, $field = null)
97. {
98. if (substr($value, 0, 1) == '(') {
99. // trim brackets
100. $trimmed = Doctrine_Tokenizer::bracketTrim($value);
102. if (substr($trimmed, 0, 4) == 'FROM' ||
103. substr($trimmed, 0, 6) == 'SELECT') {
105. // subquery found
106. $q = new Doctrine_Query();
107. $value = '(' . $q->isSubquery(true)->parseQuery($trimmed)->getQuery() . ')';
109. } elseif (substr($trimmed, 0, 4) == 'SQL:') {
110. $value = '(' . substr($trimmed, 4) . ')';
111. } else {
112. // simple in expression found
113. $e = Doctrine_Tokenizer::sqlExplode($trimmed, ',');
115. $value = array();
117. $index = false;
119. foreach ($e as $part) {
120. if (isset($table) && isset($field)) {
121. $index = $table->enumIndex($field, trim($part, "'"));
122. }
124. if ($index !== false) {
125. $value[] = $index;
126. } else {
127. $value[] = $this->parseLiteralValue($part);
128. }
129. }
130. $value = '(' . implode(', ', $value) . ')';
131. }
132. } elseif(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. }
146. if ($enumIndex !== false) {
147. $value = $enumIndex;
148. } else {
149. $value = $this->parseLiteralValue($value);
150. }
151. }
152. return $value;
153. }
154. /**
155. * parses an EXISTS expression
156. *
157. * @param string $where query where part to be parsed
158. * @param boolean $negation whether or not to use the NOT keyword
159. * @return string
160. */
161. public function parseExists($where, $negation)
162. {
163. $operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';
165. $pos = strpos($where, '(');
167. if ($pos == false) {
168. throw new Doctrine_Query_Exception('Unknown expression, expected a subquery with () -marks');
169. }
171. $sub = Doctrine_Tokenizer::bracketTrim(substr($where, $pos));
173. return $operator . ' (' . $this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
174. }
175. }