2026-09-03 17:35:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
|
|
|
|
use PDO;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Thin wrapper around a PDO connection to the SQLite database.
|
|
|
|
|
*/
|
|
|
|
|
final class Database
|
|
|
|
|
{
|
|
|
|
|
private PDO $pdo;
|
|
|
|
|
|
|
|
|
|
public function __construct(string $path)
|
|
|
|
|
{
|
|
|
|
|
$this->pdo = new PDO('sqlite:' . $path, null, null, [
|
|
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
|
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->pdo->exec('PRAGMA foreign_keys = ON');
|
|
|
|
|
$this->pdo->exec('PRAGMA journal_mode = WAL');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function pdo(): PDO
|
|
|
|
|
{
|
|
|
|
|
return $this->pdo;
|
|
|
|
|
}
|
2026-09-04 21:41:25 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SQL expression for the current UTC time, in the format every
|
|
|
|
|
* created_at/updated_at/*_at column uses (both as their DEFAULT in the
|
|
|
|
|
* migrations and wherever a repository sets one explicitly).
|
|
|
|
|
*/
|
|
|
|
|
public static function nowExpr(): string
|
|
|
|
|
{
|
|
|
|
|
return "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')";
|
|
|
|
|
}
|
2026-09-03 17:35:10 +01:00
|
|
|
}
|