# Source for file Formatter.php

Documentation is available at Formatter.php

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. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @category Object Relational Mapping
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;
52. $text = str_replace($tmp['escape_pattern'],
53. $tmp['escape_pattern'] .
54. $tmp['escape_pattern'], $text);
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);
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 'int':
152. return $input;
153. case 'array':
154. case 'object':
155. $input = serialize($input);
156. case 'string':
157. case 'char':
158. case 'varchar':
159. case 'text':
160. case 'gzip':
161. case 'blob':
162. case 'clob':
163. $this->conn->connect();
165. return $this->conn->getDbh()->quote($input);
166. }
167. }
168. /**
169. * Removes any formatting in an sequence name using the 'seqname_format' option
170. *
171. * @param string $sqn string that containts name of a potential sequence
172. * @return string name of the sequence with possible formatting removed
173. */
174. public function fixSequenceName($sqn)
175. {
176. $seqPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT)).'$/i';
177. $seqName = preg_replace($seqPattern, '\\1', $sqn);
179. if ($seqName && ! strcasecmp($sqn, $this->getSequenceName($seqName))) {
180. return $seqName;
181. }
182. return $sqn;
183. }
184. /**
185. * Removes any formatting in an index name using the 'idxname_format' option
186. *
187. * @param string $idx string that containts name of anl index
188. * @return string name of the index with possible formatting removed
189. */
190. public function fixIndexName($idx)
191. {
192. $indexPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT)).'$/i';
193. $indexName = preg_replace($indexPattern, '\\1', $idx);
194. if ($indexName && ! strcasecmp($idx, $this->getIndexName($indexName))) {
195. return $indexName;
196. }
197. return $idx;
198. }
199. /**
200. * adds sequence name formatting to a sequence name
201. *
202. * @param string name of the sequence
203. * @return string formatted sequence name
204. */
205. public function getSequenceName($sqn)
206. {
207. return sprintf($this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT),
208. preg_replace('/[^a-z0-9_\$.]/i', '_', $sqn));
209. }
210. /**
211. * adds index name formatting to a index name
212. *
213. * @param string name of the index
214. * @return string formatted index name
215. */
216. public function getIndexName($idx)
217. {
218. return sprintf($this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT),
219. preg_replace('/[^a-z0-9_\$]/i', '_', $idx));
220. }
221. }