Condition.php 3.75 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_Part');
/**
 * Doctrine_Query_Condition
 *
 * @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
abstract class Doctrine_Query_Condition extends Doctrine_Query_Part
{
doctrine's avatar
doctrine committed
35 36
    /**
     * DQL CONDITION PARSER
zYne's avatar
zYne committed
37
     * parses the join condition/where/having part of the query string
doctrine's avatar
doctrine committed
38 39 40 41
     *
     * @param string $str
     * @return string
     */
lsmith's avatar
lsmith committed
42 43
    final public function parse($str)
    {
doctrine's avatar
doctrine committed
44 45
        $tmp = trim($str);

46 47
        $parts = Doctrine_Query::bracketExplode($str, array(' \&\& ', ' AND '), '(', ')');

lsmith's avatar
lsmith committed
48
        if (count($parts) > 1) {
doctrine's avatar
doctrine committed
49
            $ret = array();
lsmith's avatar
lsmith committed
50
            foreach ($parts as $part) {
51
                $part = Doctrine_Query::bracketTrim($part, '(', ')');
52
                $ret[] = $this->parse($part);
doctrine's avatar
doctrine committed
53
            }
54
            $r = implode(' AND ',$ret);
doctrine's avatar
doctrine committed
55
        } else {
56

57
            $parts = Doctrine_Query::bracketExplode($str, array(' \|\| ', ' OR '), '(', ')');
lsmith's avatar
lsmith committed
58
            if (count($parts) > 1) {
doctrine's avatar
doctrine committed
59
                $ret = array();
lsmith's avatar
lsmith committed
60
                foreach ($parts as $part) {
61
                    $part = Doctrine_Query::bracketTrim($part, '(', ')');
doctrine's avatar
doctrine committed
62 63
                    $ret[] = $this->parse($part);
                }
zYne's avatar
zYne committed
64
                $r = implode(' OR ', $ret);
doctrine's avatar
doctrine committed
65
            } else {
lsmith's avatar
lsmith committed
66
                if (substr($parts[0],0,1) == '(' && substr($parts[0],-1) == ')') {
67
                    return $this->parse(substr($parts[0],1,-1));
lsmith's avatar
lsmith committed
68
                } else {
69
                    return $this->load($parts[0]);
lsmith's avatar
lsmith committed
70
                }
doctrine's avatar
doctrine committed
71 72
            }
        }
73

74
        return '(' . $r . ')';
doctrine's avatar
doctrine committed
75
    }
76 77 78 79 80 81 82 83 84 85 86 87 88 89
    /**
     * 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
     */
    public function parseLiteralValue($value)
    {
        // check that value isn't a string
        if (strpos($value, '\'') === false) {
            // parse booleans
zYne's avatar
zYne committed
90 91
            $value = $this->query->getConnection()
                     ->dataDict->parseBoolean($value);  
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

            $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
        }

        return $value;
    }
doctrine's avatar
doctrine committed
109
}