~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Query/Where.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: Where.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_Where
 
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_Where extends Doctrine_Query_Condition
 
34
{
 
35
    public function load($where) 
 
36
    {
 
37
        $where = $this->_tokenizer->bracketTrim(trim($where));
 
38
        $conn  = $this->query->getConnection();
 
39
        $terms = $this->_tokenizer->sqlExplode($where);  
 
40
 
 
41
        if (count($terms) > 1) {
 
42
            if (substr($where, 0, 6) == 'EXISTS') {
 
43
                return $this->parseExists($where, true);
 
44
            } elseif (substr($where, 0, 10) == 'NOT EXISTS') {
 
45
                return $this->parseExists($where, false);
 
46
            }
 
47
        }
 
48
 
 
49
        if (count($terms) < 3) {
 
50
            $terms = $this->_tokenizer->sqlExplode($where, array('=', '<', '<>', '>', '!='));
 
51
        }
 
52
 
 
53
        if (count($terms) > 1) {
 
54
            $first = array_shift($terms);
 
55
            $value = array_pop($terms);
 
56
            $operator = trim(substr($where, strlen($first), -strlen($value)));
 
57
            $table = null;
 
58
            $field = null;
 
59
 
 
60
            if (strpos($first, "'") === false && strpos($first, '(') === false) {
 
61
                // normal field reference found
 
62
                $a = explode('.', $first);
 
63
        
 
64
                $field = array_pop($a);
 
65
                $reference = implode('.', $a);
 
66
                
 
67
                if (empty($reference)) {
 
68
                    $map = $this->query->getRootDeclaration();  
 
69
                    
 
70
                    $alias = $this->query->getTableAlias($this->query->getRootAlias());
 
71
                    $table = $map['table'];
 
72
                } else {
 
73
                    $map = $this->query->load($reference, false);
 
74
    
 
75
                    $alias = $this->query->getTableAlias($reference);
 
76
                    $table = $map['table'];
 
77
                }
 
78
            }
 
79
            $first = $this->query->parseClause($first);
 
80
            
 
81
            $sql = $first . ' ' . $operator . ' ' . $this->parseValue($value, $table, $field);
 
82
        
 
83
            return $sql;  
 
84
        } else {
 
85
            return $where;
 
86
        }
 
87
    }
 
88
 
 
89
    public function parseValue($value, Doctrine_Table $table = null, $field = null)
 
90
    {
 
91
        $conn = $this->query->getConnection();
 
92
 
 
93
        // If value is contained in paranthesis
 
94
        if (substr($value, 0, 1) == '(') {
 
95
            // trim brackets
 
96
            $trimmed = $this->_tokenizer->bracketTrim($value);
 
97
 
 
98
            // If subquery found which begins with FROM and SELECT
 
99
            // FROM User u WHERE u.id IN(SELECT u.id FROM User u WHERE u.id = 1)
 
100
            if (substr($trimmed, 0, 4) == 'FROM' ||
 
101
                substr($trimmed, 0, 6) == 'SELECT') {
 
102
 
 
103
                // subquery found
 
104
                $q     = $this->query->createSubquery()->parseQuery($trimmed, false);
 
105
                $sql   = $q->getSql();
 
106
                $value = '(' . $sql . ')';
 
107
                $q->free();
 
108
            // If custom sql for custom subquery
 
109
            // You can specify SQL: followed by any valid sql expression
 
110
            // FROM User u WHERE u.id = SQL:(select id from user where id = 1)
 
111
            } elseif (substr($trimmed, 0, 4) == 'SQL:') {
 
112
                $value = '(' . substr($trimmed, 4) . ')';
 
113
            // simple in expression found
 
114
            } else {
 
115
                $e = $this->_tokenizer->sqlExplode($trimmed, ',');
 
116
 
 
117
                $value = array();
 
118
 
 
119
                $index = false;
 
120
 
 
121
                foreach ($e as $part) {
 
122
                    $value[] = $this->parseLiteralValue($part);
 
123
                }
 
124
 
 
125
                $value = '(' . implode(', ', $value) . ')';
 
126
            }
 
127
        } else {
 
128
            $value = $this->parseLiteralValue($value);
 
129
        }
 
130
        return $value;
 
131
    }
 
132
 
 
133
    /**
 
134
     * parses an EXISTS expression
 
135
     *
 
136
     * @param string $where         query where part to be parsed
 
137
     * @param boolean $negation     whether or not to use the NOT keyword
 
138
     * @return string
 
139
     */
 
140
    public function parseExists($where, $negation)
 
141
    {
 
142
        $operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';
 
143
 
 
144
        $pos = strpos($where, '(');
 
145
 
 
146
        if ($pos == false) {
 
147
            throw new Doctrine_Query_Exception('Unknown expression, expected a subquery with () -marks');
 
148
        }
 
149
 
 
150
        $sub = $this->_tokenizer->bracketTrim(substr($where, $pos));
 
151
 
 
152
        $q = $this->query->createSubquery()->parseQuery($sub, false);
 
153
        $sql = $q->getSql();
 
154
        $q->free();
 
155
        return $operator . ' (' . $sql . ')';
 
156
    }
 
157
}