1 |
<?php
|
2 |
/*
|
3 |
* $Id: Where.php 3212 2007-11-24 18:58:33Z romanb $
|
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: 3212 $
|
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 |
if (substr($value, 0, 1) == '(') {
|
92 |
// trim brackets
|
93 |
$trimmed = $this->_tokenizer->bracketTrim($value);
|
94 |
|
95 |
if (substr($trimmed, 0, 4) == 'FROM' ||
|
96 |
substr($trimmed, 0, 6) == 'SELECT') {
|
97 |
|
98 |
// subquery found
|
99 |
$q = new Doctrine_Query();
|
100 |
$value = '(' . $this->query->createSubquery()->parseQuery($trimmed, false)->getQuery() . ')';
|
101 |
|
102 |
} elseif (substr($trimmed, 0, 4) == 'SQL:') {
|
103 |
$value = '(' . substr($trimmed, 4) . ')';
|
104 |
} else {
|
105 |
// simple in expression found
|
106 |
$e = $this->_tokenizer->sqlExplode($trimmed, ',');
|
107 |
|
108 |
$value = array();
|
109 |
|
110 |
$index = false;
|
111 |
|
112 |
foreach ($e as $part) {
|
113 |
if (isset($table) && isset($field)) {
|
114 |
$index = $table->enumIndex($field, trim($part, "'"));
|
115 |
}
|
116 |
|
117 |
if ($index !== false) {
|
118 |
$value[] = $index;
|
119 |
} else {
|
120 |
$value[] = $this->parseLiteralValue($part);
|
121 |
}
|
122 |
}
|
123 |
|
124 |
$value = '(' . implode(', ', $value) . ')';
|
125 |
}
|
126 |
} else if (substr($value, 0, 1) == ':' || $value === '?') {
|
127 |
// placeholder found
|
128 |
if (isset($table) && isset($field) && $table->getTypeOf($field) == 'enum') {
|
129 |
$this->query->addEnumParam($value, $table, $field);
|
130 |
} else {
|
131 |
$this->query->addEnumParam($value, null, null);
|
132 |
}
|
133 |
} else {
|
134 |
$enumIndex = false;
|
135 |
if (isset($table) && isset($field)) {
|
136 |
// check if value is enumerated value
|
137 |
$enumIndex = $table->enumIndex($field, trim($value, "'"));
|
138 |
}
|
139 |
|
140 |
if ($enumIndex !== false) {
|
141 |
$value = $enumIndex;
|
142 |
} else {
|
143 |
$value = $this->parseLiteralValue($value);
|
144 |
}
|
145 |
}
|
146 |
return $value;
|
147 |
}
|
148 |
|
149 |
/**
|
150 |
* parses an EXISTS expression
|
151 |
*
|
152 |
* @param string $where query where part to be parsed
|
153 |
* @param boolean $negation whether or not to use the NOT keyword
|
154 |
* @return string
|
155 |
*/
|
156 |
public function parseExists($where, $negation)
|
157 |
{
|
158 |
$operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';
|
159 |
|
160 |
$pos = strpos($where, '(');
|
161 |
|
162 |
if ($pos == false) {
|
163 |
throw new Doctrine_Query_Exception('Unknown expression, expected a subquery with () -marks');
|
164 |
}
|
165 |
|
166 |
$sub = $this->_tokenizer->bracketTrim(substr($where, $pos));
|
167 |
|
168 |
return $operator . ' (' . $this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
|
169 |
}
|
170 |
}
|