| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id$
|
|
|
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_Formatter
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Formatter
|
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
28 |
* @link www.doctrine-project.org
|
|
|
29 |
* @since 1.0
|
|
|
30 |
* @version $Revision$
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Formatter extends Doctrine_Connection_Module
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* Quotes pattern (% and _) characters in a string)
|
|
|
37 |
*
|
|
|
38 |
* EXPERIMENTAL
|
|
|
39 |
*
|
|
|
40 |
* WARNING: this function is experimental and may change signature at
|
|
|
41 |
* any time until labelled as non-experimental
|
|
|
42 |
*
|
|
|
43 |
* @param string the input string to quote
|
|
|
44 |
*
|
|
|
45 |
* @return string quoted string
|
|
|
46 |
*/
|
|
|
47 |
public function escapePattern($text)
|
|
|
48 |
{
|
|
|
49 |
if ( ! $this->string_quoting['escape_pattern']) {
|
|
|
50 |
return $text;
|
|
|
51 |
}
|
|
|
52 |
$tmp = $this->conn->string_quoting;
|
|
|
53 |
|
|
|
54 |
$text = str_replace($tmp['escape_pattern'],
|
|
|
55 |
$tmp['escape_pattern'] .
|
|
|
56 |
$tmp['escape_pattern'], $text);
|
|
|
57 |
|
|
|
58 |
foreach ($this->wildcards as $wildcard) {
|
|
|
59 |
$text = str_replace($wildcard, $tmp['escape_pattern'] . $wildcard, $text);
|
|
|
60 |
}
|
|
|
61 |
return $text;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* convertBooleans
|
|
|
66 |
* some drivers need the boolean values to be converted into integers
|
|
|
67 |
* when using DQL API
|
|
|
68 |
*
|
|
|
69 |
* This method takes care of that conversion
|
|
|
70 |
*
|
|
|
71 |
* @param array $item
|
|
|
72 |
* @return void
|
|
|
73 |
*/
|
|
|
74 |
public function convertBooleans($item)
|
|
|
75 |
{
|
|
|
76 |
if (is_array($item)) {
|
|
|
77 |
foreach ($item as $k => $value) {
|
|
|
78 |
if (is_bool($value)) {
|
|
|
79 |
$item[$k] = (int) $value;
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
} else {
|
|
|
83 |
if (is_bool($item)) {
|
|
|
84 |
$item = (int) $item;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
return $item;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Quote a string so it can be safely used as a table or column name
|
|
|
92 |
*
|
|
|
93 |
* Delimiting style depends on which database driver is being used.
|
|
|
94 |
*
|
|
|
95 |
* NOTE: just because you CAN use delimited identifiers doesn't mean
|
|
|
96 |
* you SHOULD use them. In general, they end up causing way more
|
|
|
97 |
* problems than they solve.
|
|
|
98 |
*
|
|
|
99 |
* Portability is broken by using the following characters inside
|
|
|
100 |
* delimited identifiers:
|
|
|
101 |
* + backtick (<kbd>`</kbd>) -- due to MySQL
|
|
|
102 |
* + double quote (<kbd>"</kbd>) -- due to Oracle
|
|
|
103 |
* + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
|
|
|
104 |
*
|
|
|
105 |
* Delimited identifiers are known to generally work correctly under
|
|
|
106 |
* the following drivers:
|
|
|
107 |
* + mssql
|
|
|
108 |
* + mysql
|
|
|
109 |
* + mysqli
|
|
|
110 |
* + oci8
|
|
|
111 |
* + pgsql
|
|
|
112 |
* + sqlite
|
|
|
113 |
*
|
|
|
114 |
* InterBase doesn't seem to be able to use delimited identifiers
|
|
|
115 |
* via PHP 4. They work fine under PHP 5.
|
|
|
116 |
*
|
|
|
117 |
* @param string $str identifier name to be quoted
|
|
|
118 |
* @param bool $checkOption check the 'quote_identifier' option
|
|
|
119 |
*
|
|
|
120 |
* @return string quoted identifier string
|
|
|
121 |
*/
|
|
|
122 |
public function quoteIdentifier($str, $checkOption = true)
|
|
|
123 |
{
|
|
|
124 |
if ($checkOption && ! $this->conn->getAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER)) {
|
|
|
125 |
return $str;
|
|
|
126 |
}
|
|
|
127 |
$tmp = $this->conn->identifier_quoting;
|
|
|
128 |
$str = str_replace($tmp['end'],
|
|
|
129 |
$tmp['escape'] .
|
|
|
130 |
$tmp['end'], $str);
|
|
|
131 |
|
|
|
132 |
return $tmp['start'] . $str . $tmp['end'];
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* quoteMultipleIdentifier
|
|
|
138 |
* Quotes multiple identifier strings
|
|
|
139 |
*
|
|
|
140 |
* @param array $arr identifiers array to be quoted
|
|
|
141 |
* @param bool $checkOption check the 'quote_identifier' option
|
|
|
142 |
*
|
|
|
143 |
* @return string quoted identifier string
|
|
|
144 |
*/
|
|
|
145 |
public function quoteMultipleIdentifier($arr, $checkOption = true)
|
|
|
146 |
{
|
|
|
147 |
foreach ($arr as $k => $v) {
|
|
|
148 |
$arr[$k] = $this->quoteIdentifier($v, $checkOption);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
return $arr;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* quote
|
|
|
156 |
* quotes given input parameter
|
|
|
157 |
*
|
|
|
158 |
* @param mixed $input parameter to be quoted
|
|
|
159 |
* @param string $type
|
|
|
160 |
* @return string
|
|
|
161 |
*/
|
|
|
162 |
public function quote($input, $type = null)
|
|
|
163 |
{
|
|
|
164 |
if ($type == null) {
|
|
|
165 |
$type = gettype($input);
|
|
|
166 |
}
|
|
|
167 |
switch ($type) {
|
|
|
168 |
case 'integer':
|
|
|
169 |
case 'double':
|
|
|
170 |
case 'float':
|
|
|
171 |
case 'bool':
|
|
|
172 |
case 'decimal':
|
|
|
173 |
case 'int':
|
|
|
174 |
return $input;
|
|
|
175 |
case 'array':
|
|
|
176 |
case 'object':
|
|
|
177 |
$input = serialize($input);
|
|
|
178 |
case 'date':
|
|
|
179 |
case 'time':
|
|
|
180 |
case 'timestamp':
|
|
|
181 |
case 'string':
|
|
|
182 |
case 'char':
|
|
|
183 |
case 'varchar':
|
|
|
184 |
case 'text':
|
|
|
185 |
case 'gzip':
|
|
|
186 |
case 'blob':
|
|
|
187 |
case 'clob':
|
|
|
188 |
case 'enum':
|
|
|
189 |
case 'set':
|
|
|
190 |
case 'boolean':
|
|
|
191 |
return "'" . str_replace("'","''",$input) . "'";
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Removes any formatting in an sequence name using the 'seqname_format' option
|
|
|
197 |
*
|
|
|
198 |
* @param string $sqn string that containts name of a potential sequence
|
|
|
199 |
* @return string name of the sequence with possible formatting removed
|
|
|
200 |
*/
|
|
|
201 |
public function fixSequenceName($sqn)
|
|
|
202 |
{
|
|
|
203 |
$seqPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine_Core::ATTR_SEQNAME_FORMAT)).'$/i';
|
|
|
204 |
$seqName = preg_replace($seqPattern, '\\1', $sqn);
|
|
|
205 |
|
|
|
206 |
if ($seqName && ! strcasecmp($sqn, $this->getSequenceName($seqName))) {
|
|
|
207 |
return $seqName;
|
|
|
208 |
}
|
|
|
209 |
return $sqn;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* Removes any formatting in an index name using the 'idxname_format' option
|
|
|
214 |
*
|
|
|
215 |
* @param string $idx string that containts name of anl index
|
|
|
216 |
* @return string name of the index with possible formatting removed
|
|
|
217 |
*/
|
|
|
218 |
public function fixIndexName($idx)
|
|
|
219 |
{
|
|
|
220 |
$indexPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine_Core::ATTR_IDXNAME_FORMAT)).'$/i';
|
|
|
221 |
$indexName = preg_replace($indexPattern, '\\1', $idx);
|
|
|
222 |
if ($indexName && ! strcasecmp($idx, $this->getIndexName($indexName))) {
|
|
|
223 |
return $indexName;
|
|
|
224 |
}
|
|
|
225 |
return $idx;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
* adds sequence name formatting to a sequence name
|
|
|
230 |
*
|
|
|
231 |
* @param string name of the sequence
|
|
|
232 |
* @return string formatted sequence name
|
|
|
233 |
*/
|
|
|
234 |
public function getSequenceName($sqn)
|
|
|
235 |
{
|
|
|
236 |
return sprintf($this->conn->getAttribute(Doctrine_Core::ATTR_SEQNAME_FORMAT),
|
|
|
237 |
preg_replace('/[^a-z0-9_\$.]/i', '_', $sqn));
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* adds index name formatting to a index name
|
|
|
242 |
*
|
|
|
243 |
* @param string name of the index
|
|
|
244 |
* @return string formatted index name
|
|
|
245 |
*/
|
|
|
246 |
public function getIndexName($idx)
|
|
|
247 |
{
|
|
|
248 |
return sprintf($this->conn->getAttribute(Doctrine_Core::ATTR_IDXNAME_FORMAT),
|
|
|
249 |
preg_replace('/[^a-z0-9_\$]/i', '_', $idx));
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
* Formatting a foreign Key name
|
|
|
254 |
*
|
|
|
255 |
* @param string name of the foreign key
|
|
|
256 |
* @return string formatted foreign key name
|
|
|
257 |
*/
|
|
|
258 |
public function getForeignKeyName($fkey)
|
|
|
259 |
{
|
|
|
260 |
return sprintf($this->conn->getAttribute(Doctrine_Core::ATTR_FKNAME_FORMAT),
|
|
|
261 |
preg_replace('/[^a-z0-9_\$]/i', '_', $fkey));
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
/**
|
|
|
265 |
* adds table name formatting to a table name
|
|
|
266 |
*
|
|
|
267 |
* @param string name of the table
|
|
|
268 |
* @return string formatted table name
|
|
|
269 |
*/
|
|
|
270 |
public function getTableName($table)
|
|
|
271 |
{
|
|
|
272 |
$format = $this->conn->getAttribute(Doctrine_Core::ATTR_TBLNAME_FORMAT);
|
|
|
273 |
return sprintf($format, str_replace(sprintf($format, null), null, $table));
|
|
|
274 |
}
|
|
|
275 |
}
|