|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Micro |
|
6
|
|
|
* |
|
7
|
|
|
* @author Raffael Sahli <[email protected]> |
|
8
|
|
|
* @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Micro\Db\Wrapper; |
|
13
|
|
|
|
|
14
|
|
|
use \Psr\Log\LoggerInterface; |
|
15
|
|
|
use \Pdo as PdoServer; |
|
16
|
|
|
use \PDOStatement; |
|
17
|
|
|
|
|
18
|
|
|
class Pdo |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Logger |
|
22
|
|
|
* |
|
23
|
|
|
* @var Logger |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $logger; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Dsn |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $dsn = 'mysql:host=localhost;dbname=mysql'; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Username |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $username = 'root'; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Password |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $password = ''; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Driver specific options |
|
54
|
|
|
* |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $options = []; |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Connection resource |
|
62
|
|
|
* |
|
63
|
|
|
* @var resource |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $connection; |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Last inserted id |
|
70
|
|
|
* |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $last_inserted_ids; |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* construct |
|
78
|
|
|
* |
|
79
|
|
|
* @param Iterable $config |
|
80
|
|
|
* @param LoggerInterface $logger |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __construct(? Iterable $config, LoggerInterface $logger) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->setOptions($config); |
|
85
|
|
|
$this->logger = $logger; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Connect |
|
91
|
|
|
* |
|
92
|
|
|
* @return Pdo |
|
93
|
|
|
*/ |
|
94
|
|
|
public function connect(): Pdo |
|
95
|
|
|
{ |
|
96
|
|
|
$this->connection = new PdoServer($this->dsn, $this->username, $this->password, $this->options); |
|
|
|
|
|
|
97
|
|
|
$this->logger->info('connection to db server ['.$this->dsn.'] using pdo was succesful', [ |
|
98
|
|
|
'category' => get_class($this), |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Forward calls |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $method |
|
109
|
|
|
* @param array $arguments |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function __call(string $method, array $arguments = []) |
|
113
|
|
|
{ |
|
114
|
|
|
return call_user_func_array([&$this->connection, $method], $arguments); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Set options |
|
120
|
|
|
* |
|
121
|
|
|
* @param Iterable $config |
|
122
|
|
|
* @return Pdo |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setOptions(? Iterable $config = null) : Pdo |
|
125
|
|
|
{ |
|
126
|
|
|
if ($config === null) { |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
foreach ($config as $option => $value) { |
|
131
|
|
|
switch ($option) { |
|
132
|
|
|
case 'dsn': |
|
133
|
|
|
$this->dsn = (string)$value; |
|
134
|
|
|
break; |
|
135
|
|
|
case 'username': |
|
136
|
|
|
$this->username = (string)$value; |
|
137
|
|
|
break; |
|
138
|
|
|
case 'password': |
|
139
|
|
|
$this->password = (string)$value; |
|
140
|
|
|
break; |
|
141
|
|
|
case 'options': |
|
142
|
|
|
foreach ($value as $opt => $val) { |
|
143
|
|
|
$this->options[$opt] = (string)$val; |
|
144
|
|
|
} |
|
145
|
|
|
break; |
|
146
|
|
|
default: |
|
147
|
|
|
throw new Exception('invalid option '.$option.' given'); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get connection |
|
157
|
|
|
* |
|
158
|
|
|
* @return resource |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getResource() |
|
161
|
|
|
{ |
|
162
|
|
|
if ($this->connection === null) { |
|
163
|
|
|
$this->connect(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $this->connection; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Query |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $query |
|
174
|
|
|
* @return PDOStatement |
|
175
|
|
|
*/ |
|
176
|
|
|
public function select(string $query): PDOStatement |
|
177
|
|
|
{ |
|
178
|
|
|
$this->logger->debug('execute sql query ['.$query.']', [ |
|
179
|
|
|
'category' => get_class($this), |
|
180
|
|
|
]); |
|
181
|
|
|
|
|
182
|
|
|
$link = $this->getResource(); |
|
183
|
|
|
$result = $link->query($query); |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
if ($result === false) { |
|
186
|
|
|
throw new Exception('failed to execute sql query with error '.$link->errorInfo()[2].' ('.$link->errorCode().')'); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $result; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Select query |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $query |
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
|
|
public function query(string $query): bool |
|
200
|
|
|
{ |
|
201
|
|
|
$this->logger->debug('execute sql query ['.$query.']', [ |
|
202
|
|
|
'category' => get_class($this), |
|
203
|
|
|
]); |
|
204
|
|
|
|
|
205
|
|
|
$link = $this->getResource(); |
|
206
|
|
|
$result = $link->exec($query); |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
if ($result === false) { |
|
209
|
|
|
throw new Exception('failed to execute sql query with error '.$link->errorInfo().' ('.$link->errorCode().')'); |
|
|
|
|
|
|
210
|
|
|
} else { |
|
211
|
|
|
$this->logger->debug('sql query affected ['.$result.'] rows', [ |
|
212
|
|
|
'category' => get_class($this), |
|
213
|
|
|
]); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return true; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Prepare query |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $query |
|
224
|
|
|
* @param Iterable $values |
|
225
|
|
|
* @return mysqli_stmt |
|
226
|
|
|
*/ |
|
227
|
|
|
public function prepare(string $query, Iterable $values): mysqli_stmt |
|
228
|
|
|
{ |
|
229
|
|
|
$this->logger->debug('prepare and execute mysql query ['.$query.'] with values [{values}]', [ |
|
230
|
|
|
'category' => get_class($this), |
|
231
|
|
|
'values' => $values |
|
232
|
|
|
]); |
|
233
|
|
|
|
|
234
|
|
|
$link = $this->getResource(); |
|
235
|
|
|
$stmt = $link->prepare($query); |
|
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
if (!($stmt instanceof mysqli_stmt)) { |
|
|
|
|
|
|
238
|
|
|
throw new Exception('failed to prepare mysql query with error '.$link->error.' ('.$link->errno.')'); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$types = ''; |
|
242
|
|
|
foreach ($values as $attr => $value) { |
|
243
|
|
|
$types .= 's'; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$stmt->bind_param($types, ...$values); |
|
247
|
|
|
$stmt->execute(); |
|
248
|
|
|
|
|
249
|
|
|
return $stmt; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..