Where.php 8.71 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 46 47
        $where = trim($where);

        $e     = Doctrine_Query::sqlExplode($where);

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 60
            $e = Doctrine_Query::sqlExplode($where, array('=', '<', '>', '!='));
        }
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 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 114
                            $where[] = $alias . '.' . $relation->getLocal() 
                                     . ' IN (SELECT '.$relation->getForeign()
                                     . ' FROM ' . $relation->getTable()->getTableName() 
                                     . ' 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 122 123 124
                }
            } else {
                $table     = $this->query->load($reference, false);
                $alias     = $this->query->getTableAlias($reference);
                $table     = $this->query->getTable($alias);
zYne's avatar
zYne committed
125 126
                
                $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 132 133
                    // trim brackets
                    $trimmed   = Doctrine_Query::bracketTrim($value);

lsmith's avatar
lsmith committed
134
                    if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') {
135

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 144
                        $value = '(' . substr($trimmed, 4) . ')';
                    } else {
                        // simple in expression found
                        $e     = Doctrine_Query::sqlExplode($trimmed, ',');
lsmith's avatar
lsmith committed
145

146
                        $value = array();
lsmith's avatar
lsmith committed
147
                        foreach ($e as $part) {
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
                    }
157
                } else {
lsmith's avatar
lsmith committed
158
                    if ($enumIndex !== false) {
159
                        $value = $enumIndex;
lsmith's avatar
lsmith committed
160
                    } else {
161
                        $value = $this->parseLiteralValue($value);
lsmith's avatar
lsmith committed
162
                    }
163
                }
zYne's avatar
zYne committed
164

lsmith's avatar
lsmith committed
165
                switch ($operator) {
166 167 168 169 170 171 172 173
                    case '<':
                    case '>':
                    case '=':
                    case '!=':
                        if ($enumIndex !== false) {
                            $value  = $enumIndex;
                        }
                    default:
174 175 176 177 178 179 180

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

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

        if ($pos == false)
202 203 204 205
            throw new Doctrine_Query_Exception("Unknown expression, expected '('");

        $sub = Doctrine_Query::bracketTrim(substr($where, $pos));

zYne's avatar
zYne committed
206
        return $operator . ' (' . $this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
207
    }
zYne's avatar
zYne committed
208 209 210 211 212 213
    /**
     * getOperator
     *
     * @param string $func
     * @return string
     */
lsmith's avatar
lsmith committed
214 215
    public function getOperator($func)
    {
lsmith's avatar
lsmith committed
216
        switch ($func) {
217 218 219 220 221 222 223 224 225
            case 'contains':
                $operator = ' = ';
                break;
            case 'regexp':
                $operator = $this->query->getConnection()->getRegexpOperator();
                break;
            case 'like':
                $operator = ' LIKE ';
                break;
226 227 228
        }
        return $operator;
    }
zYne's avatar
zYne committed
229 230 231 232 233 234
    /**
     * __toString
     * return string representation of this object
     *
     * @return string
     */
lsmith's avatar
lsmith committed
235 236
    public function __toString()
    {
zYne's avatar
zYne committed
237
        return ( ! empty($this->parts))?implode(' AND ', $this->parts):'';
doctrine's avatar
doctrine committed
238 239
    }
}