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 |
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::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 |
* quote
|
137 |
* quotes given input parameter
|
138 |
*
|
139 |
* @param mixed $input parameter to be quoted
|
140 |
* @param string $type
|
141 |
* @return mixed
|
142 |
*/
|
143 |
public function quote($input, $type = null)
|
144 |
{
|
145 |
if ($type == null) {
|
146 |
$type = gettype($input);
|
147 |
}
|
148 |
switch ($type) {
|
149 |
case 'integer':
|
150 |
case 'enum':
|
151 |
case 'boolean':
|
152 |
case 'double':
|
153 |
case 'float':
|
154 |
case 'bool':
|
155 |
case 'decimal':
|
156 |
case 'int':
|
157 |
return $input;
|
158 |
case 'array':
|
159 |
case 'object':
|
160 |
$input = serialize($input);
|
161 |
case 'string':
|
162 |
case 'char':
|
163 |
case 'varchar':
|
164 |
case 'text':
|
165 |
case 'gzip':
|
166 |
case 'blob':
|
167 |
case 'clob':
|
168 |
$this->conn->connect();
|
169 |
|
170 |
return $this->conn->getDbh()->quote($input);
|
171 |
}
|
172 |
}
|
173 |
|
174 |
/**
|
175 |
* Removes any formatting in an sequence name using the 'seqname_format' option
|
176 |
*
|
177 |
* @param string $sqn string that containts name of a potential sequence
|
178 |
* @return string name of the sequence with possible formatting removed
|
179 |
*/
|
180 |
public function fixSequenceName($sqn)
|
181 |
{
|
182 |
$seqPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT)).'$/i';
|
183 |
$seqName = preg_replace($seqPattern, '\\1', $sqn);
|
184 |
|
185 |
if ($seqName && ! strcasecmp($sqn, $this->getSequenceName($seqName))) {
|
186 |
return $seqName;
|
187 |
}
|
188 |
return $sqn;
|
189 |
}
|
190 |
|
191 |
/**
|
192 |
* Removes any formatting in an index name using the 'idxname_format' option
|
193 |
*
|
194 |
* @param string $idx string that containts name of anl index
|
195 |
* @return string name of the index with possible formatting removed
|
196 |
*/
|
197 |
public function fixIndexName($idx)
|
198 |
{
|
199 |
$indexPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT)).'$/i';
|
200 |
$indexName = preg_replace($indexPattern, '\\1', $idx);
|
201 |
if ($indexName && ! strcasecmp($idx, $this->getIndexName($indexName))) {
|
202 |
return $indexName;
|
203 |
}
|
204 |
return $idx;
|
205 |
}
|
206 |
|
207 |
/**
|
208 |
* adds sequence name formatting to a sequence name
|
209 |
*
|
210 |
* @param string name of the sequence
|
211 |
* @return string formatted sequence name
|
212 |
*/
|
213 |
public function getSequenceName($sqn)
|
214 |
{
|
215 |
return sprintf($this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT),
|
216 |
preg_replace('/[^a-z0-9_\$.]/i', '_', $sqn));
|
217 |
}
|
218 |
|
219 |
/**
|
220 |
* adds index name formatting to a index name
|
221 |
*
|
222 |
* @param string name of the index
|
223 |
* @return string formatted index name
|
224 |
*/
|
225 |
public function getIndexName($idx)
|
226 |
{
|
227 |
return sprintf($this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT),
|
228 |
preg_replace('/[^a-z0-9_\$]/i', '_', $idx));
|
229 |
}
|
230 |
|
231 |
/**
|
232 |
* adds table name formatting to a table name
|
233 |
*
|
234 |
* @param string name of the table
|
235 |
* @return string formatted table name
|
236 |
*/
|
237 |
public function getTableName($table)
|
238 |
{
|
239 |
return sprintf($this->conn->getAttribute(Doctrine::ATTR_TBLNAME_FORMAT),
|
240 |
preg_replace('/[^a-z0-9_\$]/i', '_', $table));
|
241 |
}
|
242 |
}
|