1 |
<?php
|
2 |
/*
|
3 |
* $Id: Where.php 2702 2007-10-03 21:43:22Z Jonathan.Wage $
|
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 |
* @subpackage Query
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
28 |
* @link www.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision: 2702 $
|
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);
|
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 = Doctrine_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 |
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);
|
89 |
|
90 |
return $sql;
|
91 |
} else {
|
92 |
|
93 |
}
|
94 |
}
|
95 |
|
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);
|
101 |
|
102 |
if (substr($trimmed, 0, 4) == 'FROM' ||
|
103 |
substr($trimmed, 0, 6) == 'SELECT') {
|
104 |
|
105 |
// subquery found
|
106 |
$q = new Doctrine_Query();
|
107 |
$value = '(' . $q->isSubquery(true)->parseQuery($trimmed)->getQuery() . ')';
|
108 |
|
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, ',');
|
114 |
|
115 |
$value = array();
|
116 |
|
117 |
$index = false;
|
118 |
|
119 |
foreach ($e as $part) {
|
120 |
if (isset($table) && isset($field)) {
|
121 |
$index = $table->enumIndex($field, trim($part, "'"));
|
122 |
}
|
123 |
|
124 |
if ($index !== false) {
|
125 |
$value[] = $index;
|
126 |
} else {
|
127 |
$value[] = $this->parseLiteralValue($part);
|
128 |
}
|
129 |
}
|
130 |
|
131 |
$value = '(' . implode(', ', $value) . ')';
|
132 |
}
|
133 |
} elseif (substr($value, 0, 1) == ':' || $value === '?') {
|
134 |
// placeholder found
|
135 |
if (isset($table) && isset($field) && $table->getTypeOf($field) == 'enum') {
|
136 |
$this->query->addEnumParam($value, $table, $field);
|
137 |
} else {
|
138 |
$this->query->addEnumParam($value, null, null);
|
139 |
}
|
140 |
} else {
|
141 |
$enumIndex = false;
|
142 |
if (isset($table) && isset($field)) {
|
143 |
// check if value is enumerated value
|
144 |
$enumIndex = $table->enumIndex($field, trim($value, "'"));
|
145 |
}
|
146 |
|
147 |
if ($enumIndex !== false) {
|
148 |
$value = $enumIndex;
|
149 |
} else {
|
150 |
$value = $this->parseLiteralValue($value);
|
151 |
}
|
152 |
}
|
153 |
return $value;
|
154 |
}
|
155 |
/**
|
156 |
* parses an EXISTS expression
|
157 |
*
|
158 |
* @param string $where query where part to be parsed
|
159 |
* @param boolean $negation whether or not to use the NOT keyword
|
160 |
* @return string
|
161 |
*/
|
162 |
public function parseExists($where, $negation)
|
163 |
{
|
164 |
$operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';
|
165 |
|
166 |
$pos = strpos($where, '(');
|
167 |
|
168 |
if ($pos == false) {
|
169 |
throw new Doctrine_Query_Exception('Unknown expression, expected a subquery with () -marks');
|
170 |
}
|
171 |
|
172 |
$sub = Doctrine_Tokenizer::bracketTrim(substr($where, $pos));
|
173 |
|
174 |
return $operator . ' (' . $this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
|
175 |
}
|
176 |
} |