Files

44 lines
1.0 KiB
PHP
Raw Permalink Normal View History

2024-04-16 17:24:22 +08:00
<?php
namespace Database\Base;
class PDO extends \PDO
{
2025-12-16 20:20:08 +08:00
readonly public string $driver;
2024-04-16 17:24:22 +08:00
/**
* @param string $database
* @param string $host
* @param string $username
* @param string $password
* @param array $options
2025-12-16 20:20:08 +08:00
* @param string $driver
2024-04-16 17:24:22 +08:00
*/
public function __construct(
readonly public string $database,
readonly public string $host,
readonly public string $username,
readonly public string $password,
readonly public array $options,
2025-12-16 20:20:08 +08:00
string $driver = 'mysql',
2024-04-16 17:24:22 +08:00
)
{
2025-12-16 20:20:08 +08:00
$this->driver = strtolower($driver);
$dsn = $this->buildDsn();
parent::__construct($dsn, $this->username, $this->password, $this->options);
2024-04-16 17:24:22 +08:00
}
2025-12-16 20:20:08 +08:00
/**
* @return string
*/
private function buildDsn(): string
{
return match ($this->driver) {
'pgsql', 'postgresql' => 'pgsql:host=' . $this->host . ';dbname=' . $this->database,
default => 'mysql:dbname=' . $this->database . ';host=' . $this->host,
};
}
2024-04-16 17:24:22 +08:00
}