~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Query/JoinCondition.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 *  $Id: JoinCondition.php 5501 2009-02-16 20:35:26Z 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.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: 5501 $
 
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 
32
 */
 
33
class Doctrine_Query_JoinCondition extends Doctrine_Query_Condition 
 
34
{
 
35
    public function load($condition) 
 
36
    {
 
37
        $condition = trim($condition);
 
38
        $e = $this->_tokenizer->sqlExplode($condition);
 
39
 
 
40
        if (($l = count($e)) > 2) {
 
41
            $leftExpr = $this->query->parseClause($e[0]);
 
42
            $operator  = $e[1];
 
43
 
 
44
            if ($l == 4) {
 
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
 
49
 
 
50
                unset($e[3]); // Remove unused index
 
51
            } else if ($l >= 5) {
 
52
                // FIX: "field BETWEEN field2 AND field3" issue
 
53
                // Related to ticket #1488
 
54
                $e[2] .= ' ' . $e[3] . ' ' . $e[4];
 
55
                
 
56
                unset($e[3], $e[4]); // Remove unused indexes
 
57
            }
 
58
            
 
59
            if (substr(trim($e[2]), 0, 1) != '(') {
 
60
                $expr = new Doctrine_Expression($e[2], $this->query->getConnection());
 
61
                $e[2] = $expr->getSql();
 
62
            }
 
63
 
 
64
            // We need to check for agg functions here
 
65
            $rightMatches = array();
 
66
            $hasRightAggExpression = $this->_processPossibleAggExpression($e[2], $rightMatches);
 
67
 
 
68
            // Defining needed information
 
69
            $value = $e[2];
 
70
 
 
71
            if (substr($value, 0, 1) == '(') {
 
72
                // trim brackets
 
73
                $trimmed   = $this->_tokenizer->bracketTrim($value);
 
74
                $trimmed_upper = strtoupper($trimmed);
 
75
 
 
76
                if (substr($trimmed_upper, 0, 4) == 'FROM' || substr($trimmed_upper, 0, 6) == 'SELECT') {
 
77
                    // subquery found
 
78
                    $q     = $this->query->createSubquery()->parseQuery($trimmed, false);
 
79
                    $value   = '(' . $q->getSql() . ')';
 
80
                    $q->free();
 
81
                } elseif (substr($trimmed_upper, 0, 4) == 'SQL:') {
 
82
                    // Change due to bug "(" XXX ")"
 
83
                    //$value = '(' . substr($trimmed, 4) . ')';
 
84
                    $value = substr($trimmed, 4);
 
85
                } else {
 
86
                    // simple in expression found
 
87
                    $e = $this->_tokenizer->sqlExplode($trimmed, ',');
 
88
                    $value = array();
 
89
                    
 
90
                    foreach ($e as $part) {
 
91
                        $value[] = $this->parseLiteralValue($part);
 
92
                    }
 
93
 
 
94
                    $value = '(' . implode(', ', $value) . ')';
 
95
                }
 
96
            } else {
 
97
                // Possible expression found (field1 AND field2)
 
98
                // In relation to ticket #1488
 
99
                $e     = $this->_tokenizer->bracketExplode($value, array(' AND ', ' \&\& '), '(', ')');
 
100
                $value = array();
 
101
                    
 
102
                foreach ($e as $part) {
 
103
                    $value[] = $this->parseLiteralValue($part);
 
104
                }
 
105
 
 
106
                $value = implode(' AND ', $value);
 
107
            }
 
108
 
 
109
            switch ($operator) {
 
110
                case '<':
 
111
                case '>':
 
112
                case '=':
 
113
                case '!=':
 
114
                default:
 
115
                    $rightExpr = (($hasRightAggExpression) ? $rightMatches[1] . '(' : '')
 
116
                              . $value
 
117
                              . (($hasRightAggExpression) ? $rightMatches[3] . ')' : '') ;
 
118
 
 
119
                    $condition  = $leftExpr . ' ' . $operator . ' ' . $rightExpr;
 
120
            }
 
121
 
 
122
            return $condition;
 
123
        }
 
124
        
 
125
        $parser = new Doctrine_Query_Where($this->query, $this->_tokenizer);
 
126
 
 
127
        return $parser->parse($condition);
 
128
    }
 
129
    
 
130
    
 
131
    protected function _processPossibleAggExpression(& $expr, & $matches = array())
 
132
    {
 
133
        $hasAggExpr = preg_match('/(.*[^\s\(\=])\(([^\)]*)\)(.*)/', $expr, $matches);
 
134
        
 
135
        if ($hasAggExpr) {
 
136
            $expr = $matches[2];
 
137
 
 
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], ' )'), ',');
 
141
                
 
142
                $matches[3] = array();
 
143
                    
 
144
                foreach ($xplod as $part) {
 
145
                    if ($part != '') {
 
146
                        $matches[3][] = $this->parseLiteralValue($part);
 
147
                    }
 
148
                }
 
149
 
 
150
                $matches[3] = '), ' . implode(', ', $matches[3]);
 
151
            }
 
152
        }
 
153
        
 
154
        return $hasAggExpr;
 
155
    }
 
156
}