From 7dd90f56f108745966ce55ccee8f25b0eac47679 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Tue, 4 Jun 2013 19:40:17 -0700 Subject: [PATCH] MySQL: Speed up updating rows without numeric or UTF-8 primary key --- adminer/drivers/mssql.inc.php | 4 ---- adminer/drivers/mysql.inc.php | 10 +--------- adminer/drivers/oracle.inc.php | 4 ---- adminer/drivers/pgsql.inc.php | 4 ---- adminer/drivers/sqlite.inc.php | 4 ---- adminer/include/functions.inc.php | 11 +++++++++-- changes.txt | 1 + editor/include/adminer.inc.php | 2 +- todo.txt | 1 - 9 files changed, 12 insertions(+), 29 deletions(-) diff --git a/adminer/drivers/mssql.inc.php b/adminer/drivers/mssql.inc.php index 90e639f5..17049760 100644 --- a/adminer/drivers/mssql.inc.php +++ b/adminer/drivers/mssql.inc.php @@ -375,10 +375,6 @@ WHERE OBJECT_NAME(i.object_id) = " . q($table) return nl_br(h(preg_replace('~^(\\[[^]]*])+~m', '', $connection->error))); } - function exact_value($val) { - return q($val); - } - function create_database($db, $collation) { return queries("CREATE DATABASE " . idf_escape($db) . (eregi('^[a-z0-9_]+$', $collation) ? " COLLATE $collation" : "")); } diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 747a625b..552230dd 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -532,14 +532,6 @@ if (!defined("DRIVER")) { } } - /** Return expression for binary comparison - * @param string - * @return string - */ - function exact_value($val) { - return q($val) . " COLLATE utf8_bin"; - } - /** Create database * @param string * @param string @@ -983,7 +975,7 @@ if (!defined("DRIVER")) { $return = "UNHEX($return)"; } if ($field["type"] == "bit") { - return "CONV($return, 2, 10) + 0"; + $return = "CONV($return, 2, 10) + 0"; } if (ereg("geometry|point|linestring|polygon", $field["type"])) { $return = "GeomFromText($return)"; diff --git a/adminer/drivers/oracle.inc.php b/adminer/drivers/oracle.inc.php index beae8684..f6545c12 100644 --- a/adminer/drivers/oracle.inc.php +++ b/adminer/drivers/oracle.inc.php @@ -270,10 +270,6 @@ ORDER BY uc.constraint_type, uic.column_position", $connection2) as $row) { return h($connection->error); //! highlight sqltext from offset } - function exact_value($val) { - return q($val); - } - function explain($connection, $query) { $connection->query("EXPLAIN PLAN FOR $query"); return $connection->query("SELECT * FROM plan_table"); diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index 3cbe23b4..2f4af7ad 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -326,10 +326,6 @@ ORDER BY conkey, conname") as $row) { return nl_br($return); } - function exact_value($val) { - return q($val); - } - function create_database($db, $collation) { return queries("CREATE DATABASE " . idf_escape($db) . ($collation ? " ENCODING " . idf_escape($collation) : "")); } diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index 7c062d17..9059451b 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -348,10 +348,6 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) { return h($connection->error); } - function exact_value($val) { - return q($val); - } - function check_sqlite_name($name) { // avoid creating PHP files on unsecured servers global $connection; diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 8dbf25b4..0d32ce24 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -333,9 +333,16 @@ function where($where, $fields = array()) { $function_pattern = '(^[\w\(]+' . str_replace("_", ".*", preg_quote(idf_escape("_"))) . '\)+$)'; //! columns looking like functions foreach ((array) $where["where"] as $key => $val) { $key = bracket_escape($key, 1); // 1 - back - $return[] = (preg_match($function_pattern, $key) ? $key : idf_escape($key)) //! SQL injection - . (($jush == "sql" && ereg('\\.', $val)) || $jush == "mssql" ? " LIKE " . exact_value(addcslashes($val, "%_\\")) : " = " . unconvert_field($fields[$key], exact_value($val))) // LIKE because of floats, but slow with ints, in MS SQL because of text + $column = (preg_match($function_pattern, $key) ? $key : idf_escape($key)); //! SQL injection + $return[] = $column + . (($jush == "sql" && ereg('^[0-9]*\\.[0-9]*$', $val)) || $jush == "mssql" + ? " LIKE " . q(addcslashes($val, "%_\\")) + : " = " . unconvert_field($fields[$key], q($val)) + ) // LIKE because of floats but slow with ints, in MS SQL because of text ; //! enum and set + if ($jush == "sql" && ereg("[^ -@]", $val)) { // not just [a-z] to catch non-ASCII characters + $return[] = "$column = " . q($val) . " COLLATE utf8_bin"; + } } foreach ((array) $where["null"] as $key) { $return[] = idf_escape($key) . " IS NULL"; diff --git a/changes.txt b/changes.txt index a65716fc..9db1bb72 100644 --- a/changes.txt +++ b/changes.txt @@ -5,6 +5,7 @@ Don't use LIMIT 1 if inline updating unique row Don't check previous checkbox on added column in create table (bug #3614245) Order table list by name Verify UTF-8 encoding of CSV import +MySQL: Speed up updating rows without numeric or UTF-8 primary key PostgreSQL: Fix detecting oid column in PDO PostgreSQL: Handle timestamp types (bug #3614086) Add Korean translation diff --git a/editor/include/adminer.inc.php b/editor/include/adminer.inc.php index 4f1cfd9b..81c92c1b 100644 --- a/editor/include/adminer.inc.php +++ b/editor/include/adminer.inc.php @@ -143,7 +143,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5 // find all used ids $ids = array(); foreach ($rows as $row) { - $ids[$row[$key]] = exact_value($row[$key]); + $ids[$row[$key]] = q($row[$key]); } // uses constant number of queries to get the descriptions, join would be complex, multiple queries would be slow $descriptions = $this->_values[$table]; diff --git a/todo.txt b/todo.txt index 5e04a7bf..0424e290 100644 --- a/todo.txt +++ b/todo.txt @@ -21,7 +21,6 @@ Rank, Tree structure MySQL: Data longer than max_allowed_packet can be sent by mysqli_stmt_send_long_data() -COLLATE utf8_bin comparison doesn't use index with other than UTF-8 columns SQLite: Copy tables