Skip to content

Commit

Permalink
Index length
Browse files Browse the repository at this point in the history
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@166 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information
jakubvrana committed Jul 14, 2007
1 parent ad7c79b commit e78ec76
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions functions.inc.php
Expand Up @@ -79,6 +79,7 @@ function indexes($table) {
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"]]["columns"][$row["Seq_in_index"]] = $row["Column_name"];
$return[$row["Key_name"]]["lengths"][$row["Seq_in_index"]] = $row["Sub_part"];
}
$result->free();
}
Expand Down
54 changes: 34 additions & 20 deletions indexes.inc.php
@@ -1,31 +1,35 @@
<?php
$index_types = array("PRIMARY", "UNIQUE", "INDEX", "FULLTEXT");
$indexes = indexes($_GET["indexes"]);
$fields = array_keys(fields($_GET["indexes"]));
if ($_POST && !$error && !$_POST["add"]) {
$alter = array();
foreach ($_POST["indexes"] as $index) {
if (in_array($index["type"], $index_types)) {
$columns = array();
$lengths = array();
$set = array();
ksort($index["columns"]);
foreach ($index["columns"] as $column) {
if (in_array($column, $fields, true)) {
foreach ($index["columns"] as $key => $column) {
if (strlen($column)) {
$length = $index["lengths"][$key];
$set[] = idf_escape($column) . ($length ? "(" . intval($length) . ")" : "");
$columns[count($columns) + 1] = $column;
$lengths[count($lengths) + 1] = ($length ? $length : null);
}
}
if ($columns) {
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]);
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) {
$alter[] = "DROP INDEX " . idf_escape($name);
$alter[] = "DROP INDEX " . idf_escape($name));
}
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));
Expand All @@ -34,38 +38,48 @@
}
page_header(lang('Indexes') . ': ' . htmlspecialchars($_GET["indexes"]));

$fields = array_keys(fields($_GET["indexes"]));
if ($_POST) {
$row = $_POST;
if (!$_POST["add"]) {
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 {
$row = array("indexes" => $indexes);
foreach ($row["indexes"] as $key => $index) {
$row["indexes"][$key]["columns"][] = "";
}
$row["indexes"][] = array("columns" => array(1 => ""));
}
?>

<form action="" method="post">
<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
$j = 0;
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>";
ksort($index["columns"]);
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+1) . "]'><option></option>" . optionlist($fields, array()) . "</select>";
//! indexes from substring
echo "</td></tr>\n";
$j++;
echo "<tr><td><select name='indexes[$j][type]'><option></option>" . optionlist($index_types, $index["type"]) . "</select></td><td>";
ksort($index["columns"]);
foreach ($index["columns"] as $i => $column) {
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 "</td></tr>\n";
$j++;
}
//! 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>
<p>
<input type="hidden" name="token" value="<?php echo $token; ?>" />
Expand Down
11 changes: 8 additions & 3 deletions table.inc.php
Expand Up @@ -28,7 +28,11 @@
echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
foreach ($indexes as $index) {
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";
}
Expand All @@ -41,9 +45,10 @@
echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
foreach ($foreign_keys as $name => $foreign_key) {
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"]);
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 "</tr>\n";
}
Expand Down

0 comments on commit e78ec76

Please sign in to comment.