# Source for file Firebird.php

Documentation is available at Firebird.php

1. <?php
2. /*
3. * $Id: Firebird.php 1731 2007-06-18 18:30:19Z zYne $
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_DataDict');
22. /**
23. * @package Doctrine
24. * @subpackage Doctrine_DataDict
25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27. * @author Lorenzo Alberton <l.alberton@quipo.it> (PEAR MDB2 Interbase driver)
28. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
29. * @version $Revision: 1731 $
30. * @category Object Relational Mapping
31. * @link www.phpdoctrine.com
32. * @since 1.0
33. */
34. class Doctrine_DataDict_Firebird extends Doctrine_DataDict
35. {
36. /**
37. * Obtain DBMS specific SQL code portion needed to declare an text type
38. * field to be used in statements like CREATE TABLE.
39. *
40. * @param array $field associative array with the name of the properties
41. * of the field being declared as array indexes. Currently, the types
42. * of supported field properties are as follows:
43. *
44. * length
45. * Integer value that determines the maximum length of the text
46. * field. If this argument is missing the field should be
47. * declared to have the longest length allowed by the DBMS.
48. *
49. * default
50. * Text value to be used as default for this field.
51. *
52. * notnull
53. * Boolean flag that indicates whether this field is constrained
54. * to not be set to null.
55. * @return string DBMS specific SQL code portion that should be used to
56. * declare the specified field.
57. */
58. public function getNativeDeclaration($field)
59. {
60. if ( ! isset($field['type'])) {
61. throw new Doctrine_DataDict_Exception('Missing column type.');
62. }
63. switch ($field['type']) {
64. case 'varchar':
65. case 'string':
66. case 'array':
67. case 'object':
68. case 'char':
69. case 'text':
70. case 'gzip':
71. $length = !empty($field['length'])
72. ? $field['length'] : 16777215; // TODO: $this->conn->options['default_text_field_length'];
74. $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
76. return $fixed ? 'CHAR('.$length.')' : 'VARCHAR('.$length.')';
77. case 'clob':
78. return 'BLOB SUB_TYPE 1';
79. case 'blob':
80. return 'BLOB SUB_TYPE 0';
81. case 'integer':
82. case 'enum':
83. case 'int':
84. return 'INT';
85. case 'boolean':
86. return 'SMALLINT';
87. case 'date':
88. return 'DATE';
89. case 'time':
90. return 'TIME';
91. case 'timestamp':
92. return 'TIMESTAMP';
93. case 'float':
94. return 'DOUBLE PRECISION';
95. case 'decimal':
96. $length = !empty($field['length']) ? $field['length'] : 18;
97. $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES);
98. return 'DECIMAL('.$length.','.$scale.')';
99. }
101. throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.');
102. }
103. /**
104. * Maps a native array description of a field to a Doctrine datatype and length
105. *
106. * @param array $field native field description
107. * @return array containing the various possible types, length, sign, fixed
108. */
109. public function getPortableDeclaration($field)
110. {
111. $length = (isset($field['length']) && $field['length'] > 0) ? $field['length'] : null;
113. $type = array();
114. $unsigned = $fixed = null;
115. $dbType = strtolower($field['type']);
116. $field['field_sub_type'] = !empty($field['field_sub_type'])
117. ? strtolower($field['field_sub_type']) : null;
119. if ( ! isset($field['name'])) {
120. $field['name'] = '';
121. }
123. switch ($dbType) {
124. case 'smallint':
125. case 'integer':
126. case 'int64':
127. //these may be 'numeric' or 'decimal'
128. if (isset($field['field_sub_type'])) {
129. $field['type'] = $field['field_sub_type'];
130. return $this->getPortableDeclaration($field);
131. }
132. case 'bigint':
133. case 'quad':
134. $type[] = 'integer';
135. if ($length == '1') {
136. $type[] = 'boolean';
137. if (preg_match('/^(is|has)/', $field['name'])) {
138. $type = array_reverse($type);
139. }
140. }
141. break;
142. case 'varchar':
143. $fixed = false;
144. case 'char':
145. case 'cstring':
146. $type[] = 'string';
147. if ($length == '1') {
148. $type[] = 'boolean';
149. if (preg_match('/^(is|has)/', $field['name'])) {
150. $type = array_reverse($type);
151. }
152. }
153. if ($fixed !== false) {
154. $fixed = true;
155. }
156. break;
157. case 'date':
158. $type[] = 'date';
159. $length = null;
160. break;
161. case 'timestamp':
162. $type[] = 'timestamp';
163. $length = null;
164. break;
165. case 'time':
166. $type[] = 'time';
167. $length = null;
168. break;
169. case 'float':
170. case 'double':
171. case 'double precision':
172. case 'd_float':
173. $type[] = 'float';
174. break;
175. case 'decimal':
176. case 'numeric':
177. $type[] = 'decimal';
178. break;
179. case 'blob':
180. $type[] = ($field['field_sub_type'] == 'text') ? 'clob' : 'blob';
181. $length = null;
182. break;
183. default:
184. throw new Doctrine_DataDict_Exception('unknown database attribute type: '.$dbType);
185. }
187. return array('type' => $type,
188. 'length' => $length,
189. 'unsigned' => $unsigned,
190. 'fixed' => $fixed);
191. }
192. /**
193. * Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
194. * of a field declaration to be used in statements like CREATE TABLE.
195. *
196. * @param string $charset name of the charset
197. * @return string DBMS specific SQL code portion needed to set the CHARACTER SET
198. * of a field declaration.
199. */
200. public function getCharsetFieldDeclaration($charset)
201. {
202. return 'CHARACTER SET ' . $charset;
203. }
204. /**
205. * Obtain DBMS specific SQL code portion needed to set the COLLATION
206. * of a field declaration to be used in statements like CREATE TABLE.
207. *
208. * @param string $collation name of the collation
209. * @return string DBMS specific SQL code portion needed to set the COLLATION
210. * of a field declaration.
211. */
212. public function getCollationFieldDeclaration($collation)
213. {
214. return 'COLLATE ' . $collation;
215. }
216. }