123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /*
- * Part of Shuzanne - An extensible sequel to an open-source imageboard.
- *
- * @package Shuzanne
- * @author MisleadingName, Shuzanne Contributors
- * @license MPL v2.0
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- */
- namespace app\Types;
- use app\Interfaces\IDatabase;
- use app\Logger;
- use PDO;
- use PDOStatement;
- use PDOException;
- class MariaDBDatabase implements IDatabase
- {
- private ?PDO $pdo;
- private PDOStatement $stmt;
- private array $parameters = [];
- public function __construct(array $engineConfig)
- {
- $host = $engineConfig["Host"];
- $port = $engineConfig["Port"] ?? 3306;
- $username = $engineConfig["Username"];
- $password = $engineConfig["Password"];
- $dbname = $engineConfig["Database"];
- $charset = $engineConfig["Charset"] ?? "utf8mb4";
- $dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
- $options = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- ];
- try {
- $this->pdo = new PDO($dsn, $username, $password, $options);
- } catch (PDOException $e) {
- Logger::Error("Database connection failed: " . $e->getMessage());
- throw $e;
- }
- }
- public function CloseConnection(): void
- {
- $this->pdo = null;
- }
- public function Bind(string $param, mixed $value): void
- {
- $this->parameters[$param] = $value;
- }
- public function BindMore(array $params): void
- {
- foreach ($params as $param => $value) {
- $this->Bind($param, $value);
- }
- }
- private function executeStatement(string $query, ?array $params = null): PDOStatement
- {
- try {
- $this->stmt = $this->pdo->prepare($query);
- if ($params) {
- foreach ($params as $param => $value) {
- $paramType = match (gettype($value)) {
- 'integer' => PDO::PARAM_INT,
- 'boolean' => PDO::PARAM_BOOL,
- 'NULL' => PDO::PARAM_NULL,
- default => PDO::PARAM_STR
- };
- $this->stmt->bindValue(is_numeric($param) ? $param + 1 : $param, $value, $paramType);
- }
- }
- $this->stmt->execute();
- return $this->stmt;
- } catch (PDOException $e) {
- Logger::Error("Query execution failed: " . $e->getMessage() . " - Query: $query");
- throw $e;
- }
- }
- public function Query(string $query, ?array $params = null, int $fetchMode = PDO::FETCH_ASSOC): mixed
- {
- $stmt = $this->executeStatement($query, $params ?? $this->parameters);
- $this->parameters = [];
- return $stmt->fetchAll($fetchMode);
- }
- public function QueryRowCount(string $query, ?array $params = null, int $fetchMode = PDO::FETCH_ASSOC): int
- {
- $stmt = $this->executeStatement($query, $params ?? $this->parameters);
- $this->parameters = [];
- return $stmt->rowCount();
- }
- public function LastInsertId(): string
- {
- return $this->pdo->lastInsertId();
- }
- public function BeginTransaction(): bool
- {
- return $this->pdo->beginTransaction();
- }
- public function ExecuteTransaction(): bool
- {
- return $this->pdo->commit();
- }
- public function Rollback(): bool
- {
- return $this->pdo->rollBack();
- }
- public function Column(string $query, ?array $params = null): array
- {
- $stmt = $this->executeStatement($query, $params ?? $this->parameters);
- $this->parameters = [];
- return $stmt->fetchAll(PDO::FETCH_COLUMN);
- }
- public function Row(string $query, ?array $params = null, int $fetchMode = PDO::FETCH_ASSOC): array
- {
- $stmt = $this->executeStatement($query, $params ?? $this->parameters);
- $this->parameters = [];
- $result = $stmt->fetch($fetchMode);
- return $result ?: [];
- }
- public function Single(string $query, ?array $params = null): mixed
- {
- $stmt = $this->executeStatement($query, $params ?? $this->parameters);
- $this->parameters = [];
- $result = $stmt->fetchColumn();
- return $result !== false ? $result : null;
- }
- }
|