diff options
| author | winter | 2024-12-09 21:31:54 +0000 |
|---|---|---|
| committer | winter | 2024-12-09 21:31:54 +0000 |
| commit | 4c7bde1400820f36caf8b2a5374007384c3018f3 (patch) | |
| tree | 83ed26ff368117b8e392f7bc28736482077fc2f4 /Digitigrade/Db.php | |
| parent | 50edbd3907c26a2d4d616421de9bb51a4d083c1b (diff) | |
rename to Digitigrade / PawPub
:3
Diffstat (limited to 'Digitigrade/Db.php')
| -rw-r--r-- | Digitigrade/Db.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Digitigrade/Db.php b/Digitigrade/Db.php new file mode 100644 index 0000000..c9f9df9 --- /dev/null +++ b/Digitigrade/Db.php @@ -0,0 +1,42 @@ +<?php +namespace Digitigrade; + +use Digitigrade\Model\Actor; + +class Db extends Singleton { + private const INIT_SCHEMA_PATH = __DIR__ . '/../schema.sql'; + + private \PDO $conn; + + protected function __construct() { + $conf = GlobalConfig::getInstance(); + $this->conn = new \PDO($conf->getDbConnection(), $conf->getDbUser(), $conf->getDbPassword(), [ + \PDO::ATTR_PERSISTENT => true, + ]); + } + + public function runInitialSchema() { + $this->conn->exec(file_get_contents(self::INIT_SCHEMA_PATH)); + } + + public function getDbVersion(): int { + $result = $this->conn->query('SELECT db_version FROM db_version'); + return $result->fetch()['db_version']; + } + + /** + * Changes the database schema version + * + * CAREFUL! this shouldn't be used unless you absolutely know why + * @param int $newVersion the new version number + * @return void + */ + public function setDbVersion(int $newVersion) { + $stmt = $this->conn->prepare('UPDATE db_version SET db_version = ?'); + $stmt->execute([$newVersion]); + } + + public function getPdo(): \PDO { + return $this->conn; + } +}
\ No newline at end of file |
