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.phpdoctrine.com>.
|
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.phpdoctrine.com
|
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 |
/**
|
47 |
* @param Doctrine_Table|string $table Doctrine_Table object
|
48 |
*/
|
49 |
public function __construct($table)
|
50 |
{
|
51 |
if ( ! ($table instanceof Doctrine_Table)) {
|
52 |
$table = Doctrine_Manager::getInstance()
|
53 |
->getCurrentConnection()
|
54 |
->getTable($table);
|
55 |
}
|
56 |
$this->table = $table;
|
57 |
}
|
58 |
|
59 |
/**
|
60 |
* getTable
|
61 |
* returns the table object associated with this object
|
62 |
*
|
63 |
* @return Doctrine_Connection
|
64 |
*/
|
65 |
public function getTable()
|
66 |
{
|
67 |
return $this->table;
|
68 |
}
|
69 |
|
70 |
/**
|
71 |
* parse
|
72 |
*
|
73 |
* @param string $dql DQL CHECK constraint definition
|
74 |
* @return string
|
75 |
*/
|
76 |
public function parse($dql)
|
77 |
{
|
78 |
$this->sql = $this->parseClause($dql);
|
79 |
}
|
80 |
|
81 |
/**
|
82 |
* parseClause
|
83 |
*
|
84 |
* @param string $alias component alias
|
85 |
* @param string $field the field name
|
86 |
* @param mixed $value the value of the field
|
87 |
* @return void
|
88 |
*/
|
89 |
public function parseClause($dql)
|
90 |
{
|
91 |
$parts = Doctrine_Tokenizer::sqlExplode($dql, ' AND ');
|
92 |
|
93 |
if (count($parts) > 1) {
|
94 |
$ret = array();
|
95 |
foreach ($parts as $part) {
|
96 |
$ret[] = $this->parseSingle($part);
|
97 |
}
|
98 |
|
99 |
$r = implode(' AND ', $ret);
|
100 |
} else {
|
101 |
$parts = Doctrine_Tokenizer::quoteExplode($dql, ' OR ');
|
102 |
if (count($parts) > 1) {
|
103 |
$ret = array();
|
104 |
foreach ($parts as $part) {
|
105 |
$ret[] = $this->parseClause($part);
|
106 |
}
|
107 |
|
108 |
$r = implode(' OR ', $ret);
|
109 |
} else {
|
110 |
$ret = $this->parseSingle($dql);
|
111 |
return $ret;
|
112 |
}
|
113 |
}
|
114 |
return '(' . $r . ')';
|
115 |
}
|
116 |
public function parseSingle($part)
|
117 |
{
|
118 |
$e = explode(' ', $part);
|
119 |
|
120 |
$e[0] = $this->parseFunction($e[0]);
|
121 |
|
122 |
switch ($e[1]) {
|
123 |
case '>':
|
124 |
case '<':
|
125 |
case '=':
|
126 |
case '!=':
|
127 |
case '<>':
|
128 |
|
129 |
break;
|
130 |
default:
|
131 |
throw new Doctrine_Query_Exception('Unknown operator ' . $e[1]);
|
132 |
}
|
133 |
|
134 |
return implode(' ', $e);
|
135 |
}
|
136 |
public function parseFunction($dql)
|
137 |
{
|
138 |
if (($pos = strpos($dql, '(')) !== false) {
|
139 |
$func = substr($dql, 0, $pos);
|
140 |
$value = substr($dql, ($pos + 1), -1);
|
141 |
|
142 |
$expr = $this->table->getConnection()->expression;
|
143 |
|
144 |
if ( ! method_exists($expr, $func)) {
|
145 |
throw new Doctrine_Query_Exception('Unknown function ' . $func);
|
146 |
}
|
147 |
|
148 |
$func = $expr->$func($value);
|
149 |
}
|
150 |
return $func;
|
151 |
}
|
152 |
|
153 |
/**
|
154 |
* getSql
|
155 |
*
|
156 |
* returns database specific sql CHECK constraint definition
|
157 |
* parsed from the given dql CHECK definition
|
158 |
*
|
159 |
* @return string
|
160 |
*/
|
161 |
public function getSql()
|
162 |
{
|
163 |
return $this->sql;
|
164 |
}
|
165 |
} |