47 lines
1.6 KiB
PHP
Executable File
47 lines
1.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App;
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
class Database {
|
|
private static ?PDO $instance = null;
|
|
// Gebruik de globale constante DB_PATH uit config.php
|
|
private static string $dbFile = DB_PATH;
|
|
|
|
public static function getInstance(): PDO {
|
|
if (self::$instance === null) {
|
|
try {
|
|
self::$instance = new PDO('sqlite:' . self::$dbFile);
|
|
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
self::createTables();
|
|
} catch (PDOException $e) {
|
|
error_log("Database connection failed: " . $e->getMessage());
|
|
// Verbeterde foutmelding voor de gebruiker
|
|
die("FATAL ERROR: Database connection failed. This is often a permission issue.
|
|
<br>Error: " . $e->getMessage() . "
|
|
<br>Database Path: " . self::$dbFile . "
|
|
<br>Please ensure the web server user (www-data) has write permissions to the database file and its directory.");
|
|
}
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private static function createTables(): void {
|
|
$db = self::getInstance();
|
|
$db->exec('CREATE TABLE IF NOT EXISTS categories (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL UNIQUE,
|
|
parent_id INTEGER DEFAULT NULL
|
|
)');
|
|
$db->exec('CREATE TABLE IF NOT EXISTS items (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
description TEXT,
|
|
category_id INTEGER,
|
|
FOREIGN KEY (category_id) REFERENCES categories(id)
|
|
)');
|
|
}
|
|
}
|