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

Move connect() to Driver

This commit is contained in:
Jakub Vrana
2025-03-30 07:51:47 +02:00
parent 992561f75e
commit 7ee6f4f7ac
13 changed files with 96 additions and 113 deletions

View File

@@ -213,6 +213,13 @@ if (isset($_GET["mssql"])) {
public array $generated = array("PERSISTED", "VIRTUAL"); public array $generated = array("PERSISTED", "VIRTUAL");
public string $onActions = "NO ACTION|CASCADE|SET NULL|SET DEFAULT"; 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) { function __construct(Db $connection) {
parent::__construct($connection); parent::__construct($connection);
$this->types = array( //! use sys.types $this->types = array( //! use sys.types
@@ -289,14 +296,6 @@ if (isset($_GET["mssql"])) {
return ($_GET["ns"] != "" ? idf_escape($_GET["ns"]) . "." : "") . idf_escape($idf); 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) { function get_databases($flush) {
return get_vals("SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')"); return get_vals("SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')");
} }

View 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 $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"); /** @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) { function __construct(Db $connection) {
parent::__construct($connection); parent::__construct($connection);
$this->types = array( $this->types = array(
@@ -351,26 +366,6 @@ if (!defined('Adminer\DRIVER')) {
return idf_escape($idf); 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 /** Get cached list of databases
* @return list<string> * @return list<string>
*/ */

View File

@@ -188,11 +188,6 @@ if (isset($_GET["oracle"])) {
return idf_escape($idf); return idf_escape($idf);
} }
function connect($credentials) {
$connection = new Db;
return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
}
function get_databases($flush) { function get_databases($flush) {
return get_vals( return get_vals(
"SELECT DISTINCT tablespace_name FROM ( "SELECT DISTINCT tablespace_name FROM (

View File

@@ -171,6 +171,23 @@ if (isset($_GET["pgsql"])) {
public array $functions = array("char_length", "lower", "round", "to_hex", "to_timestamp", "upper"); public array $functions = array("char_length", "lower", "round", "to_hex", "to_timestamp", "upper");
public array $grouping = array("avg", "count", "count distinct", "max", "min", "sum"); 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) { function __construct(Db $connection) {
parent::__construct($connection); parent::__construct($connection);
$this->types = array( //! arrays $this->types = array( //! arrays
@@ -295,24 +312,6 @@ if (isset($_GET["pgsql"])) {
return idf_escape($idf); 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) { function get_databases($flush) {
return get_vals("SELECT datname FROM pg_database return get_vals("SELECT datname FROM pg_database
WHERE datallowconn = TRUE AND has_database_privilege(datname, 'CONNECT') WHERE datallowconn = TRUE AND has_database_privilege(datname, 'CONNECT')

View File

@@ -122,6 +122,13 @@ if (isset($_GET["sqlite"])) {
public array $functions = array("hex", "length", "lower", "round", "unixepoch", "upper"); public array $functions = array("hex", "length", "lower", "round", "unixepoch", "upper");
public array $grouping = array("avg", "count", "count distinct", "group_concat", "max", "min", "sum"); 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) { function __construct(Db $connection) {
parent::__construct($connection); parent::__construct($connection);
if (min_version(3.31, 0, $connection)) { if (min_version(3.31, 0, $connection)) {
@@ -166,15 +173,6 @@ if (isset($_GET["sqlite"])) {
return idf_escape($idf); 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) { function get_databases($flush) {
return array(); return array();
} }

View File

@@ -30,6 +30,14 @@ abstract class SqlDriver {
public string $enumLength = "'(?:''|[^'\\\\]|\\\\.)*'"; // regular expression for parsing enum lengths public string $enumLength = "'(?:''|[^'\\\\]|\\\\.)*'"; // regular expression for parsing enum lengths
/** @var list<string> */ public array $generated = array(); // allowed types of generated columns /** @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 */ /** Create object for performing database operations */
function __construct(Db $connection) { function __construct(Db $connection) {
$this->conn = $connection; $this->conn = $connection;

View File

@@ -24,6 +24,14 @@ function driver(): Driver {
return Driver::$instance; 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 /** Unescape database identifier
* @param string $idf text inside `` * @param string $idf text inside ``
*/ */

View 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 $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"); 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) { function __construct(Db $connection) {
parent::__construct($connection); parent::__construct($connection);
$this->types = array( //! arrays $this->types = array( //! arrays
@@ -224,15 +231,6 @@ if (isset($_GET["clickhouse"])) {
return apply_queries("DROP TABLE", $tables); 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) { function get_databases($flush) {
$result = get_rows('SHOW DATABASES'); $result = get_rows('SHOW DATABASES');

View File

@@ -110,6 +110,16 @@ if (isset($_GET["elastic"])) {
public array $insertFunctions = array("json"); public array $insertFunctions = array("json");
public array $operators = array("=", "must", "should", "must_not"); 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) { function __construct(Db $connection) {
parent::__construct($connection); parent::__construct($connection);
$this->types = array( $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) { function support($feature) {
return preg_match("~table|columns~", $feature); return preg_match("~table|columns~", $feature);
} }

View File

@@ -101,11 +101,6 @@ if (isset($_GET["firebird"])) {
return idf_escape($idf); return idf_escape($idf);
} }
function connect($credentials) {
$connection = new Db;
return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
}
function get_databases($flush) { function get_databases($flush) {
return array("domain"); return array("domain");
} }

View File

@@ -270,11 +270,6 @@ if (isset($_GET["imap"])) {
return connection()->expunge(); return connection()->expunge();
} }
function connect($credentials) {
$connection = new Db;
return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
}
function support($feature) { function support($feature) {
return false; return false;
} }

View File

@@ -323,6 +323,13 @@ if (isset($_GET["mongo"])) {
public $primary = "_id"; 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) { function select(string $table, array $select, array $where, array $group, array $order = array(), $limit = 1, ?int $page = 0, bool $print = false) {
$select = ($select == array("*") $select = ($select == array("*")
? array() ? array()
@@ -430,15 +437,6 @@ if (isset($_GET["mongo"])) {
return $credentials[1]; 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) { function alter_indexes($table, $alter) {
foreach ($alter as $val) { foreach ($alter as $val) {
list($type, $name, $set) = $val; list($type, $name, $set) = $val;

View File

@@ -119,6 +119,16 @@ if (isset($_GET["simpledb"])) {
public $primary = "itemName()"; 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()) { private function chunkRequest($ids, $action, $params, $expand = array()) {
foreach (array_chunk($ids, 25) as $chunk) { foreach (array_chunk($ids, 25) as $chunk) {
$params2 = $params; $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) { function support($feature) {
return preg_match('~sql~', $feature); return preg_match('~sql~', $feature);
} }