Where.php 8.94 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
Doctrine::autoload('Doctrine_Query_Condition');
/**
 * Doctrine_Query_Where
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
lsmith's avatar
lsmith committed
33 34
class Doctrine_Query_Where extends Doctrine_Query_Condition
{
doctrine's avatar
doctrine committed
35
    /**
36
     * load
doctrine's avatar
doctrine committed
37 38 39 40 41
     * returns the parsed query part
     *
     * @param string $where
     * @return string
     */
lsmith's avatar
lsmith committed
42 43
    public function load($where)
    {
44
        $where = trim($where);
zYne's avatar
zYne committed
45
        $conn  = $this->query->getConnection();
46

zYne's avatar
zYne committed
47
        $e     = Doctrine_Tokenizer::sqlExplode($where);
48

lsmith's avatar
lsmith committed
49
        if (count($e) > 1) {
50
            $tmp   = $e[0] . ' ' . $e[1];
51

lsmith's avatar
lsmith committed
52
            if (substr($tmp, 0, 6) == 'EXISTS') {
53
                return $this->parseExists($where, true);
lsmith's avatar
lsmith committed
54
            } elseif (substr($where, 0, 10) == 'NOT EXISTS') {
55
                return $this->parseExists($where, false);
lsmith's avatar
lsmith committed
56
            }
57
        }
58

lsmith's avatar
lsmith committed
59
        if (count($e) < 3) {
zYne's avatar
zYne committed
60
            $e = Doctrine_Tokenizer::sqlExplode($where, array('=', '<', '>', '!='));
zYne's avatar
zYne committed
61
        }
doctrine's avatar
doctrine committed
62
        $r = array_shift($e);
zYne's avatar
zYne committed
63

64
        $a = explode('.', $r);
doctrine's avatar
doctrine committed
65

lsmith's avatar
lsmith committed
66
        if (count($a) > 1) {
doctrine's avatar
doctrine committed
67
            $field     = array_pop($a);
68 69 70
            $count     = count($e);
            $slice     = array_slice($e, -1, 1);
            $value     = implode('', $slice);
zYne's avatar
zYne committed
71
            $operator  = trim(substr($where, strlen($r), -strlen($value)));
72

zYne's avatar
zYne committed
73
            $reference = implode('.', $a);
doctrine's avatar
doctrine committed
74 75
            $count     = count($a);

zYne's avatar
zYne committed
76
            $pos       = strpos($field, '(');
77

lsmith's avatar
lsmith committed
78
            if ($pos !== false) {
79
                $func   = substr($field, 0, $pos);
80
                $value  = trim(substr($field, ($pos + 1), -1));
81 82 83 84

                $values = Doctrine_Query::sqlExplode($value, ',');

                $field      = array_pop($a);
zYne's avatar
zYne committed
85
                $reference  = implode('.', $a);
86
                $table      = $this->query->load($reference, false);
zYne's avatar
zYne committed
87 88
                $field      = $table->getColumnName($field);

zYne's avatar
zYne committed
89
                array_pop($a);
zYne's avatar
zYne committed
90
                
zYne's avatar
zYne committed
91
                $reference2 = implode('.', $a);
zYne's avatar
zYne committed
92
                
93
                $alias      = $this->query->getTableAlias($reference2);
94

zYne's avatar
zYne committed
95 96
                $stack      = $this->query->getRelationStack();
                $relation   = end($stack);
lsmith's avatar
lsmith committed
97

zYne's avatar
zYne committed
98 99
                $stack      = $this->query->getTableStack();

lsmith's avatar
lsmith committed
100
                switch ($func) {
101 102 103 104
                    case 'contains':
                    case 'regexp':
                    case 'like':
                        $operator = $this->getOperator($func);
lsmith's avatar
lsmith committed
105

106 107 108 109 110
                        if (empty($relation)) {
                            throw new Doctrine_Query_Exception('DQL functions contains/regexp/like can only be used for fields of related components');
                        }
                        $where = array();
                        foreach ($values as $value) {
zYne's avatar
zYne committed
111 112 113
                            $where[] = $conn->quoteIdentifier($alias . '.' . $relation->getLocal())
                                     . ' IN (SELECT ' . $conn->quoteIdentifier($relation->getForeign())
                                     . ' FROM ' . $conn->quoteIdentifier($relation->getTable()->getTableName())
zYne's avatar
zYne committed
114
                                     . ' WHERE ' . $field . $operator . $value . ')';
115 116 117 118 119
                        }
                        $where = implode(' AND ', $where);
                        break;
                    default:
                        throw new Doctrine_Query_Exception('Unknown DQL function: '.$func);
zYne's avatar
zYne committed
120 121
                }
            } else {
zYne's avatar
zYne committed
122 123 124 125 126 127
                $map = $this->query->load($reference, false);

                $alias = $this->query->getTableAlias($reference);
                $table = $map['table'];

                $field = $table->getColumnName($field);
128 129
                // check if value is enumerated value
                $enumIndex = $table->enumIndex($field, trim($value, "'"));
zYne's avatar
zYne committed
130

lsmith's avatar
lsmith committed
131
                if (substr($value, 0, 1) == '(') {
132
                    // trim brackets
zYne's avatar
zYne committed
133
                    $trimmed   = Doctrine_Tokenizer::bracketTrim($value);
134

lsmith's avatar
lsmith committed
135
                    if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') {
136 137
                        // subquery found
                        $q     = new Doctrine_Query();
zYne's avatar
zYne committed
138 139
                        $value = '(' . $q->isSubquery(true)->parseQuery($trimmed)->getQuery() . ')';

lsmith's avatar
lsmith committed
140
                    } elseif (substr($trimmed, 0, 4) == 'SQL:') {
141 142 143
                        $value = '(' . substr($trimmed, 4) . ')';
                    } else {
                        // simple in expression found
zYne's avatar
zYne committed
144
                        $e     = Doctrine_Tokenizer::sqlExplode($trimmed, ',');
lsmith's avatar
lsmith committed
145

146
                        $value = array();
lsmith's avatar
lsmith committed
147
                        foreach ($e as $part) {
zYne's avatar
zYne committed
148 149
                            $index = $table->enumIndex($field, trim($part, "'"));

lsmith's avatar
lsmith committed
150
                            if ($index !== false) {
151
                                $value[] = $index;
lsmith's avatar
lsmith committed
152
                            } else {
153
                                $value[] = $this->parseLiteralValue($part);
lsmith's avatar
lsmith committed
154
                            }
155
                        }
156
                        $value = '(' . implode(', ', $value) . ')';
157
                    }
zYne's avatar
zYne committed
158 159 160 161 162 163 164
                } elseif(substr($value, 0, 1) == ':' || $value === '?') {
                    // placeholder found
                    if ($table->getTypeOf($field) == 'enum') {
                        $this->query->addEnumParam($value, $table, $field);
                    } else {
                        $this->query->addEnumParam($value, null, null);
                    }
165
                } else {
lsmith's avatar
lsmith committed
166
                    if ($enumIndex !== false) {
167
                        $value = $enumIndex;
lsmith's avatar
lsmith committed
168
                    } else {
169
                        $value = $this->parseLiteralValue($value);
lsmith's avatar
lsmith committed
170
                    }
171
                }
zYne's avatar
zYne committed
172

lsmith's avatar
lsmith committed
173
                switch ($operator) {
174 175 176 177 178 179 180 181
                    case '<':
                    case '>':
                    case '=':
                    case '!=':
                        if ($enumIndex !== false) {
                            $value  = $enumIndex;
                        }
                    default:
182 183

                        if ($this->query->getType() === Doctrine_Query::SELECT) {
zYne's avatar
zYne committed
184
                            $fieldname = $alias ? $conn->quoteIdentifier($alias . '.' . $field) : $field;
185 186 187 188
                        } else {
                            $fieldname = $field;
                        }
                        
zYne's avatar
zYne committed
189 190
                        $where = $fieldname . ' '
                               . $operator . ' ' . $value;
zYne's avatar
zYne committed
191
                }
192
            }
zYne's avatar
zYne committed
193
        }
doctrine's avatar
doctrine committed
194 195
        return $where;
    }
zYne's avatar
zYne committed
196 197 198 199 200 201 202
    /**
     * parses an EXISTS expression
     *
     * @param string $where         query where part to be parsed
     * @param boolean $negation     whether or not to use the NOT keyword
     * @return string
     */
lsmith's avatar
lsmith committed
203 204
    public function parseExists($where, $negation)
    {
205 206 207
        $operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';

        $pos = strpos($where, '(');
lsmith's avatar
lsmith committed
208

zYne's avatar
zYne committed
209
        if ($pos == false) {
210
            throw new Doctrine_Query_Exception("Unknown expression, expected '('");
zYne's avatar
zYne committed
211
        }
212

zYne's avatar
zYne committed
213
        $sub = Doctrine_Tokenizer::bracketTrim(substr($where, $pos));
214

zYne's avatar
zYne committed
215
        return $operator . ' (' . $this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
216
    }
zYne's avatar
zYne committed
217 218 219 220 221 222
    /**
     * getOperator
     *
     * @param string $func
     * @return string
     */
lsmith's avatar
lsmith committed
223 224
    public function getOperator($func)
    {
lsmith's avatar
lsmith committed
225
        switch ($func) {
226 227 228 229 230 231 232 233 234
            case 'contains':
                $operator = ' = ';
                break;
            case 'regexp':
                $operator = $this->query->getConnection()->getRegexpOperator();
                break;
            case 'like':
                $operator = ' LIKE ';
                break;
235 236 237
        }
        return $operator;
    }
doctrine's avatar
doctrine committed
238
}