Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: From.php 1080 2007-02-10 18:17:08Z romanb $
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.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Query_Check
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Query
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: 1080 $
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32
 */
33
class Doctrine_Query_Check
34
{
35
    /**
36
     * @var Doctrine_Table $table           Doctrine_Table object
37
     */
38
    protected $table;
39
 
40
    /**
41
     * @var string $sql                     database specific sql CHECK constraint definition
42
     *                                      parsed from the given dql CHECK definition
43
     */
44
    protected $sql;
45
 
46
    protected $_tokenizer;
47
 
48
    /**
49
     * @param Doctrine_Table|string $table  Doctrine_Table object
50
     */
51
    public function __construct($table)
52
    {
53
        if ( ! ($table instanceof Doctrine_Table)) {
54
            $table = Doctrine_Manager::getInstance()
55
                        ->getCurrentConnection()
56
                        ->getTable($table);
57
        }
58
        $this->table = $table;
59
        $this->_tokenizer = new Doctrine_Query_Tokenizer();
60
    }
61
 
62
    /**
63
     * getTable
64
     * returns the table object associated with this object
65
     *
66
     * @return Doctrine_Connection
67
     */
68
    public function getTable()
69
    {
70
        return $this->table;
71
    }
72
 
73
    /**
74
     * parse
75
     *
76
     * @param string $dql       DQL CHECK constraint definition
77
     * @return string
78
     */
79
    public function parse($dql)
80
    {
81
        $this->sql = $this->parseClause($dql);
82
    }
83
 
84
    /**
85
     * parseClause
86
     *
87
     * @param string $alias     component alias
88
     * @param string $field     the field name
89
     * @param mixed $value      the value of the field
90
     * @return void
91
     */
92
    public function parseClause($dql)
93
    {
94
        $parts = $this->_tokenizer->sqlExplode($dql, ' AND ');
95
 
96
        if (count($parts) > 1) {
97
            $ret = array();
98
            foreach ($parts as $part) {
99
                $ret[] = $this->parseSingle($part);
100
            }
101
 
102
            $r = implode(' AND ', $ret);
103
        } else {
104
            $parts = $this->_tokenizer->quoteExplode($dql, ' OR ');
105
            if (count($parts) > 1) {
106
                $ret = array();
107
                foreach ($parts as $part) {
108
                    $ret[] = $this->parseClause($part);
109
                }
110
 
111
                $r = implode(' OR ', $ret);
112
            } else {
113
                $ret = $this->parseSingle($dql);
114
                return $ret;
115
            }
116
        }
117
        return '(' . $r . ')';
118
    }
119
 
120
    public function parseSingle($part)
121
    {
122
        $e = explode(' ', $part);
123
 
124
        $e[0] = $this->parseFunction($e[0]);
125
 
126
        switch ($e[1]) {
127
            case '>':
128
            case '<':
129
            case '=':
130
            case '!=':
131
            case '<>':
132
 
133
            break;
134
            default:
135
                throw new Doctrine_Query_Exception('Unknown operator ' . $e[1]);
136
        }
137
 
138
        return implode(' ', $e);
139
    }
140
 
141
    public function parseFunction($dql)
142
    {
143
        if (($pos = strpos($dql, '(')) !== false) {
144
            $func  = substr($dql, 0, $pos);
145
            $value = substr($dql, ($pos + 1), -1);
146
 
147
            $expr  = $this->table->getConnection()->expression;
148
 
149
            if ( ! method_exists($expr, $func)) {
150
                throw new Doctrine_Query_Exception('Unknown function ' . $func);
151
            }
152
 
153
            $func  = $expr->$func($value);
154
        }
155
        return $func;
156
    }
157
 
158
    /**
159
     * getSql
160
     *
161
     * returns database specific sql CHECK constraint definition
162
     * parsed from the given dql CHECK definition
163
     *
164
     * @return string
165
     */
166
    public function getSql()
167
    {
168
        return $this->sql;
169
    }
170
}