BlueGit

blue-star tribegen

Files

tribegen / web / src / Database / Database.php

Database.php

initial generation
timfa Tim · pushed by timfa · 9 months ago
0b655138 History
Database.php 1.1 KiB Download Raw
1<?php
2
3namespace TribeGen\Database;
4
5class Database
6{
7 private static $instance = null;
8 private $pdo;
9
10 private function __construct()
11 {
12 $dbPath = __DIR__ . '/../../data/tribegen.db';
13 $this->pdo = new \PDO('sqlite:' . $dbPath);
14 $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
15 $this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
16 }
17
18 public static function getInstance()
19 {
20 if (self::$instance === null) {
21 self::$instance = new self();
22 }
23 return self::$instance;
24 }
25
26 public function getPDO()
27 {
28 return $this->pdo;
29 }
30
31 public function initializeTables()
32 {
33 // Only the users table is stored in the database
34 // All game data is stored as JSON files in /data/saves/
35 $this->pdo->exec("
36 CREATE TABLE IF NOT EXISTS users (
37 id INTEGER PRIMARY KEY AUTOINCREMENT,
38 email TEXT UNIQUE NOT NULL,
39 password_hash TEXT NOT NULL,
40 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
41 );
42 ");
43 }
44}