1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-12 09:34:10 +02:00

Use connection() instead of $connection

This commit is contained in:
Jakub Vrana
2025-03-29 21:16:28 +01:00
parent 168ea5ae6d
commit 712d96b22c
36 changed files with 130 additions and 196 deletions

View File

@@ -170,8 +170,7 @@ if (isset($_GET["mssql"])) {
}
function last_id($result) {
global $connection;
return $connection->lastInsertId();
return connection()->lastInsertId();
}
function explain($connection, $query) {
@@ -323,10 +322,9 @@ if (isset($_GET["mssql"])) {
}
function count_tables($databases) {
global $connection;
$return = array();
foreach ($databases as $db) {
$connection->select_db($db);
connection()->select_db($db);
$return[$db] = get_val("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES");
}
return $return;
@@ -428,8 +426,7 @@ WHERE OBJECT_NAME(i.object_id) = " . q($table), $connection2) as $row
}
function error() {
global $connection;
return nl_br(h(preg_replace('~^(\[[^]]*])+~m', '', $connection->error)));
return nl_br(h(preg_replace('~^(\[[^]]*])+~m', '', connection()->error)));
}
function create_database($db, $collation) {

View File

@@ -8,6 +8,7 @@ if (!defined('Adminer\DRIVER')) {
// MySQLi supports everything, MySQL doesn't support multiple result sets, PDO_MySQL doesn't support orgtable
if (extension_loaded("mysqli") && $_GET["ext"] != "pdo") {
class Db extends \MySQLi {
/** @var Db|string */ static $instance = '';
public string $extension = "MySQLi", $flavor = '';
function __construct() {
@@ -487,8 +488,7 @@ if (!defined('Adminer\DRIVER')) {
* @return Field[]
*/
function fields(string $table): array {
global $connection;
$maria = ($connection->flavor == 'maria');
$maria = (connection()->flavor == 'maria');
$return = array();
foreach (get_rows("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . q($table) . " ORDER BY ORDINAL_POSITION") as $row) {
$field = $row["COLUMN_NAME"];
@@ -616,8 +616,7 @@ if (!defined('Adminer\DRIVER')) {
/** Get escaped error message */
function error(): string {
global $connection;
return h(preg_replace('~^You have an error.*syntax to use~U', "Syntax error", $connection->error));
return h(preg_replace('~^You have an error.*syntax to use~U', "Syntax error", connection()->error));
}
/** Create database
@@ -685,14 +684,13 @@ if (!defined('Adminer\DRIVER')) {
* @return Result|bool
*/
function alter_table(string $table, string $name, array $fields, array $foreign, ?string $comment, string $engine, string $collation, string $auto_increment, string $partitioning) {
global $connection;
$alter = array();
foreach ($fields as $field) {
if ($field[1]) {
$default = $field[1][3];
if (preg_match('~ GENERATED~', $default)) {
// swap default and null
$field[1][3] = ($connection->flavor == 'maria' ? "" : $field[1][2]); // MariaDB doesn't support NULL on virtual columns
$field[1][3] = (connection()->flavor == 'maria' ? "" : $field[1][2]); // MariaDB doesn't support NULL on virtual columns
$field[1][2] = $default;
}
$alter[] = ($table != "" ? ($field[0] != "" ? "CHANGE " . idf_escape($field[0]) : "ADD") : " ") . " " . implode($field[1]) . ($table != "" ? $field[2] : "");
@@ -762,7 +760,6 @@ if (!defined('Adminer\DRIVER')) {
* @param list<string> $views
*/
function move_tables(array $tables, array $views, string $target): bool {
global $connection;
$rename = array();
foreach ($tables as $table) {
$rename[] = table($table) . " TO " . idf_escape($target) . "." . table($table);
@@ -772,7 +769,7 @@ if (!defined('Adminer\DRIVER')) {
foreach ($views as $table) {
$definitions[table($table)] = view($table);
}
$connection->select_db($target);
connection()->select_db($target);
$db = idf_escape(DB);
foreach ($definitions as $name => $view) {
if (!queries("CREATE VIEW $name AS " . str_replace(" $db.", " ", $view["select"])) || !queries("DROP VIEW $db.$name")) {

View File

@@ -154,7 +154,6 @@ if (isset($_GET["oracle"])) {
}
function insertUpdate(string $table, array $rows, array $primary) {
global $connection;
foreach ($rows as $set) {
$update = array();
$where = array();
@@ -165,7 +164,7 @@ if (isset($_GET["oracle"])) {
}
}
if (
!(($where && queries("UPDATE " . table($table) . " SET " . implode(", ", $update) . " WHERE " . implode(" AND ", $where)) && $connection->affected_rows)
!(($where && queries("UPDATE " . table($table) . " SET " . implode(", ", $update) . " WHERE " . implode(" AND ", $where)) && connection()->affected_rows)
|| queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")"))
) {
return false;
@@ -224,9 +223,8 @@ ORDER BY 1"
}
function get_current_db() {
global $connection;
$db = $connection->_current_db ?: DB;
unset($connection->_current_db);
$db = connection()->_current_db ?: DB;
unset(connection()->_current_db);
return $db;
}
@@ -347,8 +345,7 @@ ORDER BY ac.constraint_type, aic.column_position", $connection2) as $row
}
function error() {
global $connection;
return h($connection->error); //! highlight sqltext from offset
return h(connection()->error); //! highlight sqltext from offset
}
function explain($connection, $query) {
@@ -474,9 +471,8 @@ AND c_src.TABLE_NAME = " . q($table);
}
function set_schema($scheme, $connection2 = null) {
global $connection;
if (!$connection2) {
$connection2 = $connection;
$connection2 = connection();
}
return $connection2->query("ALTER SESSION SET CURRENT_SCHEMA = " . idf_escape($scheme));
}

View File

@@ -218,7 +218,6 @@ if (isset($_GET["pgsql"])) {
}
function insertUpdate(string $table, array $rows, array $primary) {
global $connection;
foreach ($rows as $set) {
$update = array();
$where = array();
@@ -229,7 +228,7 @@ if (isset($_GET["pgsql"])) {
}
}
if (
!(($where && queries("UPDATE " . table($table) . " SET " . implode(", ", $update) . " WHERE " . implode(" AND ", $where)) && $connection->affected_rows)
!(($where && queries("UPDATE " . table($table) . " SET " . implode(", ", $update) . " WHERE " . implode(" AND ", $where)) && connection()->affected_rows)
|| queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")"))
) {
return false;
@@ -354,10 +353,9 @@ ORDER BY 1";
}
function count_tables($databases) {
global $connection;
$return = array();
foreach ($databases as $db) {
if ($connection->select_db($db)) {
if (connection()->select_db($db)) {
$return[$db] = count(tables_list());
}
}
@@ -453,9 +451,8 @@ ORDER BY a.attnum") as $row
}
function indexes($table, $connection2 = null) {
global $connection;
if (!is_object($connection2)) {
$connection2 = $connection;
$connection2 = connection();
}
$return = array();
$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);
@@ -521,8 +518,7 @@ ORDER BY conkey, conname") as $row
}
function error() {
global $connection;
$return = h($connection->error);
$return = h(connection()->error);
if (preg_match('~^(.*\n)?([^\n]*)\n( *)\^(\n.*)?$~s', $return, $match)) {
$return = $match[1] . preg_replace('~((?:[^&]|&[^;]*;){' . strlen($match[3]) . '})(.*)~', '\1<b>\2</b>', $match[2]) . $match[4];
}
@@ -534,14 +530,12 @@ ORDER BY conkey, conname") as $row
}
function drop_databases($databases) {
global $connection;
$connection->close();
connection()->close();
return apply_queries("DROP DATABASE", $databases, 'Adminer\idf_escape');
}
function rename_database($name, $collation) {
global $connection;
$connection->close();
connection()->close();
return queries("ALTER DATABASE " . idf_escape(DB) . " RENAME TO " . idf_escape($name));
}
@@ -792,9 +786,8 @@ AND typelem = 0"
}
function set_schema($schema, $connection2 = null) {
global $connection;
if (!$connection2) {
$connection2 = $connection;
$connection2 = connection();
}
$return = $connection2->query("SET search_path TO " . idf_escape($schema));
driver()->setUserTypes(types()); //! get types from current_schemas('t')
@@ -929,9 +922,8 @@ AND typelem = 0"
}
function support($feature) {
global $connection;
return preg_match('~^(check|database|table|columns|sql|indexes|descidx|comment|view|' . (min_version(9.3) ? 'materializedview|' : '') . 'scheme|' . (min_version(11) ? 'procedure|' : '') . 'routine|sequence|trigger|type|variables|drop_col'
. ($connection->flavor == 'cockroach' ? '' : '|processlist') // https://github.com/cockroachdb/cockroach/issues/24745
. (connection()->flavor == 'cockroach' ? '' : '|processlist') // https://github.com/cockroachdb/cockroach/issues/24745
. '|kill|dump)$~', $feature)
;
}

View File

@@ -270,9 +270,8 @@ if (isset($_GET["sqlite"])) {
}
function indexes($table, $connection2 = null) {
global $connection;
if (!is_object($connection2)) {
$connection2 = $connection;
$connection2 = connection();
}
$return = array();
$sql = get_val("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . q($table), 0, $connection2);
@@ -342,25 +341,22 @@ if (isset($_GET["sqlite"])) {
}
function error() {
global $connection;
return h($connection->error);
return h(connection()->error);
}
function check_sqlite_name($name) {
// avoid creating PHP files on unsecured servers
global $connection;
$extensions = "db|sdb|sqlite";
if (!preg_match("~^[^\\0]*\\.($extensions)\$~", $name)) {
$connection->error = lang('Please use one of the extensions %s.', str_replace("|", ", ", $extensions));
connection()->error = lang('Please use one of the extensions %s.', str_replace("|", ", ", $extensions));
return false;
}
return true;
}
function create_database($db, $collation) {
global $connection;
if (file_exists($db)) {
$connection->error = lang('File exists.');
connection()->error = lang('File exists.');
return false;
}
if (!check_sqlite_name($db)) {
@@ -370,7 +366,7 @@ if (isset($_GET["sqlite"])) {
$link = new Db();
$link->attach($db, '', '');
} catch (\Exception $ex) {
$connection->error = $ex->getMessage();
connection()->error = $ex->getMessage();
return false;
}
$link->query('PRAGMA encoding = "UTF-8"');
@@ -380,11 +376,10 @@ if (isset($_GET["sqlite"])) {
}
function drop_databases($databases) {
global $connection;
$connection->attach(":memory:", '', ''); // to unlock file, doesn't work in PDO on Windows
connection()->attach(":memory:", '', ''); // to unlock file, doesn't work in PDO on Windows
foreach ($databases as $db) {
if (!@unlink($db)) {
$connection->error = lang('File exists.');
connection()->error = lang('File exists.');
return false;
}
}
@@ -392,12 +387,11 @@ if (isset($_GET["sqlite"])) {
}
function rename_database($name, $collation) {
global $connection;
if (!check_sqlite_name($name)) {
return false;
}
$connection->attach(":memory:", '', '');
$connection->error = lang('File exists.');
connection()->attach(":memory:", '', '');
connection()->error = lang('File exists.');
return @rename(DB, $name);
}
@@ -406,7 +400,6 @@ if (isset($_GET["sqlite"])) {
}
function alter_table($table, $name, $fields, $foreign, $comment, $engine, $collation, $auto_increment, $partitioning) {
global $connection;
$use_all_fields = ($table == "" || $foreign);
foreach ($fields as $field) {
if ($field[0] != "" || !$field[1] || $field[2]) {
@@ -439,7 +432,7 @@ if (isset($_GET["sqlite"])) {
if ($auto_increment) {
queries("BEGIN");
queries("UPDATE sqlite_sequence SET seq = $auto_increment WHERE name = " . q($name)); // ignores error
if (!$connection->affected_rows) {
if (!connection()->affected_rows) {
queries("INSERT INTO sqlite_sequence (name, seq) VALUES (" . q($name) . ", $auto_increment)");
}
queries("COMMIT");
@@ -533,7 +526,7 @@ if (isset($_GET["sqlite"])) {
}
$temp_name = ($table == $name ? "adminer_$name" : $name);
if (!queries("CREATE TABLE " . table($temp_name) . " (\n" . implode(",\n", $changes) . "\n)")) {
// implicit ROLLBACK to not overwrite $connection->error
// implicit ROLLBACK to not overwrite connection()->error
return false;
}
if ($table != "") {