diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2020-10-05 21:44:27 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2020-10-05 21:44:27 +0200 |
commit | 01842f99b65b06d2647470c3b867719e72dabde7 (patch) | |
tree | 6ee373d5df46cf1097b06fa37bcfb0dc9b09d811 /ratatoeskr/sys/Database.php | |
parent | 5e347e4efaa81c2108256dc927208cd55dc10baa (diff) | |
download | ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.tar.gz ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.tar.bz2 ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.zip |
Introduce some new database wrappers
This will allow us to avoid using globals and use the autoloader in the
future.
Diffstat (limited to 'ratatoeskr/sys/Database.php')
-rw-r--r-- | ratatoeskr/sys/Database.php | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/ratatoeskr/sys/Database.php b/ratatoeskr/sys/Database.php new file mode 100644 index 0000000..7654e46 --- /dev/null +++ b/ratatoeskr/sys/Database.php @@ -0,0 +1,105 @@ +<?php + + +namespace r7r\cms\sys; + +use PDO; +use PDOStatement; + +class Database +{ + /** @var PDO */ + private $pdo; + + /** @var string */ + private $prefix; + + /** + * @param PDO $pdo + * @param string $prefix + */ + public function __construct(PDO $pdo, string $prefix) + { + $this->pdo = $pdo; + $this->prefix = $prefix; + } + + /** + * Create a Database object from a config. + * + * @param array $config + * @return self + */ + public static function fromConfig(array $config): self + { + $pdo = new PDO( + "mysql:host=" . $config["mysql"]["server"] . ";dbname=" . $config["mysql"]["db"] . ";charset=utf8", + $config["mysql"]["user"], + $config["mysql"]["passwd"], + [ + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', + ] + ); + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + return new self($pdo, $config["mysql"]["prefix"]); + } + + /** + * Gets the wrapped PDO object. + * + * @return PDO + */ + public function getPdo(): PDO + { + return $this->pdo; + } + + /** + * Get the table prefix. + * + * @return string + */ + public function getPrefix(): string + { + return $this->prefix; + } + + /** + * Substitutes "PREFIX_" in the input string with the prefix from the config. + * + * @param string $query + * @return string + */ + public function subPrefix(string $query): string // \mystuff\TODO: or can we make this private? + { + return str_replace("PREFIX_", $this->prefix, $query); + } + + /** + * Prepares a SQL statement for usage with the database. + * This will also replace "PREFIX_" with the prefix defined in 'config.php'. + * + * @param string $query The query / statement to prepare. + * @return PDOStatement + */ + public function prepStmt(string $query): PDOStatement // \mystuff\TODO: or can we make this private? + { + return $this->pdo->prepare($this->subPrefix($query)); + } + + /** + * Prepares a query with {@see Database::prepStmt()} and executes it with the remaining arguments. + * + * @param string $query + * @param mixed ...$args + * @return PDOStatement + */ + public function query(string $query, ...$args): PDOStatement + { + $stmt = $this->prepStmt($query); + $stmt->execute($args); + + return $stmt; + } +} |