adminerevo/adminer/indexes.inc.php

147 lines
5.1 KiB
PHP
Raw Normal View History

<?php
$TABLE = $_GET["indexes"];
$index_types = array("PRIMARY", "UNIQUE", "INDEX");
2013-04-27 03:04:57 +00:00
$table_status = table_status($TABLE, true);
2018-01-29 18:34:26 +00:00
if (preg_match('~MyISAM|M?aria' . (min_version(5.6, '10.0.5') ? '|InnoDB' : '') . '~i', $table_status["Engine"])) {
$index_types[] = "FULLTEXT";
}
2018-01-29 18:34:26 +00:00
if (preg_match('~MyISAM|M?aria' . (min_version(5.7, '10.2.2') ? '|InnoDB' : '') . '~i', $table_status["Engine"])) {
$index_types[] = "SPATIAL";
}
$indexes = indexes($TABLE);
$primary = array();
2013-08-08 22:32:36 +00:00
if ($jush == "mongo") { // doesn't support primary key
$primary = $indexes["_id_"];
unset($index_types[0]);
2013-08-08 22:32:36 +00:00
unset($indexes["_id_"]);
}
2013-05-08 15:43:15 +00:00
$row = $_POST;
2013-05-02 01:28:04 +00:00
2013-07-07 06:35:26 +00:00
if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"]) {
$alter = array();
2013-05-08 15:43:15 +00:00
foreach ($row["indexes"] as $index) {
2011-07-13 13:13:00 +00:00
$name = $index["name"];
if (in_array($index["type"], $index_types)) {
$columns = array();
$lengths = array();
2013-06-25 16:42:47 +00:00
$descs = array();
$set = array();
ksort($index["columns"]);
foreach ($index["columns"] as $key => $column) {
if ($column != "") {
$length = $index["lengths"][$key];
2013-06-25 16:42:47 +00:00
$desc = $index["descs"][$key];
$set[] = idf_escape($column) . ($length ? "(" . (+$length) . ")" : "") . ($desc ? " DESC" : "");
$columns[] = $column;
$lengths[] = ($length ? $length : null);
2013-06-25 16:42:47 +00:00
$descs[] = $desc;
}
}
if ($columns) {
2011-07-13 13:13:00 +00:00
$existing = $indexes[$name];
if ($existing) {
ksort($existing["columns"]);
ksort($existing["lengths"]);
2013-06-25 16:42:47 +00:00
ksort($existing["descs"]);
if ($index["type"] == $existing["type"]
&& array_values($existing["columns"]) === $columns
&& (!$existing["lengths"] || array_values($existing["lengths"]) === $lengths)
&& array_values($existing["descs"]) === $descs
) {
// skip existing index
unset($indexes[$name]);
2011-07-13 13:13:00 +00:00
continue;
}
}
2014-01-12 03:22:44 +00:00
$alter[] = array($index["type"], $name, $set);
}
}
}
// drop removed indexes
foreach ($indexes as $name => $existing) {
2011-07-13 13:13:00 +00:00
$alter[] = array($existing["type"], $name, "DROP");
}
if (!$alter) {
redirect(ME . "table=" . urlencode($TABLE));
}
queries_redirect(ME . "table=" . urlencode($TABLE), lang('Indexes have been altered.'), alter_indexes($TABLE, $alter));
}
page_header(lang('Indexes'), $error, array("table" => $TABLE), h($TABLE));
$fields = array_keys(fields($TABLE));
2013-05-08 15:43:15 +00:00
if ($_POST["add"]) {
foreach ($row["indexes"] as $key => $index) {
if ($index["columns"][count($index["columns"])] != "") {
$row["indexes"][$key]["columns"][] = "";
}
}
2013-05-08 15:43:15 +00:00
$index = end($row["indexes"]);
2013-07-07 06:42:37 +00:00
if ($index["type"] || array_filter($index["columns"], 'strlen')) {
2013-05-08 15:43:15 +00:00
$row["indexes"][] = array("columns" => array(1 => ""));
}
}
if (!$row) {
foreach ($indexes as $key => $index) {
$indexes[$key]["name"] = $key;
$indexes[$key]["columns"][] = "";
}
2013-05-08 15:43:15 +00:00
$indexes[] = array("columns" => array(1 => ""));
$row["indexes"] = $indexes;
}
?>
<form action="" method="post">
2018-10-27 19:20:56 +00:00
<div class="scrollable">
2010-06-30 15:09:08 +00:00
<table cellspacing="0" class="nowrap">
2013-07-10 19:44:03 +00:00
<thead><tr>
2017-01-23 22:29:00 +00:00
<th id="label-type"><?php echo lang('Index Type'); ?>
2017-01-23 22:10:50 +00:00
<th><input type="submit" class="wayoff"><?php echo lang('Column (length)'); ?>
2017-01-23 22:29:00 +00:00
<th id="label-name"><?php echo lang('Name'); ?>
2018-02-20 15:31:31 +00:00
<th><noscript><?php echo "<input type='image' class='icon' name='add[0]' src='../adminer/static/plus.gif' alt='+' title='" . lang('Add next') . "'>"; ?></noscript>
2013-07-10 19:44:03 +00:00
</thead>
<?php
if ($primary) {
echo "<tr><td>PRIMARY<td>";
foreach ($primary["columns"] as $key => $column) {
2014-01-12 03:06:25 +00:00
echo select_input(" disabled", $fields, $column);
echo "<label><input disabled type='checkbox'>" . lang('descending') . "</label> ";
}
echo "<td><td>\n";
}
2010-06-30 15:09:08 +00:00
$j = 1;
foreach ($row["indexes"] as $index) {
2013-07-07 06:35:26 +00:00
if (!$_POST["drop_col"] || $j != key($_POST["drop_col"])) {
echo "<tr><td>" . html_select("indexes[$j][type]", array(-1 => "") + $index_types, $index["type"], ($j == count($row["indexes"]) ? "indexesAddRow.call(this);" : 1), "label-type");
2013-07-07 06:35:26 +00:00
echo "<td>";
ksort($index["columns"]);
$i = 1;
foreach ($index["columns"] as $key => $column) {
2014-01-12 03:06:25 +00:00
echo "<span>" . select_input(
2018-01-12 11:42:30 +00:00
" name='indexes[$j][columns][$i]' title='" . lang('Column') . "'",
2014-01-12 03:06:25 +00:00
($fields ? array_combine($fields, $fields) : $fields),
2018-01-12 11:42:30 +00:00
$column,
"partial(" . ($i == count($index["columns"]) ? "indexesAddColumn" : "indexesChangeColumn") . ", '" . js_escape($jush == "sql" ? "" : $_GET["indexes"] . "_") . "')"
2014-01-12 03:06:25 +00:00
);
2017-01-23 22:29:00 +00:00
echo ($jush == "sql" || $jush == "mssql" ? "<input type='number' name='indexes[$j][lengths][$i]' class='size' value='" . h($index["lengths"][$key]) . "' title='" . lang('Length') . "'>" : "");
echo (support("descidx") ? checkbox("indexes[$j][descs][$i]", 1, $index["descs"][$key], lang('descending')) : "");
2013-07-07 06:35:26 +00:00
echo " </span>";
$i++;
}
2017-01-23 22:29:00 +00:00
echo "<td><input name='indexes[$j][name]' value='" . h($index["name"]) . "' autocapitalize='off' aria-labelledby='label-name'>\n";
2018-01-12 15:29:18 +00:00
echo "<td><input type='image' class='icon' name='drop_col[$j]' src='../adminer/static/cross.gif' alt='x' title='" . lang('Remove') . "'>" . script("qsl('input').onclick = partial(editingRemoveRow, 'indexes\$1[type]');");
}
$j++;
}
?>
</table>
</div>
<p>
<input type="submit" value="<?php echo lang('Save'); ?>">
2011-03-08 12:43:05 +00:00
<input type="hidden" name="token" value="<?php echo $token; ?>">
</form>