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.phpdoctrine.com>.
|
20 |
*/
|
21 |
Doctrine::autoload('Doctrine_Connection_Module');
|
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.phpdoctrine.com
|
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 |
$tmp = $this->conn->string_quoting;
|
51 |
|
52 |
$text = str_replace($tmp['escape_pattern'],
|
53 |
$tmp['escape_pattern'] .
|
54 |
$tmp['escape_pattern'], $text);
|
55 |
|
56 |
foreach ($this->wildcards as $wildcard) {
|
57 |
$text = str_replace($wildcard, $tmp['escape_pattern'] . $wildcard, $text);
|
58 |
}
|
59 |
}
|
60 |
return $text;
|
61 |
}
|
62 |
|
63 |
/**
|
64 |
* convertBooleans
|
65 |
* some drivers need the boolean values to be converted into integers
|
66 |
* when using DQL API
|
67 |
*
|
68 |
* This method takes care of that conversion
|
69 |
*
|
70 |
* @param array $item
|
71 |
* @return void
|
72 |
*/
|
73 |
public function convertBooleans($item)
|
74 |
{
|
75 |
if (is_array($item)) {
|
76 |
foreach ($item as $k => $value) {
|
77 |
if (is_bool($value)) {
|
78 |
$item[$k] = (int) $value;
|
79 |
}
|
80 |
}
|
81 |
} else {
|
82 |
if (is_bool($item)) {
|
83 |
$item = (int) $item;
|
84 |
}
|
85 |
}
|
86 |
return $item;
|
87 |
}
|
88 |
|
89 |
/**
|
90 |
* Quote a string so it can be safely used as a table or column name
|
91 |
*
|
92 |
* Delimiting style depends on which database driver is being used.
|
93 |
*
|
94 |
* NOTE: just because you CAN use delimited identifiers doesn't mean
|
95 |
* you SHOULD use them. In general, they end up causing way more
|
96 |
* problems than they solve.
|
97 |
*
|
98 |
* Portability is broken by using the following characters inside
|
99 |
* delimited identifiers:
|
100 |
* + backtick (<kbd>`</kbd>) -- due to MySQL
|
101 |
* + double quote (<kbd>"</kbd>) -- due to Oracle
|
102 |
* + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
|
103 |
*
|
104 |
* Delimited identifiers are known to generally work correctly under
|
105 |
* the following drivers:
|
106 |
* + mssql
|
107 |
* + mysql
|
108 |
* + mysqli
|
109 |
* + oci8
|
110 |
* + pgsql
|
111 |
* + sqlite
|
112 |
*
|
113 |
* InterBase doesn't seem to be able to use delimited identifiers
|
114 |
* via PHP 4. They work fine under PHP 5.
|
115 |
*
|
116 |
* @param string $str identifier name to be quoted
|
117 |
* @param bool $checkOption check the 'quote_identifier' option
|
118 |
*
|
119 |
* @return string quoted identifier string
|
120 |
*/
|
121 |
public function quoteIdentifier($str, $checkOption = true)
|
122 |
{
|
123 |
if ($checkOption && ! $this->conn->getAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER)) {
|
124 |
return $str;
|
125 |
}
|
126 |
$tmp = $this->conn->identifier_quoting;
|
127 |
$str = str_replace($tmp['end'],
|
128 |
$tmp['escape'] .
|
129 |
$tmp['end'], $str);
|
130 |
|
131 |
return $tmp['start'] . $str . $tmp['end'];
|
132 |
}
|
133 |
|
134 |
/**
|
135 |
* quote
|
136 |
* quotes given input parameter
|
137 |
*
|
138 |
* @param mixed $input parameter to be quoted
|
139 |
* @param string $type
|
140 |
* @return mixed
|
141 |
*/
|
142 |
public function quote($input, $type = null)
|
143 |
{
|
144 |
if ($type == null) {
|
145 |
$type = gettype($input);
|
146 |
}
|
147 |
switch ($type) {
|
148 |
case 'integer':
|
149 |
case 'enum':
|
150 |
case 'boolean':
|
151 |
case 'double':
|
152 |
case 'float':
|
153 |
case 'bool':
|
154 |
case 'decimal':
|
155 |
case 'int':
|
156 |
return $input;
|
157 |
case 'array':
|
158 |
case 'object':
|
159 |
$input = serialize($input);
|
160 |
case 'string':
|
161 |
case 'char':
|
162 |
case 'varchar':
|
163 |
case 'text':
|
164 |
case 'gzip':
|
165 |
case 'blob':
|
166 |
case 'clob':
|
167 |
$this->conn->connect();
|
168 |
|
169 |
return $this->conn->getDbh()->quote($input);
|
170 |
}
|
171 |
}
|
172 |
|
173 |
/**
|
174 |
* Removes any formatting in an sequence name using the 'seqname_format' option
|
175 |
*
|
176 |
* @param string $sqn string that containts name of a potential sequence
|
177 |
* @return string name of the sequence with possible formatting removed
|
178 |
*/
|
179 |
public function fixSequenceName($sqn)
|
180 |
{
|
181 |
$seqPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT)).'$/i';
|
182 |
$seqName = preg_replace($seqPattern, '\\1', $sqn);
|
183 |
|
184 |
if ($seqName && ! strcasecmp($sqn, $this->getSequenceName($seqName))) {
|
185 |
return $seqName;
|
186 |
}
|
187 |
return $sqn;
|
188 |
}
|
189 |
|
190 |
/**
|
191 |
* Removes any formatting in an index name using the 'idxname_format' option
|
192 |
*
|
193 |
* @param string $idx string that containts name of anl index
|
194 |
* @return string name of the index with possible formatting removed
|
195 |
*/
|
196 |
public function fixIndexName($idx)
|
197 |
{
|
198 |
$indexPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT)).'$/i';
|
199 |
$indexName = preg_replace($indexPattern, '\\1', $idx);
|
200 |
if ($indexName && ! strcasecmp($idx, $this->getIndexName($indexName))) {
|
201 |
return $indexName;
|
202 |
}
|
203 |
return $idx;
|
204 |
}
|
205 |
|
206 |
/**
|
207 |
* adds sequence name formatting to a sequence name
|
208 |
*
|
209 |
* @param string name of the sequence
|
210 |
* @return string formatted sequence name
|
211 |
*/
|
212 |
public function getSequenceName($sqn)
|
213 |
{
|
214 |
return sprintf($this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT),
|
215 |
preg_replace('/[^a-z0-9_\$.]/i', '_', $sqn));
|
216 |
}
|
217 |
|
218 |
/**
|
219 |
* adds index name formatting to a index name
|
220 |
*
|
221 |
* @param string name of the index
|
222 |
* @return string formatted index name
|
223 |
*/
|
224 |
public function getIndexName($idx)
|
225 |
{
|
226 |
return sprintf($this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT),
|
227 |
preg_replace('/[^a-z0-9_\$]/i', '_', $idx));
|
228 |
}
|
229 |
} |