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