diff --git a/app/classes/database.php b/app/classes/database.php index 0899f56..35c2c7d 100644 --- a/app/classes/database.php +++ b/app/classes/database.php @@ -28,13 +28,13 @@ class Database { */ public function __construct($options) { // check if PDO extension is loaded - if ( !extension_loaded('pdo') ) { - $error = getError('PDO extension not loaded.'); + if (!extension_loaded('pdo')) { + throw new Exception('PDO extension not loaded.'); } // options check if (empty($options['type'])) { - $error = getError('Database type is not set.'); + throw new Exception('Database type is not set.'); } // connect based on database type @@ -42,15 +42,15 @@ class Database { case 'sqlite': $this->connectSqlite($options); break; - case 'mysql' || 'mariadb': + case 'mysql': + case 'mariadb': $this->connectMysql($options); break; default: - $error = getError("Database type \"{$options['type']}\" is not supported."); + $this->pdo = null; } } - /** * Establishes a connection to a SQLite database. * @@ -62,12 +62,17 @@ class Database { private function connectSqlite($options) { // pdo_sqlite extension is needed if (!extension_loaded('pdo_sqlite')) { - $error = getError('PDO extension for SQLite not loaded.'); + throw new Exception('PDO extension for SQLite not loaded.'); } // SQLite options - if (empty($options['dbFile']) || !file_exists($options['dbFile'])) { - $error = getError("SQLite database file \"{$options['dbFile']}\" not found."); + if (empty($options['dbFile'])) { + 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 @@ -77,11 +82,10 @@ class Database { // enable foreign key constraints (not ON by default in SQLite3) $this->pdo->exec('PRAGMA foreign_keys = ON;'); } 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. * @@ -97,25 +101,25 @@ class Database { private function connectMysql($options) { // pdo_mysql extension is needed if (!extension_loaded('pdo_mysql')) { - $error = getError('PDO extension for MySQL not loaded.'); + throw new Exception('PDO extension for MySQL not loaded.'); } // MySQL options 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 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->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { - $error = getError('MySQL connection failed: ', $e->getMessage()); + $this->pdo = null; } } - /** * Retrieves the current PDO connection instance. *