Where.php 8.79 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 45
        $where = trim($where);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

105 106 107 108 109
                        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
110 111
                            $where[] = $alias . '.' . $relation->getLocal() 
                                     . ' IN (SELECT '.$relation->getForeign()
zYne's avatar
zYne committed
112
                                     . ' FROM ' . $relation->getTable()->getTableName()
zYne's avatar
zYne committed
113
                                     . ' WHERE ' . $field . $operator . $value . ')';
114 115 116 117 118
                        }
                        $where = implode(' AND ', $where);
                        break;
                    default:
                        throw new Doctrine_Query_Exception('Unknown DQL function: '.$func);
zYne's avatar
zYne committed
119 120
                }
            } else {
zYne's avatar
zYne committed
121 122 123 124 125 126
                $map = $this->query->load($reference, false);

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

                $field = $table->getColumnName($field);
127 128 129
                // check if value is enumerated value
                $enumIndex = $table->enumIndex($field, trim($value, "'"));

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

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

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

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

lsmith's avatar
lsmith committed
149
                            if ($index !== false) {
150
                                $value[] = $index;
lsmith's avatar
lsmith committed
151
                            } else {
152
                                $value[] = $this->parseLiteralValue($part);
lsmith's avatar
lsmith committed
153
                            }
154
                        }
155
                        $value = '(' . implode(', ', $value) . ')';
156
                    }
zYne's avatar
zYne committed
157 158 159 160 161 162 163
                } 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);
                    }
164
                } else {
lsmith's avatar
lsmith committed
165
                    if ($enumIndex !== false) {
166
                        $value = $enumIndex;
lsmith's avatar
lsmith committed
167
                    } else {
168
                        $value = $this->parseLiteralValue($value);
lsmith's avatar
lsmith committed
169
                    }
170
                }
zYne's avatar
zYne committed
171

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

                        if ($this->query->getType() === Doctrine_Query::SELECT) {
                            $fieldname = $alias ? $alias . '.' . $field : $field;
                        } else {
                            $fieldname = $field;
                        }
                        
zYne's avatar
zYne committed
188 189
                        $where = $fieldname . ' '
                               . $operator . ' ' . $value;
zYne's avatar
zYne committed
190
                }
191
            }
zYne's avatar
zYne committed
192
        }
doctrine's avatar
doctrine committed
193 194
        return $where;
    }
zYne's avatar
zYne committed
195 196 197 198 199 200 201
    /**
     * 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
202 203
    public function parseExists($where, $negation)
    {
204 205 206
        $operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';

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

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

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

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