Skip to content

Commit

Permalink
Separate $where in limit function
Browse files Browse the repository at this point in the history
  • Loading branch information
vrana committed May 14, 2010
1 parent 8b2808a commit ee3e045
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion adminer/create.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
} }
if (support("partitioning")) { if (support("partitioning")) {
$from = "FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = " . $connection->quote(DB) . " AND TABLE_NAME = " . $connection->quote($TABLE); $from = "FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = " . $connection->quote(DB) . " AND TABLE_NAME = " . $connection->quote($TABLE);
$result = $connection->query("SELECT" . limit("PARTITION_METHOD, PARTITION_ORDINAL_POSITION, PARTITION_EXPRESSION $from ORDER BY PARTITION_ORDINAL_POSITION", 1)); $result = $connection->query("SELECT PARTITION_METHOD, PARTITION_ORDINAL_POSITION, PARTITION_EXPRESSION $from ORDER BY PARTITION_ORDINAL_POSITION LIMIT 1");
list($row["partition_by"], $row["partitions"], $row["partition"]) = $result->fetch_row(); list($row["partition_by"], $row["partitions"], $row["partition"]) = $result->fetch_row();
$row["partition_names"] = array(); $row["partition_names"] = array();
$row["partition_values"] = array(); $row["partition_values"] = array();
Expand Down
2 changes: 1 addition & 1 deletion adminer/download.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
$TABLE = $_GET["download"]; $TABLE = $_GET["download"];
header("Content-Type: application/octet-stream"); header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . friendly_url("$TABLE-" . implode("_", $_GET["where"])) . "." . friendly_url($_GET["field"])); header("Content-Disposition: attachment; filename=" . friendly_url("$TABLE-" . implode("_", $_GET["where"])) . "." . friendly_url($_GET["field"]));
echo $connection->result("SELECT" . limit(idf_escape($_GET["field"]) . " FROM " . table($TABLE) . " WHERE " . where($_GET), 1)); echo $connection->result("SELECT" . limit(idf_escape($_GET["field"]) . " FROM " . table($TABLE), " WHERE " . where($_GET), 1));
exit; // don't output footer exit; // don't output footer
9 changes: 4 additions & 5 deletions adminer/drivers/mssql.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function select_db($database) {
return $this->query("USE $database"); return $this->query("USE $database");
} }



function query($query, $unbuffered = false) { function query($query, $unbuffered = false) {
$result = sqlsrv_query($this->_link, $query); //! , array(), ($unbuffered ? array() : array("Scrollable" => "keyset")) $result = sqlsrv_query($this->_link, $query); //! , array(), ($unbuffered ? array() : array("Scrollable" => "keyset"))
if (!$result) { if (!$result) {
Expand Down Expand Up @@ -255,12 +254,12 @@ function get_databases() {
return get_vals("EXEC sp_databases"); return get_vals("EXEC sp_databases");
} }


function limit($query, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return (isset($limit) ? " TOP (" . ($limit + $offset) . ")" : "") . " $query"; // seek later return (isset($limit) ? " TOP (" . ($limit + $offset) . ")" : "") . " $query$where"; // seek later
} }


function limit1($query) { function limit1($query, $where) {
return limit($query, 1); return limit($query, $where, 1);
} }


function db_collation($db, $collations) { function db_collation($db, $collations) {
Expand Down
9 changes: 5 additions & 4 deletions adminer/drivers/mysql.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -266,21 +266,22 @@ function get_databases($flush = true) {


/** Formulate SQL query with limit /** Formulate SQL query with limit
* @param string everything after SELECT * @param string everything after SELECT
* @param string including WHERE
* @param int * @param int
* @param int * @param int
* @param string * @param string
* @return string * @return string
*/ */
function limit($query, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : ""); return " $query$where" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
} }


/** Formulate SQL modification query with limit 1 /** Formulate SQL modification query with limit 1
* @param string everything after UPDATE or DELETE * @param string everything after UPDATE or DELETE
* @return string * @return string
*/ */
function limit1($query) { function limit1($query, $where) {
return limit($query, 1); return limit($query, $where, 1);
} }


/** Get database collation /** Get database collation
Expand Down
30 changes: 15 additions & 15 deletions adminer/drivers/pgsql.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ function select_db($database) {
if ($database == DB) { if ($database == DB) {
return $this->_database; return $this->_database;
} }
$link = @pg_connect($this->_connection . " dbname='" . addcslashes($database, "'\\") . "'", PGSQL_CONNECT_FORCE_NEW); $return = @pg_connect($this->_connection . " dbname='" . addcslashes($database, "'\\") . "'", PGSQL_CONNECT_FORCE_NEW);
if ($link) { if ($return) {
$this->_link = $link; $this->_link = $return;
} }
return $link; return $return;
} }


function close() { function close() {
Expand Down Expand Up @@ -108,15 +108,15 @@ function fetch_row() {


function fetch_field() { function fetch_field() {
$column = $this->_offset++; $column = $this->_offset++;
$row = new stdClass; $return = new stdClass;
if (function_exists('pg_field_table')) { if (function_exists('pg_field_table')) {
$row->orgtable = pg_field_table($this->_result, $column); $return->orgtable = pg_field_table($this->_result, $column);
} }
$row->name = pg_field_name($this->_result, $column); $return->name = pg_field_name($this->_result, $column);
$row->orgname = $row->name; $return->orgname = $return->name;
$row->type = pg_field_type($this->_result, $column); $return->type = pg_field_type($this->_result, $column);
$row->charsetnr = ($row->type == "bytea" ? 63 : 0); // 63 - binary $return->charsetnr = ($return->type == "bytea" ? 63 : 0); // 63 - binary
return $row; return $return;
} }


function __destruct() { function __destruct() {
Expand Down Expand Up @@ -167,12 +167,12 @@ function get_databases() {
return get_vals("SELECT datname FROM pg_database"); return get_vals("SELECT datname FROM pg_database");
} }


function limit($query, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : ""); return " $query$where" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
} }


function limit1($query) { function limit1($query, $where) {
return " $query"; return " $query$where";
} }


function db_collation($db, $collations) { function db_collation($db, $collations) {
Expand Down
8 changes: 4 additions & 4 deletions adminer/drivers/sqlite.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ function get_databases() {
return array(); return array();
} }


function limit($query, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : ""); return " $query$where" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
} }


function limit1($query) { function limit1($query, $where) {
global $connection; global $connection;
return ($connection->result("SELECT sqlite_compileoption_used('ENABLE_UPDATE_DELETE_LIMIT')") ? limit($query, 1) : " $query"); return ($connection->result("SELECT sqlite_compileoption_used('ENABLE_UPDATE_DELETE_LIMIT')") ? limit($query, $where, 1) : " $query$where");
} }


function db_collation($db, $collations) { function db_collation($db, $collations) {
Expand Down
6 changes: 3 additions & 3 deletions adminer/edit.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$location = ME . "select=" . urlencode($TABLE); $location = ME . "select=" . urlencode($TABLE);
} }
if (isset($_POST["delete"])) { if (isset($_POST["delete"])) {
query_redirect("DELETE" . limit1("FROM " . table($TABLE) . "\nWHERE $where"), $location, lang('Item has been deleted.')); query_redirect("DELETE" . limit1("FROM " . table($TABLE), $where), $location, lang('Item has been deleted.'));
} else { } else {
$set = array(); $set = array();
foreach ($fields as $name => $field) { foreach ($fields as $name => $field) {
Expand All @@ -29,7 +29,7 @@
if (!$set) { if (!$set) {
redirect($location); redirect($location);
} }
query_redirect("UPDATE" . limit1(table($TABLE) . " SET" . implode(",", $set) . "\nWHERE $where"), $location, lang('Item has been updated.')); query_redirect("UPDATE" . limit1(table($TABLE) . " SET" . implode(",", $set), "\nWHERE $where"), $location, lang('Item has been updated.'));
} else { } else {
$result = insert_into($TABLE, $set); $result = insert_into($TABLE, $set);
$last_id = ($result ? last_id() : 0); $last_id = ($result ? last_id() : 0);
Expand Down Expand Up @@ -58,7 +58,7 @@
} }
$row = array(); $row = array();
if ($select) { if ($select) {
$result = $connection->query("SELECT" . limit(implode(", ", $select) . " FROM " . table($TABLE) . " WHERE $where", (isset($_GET["select"]) ? 2 : 1))); $result = $connection->query("SELECT" . limit(implode(", ", $select) . " FROM " . table($TABLE), " WHERE $where", (isset($_GET["select"]) ? 2 : 1)));
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
if (isset($_GET["select"]) && $result->fetch_assoc()) { if (isset($_GET["select"]) && $result->fetch_assoc()) {
$row = null; $row = null;
Expand Down
2 changes: 1 addition & 1 deletion adminer/include/functions.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ function search_tables() {
foreach (table_status() as $table => $table_status) { foreach (table_status() as $table => $table_status) {
$name = $adminer->tableName($table_status); $name = $adminer->tableName($table_status);
if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) { if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
$result = $connection->query("SELECT" . limit("1 FROM " . table($table) . " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1)); $result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1));
if ($result->num_rows) { if ($result->num_rows) {
if (!$found) { if (!$found) {
echo "<ul>\n"; echo "<ul>\n";
Expand Down
16 changes: 10 additions & 6 deletions adminer/select.inc.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$where = $adminer->selectSearchProcess($fields, $indexes); $where = $adminer->selectSearchProcess($fields, $indexes);
$order = $adminer->selectOrderProcess($fields, $indexes); $order = $adminer->selectOrderProcess($fields, $indexes);
$limit = $adminer->selectLimitProcess(); $limit = $adminer->selectLimitProcess();
$from = ($select ? implode(", ", $select) : "*") . "\nFROM " . table($TABLE) . ($where ? "\nWHERE " . implode(" AND ", $where) : ""); $from = ($select ? implode(", ", $select) : "*") . "\nFROM " . table($TABLE);
$group_by = ($group && count($group) < count($select) ? "\nGROUP BY " . implode(", ", $group) : "") . ($order ? "\nORDER BY " . implode(", ", $order) : ""); $group_by = ($group && count($group) < count($select) ? "\nGROUP BY " . implode(", ", $group) : "") . ($order ? "\nORDER BY " . implode(", ", $order) : "");


if ($_POST && !$error) { if ($_POST && !$error) {
Expand All @@ -49,12 +49,16 @@
dump_csv($row); dump_csv($row);
} }
if (!is_array($_POST["check"]) || $primary === array()) { if (!is_array($_POST["check"]) || $primary === array()) {
dump_data($TABLE, "INSERT", "SELECT $from" . (is_array($_POST["check"]) ? ($where ? " AND " : " WHERE ") . "($where_check)" : "") . $group_by); $where2 = $where;
if (is_array($_POST["check"])) {
$where2[] = "($where_check)";
}
dump_data($TABLE, "INSERT", "SELECT $from" . ($where2 ? "\nWHERE " . implode(" AND ", $where2) : "") . $group_by);
} else { } else {
$union = array(); $union = array();
foreach ($_POST["check"] as $val) { foreach ($_POST["check"] as $val) {
// where is not unique so OR can't be used // where is not unique so OR can't be used
$union[] = "(SELECT" . limit("$from " . ($where ? "AND " : "WHERE ") . where_check($val) . $group_by, 1) . ")"; $union[] = "(SELECT" . limit($from, "\nWHERE " . ($where ? implode(" AND ", $where) . " AND " : "") . where_check($val) . $group_by, 1) . ")";
} }
dump_data($TABLE, "INSERT", implode(" UNION ALL ", $union)); dump_data($TABLE, "INSERT", implode(" UNION ALL ", $union));
} }
Expand Down Expand Up @@ -95,7 +99,7 @@
} else { } else {
foreach ((array) $_POST["check"] as $val) { foreach ((array) $_POST["check"] as $val) {
// where is not unique so OR can't be used // where is not unique so OR can't be used
$result = queries($command . limit1($query . "\nWHERE " . where_check($val))); $result = queries($command . limit1($query, "\nWHERE " . where_check($val)));
if (!$result) { if (!$result) {
break; break;
} }
Expand All @@ -117,7 +121,7 @@
$key = bracket_escape($key, 1); // 1 - back $key = bracket_escape($key, 1); // 1 - back
$set[] = idf_escape($key) . " = " . $adminer->processInput($fields[$key], $val); $set[] = idf_escape($key) . " = " . $adminer->processInput($fields[$key], $val);
} }
$result = queries("UPDATE" . limit1(table($TABLE) . " SET " . implode(", ", $set) . " WHERE " . where_check($unique_idf) . ($where ? " AND " . implode(" AND ", $where) : ""))); // can change row on a different page without unique key $result = queries("UPDATE" . limit1(table($TABLE) . " SET " . implode(", ", $set), " WHERE " . where_check($unique_idf) . ($where ? " AND " . implode(" AND ", $where) : ""))); // can change row on a different page without unique key
if (!$result) { if (!$result) {
break; break;
} }
Expand Down Expand Up @@ -202,7 +206,7 @@
$page = floor(($found_rows - 1) / $limit); $page = floor(($found_rows - 1) / $limit);
} }


$query = "SELECT" . limit((intval($limit) && $group && count($group) < count($select) && $jush == "sql" ? "SQL_CALC_FOUND_ROWS " : "") . $from . $group_by, ($limit != "" ? intval($limit) : null), ($page ? $limit * $page : 0), "\n"); $query = "SELECT" . limit((intval($limit) && $group && count($group) < count($select) && $jush == "sql" ? "SQL_CALC_FOUND_ROWS " : "") . $from, ($where ? "\nWHERE " . implode(" AND ", $where) : "") . $group_by, ($limit != "" ? intval($limit) : null), ($page ? $limit * $page : 0), "\n");
echo $adminer->selectQuery($query); echo $adminer->selectQuery($query);


$result = $connection->query($query); $result = $connection->query($query);
Expand Down
1 change: 1 addition & 0 deletions todo.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Dollar terminated string in SQL command
bool in Editor bool in Editor


MS SQL: MS SQL:
Non UTF-8 character sets
Rename by sp_rename Rename by sp_rename
Detection of table collation Detection of table collation
PDO driver with seek PDO driver with seek

0 comments on commit ee3e045

Please sign in to comment.