<?php
require_once __DIR__.'/config.php';
function db(): PDO {
  static $pdo=null;
  if ($pdo===null){
    if (strpos(SQLITE_PATH, DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR)!==false) {
      throw new RuntimeException('DB tidak boleh berada dalam /public');
    }
    $pdo = new PDO('sqlite:'.SQLITE_PATH);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $pdo->exec('PRAGMA foreign_keys = ON');
  }
  return $pdo;
}