# Source for file Oracle.php

Documentation is available at Oracle.php

1. <?php
2. /*
3. * $Id: Oracle.php 1334 2007-05-11 19:20:38Z lsmith $
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. * @version $Revision: 1334 $
28. * @category Object Relational Mapping
29. * @link www.phpdoctrine.com
30. * @since 1.0
31. */
32. class Doctrine_DataDict_Oracle extends Doctrine_DataDict
33. {
34. /**
35. * Obtain DBMS specific SQL code portion needed to declare an text type
36. * field to be used in statements like CREATE TABLE.
37. *
38. * @param array $field associative array with the name of the properties
39. * of the field being declared as array indexes. Currently, the types
40. * of supported field properties are as follows:
41. *
42. * length
43. * Integer value that determines the maximum length of the text
44. * field. If this argument is missing the field should be
45. * declared to have the longest length allowed by the DBMS.
46. *
47. * default
48. * Text value to be used as default for this field.
49. *
50. * notnull
51. * Boolean flag that indicates whether this field is constrained
52. * to not be set to null.
53. * @return string DBMS specific SQL code portion that should be used to
54. * declare the specified field.
55. */
56. public function getNativeDeclaration(array $field)
57. {
58. if ( ! isset($field['type'])) {
59. throw new Doctrine_DataDict_Exception('Missing column type.');
60. }
61. switch ($field['type']) {
62. case 'string':
63. case 'array':
64. case 'object':
65. case 'gzip':
66. case 'char':
67. case 'varchar':
68. $length = !empty($field['length'])
69. ? $field['length'] : 16777215; // TODO: $this->conn->options['default_text_field_length'];
71. $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
73. return $fixed ? 'CHAR('.$length.')' : 'VARCHAR2('.$length.')';
74. case 'clob':
75. return 'CLOB';
76. case 'blob':
77. return 'BLOB';
78. case 'integer':
79. case 'enum':
80. case 'int':
81. if (!empty($field['length'])) {
82. return 'NUMBER('.$field['length'].')';
83. }
84. return 'INT';
85. case 'boolean':
86. return 'NUMBER(1)';
87. case 'date':
88. case 'time':
89. case 'timestamp':
90. return 'DATE';
91. case 'float':
92. case 'double':
93. return 'NUMBER';
94. case 'decimal':
95. $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES);
96. return 'NUMBER(*,'.$scale.')';
97. default:
98. }
99. throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.');
100. }
101. /**
102. * Maps a native array description of a field to a doctrine datatype and length
103. *
104. * @param array $field native field description
105. * @return array containing the various possible types, length, sign, fixed
106. * @throws Doctrine_DataDict_Oracle_Exception
107. */
108. public function getPortableDeclaration(array $field)
109. {
110. $dbType = strtolower($field['type']);
111. $type = array();
112. $length = $unsigned = $fixed = null;
113. if (!empty($field['length'])) {
114. $length = $field['length'];
115. }
117. if ( ! isset($field['name'])) {
118. $field['name'] = '';
119. }
121. switch ($dbType) {
122. case 'integer':
123. case 'pls_integer':
124. case 'binary_integer':
125. $type[] = 'integer';
126. if ($length == '1') {
127. $type[] = 'boolean';
128. if (preg_match('/^(is|has)/', $field['name'])) {
129. $type = array_reverse($type);
130. }
131. }
132. break;
133. case 'varchar':
134. case 'varchar2':
135. case 'nvarchar2':
136. $fixed = false;
137. case 'char':
138. case 'nchar':
139. $type[] = 'string';
140. if ($length == '1') {
141. $type[] = 'boolean';
142. if (preg_match('/^(is|has)/', $field['name'])) {
143. $type = array_reverse($type);
144. }
145. }
146. if ($fixed !== false) {
147. $fixed = true;
148. }
149. break;
150. case 'date':
151. case 'timestamp':
152. $type[] = 'timestamp';
153. $length = null;
154. break;
155. case 'float':
156. $type[] = 'float';
157. break;
158. case 'number':
159. if (!empty($field['scale'])) {
160. $type[] = 'decimal';
161. } else {
162. $type[] = 'integer';
163. if ($length == '1') {
164. $type[] = 'boolean';
165. if (preg_match('/^(is|has)/', $field['name'])) {
166. $type = array_reverse($type);
167. }
168. }
169. }
170. break;
171. case 'long':
172. $type[] = 'string';
173. case 'clob':
174. case 'nclob':
175. $type[] = 'clob';
176. break;
177. case 'blob':
178. case 'raw':
179. case 'long raw':
180. case 'bfile':
181. $type[] = 'blob';
182. $length = null;
183. break;
184. case 'rowid':
185. case 'urowid':
186. default:
187. throw new Doctrine_DataDict_Exception('unknown database attribute type: ' . $dbType);
188. }
190. return array('type' => $type,
191. 'length' => $length,
192. 'unsigned' => $unsigned,
193. 'fixed' => $fixed);
194. }
195. }