Where.php 7.46 KB
Newer Older
doctrine's avatar
doctrine committed
1 2 3 4 5
<?php
require_once("Condition.php");

class Doctrine_Query_Where extends Doctrine_Query_Condition {
    /**
6
     * load
doctrine's avatar
doctrine committed
7 8 9 10 11
     * returns the parsed query part
     *
     * @param string $where
     * @return string
     */
12
    public function load($where) {
13 14 15 16 17 18 19 20 21 22 23 24
        $where = trim($where);

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

        if(count($e) > 1) {
            $tmp   = $e[0].' '.$e[1];

            if(substr($tmp, 0, 6) == 'EXISTS')
                return $this->parseExists($where, true);
            elseif(substr($where, 0, 10) == 'NOT EXISTS')
                return $this->parseExists($where, false);
        }
25

zYne's avatar
zYne committed
26 27 28
        if(count($e) < 3) {
            $e = Doctrine_Query::sqlExplode($where, array('=', '<', '>', '!='));
        }
doctrine's avatar
doctrine committed
29
        $r = array_shift($e);
zYne's avatar
zYne committed
30

31
        $a = explode('.', $r);
doctrine's avatar
doctrine committed
32 33 34

        if(count($a) > 1) {
            $field     = array_pop($a);
35 36 37
            $count     = count($e);
            $slice     = array_slice($e, -1, 1);
            $value     = implode('', $slice);
zYne's avatar
zYne committed
38
            $operator  = trim(substr($where, strlen($r), -strlen($value)));
39

doctrine's avatar
doctrine committed
40 41 42
            $reference = implode(".",$a);
            $count     = count($a);

43

44

zYne's avatar
zYne committed
45
            $pos       = strpos($field, "(");
46

zYne's avatar
zYne committed
47
            if($pos !== false) {
48
                $func   = substr($field, 0, $pos);
49
                $value  = trim(substr($field, ($pos + 1), -1));
50 51 52 53 54 55

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

                $field      = array_pop($a);
                $reference  = implode(".",$a);
                $table      = $this->query->load($reference, false);
zYne's avatar
zYne committed
56 57
                array_pop($a);
                $reference2 = implode('.', $a);
58
                $alias      = $this->query->getTableAlias($reference2);
59

zYne's avatar
zYne committed
60 61 62 63 64 65
                $stack      = $this->query->getRelationStack();
                $relation   = end($stack);
                
                $stack      = $this->query->getTableStack();

                switch($func) {
66 67 68
                    case 'contains':
                    case 'regexp':
                    case 'like':
69
                        $operator = $this->getOperator($func);
zYne's avatar
zYne committed
70

71 72 73 74 75 76
                        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) {
                            $where[] = $alias.'.'.$relation->getLocal().
zYne's avatar
zYne committed
77
                              ' IN (SELECT '.$relation->getForeign().
78 79 80
                              ' FROM '.$relation->getTable()->getTableName().' WHERE '.$field.$operator.$value.')';
                        }
                        $where = implode(' AND ', $where);
zYne's avatar
zYne committed
81 82 83 84 85 86 87 88
                    break;
                    default:
                        throw new Doctrine_Query_Exception('Unknown DQL function: '.$func);
                }
            } else {
                $table     = $this->query->load($reference, false);
                $alias     = $this->query->getTableAlias($reference);
                $table     = $this->query->getTable($alias);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                // check if value is enumerated value
                $enumIndex = $table->enumIndex($field, trim($value, "'"));

                if(substr($value, 0, 1) == '(') {
                    // trim brackets
                    $trimmed   = Doctrine_Query::bracketTrim($value);

                    if(substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') {
                        // subquery found
                        $q     = new Doctrine_Query();
                        $value = '(' . $q->parseQuery($trimmed)->getQuery() . ')';
                    } elseif(substr($trimmed, 0, 4) == 'SQL:') {
                        $value = '(' . substr($trimmed, 4) . ')';
                    } else {
                        // simple in expression found
                        $e     = Doctrine_Query::sqlExplode($trimmed, ',');
                        
                        $value = array();
                        foreach($e as $part) {
                            $index   = $table->enumIndex($field, trim($part, "'"));
                            if($index !== false)
                                $value[] = $index;
                            else
                                $value[] = $this->parseLiteralValue($part);
113
                        }
114
                        $value = '(' . implode(', ', $value) . ')';
115
                    }
116 117 118 119 120
                } else {
                    if($enumIndex !== false)
                        $value = $enumIndex;
                    else
                        $value = $this->parseLiteralValue($value);
121
                }
zYne's avatar
zYne committed
122

123

zYne's avatar
zYne committed
124 125 126 127
                switch($operator) {
                    case '<':
                    case '>':
                    case '=':
128
                    case '!=':
zYne's avatar
zYne committed
129
                        if($enumIndex !== false)
130
                            $value  = $enumIndex;
zYne's avatar
zYne committed
131
                    default:
132
                        $where      = $alias.'.'.$field.' '.$operator.' '.$value;
zYne's avatar
zYne committed
133
                }
134
            }
zYne's avatar
zYne committed
135
        }
doctrine's avatar
doctrine committed
136 137
        return $where;
    }
zYne's avatar
zYne committed
138 139 140 141 142 143 144 145 146
    /**
     * parses a literal value and returns the parsed value
     * 
     * boolean literals are parsed to integers
     * components are parsed to associated table aliases
     *
     * @param string $value         literal value to be parsed
     * @return string
     */
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    public function parseLiteralValue($value) {
        // check that value isn't a string
        if(strpos($value, '\'') === false) {
                        
            // parse booleans
            if($value == 'true')
                $value = 1;
            elseif($value == 'false')
                $value = 0;

            $a = explode('.', $value);

            if(count($a) > 1) {
            // either a float or a component..
    
                if( ! is_numeric($a[0])) {
                    // a component found
                    $value = $this->query->getTableAlias($a[0]). '.' . $a[1];
                }
            }
        } else {
            // string literal found
        }
doctrine's avatar
doctrine committed
170

171 172
        return $value;
    }
zYne's avatar
zYne committed
173 174 175 176 177 178 179
    /**
     * 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
     */
180 181 182 183 184 185 186 187 188 189 190 191 192
    public function parseExists($where, $negation) {
        $operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';

        $pos = strpos($where, '(');
        
        if($pos == false)
            throw new Doctrine_Query_Exception("Unknown expression, expected '('");

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

        return $operator . ' ('.$this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
    }

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    public function getOperator($func) {
        switch($func) {
            case 'contains':
                $operator = ' = ';
            break;
            case 'regexp':
                $operator = $this->query->getConnection()->getRegexpOperator();
            break;
            case 'like':
                $operator = ' LIKE ';
            break;
        }
        return $operator;
    }

doctrine's avatar
doctrine committed
208 209 210 211
    public function __toString() {
        return ( ! empty($this->parts))?implode(" AND ", $this->parts):'';
    }
}
212