Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Alter indexes

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@29 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
commit 0b6ab614363034ee1babf3ff6b5436eddd4f60f3 1 parent 720f5fc
authored July 05, 2007
2  functions.inc.php
@@ -23,7 +23,7 @@ function optionlist($options, $selected = array(), $not_vals = false) {
23 23
 			$return .= '<optgroup label="' . htmlspecialchars($k) . '">';
24 24
 		}
25 25
 		foreach ((is_array($v) ? $v : array($k => $v)) as $key => $val) {
26  
-			$checked = in_array(($not_vals ? $val : $key), (array) $selected);
  26
+			$checked = in_array(($not_vals ? $val : $key), (array) $selected, true);
27 27
 			$return .= '<option' . ($not_vals ? '' : ' value="' . htmlspecialchars($key) . '"') . ($checked ? ' selected="selected"' : '') . '>' . htmlspecialchars($val) . '</option>';
28 28
 		}
29 29
 		if (is_array($v)) {
35  indexes.inc.php
... ...
@@ -1,6 +1,36 @@
1 1
 <?php
2 2
 $index_types = array("PRIMARY", "UNIQUE", "INDEX", "FULLTEXT");
  3
+$indexes = indexes($_GET["indexes"]);
  4
+$fields = array_keys(fields($_GET["indexes"]));
3 5
 if ($_POST) {
  6
+	$alter = array();
  7
+	foreach ($_POST["indexes"] as $index) {
  8
+		if (in_array($index["type"], $index_types)) {
  9
+			$columns = array();
  10
+			ksort($index["columns"]);
  11
+			foreach ($index["columns"] as $column) {
  12
+				if (in_array($column, $fields, true)) {
  13
+					$columns[count($columns) + 1] = $column;
  14
+				}
  15
+			}
  16
+			if ($columns) {
  17
+				foreach ($indexes as $name => $existing) {
  18
+					if ($index["type"] == $existing["type"] && $existing["columns"] == $columns) {
  19
+						unset($indexes[$name]);
  20
+						continue 2;
  21
+					}
  22
+				}
  23
+				$alter[] = "ADD $index[type]" . ($index["type"] == "PRIMARY" ? " KEY" : "") . " (" . implode(", ", array_map('idf_escape', $columns)) . ")";
  24
+			}
  25
+		}
  26
+	}
  27
+	foreach ($indexes as $name => $existing) {
  28
+		$alter[] = "DROP INDEX " . idf_escape($name);
  29
+	}
  30
+	if (!$alter || mysql_query("ALTER TABLE " . idf_escape($_GET["indexes"]) . " " . implode(", ", $alter))) {
  31
+		redirect($SELF . "table=" . urlencode($_GET["indexes"]), ($alter ? lang('Indexes has been altered.') : null));
  32
+	}
  33
+	$error = mysql_error();
4 34
 }
5 35
 
6 36
 page_header(lang('Indexes') . ': ' . htmlspecialchars($_GET["indexes"]));
@@ -10,17 +40,16 @@
10 40
 	echo "<p class='error'>" . lang('Unable to operate indexes') . ": " . htmlspecialchars($error) . "</p>\n";
11 41
 	$row = $_POST;
12 42
 } else {
13  
-	$row = array("indexes" => indexes($_GET["indexes"]));
  43
+	$row = array("indexes" => $indexes);
14 44
 }
15 45
 ?>
16 46
 <form action="" method="post">
17 47
 <table border="0" cellspacing="0" cellpadding="2">
18 48
 <?php
19  
-$fields = array_keys(fields($_GET["indexes"]));
20 49
 $j = 0;
21 50
 foreach ($row["indexes"] as $index) {
22 51
 	echo "<tr><td><select name='indexes[$j][type]'><option></option>" . optionlist($index_types, $index["type"], "not_vals") . "</select></td><td>";
23  
-	sort($index["columns"]);
  52
+	ksort($index["columns"]);
24 53
 	foreach ($index["columns"] as $i => $column) {
25 54
 		echo "<select name='indexes[$j][columns][$i]'><option></option>" . optionlist($fields, $column, "not_vals") . "</select>";
26 55
 	}
2  table.inc.php
@@ -16,7 +16,7 @@
16 16
 if ($indexes) {
17 17
 	echo "<table border='1' cellspacing='0' cellpadding='2'>\n";
18 18
 	foreach ($indexes as $index) {
19  
-		sort($index["columns"]);
  19
+		ksort($index["columns"]);
20 20
 		echo "<tr><td>$index[type]</td><td><i>" . implode("</i>, <i>", $index["columns"]) . "</i></td></tr>\n";
21 21
 	}
22 22
 	echo "</table>\n";

0 notes on commit 0b6ab61

Please sign in to comment.
Something went wrong with that request. Please try again.