Separate $where in limit function

This commit is contained in:
Jakub Vrana 2010-05-14 15:51:54 +02:00
parent 8b2808ab99
commit ee3e04590a
10 changed files with 45 additions and 40 deletions

View file

@ -119,7 +119,7 @@ if ($_POST) {
}
if (support("partitioning")) {
$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();
$row["partition_names"] = array();
$row["partition_values"] = array();

View file

@ -2,5 +2,5 @@
$TABLE = $_GET["download"];
header("Content-Type: application/octet-stream");
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

View file

@ -44,7 +44,6 @@ if (isset($_GET["mssql"])) {
return $this->query("USE $database");
}
function query($query, $unbuffered = false) {
$result = sqlsrv_query($this->_link, $query); //! , array(), ($unbuffered ? array() : array("Scrollable" => "keyset"))
if (!$result) {
@ -255,12 +254,12 @@ if (isset($_GET["mssql"])) {
return get_vals("EXEC sp_databases");
}
function limit($query, $limit, $offset = 0, $separator = " ") {
return (isset($limit) ? " TOP (" . ($limit + $offset) . ")" : "") . " $query"; // seek later
function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return (isset($limit) ? " TOP (" . ($limit + $offset) . ")" : "") . " $query$where"; // seek later
}
function limit1($query) {
return limit($query, 1);
function limit1($query, $where) {
return limit($query, $where, 1);
}
function db_collation($db, $collations) {

View file

@ -266,21 +266,22 @@ if (!defined("DRIVER")) {
/** Formulate SQL query with limit
* @param string everything after SELECT
* @param string including WHERE
* @param int
* @param int
* @param string
* @return string
*/
function limit($query, $limit, $offset = 0, $separator = " ") {
return " $query" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
}
/** Formulate SQL modification query with limit 1
* @param string everything after UPDATE or DELETE
* @return string
*/
function limit1($query) {
return limit($query, 1);
function limit1($query, $where) {
return limit($query, $where, 1);
}
/** Get database collation

View file

@ -45,11 +45,11 @@ if (isset($_GET["pgsql"])) {
if ($database == DB) {
return $this->_database;
}
$link = @pg_connect($this->_connection . " dbname='" . addcslashes($database, "'\\") . "'", PGSQL_CONNECT_FORCE_NEW);
if ($link) {
$this->_link = $link;
$return = @pg_connect($this->_connection . " dbname='" . addcslashes($database, "'\\") . "'", PGSQL_CONNECT_FORCE_NEW);
if ($return) {
$this->_link = $return;
}
return $link;
return $return;
}
function close() {
@ -108,15 +108,15 @@ if (isset($_GET["pgsql"])) {
function fetch_field() {
$column = $this->_offset++;
$row = new stdClass;
$return = new stdClass;
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);
$row->orgname = $row->name;
$row->type = pg_field_type($this->_result, $column);
$row->charsetnr = ($row->type == "bytea" ? 63 : 0); // 63 - binary
return $row;
$return->name = pg_field_name($this->_result, $column);
$return->orgname = $return->name;
$return->type = pg_field_type($this->_result, $column);
$return->charsetnr = ($return->type == "bytea" ? 63 : 0); // 63 - binary
return $return;
}
function __destruct() {
@ -167,12 +167,12 @@ if (isset($_GET["pgsql"])) {
return get_vals("SELECT datname FROM pg_database");
}
function limit($query, $limit, $offset = 0, $separator = " ") {
return " $query" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
}
function limit1($query) {
return " $query";
function limit1($query, $where) {
return " $query$where";
}
function db_collation($db, $collations) {

View file

@ -215,13 +215,13 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
return array();
}
function limit($query, $limit, $offset = 0, $separator = " ") {
return " $query" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . (isset($limit) ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
}
function limit1($query) {
function limit1($query, $where) {
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) {

View file

@ -16,7 +16,7 @@ if ($_POST && !$error && !isset($_GET["select"])) {
$location = ME . "select=" . urlencode($TABLE);
}
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 {
$set = array();
foreach ($fields as $name => $field) {
@ -29,7 +29,7 @@ if ($_POST && !$error && !isset($_GET["select"])) {
if (!$set) {
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 {
$result = insert_into($TABLE, $set);
$last_id = ($result ? last_id() : 0);
@ -58,7 +58,7 @@ if ($_POST["save"]) {
}
$row = array();
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();
if (isset($_GET["select"]) && $result->fetch_assoc()) {
$row = null;

View file

@ -609,7 +609,7 @@ function search_tables() {
foreach (table_status() as $table => $table_status) {
$name = $adminer->tableName($table_status);
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 (!$found) {
echo "<ul>\n";

View file

@ -23,7 +23,7 @@ list($select, $group) = $adminer->selectColumnsProcess($columns, $indexes);
$where = $adminer->selectSearchProcess($fields, $indexes);
$order = $adminer->selectOrderProcess($fields, $indexes);
$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) : "");
if ($_POST && !$error) {
@ -49,12 +49,16 @@ if ($_POST && !$error) {
dump_csv($row);
}
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 {
$union = array();
foreach ($_POST["check"] as $val) {
// 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));
}
@ -95,7 +99,7 @@ if ($_POST && !$error) {
} else {
foreach ((array) $_POST["check"] as $val) {
// 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) {
break;
}
@ -117,7 +121,7 @@ if ($_POST && !$error) {
$key = bracket_escape($key, 1); // 1 - back
$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) {
break;
}
@ -202,7 +206,7 @@ if (!$columns) {
$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);
$result = $connection->query($query);

View file

@ -40,6 +40,7 @@ Dollar terminated string in SQL command
bool in Editor
MS SQL:
Non UTF-8 character sets
Rename by sp_rename
Detection of table collation
PDO driver with seek