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