Where.php 6.18 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
/*
 *  $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
26
 * @subpackage  Query
zYne's avatar
zYne committed
27 28 29 30 31 32
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @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
{
zYne's avatar
zYne committed
35
    public function load($where) 
lsmith's avatar
lsmith committed
36
    {
37
        $where = $this->_tokenizer->bracketTrim(trim($where));
zYne's avatar
zYne committed
38
        $conn  = $this->query->getConnection();
39
        $terms = $this->_tokenizer->sqlExplode($where);  
40

zYne's avatar
zYne committed
41 42
        if (count($terms) > 1) {
            if (substr($where, 0, 6) == 'EXISTS') {
43
                return $this->parseExists($where, true);
lsmith's avatar
lsmith committed
44
            } elseif (substr($where, 0, 10) == 'NOT EXISTS') {
45
                return $this->parseExists($where, false);
lsmith's avatar
lsmith committed
46
            }
47
        }
48

zYne's avatar
zYne committed
49
        if (count($terms) < 3) {
50
            $terms = $this->_tokenizer->sqlExplode($where, array('=', '<', '<>', '>', '!='));
zYne's avatar
zYne committed
51
        }
zYne's avatar
zYne committed
52

zYne's avatar
zYne committed
53 54 55 56 57 58 59 60 61 62 63 64 65
        if (count($terms) > 1) {
            $first = array_shift($terms);
            $value = array_pop($terms);
            $operator = trim(substr($where, strlen($first), -strlen($value)));
            $table = null;
            $field = null;

            if (strpos($first, "'") === false && strpos($first, '(') === false) {
                // normal field reference found
                $a = explode('.', $first);
        
                $field = array_pop($a);
                $reference = implode('.', $a);
zYne's avatar
zYne committed
66 67 68 69 70
                
                if (empty($reference)) {
                    $map = $this->query->getRootDeclaration();  
                    
                    $alias = $this->query->getTableAlias($this->query->getRootAlias());
zYne's avatar
zYne committed
71
                    $table = $map['table'];
zYne's avatar
zYne committed
72 73 74 75 76
                } else {
                    $map = $this->query->load($reference, false);
    
                    $alias = $this->query->getTableAlias($reference);
                    $table = $map['table'];
77
                }
zYne's avatar
zYne committed
78
            }
79
            $first = $this->query->parseClause($first);
romanb's avatar
romanb committed
80
            
zYne's avatar
zYne committed
81 82 83 84
            $sql = $first . ' ' . $operator . ' ' . $this->parseValue($value, $table, $field);
        
            return $sql;  
        } else {
zYne's avatar
zYne committed
85

zYne's avatar
zYne committed
86 87
        }
    }
zYne's avatar
zYne committed
88

zYne's avatar
zYne committed
89 90 91 92
    public function parseValue($value, Doctrine_Table $table = null, $field = null)
    {
        if (substr($value, 0, 1) == '(') {
            // trim brackets
93
            $trimmed = $this->_tokenizer->bracketTrim($value);
94

zYne's avatar
zYne committed
95 96
            if (substr($trimmed, 0, 4) == 'FROM' ||
                substr($trimmed, 0, 6) == 'SELECT') {
zYne's avatar
zYne committed
97

zYne's avatar
zYne committed
98 99
                // subquery found
                $q     = new Doctrine_Query();
100
                $value = '(' . $this->query->createSubquery()->parseQuery($trimmed, false)->getQuery() . ')';
zYne's avatar
zYne committed
101 102 103 104 105

            } elseif (substr($trimmed, 0, 4) == 'SQL:') {
                $value = '(' . substr($trimmed, 4) . ')';
            } else {
                // simple in expression found
106
                $e = $this->_tokenizer->sqlExplode($trimmed, ',');
zYne's avatar
zYne committed
107 108 109 110 111 112 113 114

                $value = array();

                $index = false;

                foreach ($e as $part) {
                    if (isset($table) && isset($field)) {
                        $index = $table->enumIndex($field, trim($part, "'"));
zYne's avatar
zYne committed
115
                    }
zYne's avatar
zYne committed
116 117 118

                    if ($index !== false) {
                        $value[] = $index;
lsmith's avatar
lsmith committed
119
                    } else {
zYne's avatar
zYne committed
120
                        $value[] = $this->parseLiteralValue($part);
lsmith's avatar
lsmith committed
121
                    }
122
                }
zYne's avatar
zYne committed
123

zYne's avatar
zYne committed
124 125
                $value = '(' . implode(', ', $value) . ')';
            }
126
        } else if (substr($value, 0, 1) == ':' || $value === '?') {
zYne's avatar
zYne committed
127 128 129 130 131 132 133
            // placeholder found
            if (isset($table) && isset($field) && $table->getTypeOf($field) == 'enum') {
                $this->query->addEnumParam($value, $table, $field);
            } else {
                $this->query->addEnumParam($value, null, null);
            }
        } else {
zYne's avatar
zYne committed
134 135 136 137 138
            $enumIndex = false;
            if (isset($table) && isset($field)) {
                // check if value is enumerated value
                $enumIndex = $table->enumIndex($field, trim($value, "'"));
            }
zYne's avatar
zYne committed
139

zYne's avatar
zYne committed
140 141 142 143
            if ($enumIndex !== false) {
                $value = $enumIndex;
            } else {
                $value = $this->parseLiteralValue($value);
144
            }
zYne's avatar
zYne committed
145
        }
zYne's avatar
zYne committed
146
        return $value;
doctrine's avatar
doctrine committed
147
    }
148

zYne's avatar
zYne committed
149 150 151 152 153 154 155
    /**
     * 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
156 157
    public function parseExists($where, $negation)
    {
158 159 160
        $operator = ($negation) ? 'EXISTS' : 'NOT EXISTS';

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

zYne's avatar
zYne committed
162
        if ($pos == false) {
zYne's avatar
zYne committed
163
            throw new Doctrine_Query_Exception('Unknown expression, expected a subquery with () -marks');
zYne's avatar
zYne committed
164
        }
165

166
        $sub = $this->_tokenizer->bracketTrim(substr($where, $pos));
167

zYne's avatar
zYne committed
168
        return $operator . ' (' . $this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')';
169
    }
170
}