Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Single faster POST query with primary key

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@822 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
commit 16c4ddb42d9a0d4d3c7fe2213197cae2c0e729c3 1 parent 0a26222
authored

Showing 1 changed file with 24 additions and 8 deletions. Show diff stats Hide diff stats

  1. 32  adminer/select.inc.php
32  adminer/select.inc.php
@@ -3,6 +3,12 @@
3 3
 $grouping = array("avg", "count", "distinct", "group_concat", "max", "min", "sum"); // distinct is short for COUNT(DISTINCT)
4 4
 $table_status = table_status($_GET["select"]);
5 5
 $indexes = indexes($_GET["select"]);
  6
+$primary = null; // empty array means that all primary fields are selected
  7
+foreach ($indexes as $index) {
  8
+	if ($index["type"] == "PRIMARY") {
  9
+		$primary = array_flip($index["columns"]);
  10
+	}
  11
+}
6 12
 $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "REGEXP", "IN", "IS NULL", "NOT LIKE", "NOT REGEXP", "NOT IN", "IS NOT NULL");
7 13
 if (eregi('^(MyISAM|Maria)$', $table_status["Engine"])) {
8 14
 	$operators[] = "AGAINST";
@@ -17,6 +23,9 @@
17 23
 		if (ereg('text|blob', $field["type"])) {
18 24
 			$text_length = (isset($_GET["text_length"]) ? $_GET["text_length"] : "100");
19 25
 		}
  26
+		if (!$_GET["columns"]) {
  27
+			unset($primary[$key]);
  28
+		}
20 29
 	}
21 30
 	$rights += $field["privileges"];
22 31
 }
@@ -29,8 +38,12 @@
29 38
 		if (!in_array($val["fun"], $grouping)) {
30 39
 			$group[] = $select[$key];
31 40
 		}
  41
+		if (!$val["fun"]) {
  42
+			unset($primary[$val["col"]]);
  43
+		}
32 44
 	}
33 45
 }
  46
+
34 47
 $where = array(); // where expressions - will be joined by AND
35 48
 foreach ($indexes as $i => $index) {
36 49
 	if ($index["type"] == "FULLTEXT" && strlen($_GET["fulltext"][$i])) {
@@ -59,29 +72,32 @@
59 72
 		}
60 73
 	}
61 74
 }
  75
+
62 76
 $order = array(); // order expressions - will be joined by comma
63 77
 foreach ((array) $_GET["order"] as $key => $val) {
64 78
 	if (isset($columns[$val]) || in_array($val, $select, true)) {
65 79
 		$order[] = idf_escape($val) . (isset($_GET["desc"][$key]) ? " DESC" : "");
66 80
 	}
67 81
 }
  82
+
68 83
 $limit = (isset($_GET["limit"]) ? $_GET["limit"] : "30");
69 84
 $from = ($select ? implode(", ", $select) : "*") . " FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "");
70 85
 $group_by = ($group && count($group) < count($select) ? " GROUP BY " . implode(", ", $group) : "") . ($order ? " ORDER BY " . implode(", ", $order) : "");
71 86
 
72 87
 if ($_POST && !$error) {
  88
+	$where_check = "(" . implode(") OR (", array_map('where_check', $_POST["check"])) . ")";
73 89
 	if ($_POST["export"]) {
74 90
 		dump_headers($_GET["select"]);
75 91
 		dump_table($_GET["select"], "");
76  
-		if (is_array($_POST["check"])) {
  92
+		if (!is_array($_POST["check"]) || $primary === array()) {
  93
+			dump_data($_GET["select"], "INSERT", "SELECT $from" . (is_array($_POST["check"]) ? ($where ? " AND " : " WHERE ") . "($where_check)" : "") . $group_by);
  94
+		} else {
77 95
 			$union = array();
78 96
 			foreach ($_POST["check"] as $val) {
79  
-				// where may not be unique so OR can't be used
  97
+				// where is not unique so OR can't be used
80 98
 				$union[] = "(SELECT $from " . ($where ? "AND " : "WHERE ") . where_check($val) . $group_by . " LIMIT 1)";
81 99
 			}
82 100
 			dump_data($_GET["select"], "INSERT", implode(" UNION ALL ", $union));
83  
-		} else {
84  
-			dump_data($_GET["select"], "INSERT", "SELECT $from$group_by");
85 101
 		}
86 102
 		exit;
87 103
 	}
@@ -89,7 +105,7 @@
89 105
 		$sent = 0;
90 106
 		if ($_POST["all"] || $_POST["check"]) {
91 107
 			$field = idf_escape($_POST["email_field"]);
92  
-			$result = $dbh->query("SELECT DISTINCT $field FROM " . idf_escape($_GET["select"]) . " WHERE $field IS NOT NULL AND $field != ''" . ($where ? " AND " . implode(" AND ", $where) : "") . ($_POST["all"] ? "" : " AND ((" . implode(") OR (", array_map('where_check', $_POST["check"])) . "))"));
  108
+			$result = $dbh->query("SELECT DISTINCT $field FROM " . idf_escape($_GET["select"]) . " WHERE $field IS NOT NULL AND $field != ''" . ($where ? " AND " . implode(" AND ", $where) : "") . ($_POST["all"] ? "" : " AND ($where_check)"));
93 109
 			while ($row = $result->fetch_row()) {
94 110
 				$sent += mail($row[0], email_header($_POST["email_subject"]), $_POST["email_message"], "MIME-Version: 1.0\nContent-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: 8bit" . ($_POST["email_from"] ? "\nFrom: " . email_header($_POST["email_from"]) : ""));
95 111
 			}
@@ -113,12 +129,12 @@
113 129
 			$command .= ($_POST["clone"] ? "\nSELECT " . implode(", ", $set) . "\nFROM " . idf_escape($_GET["select"]) : " SET\n" . implode(",\n", $set));
114 130
 		}
115 131
 		if ($_POST["delete"] || $set) {
116  
-			if ($_POST["all"]) {
117  
-				$result = queries($command . ($where ? "\nWHERE " . implode(" AND ", $where) : ""));
  132
+			if ($_POST["all"] || ($primary === array() && $_POST["check"])) {
  133
+				$result = queries($command . ($_POST["all"] ? ($where ? "\nWHERE " . implode(" AND ", $where) : "") : "\nWHERE $where_check"));
118 134
 				$affected = $dbh->affected_rows;
119 135
 			} else {
120 136
 				foreach ((array) $_POST["check"] as $val) {
121  
-					// where may not be unique so OR can't be used
  137
+					// where is not unique so OR can't be used
122 138
 					$result = queries($command . "\nWHERE " . where_check($val) . "\nLIMIT 1");
123 139
 					if (!$result) {
124 140
 						break;

0 notes on commit 16c4ddb

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