3
* $Id: JoinCondition.php 5501 2009-02-16 20:35:26Z jwage $
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.
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>.
23
* Doctrine_Query_JoinCondition
27
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28
* @link www.phpdoctrine.org
30
* @version $Revision: 5501 $
31
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
33
class Doctrine_Query_JoinCondition extends Doctrine_Query_Condition
35
public function load($condition)
37
$condition = trim($condition);
38
$e = $this->_tokenizer->sqlExplode($condition);
40
if (($l = count($e)) > 2) {
41
$leftExpr = $this->query->parseClause($e[0]);
45
// FIX: "field NOT IN (XXX)" issue
46
// Related to ticket #1329
47
$operator .= ' ' . $e[2]; // Glue "NOT" and "IN"
48
$e[2] = $e[3]; // Move "(XXX)" to previous index
50
unset($e[3]); // Remove unused index
52
// FIX: "field BETWEEN field2 AND field3" issue
53
// Related to ticket #1488
54
$e[2] .= ' ' . $e[3] . ' ' . $e[4];
56
unset($e[3], $e[4]); // Remove unused indexes
59
if (substr(trim($e[2]), 0, 1) != '(') {
60
$expr = new Doctrine_Expression($e[2], $this->query->getConnection());
61
$e[2] = $expr->getSql();
64
// We need to check for agg functions here
65
$rightMatches = array();
66
$hasRightAggExpression = $this->_processPossibleAggExpression($e[2], $rightMatches);
68
// Defining needed information
71
if (substr($value, 0, 1) == '(') {
73
$trimmed = $this->_tokenizer->bracketTrim($value);
74
$trimmed_upper = strtoupper($trimmed);
76
if (substr($trimmed_upper, 0, 4) == 'FROM' || substr($trimmed_upper, 0, 6) == 'SELECT') {
78
$q = $this->query->createSubquery()->parseQuery($trimmed, false);
79
$value = '(' . $q->getSql() . ')';
81
} elseif (substr($trimmed_upper, 0, 4) == 'SQL:') {
82
// Change due to bug "(" XXX ")"
83
//$value = '(' . substr($trimmed, 4) . ')';
84
$value = substr($trimmed, 4);
86
// simple in expression found
87
$e = $this->_tokenizer->sqlExplode($trimmed, ',');
90
foreach ($e as $part) {
91
$value[] = $this->parseLiteralValue($part);
94
$value = '(' . implode(', ', $value) . ')';
97
// Possible expression found (field1 AND field2)
98
// In relation to ticket #1488
99
$e = $this->_tokenizer->bracketExplode($value, array(' AND ', ' \&\& '), '(', ')');
102
foreach ($e as $part) {
103
$value[] = $this->parseLiteralValue($part);
106
$value = implode(' AND ', $value);
115
$rightExpr = (($hasRightAggExpression) ? $rightMatches[1] . '(' : '')
117
. (($hasRightAggExpression) ? $rightMatches[3] . ')' : '') ;
119
$condition = $leftExpr . ' ' . $operator . ' ' . $rightExpr;
125
$parser = new Doctrine_Query_Where($this->query, $this->_tokenizer);
127
return $parser->parse($condition);
131
protected function _processPossibleAggExpression(& $expr, & $matches = array())
133
$hasAggExpr = preg_match('/(.*[^\s\(\=])\(([^\)]*)\)(.*)/', $expr, $matches);
138
// We need to process possible comma separated items
139
if (substr(trim($matches[3]), 0, 1) == ',') {
140
$xplod = $this->_tokenizer->sqlExplode(trim($matches[3], ' )'), ',');
142
$matches[3] = array();
144
foreach ($xplod as $part) {
146
$matches[3][] = $this->parseLiteralValue($part);
150
$matches[3] = '), ' . implode(', ', $matches[3]);