Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual columns #325

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion adminer/create.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
if ($field["field"] != "") {
if (!$field["has_default"]) {
$field["default"] = null;
} elseif (in_array($field["has_default"], array('STORED', 'VIRTUAL'))) {
$field["default"] = "GENERATED ALWAYS AS ($field[default]) $field[has_default]";
$field["collation"] = null;
} elseif (in_array($orig_field["has_default"], array('STORED', 'VIRTUAL'))) {
$orig_field["default"] = "GENERATED ALWAYS AS ($orig_field[default]) $orig_field[virtual]";
$orig_field["collation"] = null;
}
if ($key == $row["auto_increment_col"]) {
$field["auto_increment"] = true;
Expand Down Expand Up @@ -132,7 +138,7 @@
$row["Auto_increment"] = "";
}
foreach ($orig_fields as $field) {
$field["has_default"] = isset($field["default"]);
$field["has_default"] = isset($field["has_default"]) ? $field["has_default"] : $field["default"];
$row["fields"][] = $field;
}

Expand Down
33 changes: 20 additions & 13 deletions adminer/drivers/mysql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,23 +535,30 @@ function fk_support($table_status) {
*/
function fields($table) {
$return = array();
foreach (get_rows("SHOW FULL COLUMNS FROM " . table($table)) as $row) {
preg_match('~^([^( ]+)(?:\((.+)\))?( unsigned)?( zerofill)?$~', $row["Type"], $match);
$return[$row["Field"]] = array(
"field" => $row["Field"],
"full_type" => $row["Type"],
$rows = get_rows("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . q($table));

foreach ($rows as $row) {
preg_match('~^([^( ]+)(?:\((.+)\))?( unsigned)?( zerofill)?$~', $row["COLUMN_TYPE"], $match);
$return[$row["COLUMN_NAME"]] = array(
"field" => $row["COLUMN_NAME"],
"full_type" => $row["COLUMN_TYPE"],
"type" => $match[1],
"length" => $match[2],
"unsigned" => ltrim($match[3] . $match[4]),
"default" => ($row["Default"] != "" || preg_match("~char|set~", $match[1]) ? $row["Default"] : null),
"null" => ($row["Null"] == "YES"),
"auto_increment" => ($row["Extra"] == "auto_increment"),
"on_update" => (preg_match('~^on update (.+)~i', $row["Extra"], $match) ? $match[1] : ""), //! available since MySQL 5.1.23
"collation" => $row["Collation"],
"privileges" => array_flip(preg_split('~, *~', $row["Privileges"])),
"comment" => $row["Comment"],
"primary" => ($row["Key"] == "PRI"),
"default" => ($row["COLUMN_DEFAULT"] != "" || preg_match("~char|set~", $match[1])) ? trim($row["COLUMN_DEFAULT"], "'") : null, // COLUMN_DEFAULT => "'my_value'"
"null" => ($row["IS_NULLABLE"] == "YES"),
"auto_increment" => ($row["EXTRA"] == "auto_increment"),
"on_update" => (preg_match('~^on update (.+)~i', $row["EXTRA"], $match) ? $match[1] : ""), //! available since MySQL 5.1.23
"collation" => $row["COLLATION_NAME"],
"privileges" => array_flip(preg_split('~, *~', $row["PRIVILEGES"])),
"comment" => $row["COLUMN_COMMENT"],
"primary" => ($row["COLUMN_KEY"] == "PRI")
);

if ($virtual = $row["EXTRA"] == "STORED GENERATED" ? 'STORED' : ($row["EXTRA"] == "VIRTUAL GENERATED" ? 'VIRTUAL' : false)) {
$return[$row["COLUMN_NAME"]]["has_default"] = $virtual;
$return[$row["COLUMN_NAME"]]["default"] = isset($row['GENERATION_EXPRESSION']) ? $row['GENERATION_EXPRESSION'] : null;
}
}
return $return;
}
Expand Down
3 changes: 3 additions & 0 deletions adminer/edit.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
if (!isset($field["privileges"][$update ? "update" : "insert"]) || $adminer->fieldName($field) == "") {
unset($fields[$name]);
}
if (in_array($field["has_default"], array('STORED', 'VIRTUAL'))) {
unset($fields[$name]);
}
}

if ($_POST && !$error && !isset($_GET["select"])) {
Expand Down
17 changes: 17 additions & 0 deletions adminer/include/adminer.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,12 @@ function dumpData($table, $style, $query) {
echo truncate_sql($table) . ";\n";
}
$fields = fields($table);
foreach ($fields as $name => $field) {
//exclude virtual columns
if (in_array($field["has_default"], array('STORED', 'VIRTUAL'))) {
$fields[$name] = false;
}
}
}
$result = $connection->query($query, 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers
if ($result) {
Expand All @@ -824,6 +830,12 @@ function dumpData($table, $style, $query) {
$values = array();
foreach ($row as $val) {
$field = $result->fetch_field();
if ($_POST["format"] == "sql") {
//skip virtual columns
if (!$fields[$field->name]) {
continue;
}
}
$keys[] = $field->name;
$key = idf_escape($field->name);
$values[] = "$key = VALUES($key)";
Expand All @@ -842,6 +854,11 @@ function dumpData($table, $style, $query) {
}
foreach ($row as $key => $val) {
$field = $fields[$key];
//skip virtual columns
if (!$field) {
unset($row[$key]);
continue;
}
$row[$key] = ($val !== null
? unconvert_field($field, preg_match(number_type(), $field["type"]) && $val != '' ? $val : q(($val === false ? 0 : $val)))
: "NULL"
Expand Down
24 changes: 21 additions & 3 deletions adminer/include/editing.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ function process_type($field, $collate = "COLLATE") {
* @return array array("field", "type", "NULL", "DEFAULT", "ON UPDATE", "COMMENT", "AUTO_INCREMENT")
*/
function process_field($field, $type_field) {
if (in_array($field["has_default"], array('STORED', 'VIRTUAL'))) {
return array(
idf_escape(trim($field["field"])),
process_type($field),
" $field[default]",
(support("comment") && $field["comment"] != "" ? " COMMENT " . q($field["comment"]) : ""),
);
}

return array(
idf_escape(trim($field["field"])),
process_type($type_field),
Expand Down Expand Up @@ -239,7 +248,7 @@ function type_class($type) {
* @return null
*/
function edit_fields($fields, $collations, $type = "TABLE", $foreign_keys = array(), $comments = false) {
global $inout;
global $inout, $jush;
$fields = array_values($fields);
?>
<thead><tr>
Expand Down Expand Up @@ -277,8 +286,17 @@ function edit_fields($fields, $collations, $type = "TABLE", $foreign_keys = arra
<?php edit_type("fields[$i]", $field, $collations, $foreign_keys); ?>
<?php if ($type == "TABLE") { ?>
<td><?php echo checkbox("fields[$i][null]", 1, $field["null"], "", "", "block", "label-null"); ?>
<td><label class="block"><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked<?php } ?> aria-labelledby="label-ai"></label><td><?php
echo checkbox("fields[$i][has_default]", 1, $field["has_default"], "", "", "", "label-default"); ?><input name="fields[<?php echo $i; ?>][default]" value="<?php echo h($field["default"]); ?>" aria-labelledby="label-default"><?php
<td><label class="block"><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked<?php } ?> aria-labelledby="label-ai"></label>
<td><select name="fields[<?php echo $i ?>][has_default]">
<option selected value="">
<option <?php echo $field["has_default"] ? 'selected' : '' ?> value="1">default
<?php if ($jush == 'sql') { ?>
<optgroup label="Virtual column">
<option <?php echo $field["has_default"] === 'STORED' ? 'selected' : '' ?> value="STORED">stored
<option <?php echo $field["has_default"] === 'VIRTUAL' ? 'selected' : '' ?> value="VIRTUAL">virtual
</optgroup>
<?php } ?>
</select><input name="fields[<?php echo $i; ?>][default]" value="<?php echo h($field["default"]); ?>" aria-labelledby="label-default"><?php
echo (support("comment") ? "<td" . ($comments ? "" : " class='hidden'") . "><input name='fields[$i][comment]' value='" . h($field["comment"]) . "' data-maxlength='" . (min_version(5.5) ? 1024 : 255) . "' aria-labelledby='label-comment'>" : "");
}
echo "<td>";
Expand Down
3 changes: 3 additions & 0 deletions adminer/include/functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,9 @@ function edit_form($TABLE, $fields, $row, $update) {
echo "<table cellspacing='0'>" . script("qsl('table').onkeydown = editingKeydown;");

foreach ($fields as $name => $field) {
if (in_array($field["has_default"], array('STORED', 'VIRTUAL'))) {
continue;
}
echo "<tr><th>" . $adminer->fieldName($field);
$default = $_GET["set"][bracket_escape($name)];
if ($default === null) {
Expand Down