Editable index names

This commit is contained in:
Jakub Vrana 2011-07-13 15:13:00 +02:00
parent ead05e6839
commit ac8a64e88a
7 changed files with 59 additions and 23 deletions

View file

@ -436,16 +436,16 @@ WHERE OBJECT_NAME(i.object_id) = " . q($table)
$index = array();
$drop = array();
foreach ($alter as $val) {
if ($val[2]) {
if ($val[2] == "DROP") {
if ($val[0] == "PRIMARY") { //! sometimes used also for UNIQUE
$drop[] = $val[1];
$drop[] = idf_escape($val[1]);
} else {
$index[] = "$val[1] ON " . table($table);
$index[] = idf_escape($val[1]) . " ON " . table($table);
}
} elseif (!queries(($val[0] != "PRIMARY"
? "CREATE" . ($val[0] != "INDEX" ? " UNIQUE" : "") . " INDEX " . idf_escape(uniqid($table . "_")) . " ON " . table($table)
? "CREATE $val[0] " . ($val[0] != "INDEX" ? "INDEX " : "") . idf_escape($val[1] != "" ? $val[1] : uniqid($table . "_")) . " ON " . table($table)
: "ALTER TABLE " . table($table) . " ADD PRIMARY KEY"
) . " $val[1]")) {
) . " $val[2]")) {
return false;
}
}

View file

@ -611,12 +611,15 @@ if (!defined("DRIVER")) {
/** Run commands to alter indexes
* @param string escaped table name
* @param array of array("index type", "(columns definition)") or array("index type", "escaped name", "DROP")
* @param array of array("index type", "name", "(columns definition)") or array("index type", "name", "DROP")
* @return bool
*/
function alter_indexes($table, $alter) {
foreach ($alter as $key => $val) {
$alter[$key] = ($val[2] ? "\nDROP INDEX " : "\nADD $val[0] " . ($val[0] == "PRIMARY" ? "KEY " : "")) . $val[1];
$alter[$key] = ($val[2] == "DROP"
? "\nDROP INDEX " . idf_escape($val[1])
: "\nADD $val[0] " . ($val[0] == "PRIMARY" ? "KEY " : "") . ($val[1] != "" ? idf_escape($val[1]) . " " : "") . $val[2]
);
}
return queries("ALTER TABLE " . table($table) . implode(",", $alter));
}

View file

@ -383,10 +383,13 @@ WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.constraint_schema = current_sche
$drop = array();
foreach ($alter as $val) {
if ($val[0] != "INDEX") {
$create[] = ($val[2] ? "\nDROP CONSTRAINT " : "\nADD $val[0] " . ($val[0] == "PRIMARY" ? "KEY " : "")) . $val[1];
} elseif ($val[2]) {
$drop[] = $val[1];
} elseif (!queries("CREATE INDEX " . idf_escape(uniqid($table . "_")) . " ON " . table($table) . " $val[1]")) {
$create[] = ($val[2] == "DROP"
? "\nDROP CONSTRAINT " . idf_escape($val[1])
: "\nADD $val[0] " . ($val[0] == "PRIMARY" ? "KEY " : "") . $val[2]
);
} elseif ($val[2] == "DROP") {
$drop[] = idf_escape($val[1]);
} elseif (!queries("CREATE INDEX " . idf_escape($val[1] != "" ? $val[1] : uniqid($table . "_")) . " ON " . table($table) . " $val[2]")) {
return false;
}
}

View file

@ -422,7 +422,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
function alter_indexes($table, $alter) {
foreach ($alter as $val) {
if (!queries(($val[2] ? "DROP INDEX" : "CREATE" . ($val[0] != "INDEX" ? " UNIQUE" : "") . " INDEX " . idf_escape(uniqid($table . "_")) . " ON " . table($table)) . " $val[1]")) {
if (!queries($val[2] == "DROP"
? "DROP INDEX " . idf_escape($val[1])
: "CREATE $val[0] " . ($val[0] != "INDEX" ? "INDEX " : "") . idf_escape($val[1] != "" ? $val[1] : uniqid($table . "_")) . " ON " . table($table) . " $val[2]"
)) {
return false;
}
}

View file

@ -13,6 +13,7 @@ if ($jush == "sqlite") { // doesn't support primary key
if ($_POST && !$error && !$_POST["add"]) {
$alter = array();
foreach ($_POST["indexes"] as $index) {
$name = $index["name"];
if (in_array($index["type"], $index_types)) {
$columns = array();
$lengths = array();
@ -27,22 +28,23 @@ if ($_POST && !$error && !$_POST["add"]) {
}
}
if ($columns) {
foreach ($indexes as $name => $existing) {
$existing = $indexes[$name];
if ($existing) {
ksort($existing["columns"]);
ksort($existing["lengths"]);
if ($index["type"] == $existing["type"] && array_values($existing["columns"]) === $columns && (!$existing["lengths"] || array_values($existing["lengths"]) === $lengths)) {
// skip existing index
unset($indexes[$name]);
continue 2;
continue;
}
}
$alter[] = array($index["type"], "(" . implode(", ", $set) . ")");
$alter[] = array($index["type"], $name, "(" . implode(", ", $set) . ")");
}
}
}
// drop removed indexes
foreach ($indexes as $name => $existing) {
$alter[] = array($existing["type"], idf_escape($name), "DROP");
$alter[] = array($existing["type"], $name, "DROP");
}
if (!$alter) {
redirect(ME . "table=" . urlencode($TABLE));
@ -69,6 +71,7 @@ if ($_POST) {
}
} else {
foreach ($row["indexes"] as $key => $index) {
$row["indexes"][$key]["name"] = $key;
$row["indexes"][$key]["columns"][] = "";
}
$row["indexes"][] = array("columns" => array(1 => ""));
@ -77,7 +80,7 @@ if ($_POST) {
<form action="" method="post">
<table cellspacing="0" class="nowrap">
<thead><tr><th><?php echo lang('Index Type'); ?><th><?php echo lang('Column (length)'); ?></thead>
<thead><tr><th><?php echo lang('Index Type'); ?><th><?php echo lang('Column (length)'); ?><th><?php echo lang('Name'); ?></thead>
<?php
$j = 1;
foreach ($row["indexes"] as $index) {
@ -85,10 +88,11 @@ foreach ($row["indexes"] as $index) {
ksort($index["columns"]);
$i = 1;
foreach ($index["columns"] as $key => $column) {
echo "<span>" . html_select("indexes[$j][columns][$i]", array(-1 => "") + $fields, $column, ($i == count($index["columns"]) ? "indexesAddColumn(this);" : 1));
echo "<span>" . html_select("indexes[$j][columns][$i]", array(-1 => "") + $fields, $column, ($i == count($index["columns"]) ? "indexesAddColumn" : "indexesChangeColumn") . "(this, '" . js_escape($jush == "sql" ? "" : $_GET["indexes"] . "_") . "');");
echo "<input name='indexes[$j][lengths][$i]' size='2' value='" . h($index["lengths"][$key]) . "'> </span>"; //! hide for non-MySQL drivers, add ASC|DESC
$i++;
}
echo "<td><input name='indexes[$j][name]' value='" . h($index["name"]) . "'>\n";
$j++;
}
?>

View file

@ -343,17 +343,38 @@ function indexesAddRow(field) {
selects[i].name = selects[i].name.replace(/indexes\[\d+/, '$&1');
selects[i].selectedIndex = 0;
}
var input = row.getElementsByTagName('input')[0];
input.name = input.name.replace(/indexes\[\d+/, '$&1');
input.value = '';
var inputs = row.getElementsByTagName('input');
for (var i=0; i < inputs.length; i++) {
inputs[i].name = inputs[i].name.replace(/indexes\[\d+/, '$&1');
inputs[i].value = '';
}
parent.parentNode.appendChild(row);
}
/** Change column in index
* @param HTMLSelectElement
* @param string name prefix
*/
function indexesChangeColumn(field, prefix) {
var columns = field.parentNode.parentNode.getElementsByTagName('select');
var names = [];
for (var i=0; i < columns.length; i++) {
var value = selectValue(columns[i]);
if (value) {
names.push(value);
}
}
field.form[field.name.replace(/\].*/, '][name]')].value = prefix + names.join('_');
}
/** Add column for index
* @param HTMLSelectElement
* @param string name prefix
*/
function indexesAddColumn(field) {
field.onchange = function () { };
function indexesAddColumn(field, prefix) {
field.onchange = function () {
indexesChangeColumn(field, prefix);
};
var select = field.form[field.name.replace(/\].*/, '][type]')];
if (!select.selectedIndex) {
select.selectedIndex = 3;
@ -367,6 +388,7 @@ function indexesAddColumn(field) {
input.name = input.name.replace(/\]\[\d+/, '$&1');
input.value = '';
field.parentNode.parentNode.appendChild(column);
field.onchange();
}

View file

@ -1,6 +1,7 @@
Adminer 3.3.0-dev:
Use Esc to disable in-place edit
Shortcut for database privileges
Editable index names
Append new index with auto index selection (bug #3282127)
Preserve original timestamp value in multiple update (bug #3312614)
Bit type default value