| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Condition.php 7490 2010-03-29 19:53:27Z 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.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Query_Condition
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Query
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision: 7490 $
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
*/
|
|
|
33 |
abstract class Doctrine_Query_Condition extends Doctrine_Query_Part
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* DQL CONDITION PARSER
|
|
|
37 |
* parses the join condition/where/having part of the query string
|
|
|
38 |
*
|
|
|
39 |
* @param string $str
|
|
|
40 |
* @return string
|
|
|
41 |
*/
|
|
|
42 |
public function parse($str)
|
|
|
43 |
{
|
|
|
44 |
$tmp = trim($str);
|
|
|
45 |
|
|
|
46 |
$parts = $this->_tokenizer->bracketExplode($str, array(' OR '), '(', ')');
|
|
|
47 |
|
|
|
48 |
if (count($parts) > 1) {
|
|
|
49 |
$ret = array();
|
|
|
50 |
foreach ($parts as $part) {
|
|
|
51 |
$part = $this->_tokenizer->bracketTrim($part, '(', ')');
|
|
|
52 |
$ret[] = $this->parse($part);
|
|
|
53 |
}
|
|
|
54 |
$r = implode(' OR ', $ret);
|
|
|
55 |
} else {
|
|
|
56 |
$parts = $this->_tokenizer->bracketExplode($str, array(' AND '), '(', ')');
|
|
|
57 |
|
|
|
58 |
// Ticket #1388: We need to make sure we're not splitting a BETWEEN ... AND ... clause
|
|
|
59 |
$tmp = array();
|
|
|
60 |
|
|
|
61 |
for ($i = 0, $l = count($parts); $i < $l; $i++) {
|
|
|
62 |
$test = $this->_tokenizer->sqlExplode($parts[$i]);
|
|
|
63 |
|
|
|
64 |
if (count($test) == 3 && strtoupper($test[1]) == 'BETWEEN') {
|
|
|
65 |
$tmp[] = $parts[$i] . ' AND ' . $parts[++$i];
|
|
|
66 |
} else if (count($test) == 4 && strtoupper($test[1]) == 'NOT' && strtoupper($test[2]) == 'BETWEEN') {
|
|
|
67 |
$tmp[] = $parts[$i] . ' AND ' . $parts[++$i];
|
|
|
68 |
} else {
|
|
|
69 |
$tmp[] = $parts[$i];
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$parts = $tmp;
|
|
|
74 |
unset($tmp);
|
|
|
75 |
|
|
|
76 |
if (count($parts) > 1) {
|
|
|
77 |
$ret = array();
|
|
|
78 |
foreach ($parts as $part) {
|
|
|
79 |
$part = $this->_tokenizer->bracketTrim($part, '(', ')');
|
|
|
80 |
$ret[] = $this->parse($part);
|
|
|
81 |
}
|
|
|
82 |
$r = implode(' AND ', $ret);
|
|
|
83 |
} else {
|
|
|
84 |
// Fix for #710
|
|
|
85 |
if (substr($parts[0],0,1) == '(' && substr($parts[0], -1) == ')') {
|
|
|
86 |
return $this->parse(substr($parts[0], 1, -1));
|
|
|
87 |
} else {
|
|
|
88 |
// Processing NOT here
|
|
|
89 |
if (strtoupper(substr($parts[0], 0, 4)) === 'NOT ') {
|
|
|
90 |
$r = 'NOT ('.$this->parse(substr($parts[0], 4)).')';
|
|
|
91 |
} else {
|
|
|
92 |
return $this->load($parts[0]);
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
return '(' . $r . ')';
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* parses a literal value and returns the parsed value
|
|
|
103 |
*
|
|
|
104 |
* boolean literals are parsed to integers
|
|
|
105 |
* components are parsed to associated table aliases
|
|
|
106 |
*
|
|
|
107 |
* @param string $value literal value to be parsed
|
|
|
108 |
* @return string
|
|
|
109 |
*/
|
|
|
110 |
public function parseLiteralValue($value)
|
|
|
111 |
{
|
|
|
112 |
// check that value isn't a string
|
|
|
113 |
if (strpos($value, '\'') === false) {
|
|
|
114 |
// parse booleans
|
|
|
115 |
$value = $this->query->getConnection()
|
|
|
116 |
->dataDict->parseBoolean($value);
|
|
|
117 |
|
|
|
118 |
$a = explode('.', $value);
|
|
|
119 |
|
|
|
120 |
if (count($a) > 1) {
|
|
|
121 |
// either a float or a component..
|
|
|
122 |
|
|
|
123 |
if ( ! is_numeric($a[0])) {
|
|
|
124 |
// a component found
|
|
|
125 |
$field = array_pop($a);
|
|
|
126 |
$reference = implode('.', $a);
|
|
|
127 |
$value = $this->query->getConnection()->quoteIdentifier(
|
|
|
128 |
$this->query->getSqlTableAlias($reference). '.' . $field
|
|
|
129 |
);
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
} else {
|
|
|
133 |
// string literal found
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
return $value;
|
|
|
137 |
}
|
|
|
138 |
}
|