2024-06-28 17:05:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Database {
|
|
|
|
private $pdo;
|
|
|
|
|
|
|
|
public function __construct($dbFile) {
|
2024-07-04 19:14:12 +00:00
|
|
|
|
2024-07-03 10:47:30 +00:00
|
|
|
// pdo and pdo_sqlite needed
|
2024-06-28 17:05:32 +00:00
|
|
|
if ( !extension_loaded('pdo_sqlite') ) {
|
|
|
|
throw new Exception('PDO extension for SQLite not loaded.');
|
|
|
|
}
|
|
|
|
|
2024-07-04 19:14:12 +00:00
|
|
|
// database file check
|
|
|
|
if (empty($dbFile) || !file_exists($dbFile)) {
|
|
|
|
throw new Exception('Database file is not found.');
|
|
|
|
}
|
|
|
|
|
2024-07-03 10:47:30 +00:00
|
|
|
// connect to database
|
|
|
|
// FIXME: add mysql/mariadb option
|
2024-06-28 17:05:32 +00:00
|
|
|
try {
|
|
|
|
$this->pdo = new PDO("sqlite:" . $dbFile);
|
|
|
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
throw new Exception('DB connection failed: ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConnection() {
|
|
|
|
return $this->pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|