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