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