1
|
|
|
<?php namespace AgelxNash\Modx\Evo\Database\Drivers; |
2
|
|
|
|
3
|
|
|
use AgelxNash\Modx\Evo\Database\Interfaces\DriverInterface; |
4
|
|
|
use AgelxNash\Modx\Evo\Database\Exceptions; |
5
|
|
|
use mysqli; |
6
|
|
|
use mysqli_result; |
7
|
|
|
use mysqli_sql_exception; |
8
|
|
|
use mysqli_driver; |
9
|
|
|
|
10
|
|
|
class MySqliDriver implements DriverInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var mysqli |
14
|
|
|
*/ |
15
|
|
|
protected $conn; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
*/ |
25
|
38 |
|
public function __construct(array $config = []) |
26
|
|
|
{ |
27
|
38 |
|
$driver = new mysqli_driver(); |
28
|
38 |
|
$driver->report_mode = MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ERROR; |
29
|
|
|
|
30
|
38 |
|
$this->config = $config; |
31
|
38 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
* @throws Exceptions\Exception |
36
|
|
|
*/ |
37
|
18 |
|
public function getConnect() |
38
|
|
|
{ |
39
|
18 |
|
if (! $this->isConnected()) { |
40
|
|
|
return $this->connect(); |
41
|
|
|
} |
42
|
|
|
|
43
|
18 |
|
return $this->conn; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return mysqli |
48
|
|
|
* @throws Exceptions\Exception |
49
|
|
|
*/ |
50
|
19 |
|
public function connect() : mysqli |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
19 |
|
$this->conn = new mysqli( |
54
|
19 |
|
$this->config['host'], |
55
|
19 |
|
$this->config['user'], |
56
|
19 |
|
$this->config['pass'], |
57
|
19 |
|
$this->config['base'] |
58
|
|
|
); |
59
|
|
|
|
60
|
18 |
|
if ($this->isConnected() && $this->getConnect()->connect_error) { |
61
|
|
|
throw new Exceptions\ConnectException($this->conn->connect_error); |
62
|
|
|
} |
63
|
|
|
|
64
|
18 |
|
if (! $this->isConnected()) { |
65
|
|
|
throw new Exceptions\ConnectException( |
66
|
18 |
|
$this->getLastError() ?: 'Failed to create the database connection!' |
|
|
|
|
67
|
|
|
); |
68
|
|
|
} |
69
|
1 |
|
} catch (mysqli_sql_exception $exception) { |
70
|
1 |
|
throw new Exceptions\ConnectException($exception->getMessage(), $exception->getCode()); |
71
|
|
|
} |
72
|
|
|
|
73
|
18 |
|
$this->setCharset($this->config['charset'], $this->config['method']); |
|
|
|
|
74
|
|
|
|
75
|
18 |
|
return $this->conn; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
1 |
|
public function disconnect() : DriverInterface |
82
|
|
|
{ |
83
|
1 |
|
if ($this->isConnected()) { |
84
|
1 |
|
$this->conn->close(); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$this->conn = null; |
88
|
|
|
|
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
19 |
|
public function isConnected() : bool |
96
|
|
|
{ |
97
|
19 |
|
return ($this->conn instanceof mysqli); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $data |
102
|
|
|
* @return mixed |
103
|
|
|
* @throws Exceptions\Exception |
104
|
|
|
*/ |
105
|
1 |
|
public function escape($data) |
106
|
|
|
{ |
107
|
1 |
|
return $this->getConnect()->escape_string($data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return mixed |
112
|
|
|
* @throws Exceptions\Exception |
113
|
|
|
*/ |
114
|
3 |
|
public function getInsertId() |
115
|
|
|
{ |
116
|
3 |
|
return $this->getConnect()->insert_id; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
121
|
|
|
* @throws Exceptions\Exception |
122
|
|
|
*/ |
123
|
7 |
|
public function getAffectedRows() : int |
124
|
|
|
{ |
125
|
7 |
|
return $this->getConnect()->affected_rows; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
* @throws Exceptions\Exception |
131
|
|
|
*/ |
132
|
1 |
|
public function getVersion() : string |
133
|
|
|
{ |
134
|
1 |
|
return $this->getConnect()->server_info; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param mysqli_result $result |
139
|
|
|
* @return int |
140
|
|
|
*/ |
141
|
9 |
|
public function getRecordCount($result) : int |
142
|
|
|
{ |
143
|
9 |
|
return $result->num_rows; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $charset |
148
|
|
|
* @return bool |
149
|
|
|
* @throws Exceptions\Exception |
150
|
|
|
*/ |
151
|
18 |
|
public function setCharset(string $charset) : bool |
152
|
|
|
{ |
153
|
18 |
|
return $this->getConnect()->set_charset($charset); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $result |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
10 |
|
public function isResult($result) : bool |
161
|
|
|
{ |
162
|
10 |
|
return $result instanceof mysqli_result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param mysqli_result $result |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
1 |
|
public function numFields($result) : int |
170
|
|
|
{ |
171
|
1 |
|
return $result->field_count; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param mysqli_result $result |
176
|
|
|
* @param int $col |
177
|
|
|
* @return string|null |
178
|
|
|
*/ |
179
|
1 |
|
public function fieldName($result, $col = 0) :? string |
180
|
|
|
{ |
181
|
1 |
|
$field = $result->fetch_field_direct($col); |
182
|
|
|
|
183
|
1 |
|
return $field->name ?? null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $name |
188
|
|
|
* @return bool |
189
|
|
|
* @throws Exceptions\Exception |
190
|
|
|
*/ |
191
|
|
|
public function selectDb(string $name) : bool |
192
|
|
|
{ |
193
|
|
|
return $this->getConnect()->select_db($name); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param mysqli_result $result |
198
|
|
|
* @param string $mode |
199
|
|
|
* @return array|mixed|object|\stdClass |
200
|
|
|
* @throws Exceptions\Exception |
201
|
|
|
*/ |
202
|
7 |
|
public function getRow($result, $mode = 'assoc') |
203
|
|
|
{ |
204
|
|
|
switch ($mode) { |
205
|
7 |
|
case 'assoc': |
206
|
5 |
|
$out = $result->fetch_assoc(); |
207
|
5 |
|
break; |
208
|
3 |
|
case 'num': |
209
|
3 |
|
$out = $result->fetch_row(); |
210
|
3 |
|
break; |
211
|
|
|
case 'object': |
212
|
|
|
$out = $result->fetch_object(); |
213
|
|
|
break; |
214
|
|
|
case 'both': |
215
|
|
|
$out = $result->fetch_array(MYSQLI_BOTH); |
216
|
|
|
break; |
217
|
|
|
default: |
218
|
|
|
throw new Exceptions\UnknownFetchTypeException( |
219
|
|
|
"Unknown get type ($mode) specified for fetchRow - must be empty, 'assoc', 'num' or 'both'." |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
7 |
|
return $out; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string $query |
228
|
|
|
* @return mixed |
229
|
|
|
* @throws Exceptions\Exception |
230
|
|
|
*/ |
231
|
13 |
|
public function query(string $query) |
232
|
|
|
{ |
233
|
|
|
try { |
234
|
13 |
|
$result = $this->getConnect()->query($query); |
235
|
2 |
|
} catch (mysqli_sql_exception $exception) { |
236
|
2 |
|
throw (new Exceptions\QueryException($exception->getMessage(), $exception->getCode())) |
237
|
2 |
|
->setQuery($query); |
238
|
|
|
} |
239
|
|
|
|
240
|
13 |
|
return $result; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $name |
245
|
|
|
* @param $result |
246
|
|
|
* @return array |
247
|
|
|
* @throws Exceptions\Exception |
248
|
|
|
*/ |
249
|
3 |
|
public function getColumn(string $name, $result) : array |
250
|
|
|
{ |
251
|
3 |
|
$col = []; |
252
|
|
|
|
253
|
3 |
|
if ($result instanceof mysqli_result) { |
254
|
3 |
|
while ($row = $this->getRow($result)) { |
255
|
3 |
|
$col[] = $row[$name]; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
3 |
|
return $col; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param $result |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
1 |
|
public function getColumnNames($result) : array |
267
|
|
|
{ |
268
|
1 |
|
$names = []; |
269
|
|
|
|
270
|
1 |
|
if ($result instanceof mysqli_result) { |
271
|
1 |
|
$limit = $this->numFields($result); |
272
|
1 |
|
for ($i = 0; $i < $limit; $i++) { |
273
|
1 |
|
$names[] = $this->fieldName($result, $i); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
1 |
|
return $names; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param $result |
282
|
|
|
* @return bool|mixed |
283
|
|
|
* @throws Exceptions\Exception |
284
|
|
|
*/ |
285
|
3 |
|
public function getValue($result) |
286
|
|
|
{ |
287
|
3 |
|
$out = false; |
288
|
|
|
|
289
|
3 |
|
if ($result instanceof mysqli_result) { |
290
|
3 |
|
$result = $this->getRow($result, 'num'); |
291
|
3 |
|
$out = $result[0] ?? false; |
292
|
|
|
} |
293
|
|
|
|
294
|
3 |
|
return $out; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param $result |
299
|
|
|
* @return array|mixed |
300
|
|
|
* @throws Exceptions\Exception |
301
|
|
|
*/ |
302
|
1 |
|
public function getTableMetaData($result) |
303
|
|
|
{ |
304
|
1 |
|
$out = []; |
305
|
|
|
|
306
|
1 |
|
if ($result instanceof mysqli_result) { |
307
|
1 |
|
while ($row = $this->getRow($result)) { |
308
|
1 |
|
$fieldName = $row['Field']; |
309
|
1 |
|
$out[$fieldName] = $row; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
1 |
|
return $out; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.