# Source for file Mssql.php

Documentation is available at Mssql.php

1. <?php
2. /*
3. * $Id: Mssql.php 1730 2007-06-18 18:27:11Z 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 Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28. * @author Frank M. Kromann <frank@kromann.info> (PEAR MDB2 Mssql driver)
29. * @author David Coallier <davidc@php.net> (PEAR MDB2 Mssql driver)
30. * @version $Revision: 1730 $
31. * @category Object Relational Mapping
32. * @link www.phpdoctrine.com
33. * @since 1.0
34. */
35. class Doctrine_DataDict_Mssql extends Doctrine_DataDict
36. {
37. /**
38. * Obtain DBMS specific SQL code portion needed to declare an text type
39. * field to be used in statements like CREATE TABLE.
40. *
41. * @param array $field associative array with the name of the properties
42. * of the field being declared as array indexes. Currently, the types
43. * of supported field properties are as follows:
44. *
45. * length
46. * Integer value that determines the maximum length of the text
47. * field. If this argument is missing the field should be
48. * declared to have the longest length allowed by the DBMS.
49. *
50. * default
51. * Text value to be used as default for this field.
52. *
53. * notnull
54. * Boolean flag that indicates whether this field is constrained
55. * to not be set to null.
56. *
57. * @return string DBMS specific SQL code portion that should be used to
58. * declare the specified field.
59. */
60. public function getNativeDeclaration($field)
61. {
62. if ( ! isset($field['type'])) {
63. throw new Doctrine_DataDict_Exception('Missing column type.');
64. }
65. switch ($field['type']) {
66. case 'array':
67. case 'object':
68. case 'text':
69. case 'char':
70. case 'varchar':
71. case 'string':
72. case 'gzip':
73. $length = !empty($field['length'])
74. ? $field['length'] : false;
76. $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
78. return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->options['default_text_field_length'].')')
79. : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
80. case 'clob':
81. if (!empty($field['length'])) {
82. $length = $field['length'];
83. if ($length <= 8000) {
84. return 'VARCHAR('.$length.')';
85. }
86. }
87. return 'TEXT';
88. case 'blob':
89. if (!empty($field['length'])) {
90. $length = $field['length'];
91. if ($length <= 8000) {
92. return "VARBINARY($length)";
93. }
94. }
95. return 'IMAGE';
96. case 'integer':
97. case 'enum':
98. case 'int':
99. return 'INT';
100. case 'boolean':
101. return 'BIT';
102. case 'date':
103. return 'CHAR(' . strlen('YYYY-MM-DD') . ')';
104. case 'time':
105. return 'CHAR(' . strlen('HH:MM:SS') . ')';
106. case 'timestamp':
107. return 'CHAR(' . strlen('YYYY-MM-DD HH:MM:SS') . ')';
108. case 'float':
109. return 'FLOAT';
110. case 'decimal':
111. $length = !empty($field['length']) ? $field['length'] : 18;
112. $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES);
113. return 'DECIMAL('.$length.','.$scale.')';
114. }
116. throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.');
117. }
118. /**
119. * Maps a native array description of a field to a MDB2 datatype and length
120. *
121. * @param array $field native field description
122. * @return array containing the various possible types, length, sign, fixed
123. */
124. public function getPortableDeclaration($field)
125. {
126. $db_type = preg_replace('/\d/','', strtolower($field['type']) );
127. $length = (isset($field['length']) && $field['length'] > 0) ? $field['length'] : null;
129. $type = array();
130. // todo: unsigned handling seems to be missing
131. $unsigned = $fixed = null;
133. if ( ! isset($field['name']))
134. $field['name'] = '';
136. switch ($db_type) {
137. case 'bit':
138. $type[0] = 'boolean';
139. break;
140. case 'int':
141. $type[0] = 'integer';
142. if ($length == 1) {
143. $type[] = 'boolean';
144. }
145. break;
146. case 'datetime':
147. $type[0] = 'timestamp';
148. break;
149. case 'float':
150. case 'real':
151. case 'numeric':
152. $type[0] = 'float';
153. break;
154. case 'decimal':
155. case 'money':
156. $type[0] = 'decimal';
157. break;
158. case 'text':
159. case 'varchar':
160. $fixed = false;
161. case 'char':
162. $type[0] = 'string';
163. if ($length == '1') {
164. $type[] = 'boolean';
165. if (preg_match('/^[is|has]/', $field['name'])) {
166. $type = array_reverse($type);
167. }
168. } elseif (strstr($db_type, 'text')) {
169. $type[] = 'clob';
170. }
171. if ($fixed !== false) {
172. $fixed = true;
173. }
174. break;
175. case 'image':
176. case 'varbinary':
177. $type[] = 'blob';
178. $length = null;
179. break;
180. default:
181. throw new Doctrine_DataDict_Exception('unknown database attribute type: '.$db_type);
182. }
184. return array('type' => $type,
185. 'length' => $length,
186. 'unsigned' => $unsigned,
187. 'fixed' => $fixed);
188. }
189. }