]> git.joonet.de Git - adminer.git/commitdiff
Move connect() to Driver
authorJakub Vrana <jakub@vrana.cz>
Sun, 30 Mar 2025 05:51:47 +0000 (07:51 +0200)
committerJakub Vrana <jakub@vrana.cz>
Mon, 31 Mar 2025 08:09:30 +0000 (10:09 +0200)
13 files changed:
adminer/drivers/mssql.inc.php
adminer/drivers/mysql.inc.php
adminer/drivers/oracle.inc.php
adminer/drivers/pgsql.inc.php
adminer/drivers/sqlite.inc.php
adminer/include/driver.inc.php
adminer/include/functions.inc.php
plugins/drivers/clickhouse.php
plugins/drivers/elastic.php
plugins/drivers/firebird.php
plugins/drivers/imap.php
plugins/drivers/mongo.php
plugins/drivers/simpledb.php

index db305a9ca134e55a17b69e1bb3c4f78a88db5b4a..58c1141dfd69ed87b502ca0443e650bcb9737c10 100644 (file)
@@ -213,6 +213,13 @@ if (isset($_GET["mssql"])) {
                public array $generated = array("PERSISTED", "VIRTUAL");
                public string $onActions = "NO ACTION|CASCADE|SET NULL|SET DEFAULT";
 
+               static function connect(?string $server, string $username, string $password) {
+                       if ($server == "") {
+                               $server = "localhost:1433";
+                       }
+                       return parent::connect($server, $username, $password);
+               }
+
                function __construct(Db $connection) {
                        parent::__construct($connection);
                        $this->types = array( //! use sys.types
@@ -289,14 +296,6 @@ if (isset($_GET["mssql"])) {
                return ($_GET["ns"] != "" ? idf_escape($_GET["ns"]) . "." : "") . idf_escape($idf);
        }
 
-       function connect($credentials) {
-               $connection = new Db;
-               if ($credentials[0] == "") {
-                       $credentials[0] = "localhost:1433";
-               }
-               return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
-       }
-
        function get_databases($flush) {
                return get_vals("SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')");
        }
index ece0ee4557eea8f3bc05c4c4a68836b7cd843198..9203a0ae8d137c771fdded46f0bd2e028e9bda4f 100644 (file)
@@ -210,6 +210,21 @@ if (!defined('Adminer\DRIVER')) {
                /** @var list<string> */ public array $functions = array("char_length", "date", "from_unixtime", "lower", "round", "floor", "ceil", "sec_to_time", "time_to_sec", "upper");
                /** @var list<string> */ public array $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum");
 
+               static function connect(?string $server, string $username, string $password) {
+                       $connection = parent::connect($server, $username, $password);
+                       if (is_string($connection)) {
+                               if (function_exists('iconv') && !is_utf8($connection) && strlen($s = iconv("windows-1250", "utf-8", $connection)) > strlen($connection)) { // windows-1250 - most common Windows encoding
+                                       $connection = $s;
+                               }
+                               return $connection;
+                       }
+                       $connection->set_charset(charset($connection));
+                       $connection->query("SET sql_quote_show_create = 1, autocommit = 1");
+                       $connection->flavor = (preg_match('~MariaDB~', $connection->server_info) ? 'maria' : 'mysql');
+                       add_driver(DRIVER, ($connection->flavor == 'maria' ? "MariaDB" : "MySQL"));
+                       return $connection;
+               }
+
                function __construct(Db $connection) {
                        parent::__construct($connection);
                        $this->types = array(
@@ -351,26 +366,6 @@ if (!defined('Adminer\DRIVER')) {
                return idf_escape($idf);
        }
 
-       /** Connect to the database
-       * @param array{?string, string, string} $credentials [$server, $username, $password]
-       * @return string|Db string for error
-       */
-       function connect(array $credentials) {
-               $connection = new Db;
-               $error = $connection->attach($credentials[0], $credentials[1], $credentials[2]);
-               if ($error) {
-                       if (function_exists('iconv') && !is_utf8($error) && strlen($s = iconv("windows-1250", "utf-8", $error)) > strlen($error)) { // windows-1250 - most common Windows encoding
-                               $error = $s;
-                       }
-                       return $error;
-               }
-               $connection->set_charset(charset($connection));
-               $connection->query("SET sql_quote_show_create = 1, autocommit = 1");
-               $connection->flavor = (preg_match('~MariaDB~', $connection->server_info) ? 'maria' : 'mysql');
-               add_driver(DRIVER, ($connection->flavor == 'maria' ? "MariaDB" : "MySQL"));
-               return $connection;
-       }
-
        /** Get cached list of databases
        * @return list<string>
        */
index 78700d007115735c98526590ee8d8204f5455dbb..88bbfb5d0cb48f2664ff6148ae3b21f28b8f847f 100644 (file)
@@ -188,11 +188,6 @@ if (isset($_GET["oracle"])) {
                return idf_escape($idf);
        }
 
-       function connect($credentials) {
-               $connection = new Db;
-               return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
-       }
-
        function get_databases($flush) {
                return get_vals(
                        "SELECT DISTINCT tablespace_name FROM (
index a274d9665f7ebcacc61ca5c4eb25715776385586..bc536d49bcbab26bacfa40cc9f32b70b11ff8c2f 100644 (file)
@@ -171,6 +171,23 @@ if (isset($_GET["pgsql"])) {
                public array $functions = array("char_length", "lower", "round", "to_hex", "to_timestamp", "upper");
                public array $grouping = array("avg", "count", "count distinct", "max", "min", "sum");
 
+               static function connect(?string $server, string $username, string $password) {
+                       $connection = parent::connect($server, $username, $password);
+                       if (is_string($connection)) {
+                               return $connection;
+                       }
+                       if (min_version(9, 0, $connection)) {
+                               $connection->query("SET application_name = 'Adminer'");
+                       }
+                       $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
+                               add_driver(DRIVER, "CockroachDB");
+                       }
+                       return $connection;
+               }
+
                function __construct(Db $connection) {
                        parent::__construct($connection);
                        $this->types = array( //! arrays
@@ -295,24 +312,6 @@ if (isset($_GET["pgsql"])) {
                return idf_escape($idf);
        }
 
-       function connect($credentials) {
-               $connection = new Db;
-               $error = $connection->attach($credentials[0], $credentials[1], $credentials[2]);
-               if ($error) {
-                       return $error;
-               }
-               if (min_version(9, 0, $connection)) {
-                       $connection->query("SET application_name = 'Adminer'");
-               }
-               $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
-                       add_driver(DRIVER, "CockroachDB");
-               }
-               return $connection;
-       }
-
        function get_databases($flush) {
                return get_vals("SELECT datname FROM pg_database
 WHERE datallowconn = TRUE AND has_database_privilege(datname, 'CONNECT')
index dd8e57982e95d8638753ca2219ab23671698005a..5a2ac2982cc6a8ef3b212d83fa4d80ee8ee8fb02 100644 (file)
@@ -122,6 +122,13 @@ if (isset($_GET["sqlite"])) {
                public array $functions = array("hex", "length", "lower", "round", "unixepoch", "upper");
                public array $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum");
 
+               static function connect(?string $server, string $username, string $password) {
+                       if ($password != "") {
+                               return lang('Database does not support password.');
+                       }
+                       return parent::connect(":memory:", "", "");
+               }
+
                function __construct(Db $connection) {
                        parent::__construct($connection);
                        if (min_version(3.31, 0, $connection)) {
@@ -166,15 +173,6 @@ if (isset($_GET["sqlite"])) {
                return idf_escape($idf);
        }
 
-       function connect($credentials) {
-               list(, , $password) = $credentials;
-               if ($password != "") {
-                       return lang('Database does not support password.');
-               }
-               $connection = new Db;
-               return ($connection->attach(":memory:", "", "") ?: $connection);
-       }
-
        function get_databases($flush) {
                return array();
        }
index 59e477fc95aa0d4ca8f65b563a852d2b0fa94876..cc9e10dab5376cb714a634f517c16119f389b23f 100644 (file)
@@ -30,6 +30,14 @@ abstract class SqlDriver {
        public string $enumLength = "'(?:''|[^'\\\\]|\\\\.)*'"; // regular expression for parsing enum lengths
        /** @var list<string> */ public array $generated = array(); // allowed types of generated columns
 
+       /** Connect to the database
+       * @return Db|string string for error
+       */
+       static function connect(?string $server, string $username, string $password) {
+               $connection = new Db;
+               return ($connection->attach($server, $username, $password) ?: $connection);
+       }
+
        /** Create object for performing database operations */
        function __construct(Db $connection) {
                $this->conn = $connection;
index 406b215c8579e620cbfaaf0f3fe9ad0b9740a801..29ac368aaba8153b505330b3dafa64b310fa6e9d 100644 (file)
@@ -24,6 +24,14 @@ function driver(): Driver {
        return Driver::$instance;
 }
 
+/** Connect to the database
+* @param array{?string, string, string} $credentials [$server, $username, $password]
+* @return Db|string string for error
+*/
+function connect(array $credentials) {
+       return driver()->connect($credentials[0], $credentials[1], $credentials[2]);
+}
+
 /** Unescape database identifier
 * @param string $idf text inside ``
 */
index 8a1adbcd00085e384e660c1e0f0f879bbafd8518..ed760560283cd6612fd95fdc0051d6e61fb0f35d 100644 (file)
@@ -123,6 +123,13 @@ if (isset($_GET["clickhouse"])) {
                public array $operators = array("=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL", "SQL");
                public array $grouping = array("avg", "count", "count distinct", "max", "min", "sum");
 
+               static function connect(?string $server, string $username, string $password) {
+                       if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) {
+                               return lang('Invalid server.');
+                       }
+                       return parent::connect($server, $username, $password);
+               }
+
                function __construct(Db $connection) {
                        parent::__construct($connection);
                        $this->types = array( //! arrays
@@ -224,15 +231,6 @@ if (isset($_GET["clickhouse"])) {
                return apply_queries("DROP TABLE", $tables);
        }
 
-       function connect($credentials) {
-               $connection = new Db;
-               list($server, $username, $password) = $credentials;
-               if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) {
-                       return lang('Invalid server.');
-               }
-               return ($connection->attach($server, $username, $password) ?: $connection);
-       }
-
        function get_databases($flush) {
                $result = get_rows('SHOW DATABASES');
 
index bd224d788658d134c922707aeb0420943feaa5f4..c7db74ef5544bf1b52d61084628cf7f63ad4c64e 100644 (file)
@@ -110,6 +110,16 @@ if (isset($_GET["elastic"])) {
                public array $insertFunctions = array("json");
                public array $operators = array("=", "must", "should", "must_not");
 
+               static function connect(?string $server, string $username, string $password) {
+                       if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) {
+                               return lang('Invalid server.');
+                       }
+                       if ($password != "" && is_object(parent::connect($server, $username, ""))) {
+                               return lang('Database does not support password.');
+                       }
+                       return parent::connect($server, $username, $password);
+               }
+
                function __construct(Db $connection) {
                        parent::__construct($connection);
                        $this->types = array(
@@ -283,20 +293,6 @@ if (isset($_GET["elastic"])) {
                }
        }
 
-       function connect($credentials) {
-               $connection = new Db;
-
-               list($server, $username, $password) = $credentials;
-               if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) {
-                       return lang('Invalid server.');
-               }
-               if ($password != "" && $connection->attach($server, $username, "")) {
-                       return lang('Database does not support password.');
-               }
-
-               return ($connection->attach($server, $username, $password) ?: $connection);
-       }
-
        function support($feature) {
                return preg_match("~table|columns~", $feature);
        }
index 663360037f1b2ef65d2eeb88c25735c57c3af765..c4a6ca6572b656efc98d65d04d9f03f73c9951a3 100644 (file)
@@ -101,11 +101,6 @@ if (isset($_GET["firebird"])) {
                return idf_escape($idf);
        }
 
-       function connect($credentials) {
-               $connection = new Db;
-               return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
-       }
-
        function get_databases($flush) {
                return array("domain");
        }
index e43cfd532c7014509ec28f7762cefda02cc961f9..31b18fbfef81a7b76bce3cf8d4d4c1323fc2b2e6 100644 (file)
@@ -270,11 +270,6 @@ if (isset($_GET["imap"])) {
                return connection()->expunge();
        }
 
-       function connect($credentials) {
-               $connection = new Db;
-               return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
-       }
-
        function support($feature) {
                return false;
        }
index a78e816595271603ba3700106ae754f321051cd1..5a77ed1187913273b50190e01b67d828d124f9ea 100644 (file)
@@ -323,6 +323,13 @@ if (isset($_GET["mongo"])) {
 
                public $primary = "_id";
 
+               static function connect(?string $server, string $username, string $password) {
+                       if ($server == "") {
+                               $server = "localhost:27017";
+                       }
+                       return parent::connect($server, $username, $password);
+               }
+
                function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) {
                        $select = ($select == array("*")
                                ? array()
@@ -430,15 +437,6 @@ if (isset($_GET["mongo"])) {
                return $credentials[1];
        }
 
-       function connect($credentials) {
-               $connection = new Db;
-               list($server, $username, $password) = $credentials;
-               if ($server == "") {
-                       $server = "localhost:27017";
-               }
-               return ($connection->attach($server, $username, $password) ?: $connection);
-       }
-
        function alter_indexes($table, $alter) {
                foreach ($alter as $val) {
                        list($type, $name, $set) = $val;
index f3f593b2374395eb7a2270b9d2bcb74eb3c16e13..4bcc0f1289df60d18705b1dca03d8de3f4c1e56a 100644 (file)
@@ -119,6 +119,16 @@ if (isset($_GET["simpledb"])) {
 
                public $primary = "itemName()";
 
+               static function connect(?string $server, string $username, string $password) {
+                       if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) {
+                               return lang('Invalid server.');
+                       }
+                       if ($password != "") {
+                               return lang('Database does not support password.');
+                       }
+                       return parent::connect($server, $username, $password);
+               }
+
                private function chunkRequest($ids, $action, $params, $expand = array()) {
                        foreach (array_chunk($ids, 25) as $chunk) {
                                $params2 = $params;
@@ -242,17 +252,6 @@ if (isset($_GET["simpledb"])) {
 
 
 
-       function connect($credentials) {
-               list($host, , $password) = $credentials;
-               if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $host)) {
-                       return lang('Invalid server.');
-               }
-               if ($password != "") {
-                       return lang('Database does not support password.');
-               }
-               return new Db;
-       }
-
        function support($feature) {
                return preg_match('~sql~', $feature);
        }