Ticket #1262: JoinCondition.php

File JoinCondition.php, 6.0 KB (added by mm, 6 months ago)

Patched file

Line 
1<?php
2/*
3 *  $Id: $
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
22/**
23 * Doctrine_Query_JoinCondition
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: 4560 $
31 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32 */
33class Doctrine_Query_JoinCondition extends Doctrine_Query_Condition
34{
35    public function load($condition)
36    {
37        $condition = trim($condition);
38
39        $e = $this->_tokenizer->sqlExplode($condition);
40
41        if (count($e) > 2) {
42            $expr = new Doctrine_Expression($e[0], $this->query->getConnection());
43            $e[0] = $expr->getSql();
44
45            $operator  = $e[1];
46
47            if (substr(trim($e[2]), 0, 1) != '(') {
48                $expr = new Doctrine_Expression($e[2], $this->query->getConnection());
49                $e[2] = $expr->getSql();
50            }
51
52            // We need to check for agg functions here
53            $hasLeftAggExpression = preg_match('/(.*)\(([^\)]*)\)([\)]*)/', $e[0], $leftMatches);
54
55            if ($hasLeftAggExpression) {
56                $e[0] = $leftMatches[2];
57            }
58
59            $hasRightAggExpression = preg_match('/(.*)\(([^\)]*)\)([\)]*)/', $e[2], $rightMatches);
60
61            if ($hasRightAggExpression) {
62                $e[2] = $rightMatches[2];
63            }
64
65            $a         = explode('.', $e[0]);
66            $field     = array_pop($a);
67            $reference = implode('.', $a);
68            $value     = $e[2];
69
70            $conn      = $this->query->getConnection();
71            $alias     = $this->query->getTableAlias($reference);
72            $map       = $this->query->getAliasDeclaration($reference);
73            $table     = $map['table'];
74            // check if value is enumerated value
75            $enumIndex = $table->enumIndex($field, trim($value, "'"));
76
77            if (false !== $enumIndex && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
78                $enumIndex = $conn->quote($enumIndex, 'text');
79            }
80
81            // FIX: Issues with "(" XXX ")"
82            if ($hasRightAggExpression) {
83                $value = '(' . $value . ')';
84            }
85
86            if (substr($value, 0, 1) == '(') {
87                // trim brackets
88                $trimmed   = $this->_tokenizer->bracketTrim($value);
89
90                if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') {
91                    // subquery found
92                    $q = $this->query->createSubquery();
93
94                    // Change due to bug "(" XXX ")"
95                    //$value = '(' . $q->parseQuery($trimmed)->getQuery() . ')';
96                    $value = $q->parseQuery($trimmed)->getQuery();
97                } elseif (substr($trimmed, 0, 4) == 'SQL:') {
98                    // Change due to bug "(" XXX ")"
99                    //$value = '(' . substr($trimmed, 4) . ')';
100                    $value = substr($trimmed, 4);
101                } else {
102                    // simple in expression found
103                    $e     = $this->_tokenizer->sqlExplode($trimmed, ',');
104
105                    $value = array();
106                    foreach ($e as $part) {
107                        $index = $table->enumIndex($field, trim($part, "'"));
108
109                        if (false !== $index && $conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
110                            $index = $conn->quote($index, 'text');
111                        }
112
113                        if ($index !== false) {
114                            $value[] = $index;
115                        } else {
116                            $value[] = $this->parseLiteralValue($part);
117                        }
118                    }
119
120                    // Change due to bug "(" XXX ")"
121                    //$value = '(' . implode(', ', $value) . ')';
122                    $value = implode(', ', $value);
123                }
124            } else {
125                if ($enumIndex !== false) {
126                    $value = $enumIndex;
127                } else {
128                    $value = $this->parseLiteralValue($value);
129                }
130            }
131
132            switch ($operator) {
133                case '<':
134                case '>':
135                case '=':
136                case '!=':
137                    if ($enumIndex !== false) {
138                        $value  = $enumIndex;
139                    }
140                default:
141                    $leftExpr = (($hasLeftAggExpression) ? $leftMatches[1] . '(' : '')
142                              . $conn->quoteIdentifier($alias . '.' . $field)
143                              . (($hasLeftAggExpression) ? $leftMatches[3] . ')' : '') ;
144
145                    $rightExpr = (($hasRightAggExpression) ? $rightMatches[1] . '(' : '')
146                              . $value
147                              . (($hasRightAggExpression) ? $rightMatches[3] . ')' : '') ;
148
149                    $condition  = $leftExpr . ' ' . $operator . ' ' . $rightExpr;
150            }
151
152        }
153
154        return $condition;
155    }
156}