Removed old database backend.
This commit is contained in:
parent
29426fafc1
commit
ad3fe74275
7 changed files with 7 additions and 189 deletions
|
@ -32,13 +32,8 @@ if(empty($dbConfig)) {
|
|||
exit;
|
||||
}
|
||||
|
||||
define('MSZ_DB_INIT', 'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';');
|
||||
|
||||
$db = DbTools::create($dbConfig['dsn']);
|
||||
$db->execute(MSZ_DB_INIT);
|
||||
|
||||
DB::init(DbTools::parse($dbConfig['dsn']));
|
||||
DB::exec(MSZ_DB_INIT);
|
||||
$db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';');
|
||||
|
||||
$cfg = new DbConfig($db);
|
||||
|
||||
|
|
56
src/DB.php
56
src/DB.php
|
@ -1,56 +0,0 @@
|
|||
<?php
|
||||
namespace Misuzu;
|
||||
|
||||
use PDO;
|
||||
use InvalidArgumentException;
|
||||
use Index\Data\IDbConnectionInfo;
|
||||
use Index\Data\MariaDB\MariaDBConnectionInfo;
|
||||
use Misuzu\Database\Database;
|
||||
|
||||
/**
|
||||
* @method static int queries()
|
||||
* @method static int exec(string $stmt)
|
||||
* @method static \Misuzu\Database\DatabaseStatement prepare(string $stmt, array $options = [])
|
||||
* @method static \Misuzu\Database\DatabaseStatement query(string $stmt, ?int $fetchMode = null, ...$args)
|
||||
* @method static int lastId()
|
||||
*/
|
||||
final class DB {
|
||||
private static $instance;
|
||||
|
||||
public const ATTRS = [
|
||||
PDO::ATTR_CASE => PDO::CASE_NATURAL,
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
|
||||
PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
];
|
||||
|
||||
public static function init(IDbConnectionInfo $connInfo) {
|
||||
if(!($connInfo instanceof MariaDBConnectionInfo))
|
||||
throw new InvalidArgumentException('$connInfo must be an instance of MariaDBConnectionInfo (only mariadb/mysql is supported).');
|
||||
|
||||
$dsn = 'mysql:';
|
||||
|
||||
if($connInfo->isUnixSocket())
|
||||
$dsn .= 'unix_socket=' . $connInfo->getSocketPath() . ';';
|
||||
else {
|
||||
$dsn .= 'host=' . $connInfo->getHost() . ';';
|
||||
$dsn .= 'port=' . $connInfo->getPort() . ';';
|
||||
}
|
||||
|
||||
$dsn .= 'dbname=' . $connInfo->getDatabaseName() . ';';
|
||||
|
||||
if($connInfo->hasCharacterSet())
|
||||
$dsn .= 'charset=' . $connInfo->getCharacterSet() . ';';
|
||||
|
||||
self::$instance = new Database($dsn, $connInfo->getUserName(), $connInfo->getPassword(), self::ATTRS);
|
||||
}
|
||||
|
||||
public static function __callStatic(string $name, array $args) {
|
||||
return self::$instance->{$name}(...$args);
|
||||
}
|
||||
|
||||
public static function getInstance(): Database {
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
<?php
|
||||
namespace Misuzu\Database;
|
||||
|
||||
use PDO;
|
||||
|
||||
class Database {
|
||||
public $pdo;
|
||||
private $stmts = [];
|
||||
|
||||
public function __construct(string $dsn, string $username = '', string $password = '', array $options = []) {
|
||||
$this->pdo = new PDO($dsn, $username, $password, $options);
|
||||
}
|
||||
|
||||
public function queries(): int {
|
||||
return ((int)$this->query('SHOW SESSION STATUS LIKE "Questions"')->fetchColumn(1));
|
||||
}
|
||||
|
||||
public function exec(string $stmt): int {
|
||||
return $this->pdo->exec($stmt);
|
||||
}
|
||||
|
||||
public function prepare(string $stmt, array $options = []): DatabaseStatement {
|
||||
$encodedOptions = serialize($options);
|
||||
|
||||
if(empty($this->stmts[$stmt][$encodedOptions])) {
|
||||
$this->stmts[$stmt][$encodedOptions] = $this->pdo->prepare($stmt, $options);
|
||||
}
|
||||
|
||||
return new DatabaseStatement($this->stmts[$stmt][$encodedOptions], $this->pdo, false);
|
||||
}
|
||||
|
||||
public function query(string $stmt, ?int $fetchMode = null, ...$args): DatabaseStatement {
|
||||
if($fetchMode === null) {
|
||||
$pdoStmt = $this->pdo->query($stmt);
|
||||
} else {
|
||||
$pdoStmt = $this->pdo->query($stmt, $fetchMode, ...$args);
|
||||
}
|
||||
|
||||
return new DatabaseStatement($pdoStmt, $this->pdo, true);
|
||||
}
|
||||
|
||||
public function lastId(): int {
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
<?php
|
||||
namespace Misuzu\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
|
||||
class DatabaseStatement {
|
||||
public $pdo;
|
||||
public $stmt;
|
||||
private $isQuery;
|
||||
|
||||
public function __construct(PDOStatement $stmt, PDO $pdo, bool $isQuery) {
|
||||
$this->stmt = $stmt;
|
||||
$this->pdo = $pdo;
|
||||
$this->isQuery = $isQuery;
|
||||
}
|
||||
|
||||
public function bind($param, $value, int $dataType = PDO::PARAM_STR): DatabaseStatement {
|
||||
$this->stmt->bindValue($param, $value, $dataType);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execute(array $params = []): bool {
|
||||
return count($params) ? $this->stmt->execute($params) : $this->stmt->execute();
|
||||
}
|
||||
|
||||
public function executeGetId(array $params = []): int {
|
||||
return $this->execute($params) ? $this->pdo->lastInsertId() : 0;
|
||||
}
|
||||
|
||||
public function fetch($default = []) {
|
||||
$out = $this->isQuery || $this->execute() ? $this->stmt->fetch(PDO::FETCH_ASSOC) : false;
|
||||
return $out ? $out : $default;
|
||||
}
|
||||
|
||||
public function fetchAll($default = []) {
|
||||
$out = $this->isQuery || $this->execute() ? $this->stmt->fetchAll(PDO::FETCH_ASSOC) : false;
|
||||
return $out ? $out : $default;
|
||||
}
|
||||
|
||||
public function fetchColumn(int $num = 0, $default = null) {
|
||||
$out = $this->isQuery || $this->execute() ? $this->stmt->fetchColumn($num) : false;
|
||||
return $out ? $out : $default;
|
||||
}
|
||||
|
||||
public function fetchObject(string $className = 'stdClass', ?array $args = null, $default = null) {
|
||||
$out = false;
|
||||
|
||||
if($this->isQuery || $this->execute()) {
|
||||
$out = $args === null ? $this->stmt->fetchObject($className) : $this->stmt->fetchObject($className, $args);
|
||||
}
|
||||
|
||||
return $out !== false ? $out : $default;
|
||||
}
|
||||
|
||||
public function fetchObjects(string $className = 'stdClass', ?array $args = null): array {
|
||||
$objects = [];
|
||||
|
||||
if($this->isQuery || $this->execute()) {
|
||||
while(($object = ($args === null ? $this->stmt->fetchObject($className) : $this->stmt->fetchObject($className, $args))) !== false) {
|
||||
$objects[] = $object;
|
||||
}
|
||||
}
|
||||
|
||||
return $objects;
|
||||
}
|
||||
}
|
|
@ -36,19 +36,12 @@ final class TwigMisuzu extends AbstractExtension {
|
|||
new TwigFunction('git_tag', fn() => GitInfo::tag()),
|
||||
new TwigFunction('git_branch', fn() => GitInfo::branch()),
|
||||
new TwigFunction('startup_time', fn(float $time = MSZ_STARTUP) => microtime(true) - $time),
|
||||
new TwigFunction('sql_query_count', fn() => $this->sqlQueryCount()),
|
||||
new TwigFunction('sql_query_count', fn() => $this->ctx->getDbQueryCount()),
|
||||
new TwigFunction('ndx_version', fn() => Environment::getIndexVersion()),
|
||||
new TwigFunction('byte_symbol', fn($bytes, $decimal = ByteFormat::DECIMAL_DEFAULT) => ByteFormat::format($bytes, $decimal)),
|
||||
];
|
||||
}
|
||||
|
||||
public function sqlQueryCount(): array {
|
||||
$ndx = $this->ctx->getDbQueryCount();
|
||||
$pdo = DB::queries();
|
||||
$total = $ndx + $pdo;
|
||||
return compact('ndx', 'pdo', 'total');
|
||||
}
|
||||
|
||||
public function timeFormat(DateTime|string|int|null $dateTime): string {
|
||||
if($dateTime === null)
|
||||
return 'never';
|
||||
|
|
|
@ -14,12 +14,11 @@
|
|||
{% endif %}
|
||||
# <a href="https://git.flash.moe/flashii/misuzu/commit/{{ git_commit_hash(true) }}" target="_blank" rel="noreferrer noopener" class="footer__link">{{ git_commit_hash() }}</a>
|
||||
{% if display_debug_info %}
|
||||
{% set sql_query_count = sql_query_count() %}
|
||||
/ Index {{ ndx_version() }}
|
||||
/ SQL Queries: {{ sql_query_count.total|number_format }} (<span title="{{ sql_query_count.ndx|number_format }} queries processed through Index">{{ sql_query_count.ndx|number_format }}</span> + <span title="{{ sql_query_count.pdo|number_format }} queries processed through PDO">{{ sql_query_count.pdo|number_format }}</span>)
|
||||
/ Took: {{ startup_time()|number_format(5) }} seconds
|
||||
/ Load: {{ (startup_time() - startup_time(constant('MSZ_TPL_RENDER')))|number_format(5) }} seconds
|
||||
/ Render: {{ startup_time(constant('MSZ_TPL_RENDER'))|number_format(5) }} seconds
|
||||
/ {{ sql_query_count()|number_format }} queries
|
||||
/ {{ (startup_time() - startup_time(constant('MSZ_TPL_RENDER')))|number_format(5) }} load
|
||||
/ {{ startup_time(constant('MSZ_TPL_RENDER'))|number_format(5) }} template
|
||||
/ {{ startup_time()|number_format(5) }} total
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endautoescape %}
|
||||
|
|
|
@ -88,9 +88,8 @@
|
|||
# <a href="https://github.com/flashwave/misuzu/commit/{{ git_commit_hash(true) }}" target="_blank" rel="noreferrer noopener">{{ git_commit_hash() }}</a>
|
||||
</div>
|
||||
{% if display_debug_info %}
|
||||
{% set sql_query_count = sql_query_count() %}
|
||||
<div class="landingv2-footer-copyright-line">
|
||||
{{ sql_query_count.total|number_format }} (<span title="{{ sql_query_count.ndx|number_format }} queries processed through Index">{{ sql_query_count.ndx|number_format }}</span> + <span title="{{ sql_query_count.pdo|number_format }} queries processed through PDO">{{ sql_query_count.pdo|number_format }}</span>) queries / {{ (startup_time() - startup_time(constant('MSZ_TPL_RENDER')))|number_format(5) }} load / {{ startup_time(constant('MSZ_TPL_RENDER'))|number_format(5) }} render / {{ startup_time()|number_format(5) }} total
|
||||
{{ sql_query_count()|number_format }} queries / {{ (startup_time() - startup_time(constant('MSZ_TPL_RENDER')))|number_format(5) }} load / {{ startup_time(constant('MSZ_TPL_RENDER'))|number_format(5) }} template / {{ startup_time()|number_format(5) }} total
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue