|
2 | 2 |
|
3 | 3 | namespace DrMVC\Database\Drivers;
|
4 | 4 |
|
5 |
| -use DrMVC\Config\ConfigInterface; |
| 5 | +use DrMVC\Database\SQLException; |
6 | 6 |
|
7 | 7 | class Mysql extends SQL
|
8 | 8 | {
|
9 |
| - const DEFAULT_HOST = 'localhost'; |
10 |
| - const DEFAULT_PORT = 3306; |
| 9 | + const DEFAULT_HOST = '127.0.0.1'; |
| 10 | + const DEFAULT_PORT = '3306'; |
| 11 | + const DEFAULT_CHARSET = 'utf8'; |
| 12 | + const DEFAULT_COLLATION = 'utf8_unicode_ci'; |
11 | 13 |
|
12 |
| - public function __construct(ConfigInterface $config, string $collection) |
13 |
| - { |
14 |
| - parent::__construct($config, $collection); |
15 |
| - $db_host = $this->getHost(); |
16 |
| - $db_port = $this->getPort(); |
17 |
| - $db_auth = $this->getAuth(); |
18 |
| - $db_driv = $this->getConfig()->get('driver'); |
19 |
| - $db_name = $this->getConfig()->get('database'); |
20 |
| - |
21 |
| - $dsn = "$db_driv://$db_auth$db_host:$db_port/$db_name"; |
22 |
| - |
23 |
| - $this->setDsn($dsn); |
24 |
| - } |
| 14 | + /** |
| 15 | + * @link https://secure.php.net/manual/en/ref.pdo-mysql.connection.php |
| 16 | + * |
| 17 | + * The PDO_MYSQL Data Source Name (DSN) is composed of the following elements: |
| 18 | + */ |
| 19 | + const AVAILABLE_ELEMENTS = [ |
| 20 | + 'host', |
| 21 | + 'port', |
| 22 | + 'dbname', |
| 23 | + 'unix_socket' |
| 24 | + ]; |
25 | 25 |
|
26 |
| - private function getHost(): string |
| 26 | + /** |
| 27 | + * Generate DSN by parameters in config |
| 28 | + * |
| 29 | + * @param array $config |
| 30 | + * @return string |
| 31 | + */ |
| 32 | + public function genDsn($config): string |
27 | 33 | {
|
28 |
| - $host = $this->getConfig()->get('host'); |
29 |
| - return $host ?? self::DEFAULT_HOST; |
| 34 | + // Parse config |
| 35 | + $dsn = ''; |
| 36 | + foreach ($config as $key => $value) { |
| 37 | + if (\in_array($key, self::AVAILABLE_ELEMENTS, false)) { |
| 38 | + $dsn .= "$key=$value;"; |
| 39 | + } |
| 40 | + } |
| 41 | + return $dsn; |
30 | 42 | }
|
31 | 43 |
|
32 |
| - private function getPort(): int |
| 44 | + /** |
| 45 | + * Initiate connection to database |
| 46 | + * |
| 47 | + * @return DriverInterface |
| 48 | + */ |
| 49 | + public function connect(): DriverInterface |
33 | 50 | {
|
34 |
| - $port = $this->getConfig()->get('port'); |
35 |
| - return $port ?? self::DEFAULT_PORT; |
| 51 | + $connection = null; |
| 52 | + try { |
| 53 | + $connection = new \PDO( |
| 54 | + $this->getDsn(), |
| 55 | + $this->getParam('username'), |
| 56 | + $this->getParam('password'), |
| 57 | + $this->getOptions() |
| 58 | + ); |
| 59 | + } catch (SQLException $e) { |
| 60 | + // __construct |
| 61 | + } |
| 62 | + |
| 63 | + $this->setConnection($connection); |
| 64 | + return $this; |
36 | 65 | }
|
37 | 66 |
|
38 |
| - private function getAuth(): string |
| 67 | + private function getOptions(): array |
39 | 68 | {
|
40 |
| - $user = $this->getConfig()->get('username'); |
41 |
| - $pass = $this->getConfig()->get('password'); |
| 69 | + // Current charset |
| 70 | + $charset = $this->getParam('charset') ?? self::DEFAULT_CHARSET; |
| 71 | + |
| 72 | + // Current collation |
| 73 | + $collation = $this->getParam('collation') ?? self::DEFAULT_COLLATION; |
42 | 74 |
|
43 |
| - return ($user && $pass) ? $user . ':' . $pass . '@' : ''; |
| 75 | + // Return array of options |
| 76 | + return [\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '$charset' COLLATE '$collation'"]; |
44 | 77 | }
|
45 | 78 |
|
46 | 79 | }
|
0 commit comments