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