1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-08 07:36:44 +02:00

PHPStan: Use int for $limit

This commit is contained in:
Jakub Vrana
2025-03-30 21:08:19 +02:00
parent 016c1b2357
commit b50d19629f
14 changed files with 25 additions and 28 deletions

View File

@@ -301,7 +301,7 @@ if (isset($_GET["mssql"])) {
} }
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return ($limit !== null ? " TOP (" . ($limit + $offset) . ")" : "") . " $query$where"; // seek later return ($limit ? " TOP (" . ($limit + $offset) . ")" : "") . " $query$where"; // seek later
} }
function limit1($table, $query, $where, $separator = "\n") { function limit1($table, $query, $where, $separator = "\n") {

View File

@@ -387,7 +387,7 @@ if (!defined('Adminer\DRIVER')) {
* @param string $where including WHERE * @param string $where including WHERE
*/ */
function limit(string $query, string $where, int $limit, int $offset = 0, string $separator = " "): string { function limit(string $query, string $where, int $limit, int $offset = 0, string $separator = " "): string {
return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : ""); return " $query$where" . ($limit ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
} }
/** Formulate SQL modification query with limit 1 /** Formulate SQL modification query with limit 1

View File

@@ -200,7 +200,7 @@ ORDER BY 1"
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return ($offset ? " * FROM (SELECT t.*, rownum AS rnum FROM (SELECT $query$where) t WHERE rownum <= " . ($limit + $offset) . ") WHERE rnum > $offset" return ($offset ? " * FROM (SELECT t.*, rownum AS rnum FROM (SELECT $query$where) t WHERE rownum <= " . ($limit + $offset) . ") WHERE rnum > $offset"
: ($limit !== null ? " * FROM (SELECT $query$where) WHERE rownum <= " . ($limit + $offset) : ($limit ? " * FROM (SELECT $query$where) WHERE rownum <= " . ($limit + $offset)
: " $query$where" : " $query$where"
)); ));
} }

View File

@@ -319,7 +319,7 @@ ORDER BY datname");
} }
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : ""); return " $query$where" . ($limit ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
} }
function limit1($table, $query, $where, $separator = "\n") { function limit1($table, $query, $where, $separator = "\n") {

View File

@@ -178,7 +178,7 @@ if (isset($_GET["sqlite"])) {
} }
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : ""); return " $query$where" . ($limit ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
} }
function limit1($table, $query, $where, $separator = "\n") { function limit1($table, $query, $where, $separator = "\n") {

View File

@@ -431,7 +431,7 @@ class Adminer {
/** Print limit box in select */ /** Print limit box in select */
function selectLimitPrint(int $limit): void { function selectLimitPrint(int $limit): void {
echo "<fieldset><legend>" . lang('Limit') . "</legend><div>"; // <div> for easy styling echo "<fieldset><legend>" . lang('Limit') . "</legend><div>"; // <div> for easy styling
echo "<input type='number' name='limit' class='size' value='" . h($limit) . "'>"; echo "<input type='number' name='limit' class='size' value='" . intval($limit) . "'>";
echo script("qsl('input').oninput = selectFieldChange;", ""); echo script("qsl('input').oninput = selectFieldChange;", "");
echo "</div></fieldset>\n"; echo "</div></fieldset>\n";
} }
@@ -613,7 +613,7 @@ class Adminer {
* @param int $page index of page starting at zero * @param int $page index of page starting at zero
* @return string empty string to use default query * @return string empty string to use default query
*/ */
function selectQueryBuild(array $select, array $where, array $group, array $order, ?int $limit, ?int $page): string { function selectQueryBuild(array $select, array $where, array $group, array $order, int $limit, ?int $page): string {
return ""; return "";
} }

View File

@@ -76,19 +76,19 @@ abstract class SqlDriver {
* @param list<string> $where result of adminer()->selectSearchProcess() * @param list<string> $where result of adminer()->selectSearchProcess()
* @param list<string> $group result of adminer()->selectColumnsProcess()[1] * @param list<string> $group result of adminer()->selectColumnsProcess()[1]
* @param list<string> $order result of adminer()->selectOrderProcess() * @param list<string> $order result of adminer()->selectOrderProcess()
* @param int|numeric-string $limit result of adminer()->selectLimitProcess() * @param int $limit result of adminer()->selectLimitProcess()
* @param int $page index of page starting at zero * @param int $page index of page starting at zero
* @param bool $print whether to print the query * @param bool $print whether to print the query
* @return Result|false * @return Result|false
*/ */
function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) { function select(string $table, array $select, array $where, array $group, array $order = array(), int $limit = 1, ?int $page = 0, bool $print = false) {
$is_group = (count($group) < count($select)); $is_group = (count($group) < count($select));
$query = adminer()->selectQueryBuild($select, $where, $group, $order, $limit, $page); $query = adminer()->selectQueryBuild($select, $where, $group, $order, $limit, $page);
if (!$query) { if (!$query) {
$query = "SELECT" . limit( $query = "SELECT" . limit(
($_GET["page"] != "last" && $limit != "" && $group && $is_group && JUSH == "sql" ? "SQL_CALC_FOUND_ROWS " : "") . implode(", ", $select) . "\nFROM " . table($table), ($_GET["page"] != "last" && $limit && $group && $is_group && JUSH == "sql" ? "SQL_CALC_FOUND_ROWS " : "") . implode(", ", $select) . "\nFROM " . table($table),
($where ? "\nWHERE " . implode(" AND ", $where) : "") . ($group && $is_group ? "\nGROUP BY " . implode(", ", $group) : "") . ($order ? "\nORDER BY " . implode(", ", $order) : ""), ($where ? "\nWHERE " . implode(" AND ", $where) : "") . ($group && $is_group ? "\nGROUP BY " . implode(", ", $group) : "") . ($order ? "\nORDER BY " . implode(", ", $order) : ""),
($limit != "" ? +$limit : null), $limit,
($page ? $limit * $page : 0), ($page ? $limit * $page : 0),
"\n" "\n"
); );

View File

@@ -273,7 +273,7 @@ if (!$columns && support("table")) {
$page = $_GET["page"]; $page = $_GET["page"];
if ($page == "last") { if ($page == "last") {
$found_rows = get_val(count_rows($TABLE, $where, $is_group, $group)); $found_rows = get_val(count_rows($TABLE, $where, $is_group, $group));
$page = floor(max(0, intval($found_rows) - 1) / intval($limit)); $page = floor(max(0, intval($found_rows) - 1) / $limit);
} }
$select2 = $select; $select2 = $select;
@@ -318,7 +318,7 @@ if (!$columns && support("table")) {
} }
// use count($rows) without LIMIT, COUNT(*) without grouping, FOUND_ROWS otherwise (slowest) // use count($rows) without LIMIT, COUNT(*) without grouping, FOUND_ROWS otherwise (slowest)
if ($_GET["page"] != "last" && $limit != "" && $group && $is_group && JUSH == "sql") { if ($_GET["page"] != "last" && $limit && $group && $is_group && JUSH == "sql") {
$found_rows = get_val(" SELECT FOUND_ROWS()"); // space to allow mysql.trace_mode $found_rows = get_val(" SELECT FOUND_ROWS()"); // space to allow mysql.trace_mode
} }
@@ -488,11 +488,11 @@ if (!$columns && support("table")) {
$exact_count = true; $exact_count = true;
$found_rows = null; $found_rows = null;
if ($_GET["page"] != "last") { if ($_GET["page"] != "last") {
if ($limit == "" || (count($rows) < $limit && ($rows || !$page))) { if (!$limit || (count($rows) < $limit && ($rows || !$page))) {
$found_rows = ($page ? $page * $limit : 0) + count($rows); $found_rows = ($page ? $page * $limit : 0) + count($rows);
} elseif (JUSH != "sql" || !$is_group) { } elseif (JUSH != "sql" || !$is_group) {
$found_rows = ($is_group ? false : found_rows($table_status, $where)); $found_rows = ($is_group ? false : found_rows($table_status, $where));
if (intval($found_rows) < max(1e4, 2 * ($page + 1) * intval($limit))) { if (intval($found_rows) < max(1e4, 2 * ($page + 1) * $limit)) {
// slow with big tables // slow with big tables
$found_rows = first(slow_query(count_rows($TABLE, $where, $is_group, $group))); $found_rows = first(slow_query(count_rows($TABLE, $where, $is_group, $group)));
} else { } else {
@@ -501,11 +501,11 @@ if (!$columns && support("table")) {
} }
} }
$pagination = ($limit != "" && ($found_rows === false || $found_rows > $limit || $page)); $pagination = ($limit && ($found_rows === false || $found_rows > $limit || $page));
if ($pagination) { if ($pagination) {
echo (($found_rows === false ? count($rows) + 1 : $found_rows - $page * $limit) > $limit echo (($found_rows === false ? count($rows) + 1 : $found_rows - $page * $limit) > $limit
? '<p><a href="' . h(remove_from_uri("page") . "&page=" . ($page + 1)) . '" class="loadmore">' . lang('Load more data') . '</a>' ? '<p><a href="' . h(remove_from_uri("page") . "&page=" . ($page + 1)) . '" class="loadmore">' . lang('Load more data') . '</a>'
. script("qsl('a').onclick = partial(selectLoadMore, " . intval($limit) . ", '" . lang('Loading') . "…');", "") . script("qsl('a').onclick = partial(selectLoadMore, $limit, '" . lang('Loading') . "…');", "")
: '' : ''
); );
echo "\n"; echo "\n";
@@ -516,7 +516,7 @@ if (!$columns && support("table")) {
// display first, previous 4, next 4 and last page // display first, previous 4, next 4 and last page
$max_page = ($found_rows === false $max_page = ($found_rows === false
? $page + (count($rows) >= $limit ? 2 : 1) ? $page + (count($rows) >= $limit ? 2 : 1)
: floor(($found_rows - 1) / intval($limit)) : floor(($found_rows - 1) / $limit)
); );
echo "<fieldset>"; echo "<fieldset>";
if (JUSH != "simpledb") { if (JUSH != "simpledb") {

View File

@@ -243,7 +243,7 @@ if (isset($_GET["clickhouse"])) {
} }
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" . ($offset ? ", $offset" : "") : ""); return " $query$where" . ($limit ? $separator . "LIMIT $limit" . ($offset ? ", $offset" : "") : "");
} }
function limit1($table, $query, $where, $separator = "\n") { function limit1($table, $query, $where, $separator = "\n") {

View File

@@ -146,7 +146,7 @@ if (isset($_GET["elastic"])) {
} }
if ($limit) { if ($limit) {
$data["size"] = +$limit; $data["size"] = $limit;
if ($page) { if ($page) {
$data["from"] = ($page * $limit); $data["from"] = ($page * $limit);
} }
@@ -308,7 +308,7 @@ if (isset($_GET["elastic"])) {
} }
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : ""); return " $query$where" . ($limit ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
} }
function collations() { function collations() {

View File

@@ -107,7 +107,7 @@ if (isset($_GET["firebird"])) {
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
$return = ''; $return = '';
$return .= ($limit !== null ? $separator . "FIRST $limit" . ($offset ? " SKIP $offset" : "") : ""); $return .= ($limit ? $separator . "FIRST $limit" . ($offset ? " SKIP $offset" : "") : "");
$return .= " $query$where"; $return .= " $query$where";
return $return; return $return;
} }

View File

@@ -208,7 +208,7 @@ if (isset($_GET["imap"])) {
} }
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : ""); return " $query$where" . ($limit ? $separator . "LIMIT $limit" . ($offset ? " OFFSET $offset" : "") : "");
} }
function idf_escape($idf) { function idf_escape($idf) {

View File

@@ -344,10 +344,7 @@ if (isset($_GET["mongo"])) {
$val = preg_replace('~ DESC$~', '', $val, 1, $count); $val = preg_replace('~ DESC$~', '', $val, 1, $count);
$sort[$val] = ($count ? -1 : 1); $sort[$val] = ($count ? -1 : 1);
} }
if (isset($_GET['limit']) && is_numeric($_GET['limit']) && $_GET['limit'] > 0) { $limit = min(200, max(1, $limit));
$limit = $_GET['limit'];
}
$limit = min(200, max(1, (int) $limit));
$skip = $page * $limit; $skip = $page * $limit;
try { try {
return new Result($this->conn->_link->executeQuery($this->conn->_db_name . ".$table", new \MongoDB\Driver\Query($where, array('projection' => $select, 'limit' => $limit, 'skip' => $skip, 'sort' => $sort)))); return new Result($this->conn->_link->executeQuery($this->conn->_db_name . ".$table", new \MongoDB\Driver\Query($where, array('projection' => $select, 'limit' => $limit, 'skip' => $skip, 'sort' => $sort))));

View File

@@ -340,7 +340,7 @@ if (isset($_GET["simpledb"])) {
} }
function limit($query, $where, $limit, $offset = 0, $separator = " ") { function limit($query, $where, $limit, $offset = 0, $separator = " ") {
return " $query$where" . ($limit !== null ? $separator . "LIMIT $limit" : ""); return " $query$where" . ($limit ? $separator . "LIMIT $limit" : "");
} }
function unconvert_field($field, $return) { function unconvert_field($field, $return) {