diff --git a/lib/index b/lib/index index 31798a6..939dcd1 160000 --- a/lib/index +++ b/lib/index @@ -1 +1 @@ -Subproject commit 31798a6b536069bc47832545b036c0d62422400c +Subproject commit 939dcd10fe7f33d567e4bf327b284dcb8a9bdd63 diff --git a/misuzu.php b/misuzu.php index 576df65..419d36c 100644 --- a/misuzu.php +++ b/misuzu.php @@ -78,8 +78,7 @@ define('MSZ_DB_INIT', 'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_T $db = DbTools::create($dbConfig['dsn']); $db->execute(MSZ_DB_INIT); -$dbConfig = $dbConfig['Database'] ?? $dbConfig['Database.mysql-main'] ?? []; -DB::init(DB::buildDSN($dbConfig), $dbConfig['username'] ?? '', $dbConfig['password'] ?? '', DB::ATTRS); +DB::init(DbTools::parse($dbConfig['dsn'])); DB::exec(MSZ_DB_INIT); $cfg = new DbConfig($db); diff --git a/src/DB.php b/src/DB.php index 3775f82..e6a5129 100644 --- a/src/DB.php +++ b/src/DB.php @@ -2,6 +2,9 @@ namespace Misuzu; use PDO; +use InvalidArgumentException; +use Index\Data\IDbConnectionInfo; +use Index\Data\MariaDB\MariaDBConnectionInfo; use Misuzu\Database\Database; /** @@ -24,8 +27,25 @@ final class DB { PDO::ATTR_EMULATE_PREPARES => false, ]; - public static function init(...$args) { - self::$instance = new Database(...$args); + 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) { @@ -35,19 +55,4 @@ final class DB { 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; - } } diff --git a/templates/500.html b/templates/500.html index 9b8fc0b..f47560c 100644 --- a/templates/500.html +++ b/templates/500.html @@ -11,6 +11,6 @@

Error 500

-

Something horrendously went wrong. Please report what you were doing to a developer.

+

Something went very wrong. Please report what you were doing to a developer.