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