misuzu/src/DB.php

54 lines
1.5 KiB
PHP

<?php
namespace Misuzu;
use PDO;
use Misuzu\Database\Database;
/**
* @method static PDO getPDO()
* @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 PREFIX = 'msz_';
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(...$args) {
self::$instance = new Database(...$args);
}
public static function __callStatic(string $name, array $args) {
return self::$instance->{$name}(...$args);
}
public static function getInstance(): Database {
return self::$instance;
}
public static function buildDSN(array $vars): string {
$dsn = ($vars['driver'] ?? 'mysql') . ':';
foreach($vars as $key => $value) {
if($key === 'driver' || $key === 'username' || $key === 'password')
continue;
if($key === 'database')
$key = 'dbname';
$dsn .= $key . '=' . $value . ';';
}
return $dsn;
}
}