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
|
|
|
private string $protocol; |
25
|
|
|
|
26
|
|
|
private string $dsn; |
27
|
|
|
|
28
|
|
|
private string $username; |
29
|
|
|
|
30
|
|
|
private string $password; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array<int,mixed> |
34
|
|
|
*/ |
35
|
|
|
private array $options; |
36
|
|
|
|
37
|
|
|
private string $host; |
38
|
|
|
|
39
|
|
|
private int $port; |
40
|
|
|
|
41
|
|
|
private PDO|null $pdo = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $protocol database protocol name (one of the PROTOCOL_* class constants) |
45
|
|
|
* @param string $dbname name of the database to connect to |
46
|
|
|
* @param string $username |
47
|
|
|
* @param string $password |
48
|
|
|
* @param array<int,mixed>|null $options PDO constructor options (attributes) |
49
|
|
|
* @param string|null $host optional hostname; defaults to "localhost" |
50
|
|
|
* @param int|null $port optional port-number; defaults to the standard port-number for the given $db type |
51
|
|
|
*/ |
52
|
1 |
|
public function __construct(string $protocol, string $dbname, string $username, string $password, array|null $options = null, string|null $host = null, int|null $port = null) |
53
|
|
|
{ |
54
|
1 |
|
static $default_port = [ |
55
|
1 |
|
self::PROTOCOL_MYSQL => 3306, |
56
|
1 |
|
self::PROTOCOL_POSTGRES => 5432, |
57
|
1 |
|
]; |
58
|
|
|
|
59
|
1 |
|
if (! isset($default_port[$protocol])) { |
60
|
1 |
|
throw new InvalidArgumentException("unsupported DBMS type: {$protocol}"); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
if ($port === null) { |
64
|
|
|
$port = $default_port[$protocol]; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
if ($host === null) { |
68
|
|
|
$host = "localhost"; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$this->protocol = $protocol; |
72
|
1 |
|
$this->dsn = "{$protocol}:host={$host};port={$port};dbname={$dbname}"; |
73
|
1 |
|
$this->username = $username; |
74
|
1 |
|
$this->password = $password; |
75
|
1 |
|
$this->options = $options ?: []; |
76
|
1 |
|
$this->host = $host; |
77
|
1 |
|
$this->port = $port; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return PDO |
82
|
|
|
*/ |
83
|
1 |
|
public function getPDO(): PDO |
84
|
|
|
{ |
85
|
1 |
|
if (! isset($this->pdo)) { |
86
|
1 |
|
$this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return $this->pdo; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string database procol name (one of the PROTOCOL_* class constants) |
94
|
|
|
*/ |
95
|
1 |
|
public function getProtocol(): string |
96
|
|
|
{ |
97
|
1 |
|
return $this->protocol; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function getDSN(): string |
101
|
|
|
{ |
102
|
1 |
|
return $this->dsn; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function getUsername(): string |
106
|
|
|
{ |
107
|
1 |
|
return $this->username; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function getPassword(): string |
111
|
|
|
{ |
112
|
1 |
|
return $this->password; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array<int,mixed> |
117
|
|
|
*/ |
118
|
1 |
|
public function getOptions(): array |
119
|
|
|
{ |
120
|
1 |
|
return $this->options; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function getHost(): string |
124
|
|
|
{ |
125
|
1 |
|
return $this->host; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
public function getPort(): int |
129
|
|
|
{ |
130
|
1 |
|
return $this->port; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|