BlueGit

blue-star tribegen

Files

tribegen / web / src / Database / Database.php

Database.php

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