1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\sql\framework\pdo; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use PDO; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This class implements a provider layer over PDO. |
10
|
|
|
* |
11
|
|
|
* You don't need to use this layer - the framework itself has no dependency on it, but you |
12
|
|
|
* may find it useful as a means of addressing certain issues with `PDO`. |
13
|
|
|
* |
14
|
|
|
* For one, `PDO` instances cannot be created without immediately opening the connection - this |
15
|
|
|
* provider does not construct `PDO` and open the connection until you ask for the `PDO` instance. |
16
|
|
|
* |
17
|
|
|
* Also, `PDO` has no ability to report properties like database-name, username, etc. if you need them. |
18
|
|
|
*/ |
19
|
|
|
class PDOProvider |
20
|
|
|
{ |
21
|
|
|
const PROTOCOL_POSTGRES = "pgsql"; |
22
|
|
|
const PROTOCOL_MYSQL = "mysql"; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $protocol; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $dsn; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $username; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $password; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $options; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $host; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $port; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var PDO|null |
61
|
|
|
*/ |
62
|
|
|
private $pdo; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $protocol database protocol name (one of the PROTOCOL_* class constants) |
66
|
|
|
* @param string $dbname name of the database to connect to |
67
|
|
|
* @param string $username |
68
|
|
|
* @param string $password |
69
|
|
|
* @param array|null $options PDO constructor options |
70
|
|
|
* @param string|null $host optional hostname; defaults to "localhost" |
71
|
|
|
* @param int|null $port optional port-number; defaults to the standard port-number for the given $db type |
72
|
|
|
*/ |
73
|
1 |
|
public function __construct($protocol, $dbname, $username, $password, $options = null, $host = null, $port = null) |
74
|
|
|
{ |
75
|
1 |
|
static $default_port = [ |
76
|
|
|
self::PROTOCOL_MYSQL => 3306, |
77
|
|
|
self::PROTOCOL_POSTGRES => 5432, |
78
|
|
|
]; |
79
|
|
|
|
80
|
1 |
|
if (! isset($default_port[$protocol])) { |
81
|
1 |
|
throw new InvalidArgumentException("unsupported DBMS type: {$protocol}"); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
if ($port === null) { |
85
|
1 |
|
$port = $default_port[$protocol]; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
if ($host === null) { |
89
|
1 |
|
$host = "localhost"; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$this->protocol = $protocol; |
93
|
1 |
|
$this->dsn = "{$protocol}:host={$host};port={$port};dbname={$dbname}"; |
94
|
1 |
|
$this->username = $username; |
95
|
1 |
|
$this->password = $password; |
96
|
1 |
|
$this->options = $options ?: []; |
97
|
1 |
|
$this->host = $host; |
98
|
1 |
|
$this->port = $port; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return PDO |
103
|
|
|
*/ |
104
|
1 |
|
public function getPDO() |
105
|
|
|
{ |
106
|
1 |
|
if (! isset($this->pdo)) { |
107
|
1 |
|
$this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $this->pdo; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string database procol name (one of the PROTOCOL_* class constants) |
115
|
|
|
*/ |
116
|
1 |
|
public function getProtocol() |
117
|
|
|
{ |
118
|
1 |
|
return $this->protocol; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
1 |
|
public function getDSN() |
125
|
|
|
{ |
126
|
1 |
|
return $this->dsn; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
1 |
|
public function getUsername() |
133
|
|
|
{ |
134
|
1 |
|
return $this->username; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
1 |
|
public function getPassword() |
141
|
|
|
{ |
142
|
1 |
|
return $this->password; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
1 |
|
public function getOptions() |
149
|
|
|
{ |
150
|
1 |
|
return $this->options; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
1 |
|
public function getHost() |
157
|
|
|
{ |
158
|
1 |
|
return $this->host; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
1 |
|
public function getPort() |
165
|
|
|
{ |
166
|
1 |
|
return $this->port; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|