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

Db: Unify connection error handling

This commit is contained in:
Jakub Vrana
2025-03-28 22:00:53 +01:00
parent d5bba383ea
commit 29339c5223
13 changed files with 95 additions and 123 deletions

View File

@@ -56,11 +56,11 @@ if (isset($_GET["clickhouse"])) {
return $this->rootQuery($this->_db, $query);
}
function connect(string $server, string $username, string $password): bool {
function attach(?string $server, string $username, string $password): string {
preg_match('~^(https?://)?(.*)~', $server, $match);
$this->url = ($match[1] ?: "http://") . urlencode($username) . ":" . urlencode($password) . "@$match[2]";
$return = $this->query('SELECT 1');
return (bool) $return;
return ($return ? '' : $this->error);
}
function select_db(string $database): bool {
@@ -230,10 +230,7 @@ if (isset($_GET["clickhouse"])) {
if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) {
return lang('Invalid server.');
}
if ($connection->connect($server, $username, $password)) {
return $connection;
}
return $connection->error;
return ($connection->attach($server, $username, $password) ?: $connection);
}
function get_databases($flush) {

View File

@@ -61,14 +61,14 @@ if (isset($_GET["elastic"])) {
}
}
function connect(string $server, string $username, string $password): bool {
function attach(?string $server, string $username, string $password): string {
preg_match('~^(https?://)?(.*)~', $server, $match);
$this->url = ($match[1] ?: "http://") . urlencode($username) . ":" . urlencode($password) . "@$match[2]";
$return = $this->rootQuery('');
if ($return) {
$this->server_info = $return['version']['number'];
}
return (bool) $return;
return ($return ? '' : $this->error);
}
function select_db(string $database): bool {
@@ -290,15 +290,11 @@ if (isset($_GET["elastic"])) {
if (!preg_match('~^(https?://)?[-a-z\d.]+(:\d+)?$~', $server)) {
return lang('Invalid server.');
}
if ($password != "" && $connection->connect($server, $username, "")) {
if ($password != "" && $connection->attach($server, $username, "")) {
return lang('Database does not support password.');
}
if ($connection->connect($server, $username, $password)) {
return $connection;
}
return $connection->error;
return ($connection->attach($server, $username, $password) ?: $connection);
}
function support($feature) {

View File

@@ -14,17 +14,15 @@ if (isset($_GET["firebird"])) {
class Db extends SqlDb {
public string $extension = "Firebird", $_link;
function connect(string $server, string $username, string $password): bool {
function attach(?string $server, string $username, string $password): string {
$this->_link = ibase_connect($server, $username, $password);
if ($this->_link) {
$url_parts = explode(':', $server);
$service_link = ibase_service_attach($url_parts[0], $username, $password);
$this->server_info = ibase_server_info($service_link, IBASE_SVC_SERVER_VERSION);
} else {
$this->errno = ibase_errcode();
$this->error = ibase_errmsg();
return '';
}
return (bool) $this->_link;
return ibase_errmsg();
}
function quote(string $string): string {
@@ -105,10 +103,7 @@ if (isset($_GET["firebird"])) {
function connect($credentials) {
$connection = new Db;
if ($connection->connect($credentials[0], $credentials[1], $credentials[2])) {
return $connection;
}
return $connection->error;
return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
}
function get_databases($flush) {

View File

@@ -24,13 +24,10 @@ if (isset($_GET["imap"])) {
private $mailbox;
private $imap;
function connect(string $server, string $username, string $password): bool {
function attach(?string $server, string $username, string $password): string {
$this->mailbox = "{" . "$server:993/ssl}"; // Adminer disallows specifying privileged port in server name
$this->imap = @imap_open($this->mailbox, $username, $password, OP_HALFOPEN, 1);
if (!$this->imap) {
$this->error = imap_last_error();
}
return $this->imap;
return ($this->imap ? '' : imap_last_error());
}
function select_db(string $database): bool {
@@ -275,10 +272,7 @@ if (isset($_GET["imap"])) {
function connect($credentials) {
$connection = new Db;
if ($connection->connect($credentials[0], $credentials[1], $credentials[2])) {
return $connection;
}
return $connection->error;
return ($connection->attach($credentials[0], $credentials[1], $credentials[2]) ?: $connection);
}
function support($feature) {

View File

@@ -12,7 +12,7 @@ if (isset($_GET["mongo"])) {
public \MongoDB\Driver\Manager $_link;
public $_db, $_db_name;
function connect(string $server, string $username, string $password): bool {
function attach(?string $server, string $username, string $password): string {
$options = array();
if ($username . $password != "") {
$options["username"] = $username;
@@ -27,6 +27,7 @@ if (isset($_GET["mongo"])) {
}
$this->_link = new \MongoDB\Driver\Manager("mongodb://$server", $options);
$this->executeDbCommand($options["db"], array('ping' => 1));
return '';
}
function executeCommand($command) {
@@ -435,11 +436,7 @@ if (isset($_GET["mongo"])) {
if ($server == "") {
$server = "localhost:27017";
}
$connection->connect($server, $username, $password);
if ($connection->error) {
return $connection->error;
}
return $connection;
return ($connection->attach($server, $username, $password) ?: $connection);
}
function alter_indexes($table, $alter) {

View File

@@ -10,6 +10,10 @@ if (isset($_GET["simpledb"])) {
class Db extends SqlDb {
public string $extension = "SimpleXML", $server_info = '2009-04-15', $timeout, $next;
function attach(?string $server, string $username, string $password): string {
return '';
}
function select_db(string $database): bool {
return ($database == "domain");
}