-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathFakePdoTrait.php
230 lines (189 loc) · 5.46 KB
/
FakePdoTrait.php
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace Vimeo\MysqlEngine;
trait FakePdoTrait
{
/**
* @var Server
*/
private $server;
/**
* @var ?\PDO
*/
private $real = null;
/**
* @var string
*/
public $lastInsertId = "0";
/**
* @var bool
*/
public $stringifyResult = true;
/**
* @var bool
*/
public $lowercaseResultKeys = false;
/** @var ?int */
private $defaultFetchMode = null;
/**
* @var bool
*/
public $strict_mode = false;
/**
* @var ?string
* @readonly
*/
public $databaseName = null;
/**
* @param array<string> $options
*/
public function __construct(string $dsn, string $username = '', string $passwd = '', array $options = [])
{
//$this->real = new \PDO($dsn, $username, $passwd, $options);
$dsn = \Nyholm\Dsn\DsnParser::parse($dsn);
$host = $dsn->getHost();
if (preg_match('/dbname=([a-zA-Z0-9_]+);/', $host, $matches)) {
$this->databaseName = $matches[1];
}
// do a quick check for this string – hacky but fast
$this->strict_mode = \array_key_exists(\PDO::MYSQL_ATTR_INIT_COMMAND, $options)
&& \strpos($options[\PDO::MYSQL_ATTR_INIT_COMMAND], 'STRICT_ALL_TABLES');
$this->server = Server::getOrCreate('primary');
}
#[\ReturnTypeWillChange]
public function setAttribute($key, $value)
{
if ($key === \PDO::ATTR_EMULATE_PREPARES) {
$this->stringifyResult = (bool) $value;
}
if ($key === \PDO::ATTR_CASE && $value === \PDO::CASE_LOWER) {
$this->lowercaseResultKeys = true;
}
if ($key === \PDO::ATTR_DEFAULT_FETCH_MODE) {
if (!is_int($value)) {
throw new \PDOException("SQLSTATE[HY000]: General error: invalid fetch mode type");
}
$this->defaultFetchMode = $value;
}
if ($this->real && $key !== \PDO::ATTR_STATEMENT_CLASS) {
return $this->real->setAttribute($key, $value);
}
return true;
}
public function getServer() : Server
{
return $this->server;
}
public function getDatabaseName() : ?string
{
return $this->databaseName;
}
public function shouldStringifyResult(): bool
{
return $this->stringifyResult;
}
public function shouldLowercaseResultKeys(): bool
{
return $this->lowercaseResultKeys;
}
public function setLastInsertId(string $last_insert_id) : void
{
$this->lastInsertId = $last_insert_id;
}
public function lastInsertId($seqname = null) : string
{
if ($this->real) {
$real_last_insert_id = $this->real->lastInsertId($seqname);
if ($this->lastInsertId !== $real_last_insert_id) {
throw new \UnexpectedValueException(
'different last insert id – saw ' . $this->lastInsertId
. ' but MySQL produced ' . $real_last_insert_id
);
}
}
return $this->lastInsertId;
}
public function useStrictMode() : bool
{
return $this->strict_mode;
}
#[\ReturnTypeWillChange]
public function beginTransaction()
{
if (Server::hasSnapshot('transaction')) {
return false;
}
Server::snapshot('transaction');
return true;
}
#[\ReturnTypeWillChange]
public function commit()
{
return Server::deleteSnapshot('transaction');
}
#[\ReturnTypeWillChange]
public function rollback()
{
if (!Server::hasSnapshot('transaction')) {
return false;
}
Server::restoreSnapshot('transaction');
return true;
}
#[\ReturnTypeWillChange]
public function inTransaction()
{
return Server::hasSnapshot('transaction');
}
/**
* @param string $statement
* @return int|false
*/
#[\ReturnTypeWillChange]
public function exec($statement)
{
$statement = trim($statement);
if (strpos($statement, 'SET ')===0) {
return false;
}
$sth = $this->prepare($statement);
if ($sth->execute()) {
return $sth->rowCount();
}
return false;
}
/**
* @param string $string
* @param int $parameter_type
* @return string
*/
#[\ReturnTypeWillChange]
public function quote($string, $parameter_type = \PDO::PARAM_STR)
{
// @see https://github.com/php/php-src/blob/php-8.0.2/ext/mysqlnd/mysqlnd_charset.c#L860-L878
$quoted = strtr($string, [
"\0" => '\0',
"\n" => '\n',
"\r" => '\r',
"\\" => '\\\\',
"\'" => '\\\'',
"\"" => '\\"',
"\032" => '\Z',
]);
// @see https://github.com/php/php-src/blob/php-8.0.2/ext/pdo_mysql/mysql_driver.c#L307-L320
$quotes = ['\'', '\''];
/** @psalm-suppress MixedOperand */
if (defined('PDO::PARAM_STR_NATL') &&
(constant('PDO::PARAM_STR_NATL') & $parameter_type) === constant('PDO::PARAM_STR_NATL')
) {
$quotes[0] = 'N\'';
}
return "{$quotes[0]}{$quoted}{$quotes[1]}";
}
/**
* @return array{0: null|string, 1: int|null, 2: null|string, 3?: mixed, 4?: mixed}
*/
public function errorInfo(): array
{
return ['00000', 0, 'PHP MySQL Engine: errorInfo() not supported.'];
}
}