1 |
<?php
|
2 |
/*
|
3 |
* $Id: Mssql.php 2702 2007-10-03 21:43:22Z Jonathan.Wage $
|
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 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: 2702 $
|
31 |
* @link www.phpdoctrine.com
|
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 'array':
|
66 |
case 'object':
|
67 |
case 'text':
|
68 |
case 'char':
|
69 |
case 'varchar':
|
70 |
case 'string':
|
71 |
case 'gzip':
|
72 |
$length = !empty($field['length'])
|
73 |
? $field['length'] : false;
|
74 |
|
75 |
$fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
|
76 |
|
77 |
return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->options['default_text_field_length'].')')
|
78 |
: ($length ? 'VARCHAR('.$length.')' : 'TEXT');
|
79 |
case 'clob':
|
80 |
if ( ! empty($field['length'])) {
|
81 |
$length = $field['length'];
|
82 |
if ($length <= 8000) {
|
83 |
return 'VARCHAR('.$length.')';
|
84 |
}
|
85 |
}
|
86 |
return 'TEXT';
|
87 |
case 'blob':
|
88 |
if ( ! empty($field['length'])) {
|
89 |
$length = $field['length'];
|
90 |
if ($length <= 8000) {
|
91 |
return "VARBINARY($length)";
|
92 |
}
|
93 |
}
|
94 |
return 'IMAGE';
|
95 |
case 'integer':
|
96 |
case 'enum':
|
97 |
case 'int':
|
98 |
return 'INT';
|
99 |
case 'boolean':
|
100 |
return 'BIT';
|
101 |
case 'date':
|
102 |
return 'CHAR(' . strlen('YYYY-MM-DD') . ')';
|
103 |
case 'time':
|
104 |
return 'CHAR(' . strlen('HH:MM:SS') . ')';
|
105 |
case 'timestamp':
|
106 |
return 'CHAR(' . strlen('YYYY-MM-DD HH:MM:SS') . ')';
|
107 |
case 'float':
|
108 |
return 'FLOAT';
|
109 |
case 'decimal':
|
110 |
$length = !empty($field['length']) ? $field['length'] : 18;
|
111 |
$scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES);
|
112 |
return 'DECIMAL('.$length.','.$scale.')';
|
113 |
}
|
114 |
|
115 |
throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.');
|
116 |
}
|
117 |
/**
|
118 |
* Maps a native array description of a field to a MDB2 datatype and length
|
119 |
*
|
120 |
* @param array $field native field description
|
121 |
* @return array containing the various possible types, length, sign, fixed
|
122 |
*/
|
123 |
public function getPortableDeclaration($field)
|
124 |
{
|
125 |
$db_type = preg_replace('/\d/','', strtolower($field['type']) );
|
126 |
$length = (isset($field['length']) && $field['length'] > 0) ? $field['length'] : null;
|
127 |
|
128 |
$type = array();
|
129 |
// todo: unsigned handling seems to be missing
|
130 |
$unsigned = $fixed = null;
|
131 |
|
132 |
if ( ! isset($field['name']))
|
133 |
$field['name'] = '';
|
134 |
|
135 |
switch ($db_type) {
|
136 |
case 'bit':
|
137 |
$type[0] = 'boolean';
|
138 |
break;
|
139 |
case 'int':
|
140 |
$type[0] = 'integer';
|
141 |
if ($length == 1) {
|
142 |
$type[] = 'boolean';
|
143 |
}
|
144 |
break;
|
145 |
case 'datetime':
|
146 |
$type[0] = 'timestamp';
|
147 |
break;
|
148 |
case 'float':
|
149 |
case 'real':
|
150 |
case 'numeric':
|
151 |
$type[0] = 'float';
|
152 |
break;
|
153 |
case 'decimal':
|
154 |
case 'money':
|
155 |
$type[0] = 'decimal';
|
156 |
break;
|
157 |
case 'text':
|
158 |
case 'varchar':
|
159 |
$fixed = false;
|
160 |
case 'char':
|
161 |
$type[0] = 'string';
|
162 |
if ($length == '1') {
|
163 |
$type[] = 'boolean';
|
164 |
if (preg_match('/^[is|has]/', $field['name'])) {
|
165 |
$type = array_reverse($type);
|
166 |
}
|
167 |
} elseif (strstr($db_type, 'text')) {
|
168 |
$type[] = 'clob';
|
169 |
}
|
170 |
if ($fixed !== false) {
|
171 |
$fixed = true;
|
172 |
}
|
173 |
break;
|
174 |
case 'image':
|
175 |
case 'varbinary':
|
176 |
$type[] = 'blob';
|
177 |
$length = null;
|
178 |
break;
|
179 |
default:
|
180 |
throw new Doctrine_DataDict_Exception('unknown database attribute type: '.$db_type);
|
181 |
}
|
182 |
|
183 |
return array('type' => $type,
|
184 |
'length' => $length,
|
185 |
'unsigned' => $unsigned,
|
186 |
'fixed' => $fixed);
|
187 |
}
|
188 |
}
|