2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Display form for changing/adding table fields/columns.
5 * Included by tbl_addfield.php and tbl_create.php
9 declare(strict_types
=1);
11 use PhpMyAdmin\Charsets
;
12 use PhpMyAdmin\Charsets\Charset
;
13 use PhpMyAdmin\Charsets\Collation
;
14 use PhpMyAdmin\DatabaseInterface
;
15 use PhpMyAdmin\Partition
;
16 use PhpMyAdmin\Relation
;
17 use PhpMyAdmin\Response
;
19 use PhpMyAdmin\TablePartitionDefinition
;
20 use PhpMyAdmin\Template
;
21 use PhpMyAdmin\Transformations
;
24 if (! defined('PHPMYADMIN')) {
31 Util
::checkParameters(
43 /** @var Relation $relation */
44 $relation = $containerBuilder->get('relation');
45 $transformations = new Transformations();
46 $template = new Template();
49 * Initialize to avoid code execution path warnings
52 if (! isset($num_fields)) {
55 if (! isset($mime_map)) {
58 if (! isset($columnMeta)) {
62 $length_values_input_size = 8;
66 /** @var string $db */
71 if ($action == 'tbl_create.php') {
72 $form_params['reload'] = 1;
74 if ($action == 'tbl_addfield.php') {
75 $form_params = array_merge(
78 'field_where' => Util
::getValueByKey($_POST, 'field_where'),
81 if (isset($_POST['field_where'])) {
82 $form_params['after_field'] = $_POST['after_field'];
85 $form_params['table'] = $table;
88 $form_params['orig_num_fields'] = $num_fields;
90 $form_params = array_merge(
93 'orig_field_where' => Util
::getValueByKey($_POST, 'field_where'),
94 'orig_after_field' => Util
::getValueByKey($_POST, 'after_field'),
98 if (isset($selected) && is_array($selected)) {
99 foreach ($selected as $o_fld_nr => $o_fld_val) {
100 $form_params['selected[' . $o_fld_nr . ']'] = $o_fld_val;
104 $is_backup = ($action != 'tbl_create.php' && $action != 'tbl_addfield.php');
106 $cfgRelation = $relation->getRelationsParam();
108 $comments_map = $relation->getComments($db, $table);
111 if (isset($fields_meta)) {
112 /** @var DatabaseInterface $dbi */
113 $dbi = $containerBuilder->get('dbi');
114 $move_columns = $dbi->getTable($db, $table)->getColumnsMeta();
117 $available_mime = [];
118 if ($cfgRelation['mimework'] && $GLOBALS['cfg']['BrowseMIME']) {
119 $mime_map = $transformations->getMime($db, $table);
120 $available_mime = $transformations->getAvailableMimeTypes();
123 // this will be used on templates/columns_definitions/transformation.twig
125 'input_transformation',
128 foreach ($mime_types as $mime_type) {
129 if (isset($available_mime[$mime_type]) and is_iterable($available_mime[$mime_type])) {
130 foreach ($available_mime[$mime_type] as $mimekey => $transform) {
131 $available_mime[$mime_type . '_file_quoted'][$mimekey] = preg_quote(
132 $available_mime[$mime_type . '_file'][$mimekey],
139 // workaround for field_fulltext, because its submitted indices contain
140 // the index as a value, not a key. Inserted here for easier maintenance
141 // and less code to change in existing files.
142 if (isset($field_fulltext) && is_array($field_fulltext)) {
143 foreach ($field_fulltext as $fulltext_nr => $fulltext_indexkey) {
144 $submit_fulltext[$fulltext_indexkey] = $fulltext_indexkey;
147 if (isset($_POST['submit_num_fields'])
148 ||
isset($_POST['submit_partition_change'])
150 //if adding new fields, set regenerate to keep the original values
154 $foreigners = $relation->getForeigners($db, $table, '', 'foreign');
155 $child_references = null;
156 // From MySQL 5.6.6 onwards columns with foreign keys can be renamed.
157 // Hence, no need to get child references
158 if ($GLOBALS['dbi']->getVersion() < 50606) {
159 $child_references = $relation->getChildReferences($db, $table);
162 for ($columnNumber = 0; $columnNumber < $num_fields; $columnNumber++
) {
166 $submit_attribute = null;
167 $extracted_columnspec = [];
169 if (! empty($regenerate)) {
170 $columnMeta = array_merge(
173 'Field' => Util
::getValueByKey(
175 "field_name.${columnNumber}",
178 'Type' => Util
::getValueByKey(
180 "field_type.${columnNumber}",
183 'Collation' => Util
::getValueByKey(
185 "field_collation.${columnNumber}",
188 'Null' => Util
::getValueByKey(
190 "field_null.${columnNumber}",
193 'DefaultType' => Util
::getValueByKey(
195 "field_default_type.${columnNumber}",
198 'DefaultValue' => Util
::getValueByKey(
200 "field_default_value.${columnNumber}",
203 'Extra' => Util
::getValueByKey(
205 "field_extra.${columnNumber}",
208 'Virtuality' => Util
::getValueByKey(
210 "field_virtuality.${columnNumber}",
213 'Expression' => Util
::getValueByKey(
215 "field_expression.${columnNumber}",
221 $columnMeta['Key'] = '';
224 Util
::getValueByKey($_POST, "field_key.${columnNumber}", ''),
227 if (count($parts) === 2 && $parts[1] == $columnNumber) {
228 $columnMeta['Key'] = Util
::getValueByKey(
233 'fulltext' => 'FULLTEXT',
234 'spatial' => 'SPATIAL',
241 $columnMeta['Comment']
242 = isset($submit_fulltext[$columnNumber])
243 && ($submit_fulltext[$columnNumber] == $columnNumber)
244 ?
'FULLTEXT' : false;
246 switch ($columnMeta['DefaultType']) {
248 $columnMeta['Default'] = null;
251 $columnMeta['Default'] = $columnMeta['DefaultValue'];
254 case 'CURRENT_TIMESTAMP':
255 case 'current_timestamp()':
256 $columnMeta['Default'] = $columnMeta['DefaultType'];
260 $length = Util
::getValueByKey($_POST, "field_length.${columnNumber}", $length);
261 $submit_attribute = Util
::getValueByKey(
263 "field_attribute.${columnNumber}",
266 $comments_map[$columnMeta['Field']] = Util
::getValueByKey(
268 "field_comments.${columnNumber}"
271 $mime_map[$columnMeta['Field']] = array_merge(
272 $mime_map[$columnMeta['Field']],
274 'mimetype' => Util
::getValueByKey($_POST, "field_mimetype.${$columnNumber}"),
275 'transformation' => Util
::getValueByKey(
277 "field_transformation.${$columnNumber}"
279 'transformation_options' => Util
::getValueByKey(
281 "field_transformation_options.${$columnNumber}"
285 } elseif (isset($fields_meta[$columnNumber])) {
286 $columnMeta = $fields_meta[$columnNumber];
293 if (in_array($columnMeta['Extra'], $virtual)) {
294 $tableObj = new Table($GLOBALS['table'], $GLOBALS['db']);
295 $expressions = $tableObj->getColumnGenerationExpression(
298 $columnMeta['Expression'] = $expressions[$columnMeta['Field']];
300 switch ($columnMeta['Default']) {
302 if ($columnMeta['Default'] === null) {
303 if ($columnMeta['Null'] == 'YES') {
304 $columnMeta['DefaultType'] = 'NULL';
305 $columnMeta['DefaultValue'] = '';
307 $columnMeta['DefaultType'] = 'NONE';
308 $columnMeta['DefaultValue'] = '';
311 $columnMeta['DefaultType'] = 'USER_DEFINED';
312 $columnMeta['DefaultValue'] = $columnMeta['Default'];
315 case 'CURRENT_TIMESTAMP':
316 case 'current_timestamp()':
317 $columnMeta['DefaultType'] = 'CURRENT_TIMESTAMP';
318 $columnMeta['DefaultValue'] = '';
321 $columnMeta['DefaultType'] = 'USER_DEFINED';
322 $columnMeta['DefaultValue'] = $columnMeta['Default'];
327 if (isset($columnMeta['Type'])) {
328 $extracted_columnspec = Util
::extractColumnSpec(
331 if ($extracted_columnspec['type'] == 'bit') {
332 $columnMeta['Default']
333 = Util
::convertBitDefaultValue($columnMeta['Default']);
335 $type = $extracted_columnspec['type'];
337 $length = $extracted_columnspec['spec_in_brackets'];
341 $columnMeta['Type'] = '';
344 // Variable tell if current column is bound in a foreign key constraint or not.
345 // MySQL version from 5.6.6 allow renaming columns with foreign keys
346 if (isset($columnMeta['Field'])
347 && isset($form_params['table'])
348 && $GLOBALS['dbi']->getVersion() < 50606
350 $columnMeta['column_status'] = $relation->checkChildForeignReferences(
352 $form_params['table'],
353 $columnMeta['Field'],
359 // some types, for example longtext, are reported as
360 // "longtext character set latin7" when their charset and / or collation
361 // differs from the ones of the corresponding database.
362 // rtrim the type, for cases like "float unsigned"
364 preg_replace('/[\s]character set[\s][\S]+/', '', $type)
368 * old column attributes
372 if (isset($columnMeta['Field'])) {
373 $form_params['field_orig[' . $columnNumber . ']']
374 = $columnMeta['Field'];
375 if (isset($columnMeta['column_status'])
376 && ! $columnMeta['column_status']['isEditable']
378 $form_params['field_name[' . $columnNumber . ']']
379 = $columnMeta['Field'];
382 $form_params['field_orig[' . $columnNumber . ']'] = '';
386 if (isset($columnMeta['Type'])) {
387 // keep in uppercase because the new type will be in uppercase
388 $form_params['field_type_orig[' . $columnNumber . ']'] = mb_strtoupper($type);
389 if (isset($columnMeta['column_status'])
390 && ! $columnMeta['column_status']['isEditable']
392 $form_params['field_type[' . $columnNumber . ']'] = mb_strtoupper($type);
395 $form_params['field_type_orig[' . $columnNumber . ']'] = '';
399 $form_params['field_length_orig[' . $columnNumber . ']'] = $length;
401 // old column default
402 $form_params = array_merge(
405 "field_default_value_orig[${columnNumber}]" => Util
::getValueByKey(
410 "field_default_type_orig[${columnNumber}]" => Util
::getValueByKey(
415 "field_collation_orig[${columnNumber}]" => Util
::getValueByKey(
420 "field_attribute_orig[${columnNumber}]" => trim(
421 Util
::getValueByKey($extracted_columnspec, 'attribute', '')
423 "field_null_orig[${columnNumber}]" => Util
::getValueByKey(
428 "field_extra_orig[${columnNumber}]" => Util
::getValueByKey(
433 "field_comments_orig[${columnNumber}]" => Util
::getValueByKey(
438 "field_virtuality_orig[${columnNumber}]" => Util
::getValueByKey(
443 "field_expression_orig[${columnNumber}]" => Util
::getValueByKey(
453 $type_upper = mb_strtoupper($type);
455 // For a TIMESTAMP, do not show the string "CURRENT_TIMESTAMP" as a default value
456 if (isset($columnMeta['DefaultValue'])) {
457 $default_value = $columnMeta['DefaultValue'];
459 if ($type_upper == 'BIN)') {
460 $default_value = Util
::convertBitDefaultValue($columnMeta['DefaultValue']);
461 } elseif ($type_upper == 'BINARY' ||
$type_upper == 'VARBINARY') {
462 $default_value = bin2hex($columnMeta['DefaultValue']);
465 $content_cells[$columnNumber] = [
466 'column_number' => $columnNumber,
467 'column_meta' => $columnMeta,
468 'type_upper' => $type_upper,
469 'default_value' => $default_value,
470 'length_values_input_size' => $length_values_input_size,
472 'extracted_columnspec' => $extracted_columnspec,
473 'submit_attribute' => $submit_attribute,
474 'comments_map' => $comments_map,
475 'fields_meta' => isset($fields_meta) ?
$fields_meta : null,
476 'is_backup' => $is_backup,
477 'move_columns' => $move_columns,
478 'cfg_relation' => $cfgRelation,
479 'available_mime' => $available_mime,
480 'mime_map' => isset($mime_map) ?
$mime_map : [],
484 $partitionDetails = TablePartitionDefinition
::getDetails();
486 $charsets = Charsets
::getCharsets($GLOBALS['dbi'], $GLOBALS['cfg']['Server']['DisableIS']);
487 $collations = Charsets
::getCollations($GLOBALS['dbi'], $GLOBALS['cfg']['Server']['DisableIS']);
489 /** @var Charset $charset */
490 foreach ($charsets as $charset) {
491 $collationsList = [];
492 /** @var Collation $collation */
493 foreach ($collations[$charset->getName()] as $collation) {
494 $collationsList[] = [
495 'name' => $collation->getName(),
496 'description' => $collation->getDescription(),
500 'name' => $charset->getName(),
501 'description' => $charset->getDescription(),
502 'collations' => $collationsList,
506 $html = $template->render('columns_definitions/column_definitions_form', [
507 'is_backup' => $is_backup,
508 'fields_meta' => isset($fields_meta) ?
$fields_meta : null,
509 'mimework' => $cfgRelation['mimework'],
511 'form_params' => $form_params,
512 'content_cells' => $content_cells,
513 'partition_details' => $partitionDetails,
514 'primary_indexes' => isset($_POST['primary_indexes']) ?
$_POST['primary_indexes'] : null,
515 'unique_indexes' => isset($_POST['unique_indexes']) ?
$_POST['unique_indexes'] : null,
516 'indexes' => isset($_POST['indexes']) ?
$_POST['indexes'] : null,
517 'fulltext_indexes' => isset($_POST['fulltext_indexes']) ?
$_POST['fulltext_indexes'] : null,
518 'spatial_indexes' => isset($_POST['spatial_indexes']) ?
$_POST['spatial_indexes'] : null,
519 'table' => isset($_POST['table']) ?
$_POST['table'] : null,
520 'comment' => isset($_POST['comment']) ?
$_POST['comment'] : null,
521 'tbl_collation' => isset($_POST['tbl_collation']) ?
$_POST['tbl_collation'] : null,
522 'charsets' => $charsetsList,
523 'tbl_storage_engine' => isset($_POST['tbl_storage_engine']) ?
$_POST['tbl_storage_engine'] : null,
524 'connection' => isset($_POST['connection']) ?
$_POST['connection'] : null,
525 'change_column' => isset($_POST['change_column']) ?
$_POST['change_column'] : null,
526 'is_virtual_columns_supported' => Util
::isVirtualColumnsSupported(),
527 'browse_mime' => isset($GLOBALS['cfg']['BrowseMIME']) ?
$GLOBALS['cfg']['BrowseMIME'] : null,
528 'server_type' => Util
::getServerType(),
529 'max_rows' => intval($GLOBALS['cfg']['MaxRows']),
530 'char_editing' => isset($GLOBALS['cfg']['CharEditing']) ?
$GLOBALS['cfg']['CharEditing'] : null,
531 'attribute_types' => $GLOBALS['dbi']->types
->getAttributes(),
532 'privs_available' => isset($GLOBALS['col_priv']) ?
$GLOBALS['col_priv'] : false
533 && isset($GLOBALS['is_reload_priv']) ?
$GLOBALS['is_reload_priv'] : false,
534 'max_length' => $GLOBALS['dbi']->getVersion() >= 50503 ?
1024 : 255,
535 'have_partitioning' => Partition
::havePartitioning(),
536 'dbi' => $GLOBALS['dbi'],
537 'disable_is' => $GLOBALS['cfg']['Server']['DisableIS'],
542 $response = Response
::getInstance();
543 $response->getHeader()->getScripts()->addFiles(
545 'vendor/jquery/jquery.uitablefilter.js',
549 $response->addHTML($html);