From 7a19fa67fd974b500335e1e79afc0ae95a62ea4d Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Fri, 28 Mar 2025 15:11:12 +0100 Subject: [PATCH] Integrate Db::result in get_val --- adminer/drivers/mysql.inc.php | 2 +- adminer/drivers/pgsql.inc.php | 6 +++--- adminer/drivers/sqlite.inc.php | 4 ++-- adminer/include/db.inc.php | 12 ------------ adminer/include/functions.inc.php | 14 ++++++++++---- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 4925f700..50fcaa34 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -332,7 +332,7 @@ if (!defined('Adminer\DRIVER')) { function hasCStyleEscapes(): bool { static $c_style; if ($c_style === null) { - $sql_mode = $this->conn->result("SHOW VARIABLES LIKE 'sql_mode'", 1); + $sql_mode = get_val("SHOW VARIABLES LIKE 'sql_mode'", 1, $this->conn); $c_style = (strpos($sql_mode, 'NO_BACKSLASH_ESCAPES') === false); } return $c_style; diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index 32213ffc..a6010ca1 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -285,7 +285,7 @@ if (isset($_GET["pgsql"])) { function hasCStyleEscapes(): bool { static $c_style; if ($c_style === null) { - $c_style = ($this->conn->result("SHOW standard_conforming_strings") == "off"); + $c_style = (get_val("SHOW standard_conforming_strings", 0, $this->conn) == "off"); } return $c_style; } @@ -308,7 +308,7 @@ if (isset($_GET["pgsql"])) { if (min_version(9, 0, $connection)) { $connection->query("SET application_name = 'Adminer'"); } - $version = $connection->result("SELECT version()"); + $version = get_val("SELECT version()", 0, $connection); $connection->flavor = (preg_match('~CockroachDB~', $version) ? 'cockroach' : ''); $connection->server_info = preg_replace('~^\D*([\d.]+[-\w]*).*~', '\1', $version); if ($connection->flavor == 'cockroach') { // we don't use "PostgreSQL / CockroachDB" by default because it's too long @@ -463,7 +463,7 @@ ORDER BY a.attnum") as $row $connection2 = $connection; } $return = array(); - $table_oid = $connection2->result("SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema()) AND relname = " . q($table)); + $table_oid = get_val("SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema()) AND relname = " . q($table), 0, $connection2); $columns = get_key_vals("SELECT attnum, attname FROM pg_attribute WHERE attrelid = $table_oid AND attnum > 0", $connection2); foreach ( get_rows("SELECT relname, indisunique::int, indisprimary::int, indkey, indoption, (indpred IS NOT NULL)::int as indispartial diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index ac23dce1..04518a50 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -151,7 +151,7 @@ if (isset($_GET["sqlite"])) { } function checkConstraints(string $table): array { - preg_match_all('~ CHECK *(\( *(((?>[^()]*[^() ])|(?1))*) *\))~', $this->conn->result("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . q($table)), $matches); //! could be inside a comment + preg_match_all('~ CHECK *(\( *(((?>[^()]*[^() ])|(?1))*) *\))~', get_val("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . q($table), 0, $this->conn), $matches); //! could be inside a comment return array_combine($matches[2], $matches[2]); } } @@ -276,7 +276,7 @@ if (isset($_GET["sqlite"])) { $connection2 = $connection; } $return = array(); - $sql = $connection2->result("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . q($table)); + $sql = get_val("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . q($table), 0, $connection2); if (preg_match('~\bPRIMARY\s+KEY\s*\((([^)"]+|"[^"]*"|`[^`]*`)++)~i', $sql, $match)) { $return[""] = array("type" => "PRIMARY", "columns" => array(), "lengths" => array(), "descs" => array()); preg_match_all('~((("[^"]*+")+|(?:`[^`]*+`)+)|(\S+))(\s+(ASC|DESC))?(,\s*|$)~i', $match[1], $matches, PREG_SET_ORDER); diff --git a/adminer/include/db.inc.php b/adminer/include/db.inc.php index 1a609b7f..bd352e2a 100644 --- a/adminer/include/db.inc.php +++ b/adminer/include/db.inc.php @@ -47,16 +47,4 @@ abstract class SqlDb { function next_result(): bool { return false; } - - /** Get single field from result - * @return string|bool - */ - function result(string $query, int $field = 0) { - $result = $this->query($query); - if (!is_object($result)) { - return false; - } - $row = $result->fetch_row(); - return ($row ? $row[$field] : false); - } } diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 1ea224f1..a38f28ba 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -164,11 +164,17 @@ function get_password() { } /** Get single value from database -* @return string or false if error +* @return string|false false if error */ -function get_val(string $query, int $field = 0): string { +function get_val(string $query, int $field = 0, ?Db $conn = null) { global $connection; - return $connection->result($query, $field); + $conn = $conn ?: $connection; + $result = $conn->query($query); + if (!is_object($result)) { + return false; + } + $row = $result->fetch_row(); + return ($row ? $row[$field] : false); } /** Get list of values from database @@ -855,7 +861,7 @@ function slow_query(string $query): array { if (!$slow_query && support("kill")) { $connection2 = connect($adminer->credentials()); if (is_object($connection2) && ($db == "" || $connection2->select_db($db))) { - $kill = $connection2->result(connection_id()); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL + $kill = get_val(connection_id(), 0, $connection2); // MySQL and MySQLi can use thread_id but it's not in PDO_MySQL echo script("const timeout = setTimeout(() => { ajax('" . js_escape(ME) . "script=kill', function () {}, 'kill=$kill&token=$token'); }, 1000 * $timeout);"); } }