diff options
author | William Desportes <williamdes@wdes.fr> | 2022-11-03 22:35:10 +0300 |
---|---|---|
committer | William Desportes <williamdes@wdes.fr> | 2022-11-03 22:35:10 +0300 |
commit | 110b609e923f59c97d174684c10d71bd0aa27c7d (patch) | |
tree | 400d32e8eb166317eca19eb2ec7de783573916f8 /libraries | |
parent | efbd8d835aaf65b89ba727525273aae3102b85d6 (diff) | |
parent | 296e6b62cef88d925d646eabb03ea7cb12a6c14b (diff) |
Merge branch 'QA_5_2'
Signed-off-by: William Desportes <williamdes@wdes.fr>
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/classes/Table/ColumnsDefinition.php | 104 |
1 files changed, 61 insertions, 43 deletions
diff --git a/libraries/classes/Table/ColumnsDefinition.php b/libraries/classes/Table/ColumnsDefinition.php index 371fdb2f8d..eaee803aa5 100644 --- a/libraries/classes/Table/ColumnsDefinition.php +++ b/libraries/classes/Table/ColumnsDefinition.php @@ -82,7 +82,7 @@ final class ColumnsDefinition if ($action === '/table/add-field') { $form_params = array_merge($form_params, ['field_where' => $_POST['field_where'] ?? null]); if (isset($_POST['field_where'])) { - $form_params['after_field'] = $_POST['after_field']; + $form_params['after_field'] = (string) $_POST['after_field']; } } @@ -243,6 +243,8 @@ final class ColumnsDefinition case 'NULL': case 'CURRENT_TIMESTAMP': case 'current_timestamp()': + case 'UUID': + case 'uuid()': $columnMeta['Default'] = $columnMeta['DefaultType']; break; } @@ -279,38 +281,8 @@ final class ColumnsDefinition $columnMeta['Expression'] = is_array($expressions) ? $expressions[$columnMeta['Field']] : null; } - switch ($columnMeta['Default']) { - case null: - if ($columnMeta['Default'] === null) { - if ($columnMeta['Null'] === 'YES') { - $columnMeta['DefaultType'] = 'NULL'; - $columnMeta['DefaultValue'] = ''; - } else { - $columnMeta['DefaultType'] = 'NONE'; - $columnMeta['DefaultValue'] = ''; - } - } else { // empty - $columnMeta['DefaultType'] = 'USER_DEFINED'; - $columnMeta['DefaultValue'] = $columnMeta['Default']; - } - - break; - case 'CURRENT_TIMESTAMP': - case 'current_timestamp()': - $columnMeta['DefaultType'] = 'CURRENT_TIMESTAMP'; - $columnMeta['DefaultValue'] = ''; - break; - default: - $columnMeta['DefaultType'] = 'USER_DEFINED'; - $columnMeta['DefaultValue'] = $columnMeta['Default']; - - if (substr($columnMeta['Type'], -4) === 'text') { - $textDefault = substr($columnMeta['Default'], 1, -1); - $columnMeta['Default'] = stripcslashes($textDefault); - } - - break; - } + $columnMetaDefault = self::decorateColumnMetaDefault($columnMeta); + $columnMeta = array_merge($columnMeta, $columnMetaDefault); } if (isset($columnMeta['Type'])) { @@ -363,14 +335,10 @@ final class ColumnsDefinition } // old column type - if (isset($columnMeta['Type'])) { - // keep in uppercase because the new type will be in uppercase - $form_params['field_type_orig[' . $columnNumber . ']'] = mb_strtoupper($type); - if (isset($columnMeta['column_status']) && ! $columnMeta['column_status']['isEditable']) { - $form_params['field_type[' . $columnNumber . ']'] = mb_strtoupper($type); - } - } else { - $form_params['field_type_orig[' . $columnNumber . ']'] = ''; + // keep in uppercase because the new type will be in uppercase + $form_params['field_type_orig[' . $columnNumber . ']'] = mb_strtoupper($type); + if (isset($columnMeta['column_status']) && ! $columnMeta['column_status']['isEditable']) { + $form_params['field_type[' . $columnNumber . ']'] = mb_strtoupper($type); } // old column length @@ -402,9 +370,11 @@ final class ColumnsDefinition } if ($type_upper === 'BIT') { - $default_value = Util::convertBitDefaultValue($columnMeta['DefaultValue']); + $default_value = ! empty($columnMeta['DefaultValue']) + ? Util::convertBitDefaultValue($columnMeta['DefaultValue']) + : ''; } elseif ($type_upper === 'BINARY' || $type_upper === 'VARBINARY') { - $default_value = bin2hex($columnMeta['DefaultValue']); + $default_value = bin2hex((string) $columnMeta['DefaultValue']); } $content_cells[$columnNumber] = [ @@ -486,4 +456,52 @@ final class ColumnsDefinition 'disable_is' => $GLOBALS['cfg']['Server']['DisableIS'], ]; } + + /** + * Set default type and default value according to the column metadata + * + * @param array $columnMeta Column Metadata + * @phpstan-param array<string, string|null> $columnMeta + * + * @return non-empty-array<array-key, mixed> + */ + public static function decorateColumnMetaDefault(array $columnMeta): array + { + $metaDefault = [ + 'DefaultType' => 'USER_DEFINED', + 'DefaultValue' => '', + ]; + + switch ($columnMeta['Default']) { + case null: + if ($columnMeta['Null'] === 'YES') { + $metaDefault['DefaultType'] = 'NULL'; + } else { + $metaDefault['DefaultType'] = 'NONE'; + } + + break; + case 'CURRENT_TIMESTAMP': + case 'current_timestamp()': + $metaDefault['DefaultType'] = 'CURRENT_TIMESTAMP'; + + break; + case 'UUID': + case 'uuid()': + $metaDefault['DefaultType'] = 'UUID'; + + break; + default: + $metaDefault['DefaultValue'] = $columnMeta['Default']; + + if (substr((string) $columnMeta['Type'], -4) === 'text') { + $textDefault = substr($columnMeta['Default'], 1, -1); + $metaDefault['Default'] = stripcslashes($textDefault); + } + + break; + } + + return $metaDefault; + } } |