1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-10 16:44:17 +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

@@ -11,11 +11,11 @@ if (isset($_GET["sqlite"])) {
public string $extension = "SQLite3";
private $link;
function connect(string $filename, string $username = '', string $password = ''): bool {
function attach(?string $filename, string $username, string $password): string {
$this->link = new \SQLite3($filename);
$version = $this->link->version();
$this->server_info = $version["versionString"];
return true;
return '';
}
function query(string $query, bool $unbuffered = false) {
@@ -75,11 +75,11 @@ if (isset($_GET["sqlite"])) {
abstract class SqliteDb extends PdoDb {
public string $extension = "PDO_SQLite";
function connect(string $filename, string $username = '', string $password = ''): bool {
function attach(?string $filename, string $username, string $password): string {
$this->dsn(DRIVER . ":$filename", "", "");
$this->query("PRAGMA foreign_keys = 1");
$this->query("PRAGMA busy_timeout = 500");
return true;
return '';
}
}
@@ -87,16 +87,16 @@ if (isset($_GET["sqlite"])) {
if (class_exists('Adminer\SqliteDb')) {
class Db extends SqliteDb {
function connect(string $filename, string $username = '', string $password = ''): bool {
parent::connect($filename);
function attach(?string $filename, string $username, string $password): string {
parent::attach($filename, $username, $password);
$this->query("PRAGMA foreign_keys = 1");
$this->query("PRAGMA busy_timeout = 500");
return true;
return '';
}
function select_db(string $filename): bool {
if (is_readable($filename) && $this->query("ATTACH " . $this->quote(preg_match("~(^[/\\\\]|:)~", $filename) ? $filename : dirname($_SERVER["SCRIPT_FILENAME"]) . "/$filename") . " AS a")) {
return self::connect($filename);
return !self::attach($filename, '', '');
}
return false;
}
@@ -172,8 +172,7 @@ if (isset($_GET["sqlite"])) {
return lang('Database does not support password.');
}
$connection = new Db;
$connection->connect(":memory:", "", "");
return $connection;
return ($connection->attach(":memory:", "", "") ?: $connection);
}
function get_databases($flush) {
@@ -369,7 +368,7 @@ if (isset($_GET["sqlite"])) {
}
try {
$link = new Db();
$link->connect($db);
$link->attach($db, '', '');
} catch (\Exception $ex) {
$connection->error = $ex->getMessage();
return false;
@@ -382,7 +381,7 @@ if (isset($_GET["sqlite"])) {
function drop_databases($databases) {
global $connection;
$connection->connect(":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.');
@@ -397,7 +396,7 @@ if (isset($_GET["sqlite"])) {
if (!check_sqlite_name($name)) {
return false;
}
$connection->connect(":memory:");
$connection->attach(":memory:", '', '');
$connection->error = lang('File exists.');
return @rename(DB, $name);
}