~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Query/Condition.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 *  $Id: Condition.php 5218 2008-11-26 00:28:49Z jwage $
 
4
 *
 
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
16
 *
 
17
 * This software consists of voluntary contributions made by many individuals
 
18
 * and is licensed under the LGPL. For more information, see
 
19
 * <http://www.phpdoctrine.org>.
 
20
 */
 
21
 
 
22
/**
 
23
 * Doctrine_Query_Condition
 
24
 *
 
25
 * @package     Doctrine
 
26
 * @subpackage  Query
 
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 
28
 * @link        www.phpdoctrine.org
 
29
 * @since       1.0
 
30
 * @version     $Revision: 5218 $
 
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 
32
 */
 
33
abstract class Doctrine_Query_Condition extends Doctrine_Query_Part
 
34
{
 
35
    /**
 
36
     * DQL CONDITION PARSER
 
37
     * parses the join condition/where/having part of the query string
 
38
     *
 
39
     * @param string $str
 
40
     * @return string
 
41
     */
 
42
    public function parse($str)
 
43
    {
 
44
        $tmp = trim($str);
 
45
        
 
46
        $parts = $this->_tokenizer->bracketExplode($str, array(' OR '), '(', ')');
 
47
 
 
48
        if (count($parts) > 1) {
 
49
            $ret = array();
 
50
            foreach ($parts as $part) {
 
51
                $part = $this->_tokenizer->bracketTrim($part, '(', ')');
 
52
                $ret[] = $this->parse($part);
 
53
            }
 
54
            $r = implode(' OR ', $ret);
 
55
        } else {
 
56
            $parts = $this->_tokenizer->bracketExplode($str, array(' AND '), '(', ')');
 
57
            
 
58
            // Ticket #1388: We need to make sure we're not splitting a BETWEEN ...  AND ... clause
 
59
            $tmp = array();
 
60
 
 
61
            for ($i = 0, $l = count($parts); $i < $l; $i++) {
 
62
                $test = $this->_tokenizer->sqlExplode($parts[$i]);
 
63
 
 
64
                if (count($test) == 3 && strtoupper($test[1]) == 'BETWEEN') {
 
65
                    $tmp[] = $parts[$i] . ' AND ' . $parts[++$i];
 
66
                } else {
 
67
                    $tmp[] = $parts[$i];
 
68
                }
 
69
            }
 
70
            
 
71
            $parts = $tmp;
 
72
            unset($tmp);
 
73
 
 
74
            if (count($parts) > 1) {
 
75
                $ret = array();
 
76
                foreach ($parts as $part) {
 
77
                    $part = $this->_tokenizer->bracketTrim($part, '(', ')');
 
78
                    $ret[] = $this->parse($part);
 
79
                }
 
80
                $r = implode(' AND ', $ret);
 
81
            } else {
 
82
                // Fix for #710
 
83
                if (substr($parts[0],0,1) == '(' && substr($parts[0], -1) == ')') {
 
84
                    return $this->parse(substr($parts[0], 1, -1));
 
85
                } else {
 
86
                    // Processing NOT here
 
87
                    if (strtoupper(substr($parts[0], 0, 4)) === 'NOT ') {
 
88
                        $r = 'NOT ('.$this->parse(substr($parts[0], 4)).')';
 
89
                    } else {
 
90
                        return $this->load($parts[0]);
 
91
                    }
 
92
                }
 
93
            }
 
94
        }
 
95
        
 
96
        return '(' . $r . ')';
 
97
    }
 
98
 
 
99
    /**
 
100
     * parses a literal value and returns the parsed value
 
101
     *
 
102
     * boolean literals are parsed to integers
 
103
     * components are parsed to associated table aliases
 
104
     *
 
105
     * @param string $value         literal value to be parsed
 
106
     * @return string
 
107
     */
 
108
    public function parseLiteralValue($value)
 
109
    {
 
110
        // check that value isn't a string
 
111
        if (strpos($value, '\'') === false) {
 
112
            // parse booleans
 
113
            $value = $this->query->getConnection()
 
114
                     ->dataDict->parseBoolean($value);
 
115
 
 
116
            $a = explode('.', $value);
 
117
 
 
118
            if (count($a) > 1) {
 
119
            // either a float or a component..
 
120
 
 
121
                if ( ! is_numeric($a[0])) {
 
122
                    // a component found
 
123
                    $field     = array_pop($a);
 
124
                          $reference = implode('.', $a);
 
125
                    $value = $this->query->getConnection()->quoteIdentifier($this->query->getTableAlias($reference). '.' . $field);
 
126
                }
 
127
            }
 
128
        } else {
 
129
            // string literal found
 
130
        }
 
131
 
 
132
        return $value;
 
133
    }
 
134
}
 
 
b'\\ No newline at end of file'