1 |
<?php
|
2 |
/*
|
3 |
* $Id: Condition.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_Part');
|
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.phpdoctrine.org
|
29 |
* @since 1.0
|
30 |
* @version $Revision: 3212 $
|
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(' \&\& ', ' AND '), '(', ')');
|
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(' AND ', $ret);
|
55 |
} else {
|
56 |
|
57 |
$parts = $this->_tokenizer->bracketExplode($str, array(' \|\| ', ' OR '), '(', ')');
|
58 |
if (count($parts) > 1) {
|
59 |
$ret = array();
|
60 |
foreach ($parts as $part) {
|
61 |
$part = $this->_tokenizer->bracketTrim($part, '(', ')');
|
62 |
$ret[] = $this->parse($part);
|
63 |
}
|
64 |
$r = implode(' OR ', $ret);
|
65 |
} else {
|
66 |
if (substr($parts[0],0,1) == '(' && substr($parts[0], -1) == ')') {
|
67 |
return $this->parse(substr($parts[0], 1, -1));
|
68 |
} else {
|
69 |
return $this->load($parts[0]);
|
70 |
}
|
71 |
}
|
72 |
}
|
73 |
|
74 |
return '(' . $r . ')';
|
75 |
}
|
76 |
|
77 |
|
78 |
|
79 |
/**
|
80 |
* parses a literal value and returns the parsed value
|
81 |
*
|
82 |
* boolean literals are parsed to integers
|
83 |
* components are parsed to associated table aliases
|
84 |
*
|
85 |
* @param string $value literal value to be parsed
|
86 |
* @return string
|
87 |
*/
|
88 |
public function parseLiteralValue($value)
|
89 |
{
|
90 |
// check that value isn't a string
|
91 |
if (strpos($value, '\'') === false) {
|
92 |
// parse booleans
|
93 |
$value = $this->query->getConnection()
|
94 |
->dataDict->parseBoolean($value);
|
95 |
|
96 |
$a = explode('.', $value);
|
97 |
|
98 |
if (count($a) > 1) {
|
99 |
// either a float or a component..
|
100 |
|
101 |
if ( ! is_numeric($a[0])) {
|
102 |
// a component found
|
103 |
$value = $this->query->getTableAlias($a[0]). '.' . $a[1];
|
104 |
}
|
105 |
}
|
106 |
} else {
|
107 |
// string literal found
|
108 |
}
|
109 |
|
110 |
return $value;
|
111 |
}
|
112 |
}
|