From f128c2d1ead9d4203d7a06cc10862cf426f6ff29 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Thu, 1 Feb 2018 23:09:40 +0100 Subject: [PATCH] Use primary key to edit rows even if not selected --- adminer/select.inc.php | 51 ++++++++++++++++++++++++------------------ changes.txt | 1 + 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 6539bc00..8be701f8 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -5,9 +5,6 @@ $indexes = indexes($TABLE); $fields = fields($TABLE); $foreign_keys = column_foreign_keys($TABLE); $oid = $table_status["Oid"]; -if ($oid) { - $indexes[] = array("type" => "PRIMARY", "columns" => array($oid)); -} parse_str($_COOKIE["adminer_import"], $adminer_import); $rights = array(); // privilege => 0 @@ -44,6 +41,24 @@ if ($_GET["val"] && is_ajax()) { exit; } +$primary = $unselected = null; +foreach ($indexes as $index) { + if ($index["type"] == "PRIMARY") { + $primary = array_flip($index["columns"]); + $unselected = ($select ? $primary : array()); + foreach ($unselected as $key => $val) { + if (in_array(idf_escape($key), $select)) { + unset($unselected[$key]); + } + } + break; + } +} +if ($oid && $unselected === null) { + $primary = $unselected = array($oid => 0); + $indexes[] = array("type" => "PRIMARY", "columns" => array($oid)); +} + if ($_POST && !$error) { $where_check = $where; if (!$_POST["all"] && is_array($_POST["check"])) { @@ -54,20 +69,6 @@ if ($_POST && !$error) { $where_check[] = "((" . implode(") OR (", $checks) . "))"; } $where_check = ($where_check ? "\nWHERE " . implode(" AND ", $where_check) : ""); - $primary = $unselected = null; - foreach ($indexes as $index) { - if ($index["type"] == "PRIMARY") { - $primary = array_flip($index["columns"]); - $unselected = ($select ? $primary : array()); - break; - } - } - foreach ((array) $unselected as $key => $val) { - if (in_array(idf_escape($key), $select)) { - unset($unselected[$key]); - } - } - if ($_POST["export"]) { cookie("adminer_import", "output=" . urlencode($_POST["output"]) . "&format=" . urlencode($_POST["format"])); dump_headers($TABLE); @@ -258,15 +259,13 @@ if (!$columns && support("table")) { } $select2 = $select; + $group2 = $group; if (!$select2) { $select2[] = "*"; $convert_fields = convert_fields($columns, $fields, $select); if ($convert_fields) { $select2[] = substr($convert_fields, 2); } - if ($oid) { - $select2[] = $oid; - } } foreach ($select as $key => $val) { $field = $fields[idf_unescape($val)]; @@ -274,7 +273,15 @@ if (!$columns && support("table")) { $select2[$key] = "$as AS $val"; } } - $result = $driver->select($TABLE, $select2, $where, $group, $order, $limit, $page, true); + if (!$is_group && $unselected) { + foreach ($unselected as $key => $val) { + $select2[] = idf_escape($key); + if ($group2) { + $group2[] = idf_escape($key); + } + } + } + $result = $driver->select($TABLE, $select2, $where, $group2, $order, $limit, $page, true); if (!$result) { echo "

" . error() . "\n"; @@ -313,7 +320,7 @@ if (!$columns && support("table")) { reset($select); $rank = 1; foreach ($rows[0] as $key => $val) { - if ($key != $oid) { + if (!isset($unselected[$key])) { $val = $_GET["columns"][key($select)]; $field = $fields[$select ? ($val ? $val["col"] : current($select)) : $key]; $name = ($field ? $adminer->fieldName($field, $rank) : ($val["fun"] ? "*" : $key)); diff --git a/changes.txt b/changes.txt index 3e2c483a..6fa52ff9 100644 --- a/changes.txt +++ b/changes.txt @@ -5,6 +5,7 @@ Fully support functions in default values Stop redirecting links via adminer.org Support X-Forwarded-Prefix Display options for timestamp columns when creating a new table +Use primary key to edit rows even if not selected MySQL, PostgreSQL: Display warnings MySQL: Add floor and ceil select functions MySQL: Add FIND_IN_SET search operator