1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-15 11:04:02 +02:00

Index length

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@166 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana
2007-07-14 07:06:53 +00:00
parent ad7c79b100
commit e78ec761ae
3 changed files with 43 additions and 23 deletions

View File

@@ -79,6 +79,7 @@ function indexes($table) {
while ($row = $result->fetch_assoc()) { while ($row = $result->fetch_assoc()) {
$return[$row["Key_name"]]["type"] = ($row["Key_name"] == "PRIMARY" ? "PRIMARY" : ($row["Index_type"] == "FULLTEXT" ? "FULLTEXT" : ($row["Non_unique"] ? "INDEX" : "UNIQUE"))); $return[$row["Key_name"]]["type"] = ($row["Key_name"] == "PRIMARY" ? "PRIMARY" : ($row["Index_type"] == "FULLTEXT" ? "FULLTEXT" : ($row["Non_unique"] ? "INDEX" : "UNIQUE")));
$return[$row["Key_name"]]["columns"][$row["Seq_in_index"]] = $row["Column_name"]; $return[$row["Key_name"]]["columns"][$row["Seq_in_index"]] = $row["Column_name"];
$return[$row["Key_name"]]["lengths"][$row["Seq_in_index"]] = $row["Sub_part"];
} }
$result->free(); $result->free();
} }

View File

@@ -1,31 +1,35 @@
<?php <?php
$index_types = array("PRIMARY", "UNIQUE", "INDEX", "FULLTEXT"); $index_types = array("PRIMARY", "UNIQUE", "INDEX", "FULLTEXT");
$indexes = indexes($_GET["indexes"]); $indexes = indexes($_GET["indexes"]);
$fields = array_keys(fields($_GET["indexes"]));
if ($_POST && !$error && !$_POST["add"]) { if ($_POST && !$error && !$_POST["add"]) {
$alter = array(); $alter = array();
foreach ($_POST["indexes"] as $index) { foreach ($_POST["indexes"] as $index) {
if (in_array($index["type"], $index_types)) { if (in_array($index["type"], $index_types)) {
$columns = array(); $columns = array();
$lengths = array();
$set = array();
ksort($index["columns"]); ksort($index["columns"]);
foreach ($index["columns"] as $column) { foreach ($index["columns"] as $key => $column) {
if (in_array($column, $fields, true)) { if (strlen($column)) {
$length = $index["lengths"][$key];
$set[] = idf_escape($column) . ($length ? "(" . intval($length) . ")" : "");
$columns[count($columns) + 1] = $column; $columns[count($columns) + 1] = $column;
$lengths[count($lengths) + 1] = ($length ? $length : null);
} }
} }
if ($columns) { if ($columns) {
foreach ($indexes as $name => $existing) { foreach ($indexes as $name => $existing) {
if ($index["type"] == $existing["type"] && $existing["columns"] == $columns) { if ($index["type"] == $existing["type"] && $existing["columns"] === $columns && $existing["lengths"] === $lengths) {
unset($indexes[$name]); unset($indexes[$name]);
continue 2; continue 2;
} }
} }
$alter[] = "ADD $index[type]" . ($index["type"] == "PRIMARY" ? " KEY" : "") . " (" . implode(", ", array_map('idf_escape', $columns)) . ")"; $alter[] = "ADD $index[type]" . ($index["type"] == "PRIMARY" ? " KEY" : "") . " (" . implode(", ", $set) . ")";
} }
} }
} }
foreach ($indexes as $name => $existing) { foreach ($indexes as $name => $existing) {
$alter[] = "DROP INDEX " . idf_escape($name); $alter[] = "DROP INDEX " . idf_escape($name));
} }
if (!$alter || $mysql->query("ALTER TABLE " . idf_escape($_GET["indexes"]) . " " . implode(", ", $alter))) { if (!$alter || $mysql->query("ALTER TABLE " . idf_escape($_GET["indexes"]) . " " . implode(", ", $alter))) {
redirect($SELF . "table=" . urlencode($_GET["indexes"]), ($alter ? lang('Indexes has been altered.') : null)); redirect($SELF . "table=" . urlencode($_GET["indexes"]), ($alter ? lang('Indexes has been altered.') : null));
@@ -34,38 +38,48 @@ if ($_POST && !$error && !$_POST["add"]) {
} }
page_header(lang('Indexes') . ': ' . htmlspecialchars($_GET["indexes"])); page_header(lang('Indexes') . ': ' . htmlspecialchars($_GET["indexes"]));
$fields = array_keys(fields($_GET["indexes"]));
if ($_POST) { if ($_POST) {
$row = $_POST;
if (!$_POST["add"]) { if (!$_POST["add"]) {
echo "<p class='error'>" . lang('Unable to operate indexes') . ": " . htmlspecialchars($error) . "</p>\n"; echo "<p class='error'>" . lang('Unable to operate indexes') . ": " . htmlspecialchars($error) . "</p>\n";
} else {
foreach ($row["indexes"] as $key => $index) {
if (strlen($index["columns"][count($index["columns"])])) {
$row["indexes"][$key]["columns"][] = "";
}
}
$index = $row["indexes"][count($row["indexes"]) - 1];
if ($index["type"] || array_filter($index["columns"], 'strlen') || array_filter($index["columns"], 'length')) {
$row["indexes"][] = array("columns" => array(1 => ""));
}
} }
$row = $_POST;
} else { } else {
$row = array("indexes" => $indexes); $row = array("indexes" => $indexes);
foreach ($row["indexes"] as $key => $index) {
$row["indexes"][$key]["columns"][] = "";
}
$row["indexes"][] = array("columns" => array(1 => ""));
} }
?> ?>
<form action="" method="post"> <form action="" method="post">
<table border="0" cellspacing="0" cellpadding="2"> <table border="0" cellspacing="0" cellpadding="2">
<thead><tr><th><?php echo lang('Index Type'); ?></th><td><?php echo lang('Column (length)'); ?></td></tr></thead>
<?php <?php
$j = 0; $j = 0;
foreach ($row["indexes"] as $index) { foreach ($row["indexes"] as $index) {
if ($index["type"] || array_filter($index["columns"], 'strlen')) {
echo "<tr><td><select name='indexes[$j][type]'><option></option>" . optionlist($index_types, $index["type"]) . "</select></td><td>"; echo "<tr><td><select name='indexes[$j][type]'><option></option>" . optionlist($index_types, $index["type"]) . "</select></td><td>";
ksort($index["columns"]); ksort($index["columns"]);
foreach ($index["columns"] as $i => $column) { foreach ($index["columns"] as $i => $column) {
if (strlen($column)) {
echo "<select name='indexes[$j][columns][$i]'><option></option>" . optionlist($fields, $column) . "</select>"; echo "<select name='indexes[$j][columns][$i]'><option></option>" . optionlist($fields, $column) . "</select>";
echo "<input name='indexes[$j][lengths][$i]' size='2' value=\"" . htmlspecialchars($index["lengths"][$i]) . "\" />\n";
} }
}
echo "<select name='indexes[$j][columns][" . ($i+1) . "]'><option></option>" . optionlist($fields, array()) . "</select>";
//! indexes from substring
echo "</td></tr>\n"; echo "</td></tr>\n";
$j++; $j++;
}
} }
//! JavaScript for adding more indexes and columns //! JavaScript for adding more indexes and columns
?> ?>
<tr><td><select name="indexes[<?php echo $j; ?>][type]"><option></option><?php echo optionlist($index_types, array()); ?></select></td><td><select name="indexes[<?php echo $j; ?>][columns][1]"><option></option><?php echo optionlist($fields, array()); ?></select></td></tr>
</table> </table>
<p> <p>
<input type="hidden" name="token" value="<?php echo $token; ?>" /> <input type="hidden" name="token" value="<?php echo $token; ?>" />

View File

@@ -28,7 +28,11 @@ if (!$result) {
echo "<table border='1' cellspacing='0' cellpadding='2'>\n"; echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
foreach ($indexes as $index) { foreach ($indexes as $index) {
ksort($index["columns"]); ksort($index["columns"]);
echo "<tr><td>$index[type]</td><td><i>" . implode("</i>, <i>", $index["columns"]) . "</i></td></tr>\n"; $print = array();
foreach ($index["columns"] as $key => $val) {
$print[] = "<i>" . htmlspecialchars($val) . "</i>" . ($index["lengths"][$key] ? "(" . $index["lengths"][$key] . ")" : "");
}
echo "<tr><td>$index[type]</td><td>" . implode(", ", $print) . "</td></tr>\n";
} }
echo "</table>\n"; echo "</table>\n";
} }
@@ -41,9 +45,10 @@ if (!$result) {
echo "<table border='1' cellspacing='0' cellpadding='2'>\n"; echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
foreach ($foreign_keys as $name => $foreign_key) { foreach ($foreign_keys as $name => $foreign_key) {
echo "<tr>"; echo "<tr>";
echo "<td><i>" . implode("</i>, <i>", $foreign_key["source"]) . "</i></td>"; echo "<td><i>" . implode("</i>, <i>", array_map('htmlspecialchars', $foreign_key["source"])) . "</i></td>";
$link = (strlen($foreign_key["db"]) ? "<strong>" . htmlspecialchars($foreign_key["db"]) . "</strong>." : "") . htmlspecialchars($foreign_key["table"]); $link = (strlen($foreign_key["db"]) ? "<strong>" . htmlspecialchars($foreign_key["db"]) . "</strong>." : "") . htmlspecialchars($foreign_key["table"]);
echo '<td><a href="' . htmlspecialchars(strlen($foreign_key["db"]) ? preg_replace('~db=[^&]*~', "db=" . urlencode($foreign_key["db"]), $SELF) : $SELF) . "table=" . urlencode($foreign_key["table"]) . "\">$link</a>(<em>" . implode("</em>, <em>", $foreign_key["target"]) . "</em>)</td>"; echo '<td><a href="' . htmlspecialchars(strlen($foreign_key["db"]) ? preg_replace('~db=[^&]*~', "db=" . urlencode($foreign_key["db"]), $SELF) : $SELF) . "table=" . urlencode($foreign_key["table"]) . "\">$link</a>";
echo "(<em>" . implode("</em>, <em>", array_map('htmlspecialchars', $foreign_key["target"])) . "</em>)</td>";
echo '<td>' . (!strlen($foreign_key["db"]) ? '<a href="' . htmlspecialchars($SELF) . 'foreign=' . urlencode($_GET["table"]) . '&amp;name=' . urlencode($name) . '">' . lang('Alter') . '</a>' : '&nbsp;') . '</td>'; echo '<td>' . (!strlen($foreign_key["db"]) ? '<a href="' . htmlspecialchars($SELF) . 'foreign=' . urlencode($_GET["table"]) . '&amp;name=' . urlencode($name) . '">' . lang('Alter') . '</a>' : '&nbsp;') . '</td>';
echo "</tr>\n"; echo "</tr>\n";
} }