forked from auraphp/Aura.Sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedPdoPHP83.php
More file actions
163 lines (149 loc) · 3.76 KB
/
ExtendedPdoPHP83.php
File metadata and controls
163 lines (149 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license https://opensource.org/licenses/MIT MIT
*
*/
namespace Aura\Sql;
use Aura\Sql\Profiler\Profiler;
use Aura\Sql\Profiler\ProfilerInterface;
use PDO;
/**
*
* A lazy-connecting PDO with extended methods.
*
* @package Aura.Sql
*
*/
class ExtendedPdo extends AbstractExtendedPdo
{
/**
*
* Constructor arguments for instantiating the PDO connection.
*
* @var array
*
*/
protected array $args = [];
/**
*
* Constructor.
*
* This overrides the parent so that it can take connection attributes as a
* constructor parameter, and set them after connection.
*
* @param string $dsn The data source name for the connection.
*
* @param string|null $username The username for the connection.
*
* @param string|null $password The password for the connection.
*
* @param array $options Driver-specific options for the connection.
*
* @param array $queries Queries to execute after the connection.
*
* @param \Aura\Sql\Profiler\ProfilerInterface|null $profiler Tracks and logs query profiles.
*
* @see http://php.net/manual/en/pdo.construct.php
*/
public function __construct(
#[\SensitiveParameter] string $dsn,
?string $username = null,
#[\SensitiveParameter] ?string $password = null,
array $options = [],
array $queries = [],
?ProfilerInterface $profiler = null
) {
// if no error mode is specified, use exceptions
if (! isset($options[PDO::ATTR_ERRMODE])) {
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
}
// retain the arguments for later
$this->args = [
$dsn,
$username,
$password,
$options,
$queries
];
// retain a profiler, instantiating a default one if needed
$this->setProfiler($profiler ?? new Profiler());
// retain a query parser
$parts = explode(":", $dsn);
$parser = $this->newParser($parts[0]);
$this->setParser($parser);
// set quotes for identifier names
$this->setQuoteName($parts[0]);
}
public function connect(): void
{
throw new \RuntimeException('The connect() method is no longer supported in Aura.Sql v6. Use lazyConnect() instead.');
}
/**
*
* Connects to the database.
*
* @return void
*/
public function lazyConnect(): void
{
if ($this->pdo) {
return;
}
// connect
$this->profiler->start(__FUNCTION__);
list($dsn, $username, $password, $options, $queries) = $this->args;
$this->pdo = new PDO($dsn, $username, $password, $options);
$this->profiler->finish();
// connection-time queries
foreach ($queries as $query) {
$this->exec($query);
}
}
/**
*
* Disconnects from the database.
*
* @return void
*
*/
public function disconnect(): void
{
$this->profiler->start(__FUNCTION__);
$this->pdo = null;
$this->profiler->finish();
}
/**
*
* The purpose of this method is to hide sensitive data from stack traces.
*
* @return array
*
*/
public function __debugInfo(): array
{
return [
'args' => [
$this->args[0],
'****',
'****',
$this->args[3],
$this->args[4],
],
];
}
/**
*
* Return the inner PDO (if any)
*
* @return \PDO
*
*/
public function getPdo(): PDO
{
$this->lazyConnect();
return $this->pdo;
}
}