ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilDBPdoMySQLFieldDefinition.php
Go to the documentation of this file.
1<?php
2require_once('./Services/Database/classes/PDO/FieldDefinition/class.ilDBPdoFieldDefinition.php');
3
10{
11
16 public function getTypeDeclaration($field)
17 {
18 $db = $this->getDBInstance();
19
20 switch ($field['type']) {
21 case 'text':
22 if (empty($field['length']) && array_key_exists('default', $field)) {
23 $field['length'] = $db->varchar_max_length;
24 }
25 $length = !empty($field['length']) ? $field['length'] : false;
26 $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
27
28 return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
29 case 'clob':
30 if (!empty($field['length'])) {
31 $length = $field['length'];
32 if ($length <= 255) {
33 return 'TINYTEXT';
34 } elseif ($length <= 65532) {
35 return 'TEXT';
36 } elseif ($length <= 16777215) {
37 return 'MEDIUMTEXT';
38 }
39 }
40
41 return 'LONGTEXT';
42 case 'blob':
43 if (!empty($field['length'])) {
44 $length = $field['length'];
45 if ($length <= 255) {
46 return 'TINYBLOB';
47 } elseif ($length <= 65532) {
48 return 'BLOB';
49 } elseif ($length <= 16777215) {
50 return 'MEDIUMBLOB';
51 }
52 }
53
54 return 'LONGBLOB';
55 case 'integer':
56 if (!empty($field['length'])) {
57 $length = $field['length'];
58 if ($length <= 1) {
59 return 'TINYINT';
60 } elseif ($length == 2) {
61 return 'SMALLINT';
62 } elseif ($length == 3) {
63 return 'MEDIUMINT';
64 } elseif ($length == 4) {
65 return 'INT';
66 } elseif ($length > 4) {
67 return 'BIGINT';
68 }
69 }
70
71 return 'INT';
72 case 'boolean':
73 return 'TINYINT(1)';
74 case 'date':
75 return 'DATE';
76 case 'time':
77 return 'TIME';
78 case 'timestamp':
79 return 'DATETIME';
80 case 'float':
81 return 'DOUBLE';
82 case 'decimal':
83 $length = !empty($field['length']) ? $field['length'] : 18;
84 $scale = !empty($field['scale']) ? $field['scale'] : $db->options['decimal_places'];
85
86 return 'DECIMAL(' . $length . ',' . $scale . ')';
87 }
88
89 return '';
90 }
91
92
99 protected function getIntegerDeclaration($name, $field)
100 {
101 $db = $this->getDBInstance();
102
103 $default = $autoinc = '';
104 if (!empty($field['autoincrement'])) {
105 $autoinc = ' AUTO_INCREMENT PRIMARY KEY';
106 } elseif (array_key_exists('default', $field)) {
107 if ($field['default'] === '') {
108 $field['default'] = empty($field['notnull']) ? null : 0;
109 }
110 $default = ' DEFAULT ' . $this->quote($field['default'], 'integer');
111 } elseif (empty($field['notnull'])) {
112 $default = ' DEFAULT NULL';
113 }
114
115 $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
116 $unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
117 $name = $db->quoteIdentifier($name, true);
118
119 return $name . ' ' . $this->getTypeDeclaration($field) . $unsigned . $default . $notnull . $autoinc;
120 }
121
122
128 protected function mapNativeDatatypeInternal($field)
129 {
130 $db_type = strtolower($field['type']);
131 $db_type = strtok($db_type, '(), ');
132 if ($db_type == 'national') {
133 $db_type = strtok('(), ');
134 }
135 if (!empty($field['length'])) {
136 $length = strtok($field['length'], ', ');
137 $decimal = strtok(', ');
138 } else {
139 $length = strtok('(), ');
140 $decimal = strtok('(), ');
141 }
142 $type = array();
143 $unsigned = $fixed = null;
144 switch ($db_type) {
145 case 'tinyint':
146 $type[] = 'integer';
147 $type[] = 'boolean';
148 if (preg_match('/^(is|has)/', $field['name'])) {
149 $type = array_reverse($type);
150 }
151 $unsigned = preg_match('/ unsigned/i', $field['type']);
152 $length = 1;
153 break;
154 case 'smallint':
155 $type[] = 'integer';
156 $unsigned = preg_match('/ unsigned/i', $field['type']);
157 $length = 2;
158 break;
159 case 'mediumint':
160 $type[] = 'integer';
161 $unsigned = preg_match('/ unsigned/i', $field['type']);
162 $length = 3;
163 break;
164 case 'int':
165 case 'integer':
166 $type[] = 'integer';
167 $unsigned = preg_match('/ unsigned/i', $field['type']);
168 $length = 4;
169 break;
170 case 'bigint':
171 $type[] = 'integer';
172 $unsigned = preg_match('/ unsigned/i', $field['type']);
173 $length = 8;
174 break;
175 case 'tinytext':
176 case 'mediumtext':
177 case 'longtext':
178 case 'text':
179 case 'text':
180 case 'varchar':
181 $fixed = false;
182 // no break
183 case 'string':
184 case 'char':
185 $type[] = 'text';
186 if ($length == '1') {
187 $type[] = 'boolean';
188 if (preg_match('/^(is|has)/', $field['name'])) {
189 $type = array_reverse($type);
190 }
191 } elseif (strstr($db_type, 'text')) {
192 $type[] = 'clob';
193 if ($decimal == 'binary') {
194 $type[] = 'blob';
195 }
196 }
197 if ($fixed !== false) {
198 $fixed = true;
199 }
200 break;
201 case 'enum':
202 $type[] = 'text';
203 preg_match_all('/\'.+\'/U', $field['type'], $matches);
204 $length = 0;
205 $fixed = false;
206 if (is_array($matches)) {
207 foreach ($matches[0] as $value) {
208 $length = max($length, strlen($value) - 2);
209 }
210 if ($length == '1' && count($matches[0]) == 2) {
211 $type[] = 'boolean';
212 if (preg_match('/^(is|has)/', $field['name'])) {
213 $type = array_reverse($type);
214 }
215 }
216 }
217 $type[] = 'integer';
218 // no break
219 case 'set':
220 $fixed = false;
221 $type[] = 'text';
222 $type[] = 'integer';
223 break;
224 case 'date':
225 $type[] = 'date';
226 $length = null;
227 break;
228 case 'datetime':
229 case 'timestamp':
230 $type[] = 'timestamp';
231 $length = null;
232 break;
233 case 'time':
234 $type[] = 'time';
235 $length = null;
236 break;
237 case 'float':
238 case 'double':
239 case 'real':
240 $type[] = 'float';
241 $unsigned = preg_match('/ unsigned/i', $field['type']);
242 break;
243 case 'unknown':
244 case 'decimal':
245 case 'numeric':
246 $type[] = 'decimal';
247 $unsigned = preg_match('/ unsigned/i', $field['type']);
248 if ($decimal !== false) {
249 $length = $length . ',' . $decimal;
250 }
251 break;
252 case 'tinyblob':
253 case 'mediumblob':
254 case 'longblob':
255 case 'blob':
256 $type[] = 'blob';
257 $length = null;
258 break;
259 case 'binary':
260 case 'varbinary':
261 $type[] = 'blob';
262 break;
263 case 'year':
264 $type[] = 'integer';
265 $type[] = 'date';
266 $length = null;
267 break;
268 default:
269 throw new ilDatabaseException('unknown database attribute type: ' . $db_type);
270 }
271
272 if ((int) $length <= 0) {
273 $length = null;
274 }
275
276 return array( $type, $length, $unsigned, $fixed );
277 }
278}
An exception for terminatinating execution or to throw for unit testing.
Class ilDBPdoFieldDefinition.
quote($value, $type=null, $quote=true, $escape_wildcards=false)
Class ilDBPdoMySQLFieldDefinition.
Class ilDatabaseException.
if($format !==null) $name
Definition: metadata.php:146
$type