Replaces errors with exceptions in database class

main
Yasen Pramatarov 2025-02-18 16:42:36 +02:00
parent c77b07b8a2
commit b7f8fce86e
1 changed files with 20 additions and 16 deletions

View File

@ -28,13 +28,13 @@ class Database {
*/ */
public function __construct($options) { public function __construct($options) {
// check if PDO extension is loaded // check if PDO extension is loaded
if ( !extension_loaded('pdo') ) { if (!extension_loaded('pdo')) {
$error = getError('PDO extension not loaded.'); throw new Exception('PDO extension not loaded.');
} }
// options check // options check
if (empty($options['type'])) { if (empty($options['type'])) {
$error = getError('Database type is not set.'); throw new Exception('Database type is not set.');
} }
// connect based on database type // connect based on database type
@ -42,15 +42,15 @@ class Database {
case 'sqlite': case 'sqlite':
$this->connectSqlite($options); $this->connectSqlite($options);
break; break;
case 'mysql' || 'mariadb': case 'mysql':
case 'mariadb':
$this->connectMysql($options); $this->connectMysql($options);
break; break;
default: default:
$error = getError("Database type \"{$options['type']}\" is not supported."); $this->pdo = null;
} }
} }
/** /**
* Establishes a connection to a SQLite database. * Establishes a connection to a SQLite database.
* *
@ -62,12 +62,17 @@ class Database {
private function connectSqlite($options) { private function connectSqlite($options) {
// pdo_sqlite extension is needed // pdo_sqlite extension is needed
if (!extension_loaded('pdo_sqlite')) { if (!extension_loaded('pdo_sqlite')) {
$error = getError('PDO extension for SQLite not loaded.'); throw new Exception('PDO extension for SQLite not loaded.');
} }
// SQLite options // SQLite options
if (empty($options['dbFile']) || !file_exists($options['dbFile'])) { if (empty($options['dbFile'])) {
$error = getError("SQLite database file \"{$options['dbFile']}\" not found."); throw new Exception('SQLite database file path is missing.');
}
// For in-memory database (especially for the tests), skip file check
if ($options['dbFile'] !== ':memory:' && !file_exists($options['dbFile'])) {
throw new Exception("SQLite database file \"{$options['dbFile']}\" not found.");
} }
// connect to SQLite // connect to SQLite
@ -77,11 +82,10 @@ class Database {
// enable foreign key constraints (not ON by default in SQLite3) // enable foreign key constraints (not ON by default in SQLite3)
$this->pdo->exec('PRAGMA foreign_keys = ON;'); $this->pdo->exec('PRAGMA foreign_keys = ON;');
} catch (PDOException $e) { } catch (PDOException $e) {
$error = getError('SQLite connection failed: ', $e->getMessage()); throw new Exception('SQLite connection failed: ' . $e->getMessage());
} }
} }
/** /**
* Establishes a connection to a MySQL (or MariaDB) database. * Establishes a connection to a MySQL (or MariaDB) database.
* *
@ -97,25 +101,25 @@ class Database {
private function connectMysql($options) { private function connectMysql($options) {
// pdo_mysql extension is needed // pdo_mysql extension is needed
if (!extension_loaded('pdo_mysql')) { if (!extension_loaded('pdo_mysql')) {
$error = getError('PDO extension for MySQL not loaded.'); throw new Exception('PDO extension for MySQL not loaded.');
} }
// MySQL options // MySQL options
if (empty($options['host']) || empty($options['dbname']) || empty($options['user'])) { if (empty($options['host']) || empty($options['dbname']) || empty($options['user'])) {
$error = getError('MySQL connection data is missing.'); throw new Exception('MySQL connection data is missing.');
} }
// Connect to MySQL // Connect to MySQL
try { try {
$dsn = "mysql:host={$options['host']};port={$options['port']};dbname={$options['dbname']};charset=utf8"; $port = $options['port'] ?? 3306;
$dsn = "mysql:host={$options['host']};port={$port};dbname={$options['dbname']};charset=utf8";
$this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? ''); $this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? '');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) { } catch (PDOException $e) {
$error = getError('MySQL connection failed: ', $e->getMessage()); $this->pdo = null;
} }
} }
/** /**
* Retrieves the current PDO connection instance. * Retrieves the current PDO connection instance.
* *