| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Mssql.php 7660 2010-06-08 18:30:22Z jwage $
|
|
|
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.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* @package Doctrine
|
|
|
24 |
* @subpackage 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: 7660 $
|
|
|
31 |
* @link www.doctrine-project.org
|
|
|
32 |
* @since 1.0
|
|
|
33 |
*/
|
|
|
34 |
class Doctrine_DataDict_Mssql 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 |
*
|
|
|
56 |
* @return string DBMS specific SQL code portion that should be used to
|
|
|
57 |
* declare the specified field.
|
|
|
58 |
*/
|
|
|
59 |
public function getNativeDeclaration($field)
|
|
|
60 |
{
|
|
|
61 |
if ( ! isset($field['type'])) {
|
|
|
62 |
throw new Doctrine_DataDict_Exception('Missing column type.');
|
|
|
63 |
}
|
|
|
64 |
switch ($field['type']) {
|
|
|
65 |
case 'enum':
|
|
|
66 |
$field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255;
|
|
|
67 |
case 'array':
|
|
|
68 |
case 'object':
|
|
|
69 |
case 'text':
|
|
|
70 |
case 'char':
|
|
|
71 |
case 'varchar':
|
|
|
72 |
case 'string':
|
|
|
73 |
case 'gzip':
|
|
|
74 |
$length = !empty($field['length'])
|
|
|
75 |
? $field['length'] : false;
|
|
|
76 |
|
|
|
77 |
$fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
|
|
|
78 |
|
|
|
79 |
return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->varchar_max_length.')')
|
|
|
80 |
: (($length && $length <= $this->conn->varchar_max_length) ? 'VARCHAR('.$length.')' : 'TEXT');
|
|
|
81 |
case 'clob':
|
|
|
82 |
if ( ! empty($field['length'])) {
|
|
|
83 |
$length = $field['length'];
|
|
|
84 |
if ($length <= 8000) {
|
|
|
85 |
return 'VARCHAR('.$length.')';
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
return 'TEXT';
|
|
|
89 |
case 'blob':
|
|
|
90 |
if ( ! empty($field['length'])) {
|
|
|
91 |
$length = $field['length'];
|
|
|
92 |
if ($length <= 8000) {
|
|
|
93 |
return "VARBINARY($length)";
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
return 'IMAGE';
|
|
|
97 |
case 'integer':
|
|
|
98 |
case 'int':
|
|
|
99 |
return (isset($field['unsigned']) && $field['unsigned']) ? 'BIGINT' : '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_Core::ATTR_DECIMAL_PLACES);
|
|
|
113 |
return 'DECIMAL('.$length.','.$scale.')';
|
|
|
114 |
}
|
|
|
115 |
return $field['type'] . (isset($field['length']) ? '('.$field['length'].')':null);
|
|
|
116 |
}
|
|
|
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;
|
|
|
128 |
|
|
|
129 |
$type = array();
|
|
|
130 |
// todo: unsigned handling seems to be missing
|
|
|
131 |
$unsigned = $fixed = null;
|
|
|
132 |
|
|
|
133 |
if ( ! isset($field['name']))
|
|
|
134 |
$field['name'] = '';
|
|
|
135 |
|
|
|
136 |
switch ($db_type) {
|
|
|
137 |
case 'bit':
|
|
|
138 |
$type[0] = 'boolean';
|
|
|
139 |
break;
|
|
|
140 |
case 'tinyint':
|
|
|
141 |
case 'smallint':
|
|
|
142 |
case 'bigint':
|
|
|
143 |
case 'int':
|
|
|
144 |
$type[0] = 'integer';
|
|
|
145 |
if ($length == 1) {
|
|
|
146 |
$type[] = 'boolean';
|
|
|
147 |
}
|
|
|
148 |
break;
|
|
|
149 |
case 'date':
|
|
|
150 |
$type[0] = 'date';
|
|
|
151 |
break;
|
|
|
152 |
case 'datetime':
|
|
|
153 |
case 'timestamp':
|
|
|
154 |
case 'smalldatetime':
|
|
|
155 |
$type[0] = 'timestamp';
|
|
|
156 |
break;
|
|
|
157 |
case 'float':
|
|
|
158 |
case 'real':
|
|
|
159 |
case 'numeric':
|
|
|
160 |
$type[0] = 'float';
|
|
|
161 |
break;
|
|
|
162 |
case 'decimal':
|
|
|
163 |
case 'money':
|
|
|
164 |
case 'smallmoney':
|
|
|
165 |
$type[0] = 'decimal';
|
|
|
166 |
break;
|
|
|
167 |
case 'text':
|
|
|
168 |
case 'varchar':
|
|
|
169 |
case 'ntext':
|
|
|
170 |
case 'nvarchar':
|
|
|
171 |
$fixed = false;
|
|
|
172 |
case 'char':
|
|
|
173 |
case 'nchar':
|
|
|
174 |
$type[0] = 'string';
|
|
|
175 |
if ($length == '1') {
|
|
|
176 |
$type[] = 'boolean';
|
|
|
177 |
if (preg_match('/^[is|has]/', $field['name'])) {
|
|
|
178 |
$type = array_reverse($type);
|
|
|
179 |
}
|
|
|
180 |
} elseif (strstr($db_type, 'text')) {
|
|
|
181 |
$type[] = 'clob';
|
|
|
182 |
}
|
|
|
183 |
if ($fixed !== false) {
|
|
|
184 |
$fixed = true;
|
|
|
185 |
}
|
|
|
186 |
break;
|
|
|
187 |
case 'image':
|
|
|
188 |
case 'varbinary':
|
|
|
189 |
$type[] = 'blob';
|
|
|
190 |
$length = null;
|
|
|
191 |
break;
|
|
|
192 |
case 'uniqueidentifier':
|
|
|
193 |
$type[] = 'string';
|
|
|
194 |
$length = 36;
|
|
|
195 |
break;
|
|
|
196 |
case 'sql_variant':
|
|
|
197 |
case 'sysname':
|
|
|
198 |
case 'binary':
|
|
|
199 |
$type[] = 'string';
|
|
|
200 |
$length = null;
|
|
|
201 |
break;
|
|
|
202 |
default:
|
|
|
203 |
$type[] = $field['type'];
|
|
|
204 |
$length = isset($field['length']) ? $field['length']:null;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
return array('type' => $type,
|
|
|
208 |
'length' => $length,
|
|
|
209 |
'unsigned' => $unsigned,
|
|
|
210 |
'fixed' => $fixed);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Obtain DBMS specific SQL code portion needed to declare an integer type
|
|
|
215 |
* field to be used in statements like CREATE TABLE.
|
|
|
216 |
*
|
|
|
217 |
* @param string $name name the field to be declared.
|
|
|
218 |
* @param string $field associative array with the name of the properties
|
|
|
219 |
* of the field being declared as array indexes.
|
|
|
220 |
* Currently, the types of supported field
|
|
|
221 |
* properties are as follows:
|
|
|
222 |
*
|
|
|
223 |
* unsigned
|
|
|
224 |
* Boolean flag that indicates whether the field
|
|
|
225 |
* should be declared as unsigned integer if
|
|
|
226 |
* possible.
|
|
|
227 |
*
|
|
|
228 |
* default
|
|
|
229 |
* Integer value to be used as default for this
|
|
|
230 |
* field.
|
|
|
231 |
*
|
|
|
232 |
* notnull
|
|
|
233 |
* Boolean flag that indicates whether this field is
|
|
|
234 |
* constrained to not be set to null.
|
|
|
235 |
* @return string DBMS specific SQL code portion that should be used to
|
|
|
236 |
* declare the specified field.
|
|
|
237 |
*/
|
|
|
238 |
public function getIntegerDeclaration($name, $field)
|
|
|
239 |
{
|
|
|
240 |
$default = $autoinc = '';
|
|
|
241 |
if ( ! empty($field['autoincrement'])) {
|
|
|
242 |
$autoinc = ' identity';
|
|
|
243 |
} elseif (array_key_exists('default', $field)) {
|
|
|
244 |
if ($field['default'] === '') {
|
|
|
245 |
$field['default'] = empty($field['notnull']) ? null : 0;
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
$value = (is_null($field['default'])
|
|
|
249 |
? 'NULL'
|
|
|
250 |
: $this->conn->quote($field['default']));
|
|
|
251 |
|
|
|
252 |
// Name the constraint if a name has been supplied
|
|
|
253 |
if (array_key_exists('defaultConstraintName', $field)) {
|
|
|
254 |
$default .= ' CONSTRAINT ' . $field['defaultConstraintName'];
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
$default .= ' DEFAULT ' . $value;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ' NULL';
|
|
|
262 |
//$unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : '';
|
|
|
263 |
// MSSQL does not support the UNSIGNED keyword
|
|
|
264 |
$unsigned = '';
|
|
|
265 |
$comment = (isset($field['comment']) && $field['comment'])
|
|
|
266 |
? " COMMENT " . $this->conn->quote($field['comment'], 'text') : '';
|
|
|
267 |
|
|
|
268 |
$name = $this->conn->quoteIdentifier($name, true);
|
|
|
269 |
|
|
|
270 |
return $name . ' ' . $this->getNativeDeclaration($field) . $unsigned
|
|
|
271 |
. $default . $notnull . $autoinc . $comment;
|
|
|
272 |
}
|
|
|
273 |
}
|