Support multiline generated values in alter table

This commit is contained in:
Jakub Vrana
2026-01-27 10:30:06 +00:00
parent cca4d26784
commit 76a8dbfb98
2 changed files with 10 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
## Adminer dev
- Pretty print JSON in edit
- Support multiline generated values in alter table
- PostgreSQL: Offer foreign keys in create table
- PostgreSQL: Add missing parentheses to CHECK export
- PostgreSQL: Allow creating NOT DEFERRABLE foreign keys

View File

@@ -250,15 +250,18 @@ function process_field(array $field, array $type_field): array {
* @param Field $field
*/
function default_value(array $field): string {
$default = $field["default"];
if ($field["default"] === null) {
return "";
}
$default = str_replace("\r", "", $field["default"]);
$generated = $field["generated"];
return ($default === null ? "" : (in_array($generated, driver()->generated)
return (in_array($generated, driver()->generated)
? (JUSH == "mssql" ? " AS ($default)" . ($generated == "VIRTUAL" ? "" : " $generated") . "" : " GENERATED ALWAYS AS ($default) $generated")
: " DEFAULT " . (!preg_match('~^GENERATED ~i', $default) && (preg_match('~char|binary|text|json|enum|set~', $field["type"]) || preg_match('~^(?![a-z])~i', $default))
? (JUSH == "sql" && preg_match('~text|json~', $field["type"]) ? "(" . q($default) . ")" : q($default)) // MySQL requires () around default value of text column
: str_ireplace("current_timestamp()", "CURRENT_TIMESTAMP", (JUSH == "sqlite" ? "($default)" : $default))
)
));
);
}
/** Get type class to use in CSS
@@ -329,7 +332,9 @@ function edit_fields(array $fields, array $collations, $type = "TABLE", array $f
? html_select("fields[$i][generated]", array_merge(array("", "DEFAULT"), driver()->generated), $field["generated"]) . " "
: checkbox("fields[$i][generated]", 1, $field["generated"], "", "", "", "label-default")
);
echo "<input name='fields[$i][default]' value='" . h($field["default"]) . "' aria-labelledby='label-default'>";
$attrs = " name='fields[$i][default]' aria-labelledby='label-default'";
$value = h($field["default"]);
echo (preg_match('~\n~', $field["default"]) ? "<textarea$attrs rows='2' cols='30' style='vertical-align: bottom;'>\n$value</textarea>" : "<input$attrs value='$value'>"); // \n to preserve the leading newline
echo (support("comment") ? "<td$comment_class><input name='fields[$i][comment]' value='" . h($field["comment"]) . "' data-maxlength='" . (min_version(5.5) ? 1024 : 255) . "' aria-labelledby='label-comment'>" : "");
}
echo "<td>";