diff --git a/misuzu.php b/misuzu.php index b726268..6f8b2ad 100644 --- a/misuzu.php +++ b/misuzu.php @@ -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); diff --git a/src/DB.php b/src/DB.php deleted file mode 100644 index cacafae..0000000 --- a/src/DB.php +++ /dev/null @@ -1,56 +0,0 @@ - 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; - } -} diff --git a/src/Database/Database.php b/src/Database/Database.php deleted file mode 100644 index 8928df8..0000000 --- a/src/Database/Database.php +++ /dev/null @@ -1,45 +0,0 @@ -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(); - } -} diff --git a/src/Database/DatabaseStatement.php b/src/Database/DatabaseStatement.php deleted file mode 100644 index 39c32e6..0000000 --- a/src/Database/DatabaseStatement.php +++ /dev/null @@ -1,67 +0,0 @@ -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; - } -} diff --git a/src/TwigMisuzu.php b/src/TwigMisuzu.php index 40ace09..aa1e5f9 100644 --- a/src/TwigMisuzu.php +++ b/src/TwigMisuzu.php @@ -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'; diff --git a/templates/_layout/footer.twig b/templates/_layout/footer.twig index 5df09a8..aa2a57e 100644 --- a/templates/_layout/footer.twig +++ b/templates/_layout/footer.twig @@ -14,12 +14,11 @@ {% endif %} # {{ git_commit_hash() }} {% if display_debug_info %} - {% set sql_query_count = sql_query_count() %} / Index {{ ndx_version() }} - / SQL Queries: {{ sql_query_count.total|number_format }} ({{ sql_query_count.ndx|number_format }} + {{ sql_query_count.pdo|number_format }}) - / 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 %} {% endautoescape %} diff --git a/templates/home/landing.twig b/templates/home/landing.twig index 6eb8143..1df47e1 100644 --- a/templates/home/landing.twig +++ b/templates/home/landing.twig @@ -88,9 +88,8 @@ # {{ git_commit_hash() }} {% if display_debug_info %} -{% set sql_query_count = sql_query_count() %} {% endif %}