1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Database; |
3
|
|
|
|
4
|
|
|
use PDO; |
5
|
|
|
use Wandu\Database\Connection\MysqlConnection; |
6
|
|
|
use Wandu\Database\Contracts\Connection; |
7
|
|
|
use Wandu\Database\Exception\DriverNotFoundException; |
8
|
|
|
|
9
|
|
|
class Connector |
10
|
|
|
{ |
11
|
|
|
const DRIVER_MYSQL = 'mysql'; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $driver = 'mysql'; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $host = 'localhost'; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
protected $port = 4403; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $username = 'root'; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $password = ''; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $database = null; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected $charset; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $collation; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $prefix = ''; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $timezone; |
42
|
|
|
|
43
|
|
|
/** @var array */ |
44
|
|
|
protected $options = [ |
45
|
|
|
PDO::ATTR_CASE => PDO::CASE_NATURAL, |
46
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
47
|
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
48
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
49
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $settings |
54
|
|
|
*/ |
55
|
16 |
|
public function __construct(array $settings = []) |
56
|
|
|
{ |
57
|
16 |
|
foreach ($settings as $name => $setting) { |
58
|
16 |
|
$this->{$name} = $setting; |
59
|
|
|
} |
60
|
16 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \Wandu\Database\Contracts\Connection |
64
|
|
|
*/ |
65
|
16 |
|
public function connect(): Connection |
66
|
|
|
{ |
67
|
16 |
|
switch ($this->driver) { |
68
|
16 |
|
case Connector::DRIVER_MYSQL: |
69
|
15 |
|
return new MysqlConnection($this->createPdo()); |
70
|
|
|
} |
71
|
1 |
|
throw new DriverNotFoundException($this->driver); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
12 |
|
public function getPrefix(): string |
78
|
|
|
{ |
79
|
12 |
|
return $this->prefix; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return \PDO |
84
|
|
|
*/ |
85
|
15 |
|
public function createPdo() |
86
|
|
|
{ |
87
|
15 |
|
$pdo = new PDO( |
88
|
15 |
|
"{$this->driver}:host={$this->host};port={$this->port};dbname={$this->database}", |
89
|
15 |
|
$this->username, |
90
|
15 |
|
$this->password, |
91
|
15 |
|
$this->options + [ |
92
|
15 |
|
PDO::ATTR_CASE => PDO::CASE_NATURAL, |
93
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
94
|
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
95
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
96
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
97
|
|
|
] |
98
|
|
|
); |
99
|
15 |
|
$this->applyCharset($pdo); |
100
|
15 |
|
$this->applyTimezone($pdo); |
101
|
15 |
|
return $pdo; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \PDO $pdo |
106
|
|
|
*/ |
107
|
15 |
|
protected function applyCharset(PDO $pdo) |
108
|
|
|
{ |
109
|
15 |
|
if ($this->charset) { |
110
|
15 |
|
$names = "SET NAMES '{$this->charset}'"; |
111
|
15 |
|
if ($this->collation) { |
112
|
15 |
|
$names .= " COLLATE '{$this->collation}'"; |
113
|
|
|
} |
114
|
15 |
|
$pdo->prepare($names)->execute(); |
115
|
|
|
} |
116
|
15 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \PDO $pdo |
120
|
|
|
*/ |
121
|
15 |
|
protected function applyTimezone(PDO $pdo) |
122
|
|
|
{ |
123
|
15 |
|
if ($this->timezone) { |
124
|
1 |
|
$pdo->prepare("SET time_zone='{$this->timezone}'")->execute(); |
125
|
|
|
} |
126
|
15 |
|
} |
127
|
|
|
} |
128
|
|
|
|