1
|
|
|
<?php namespace AgelxNash\Modx\Evo\Database\Drivers; |
2
|
|
|
|
3
|
|
|
use AgelxNash\Modx\Evo\Database\Exceptions; |
4
|
|
|
use mysqli; |
5
|
|
|
use mysqli_result; |
6
|
|
|
use mysqli_sql_exception; |
7
|
|
|
use mysqli_driver; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property mysqli $conn |
12
|
|
|
*/ |
13
|
|
|
class MySqliDriver extends AbstractDriver |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritDoc} |
17
|
|
|
*/ |
18
|
55 |
|
public function __construct(array $config = []) |
19
|
|
|
{ |
20
|
55 |
|
$driver = new mysqli_driver(); |
21
|
55 |
|
$driver->report_mode = MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ERROR; |
22
|
|
|
|
23
|
55 |
|
$this->setConfig($config); |
24
|
55 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc} |
28
|
|
|
* @return mysqli |
29
|
|
|
*/ |
30
|
32 |
|
public function getConnect() |
31
|
|
|
{ |
32
|
32 |
|
if (! $this->isConnected()) { |
33
|
32 |
|
return $this->connect(); |
34
|
|
|
} |
35
|
|
|
|
36
|
30 |
|
return $this->conn; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
32 |
|
public function isConnected() |
43
|
|
|
{ |
44
|
32 |
|
return ($this->conn instanceof mysqli); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
1 |
|
public function getLastError() |
51
|
|
|
{ |
52
|
1 |
|
return $this->getConnect()->error; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
3 |
|
public function getLastErrorNo() |
59
|
|
|
{ |
60
|
3 |
|
$out = (string)$this->getConnect()->sqlstate; |
61
|
|
|
|
62
|
3 |
|
if ($out === '00000') { |
63
|
1 |
|
$out = ''; |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
return $out; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
32 |
|
public function connect() |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
32 |
|
$this->conn = new mysqli( |
76
|
32 |
|
$this->getConfig('host'), |
77
|
32 |
|
$this->getConfig('username'), |
78
|
32 |
|
$this->getConfig('password'), |
79
|
32 |
|
$this->getConfig('database') |
80
|
|
|
); |
81
|
2 |
|
} catch (mysqli_sql_exception $exception) { |
82
|
2 |
|
$this->conn = null; |
83
|
2 |
|
throw new Exceptions\ConnectException($exception->getMessage(), $exception->getCode()); |
84
|
|
|
} |
85
|
|
|
|
86
|
31 |
|
return $this->conn; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
2 |
|
public function disconnect() |
93
|
|
|
{ |
94
|
2 |
|
if ($this->isConnected()) { |
95
|
2 |
|
$this->conn->close(); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$this->conn = null; |
99
|
|
|
|
100
|
2 |
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
*/ |
106
|
23 |
|
public function isResult($result) |
107
|
|
|
{ |
108
|
23 |
|
return $result instanceof mysqli_result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param mysqli_result $result |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
1 |
|
public function numFields($result) |
116
|
|
|
{ |
117
|
1 |
|
return $this->isResult($result) ? $result->field_count : 0; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param mysqli_result $result |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
1 |
|
public function fieldName($result, $col = 0) |
125
|
|
|
{ |
126
|
1 |
|
$field = $this->isResult($result) ? $result->fetch_field_direct($col) : []; |
127
|
|
|
|
128
|
1 |
|
return isset($field->name) ? $field->name : null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
* @throws \ReflectionException |
134
|
|
|
*/ |
135
|
30 |
|
public function setCharset($charset, $method = null) |
136
|
|
|
{ |
137
|
30 |
|
if ($method === null) { |
138
|
1 |
|
$method = $this->getConfig('method'); |
139
|
|
|
} |
140
|
|
|
|
141
|
30 |
|
$this->query($method . ' ' . $charset); |
142
|
|
|
|
143
|
30 |
|
return $this->getConnect()->set_charset($charset); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
1 |
|
public function selectDb($name) |
150
|
|
|
{ |
151
|
1 |
|
return $this->getConnect()->select_db($name); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritDoc} |
156
|
|
|
*/ |
157
|
1 |
|
public function escape($data) |
158
|
|
|
{ |
159
|
1 |
|
return $this->getConnect()->escape_string($data); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritDoc} |
164
|
|
|
* @return bool|mysqli_result |
165
|
|
|
* @throws \ReflectionException |
166
|
|
|
*/ |
167
|
30 |
|
public function query($sql) |
168
|
|
|
{ |
169
|
|
|
try { |
170
|
30 |
|
$result = $this->getConnect()->query($sql); |
171
|
3 |
|
} catch (mysqli_sql_exception $exception) { |
172
|
3 |
|
$reflect = new ReflectionClass($exception); |
173
|
3 |
|
$property = $reflect->getProperty('sqlstate'); |
174
|
3 |
|
$property->setAccessible(true); |
175
|
3 |
|
throw (new Exceptions\QueryException($exception->getMessage(), $property->getValue($exception))) |
176
|
3 |
|
->setQuery($sql); |
177
|
|
|
} |
178
|
|
|
|
179
|
30 |
|
return $result; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param mysqli_result $result |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
*/ |
186
|
17 |
|
public function getRecordCount($result) |
187
|
|
|
{ |
188
|
17 |
|
return $this->isResult($result) ? $result->num_rows : 0; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param mysqli_result $result |
193
|
|
|
* {@inheritDoc} |
194
|
|
|
*/ |
195
|
13 |
|
public function getRow($result, $mode = 'assoc') |
196
|
|
|
{ |
197
|
|
|
switch ($mode) { |
198
|
13 |
|
case 'assoc': |
199
|
7 |
|
$out = $result->fetch_assoc(); |
200
|
7 |
|
break; |
201
|
9 |
|
case 'num': |
202
|
8 |
|
$out = $result->fetch_row(); |
203
|
8 |
|
break; |
204
|
2 |
|
case 'object': |
205
|
1 |
|
$out = $result->fetch_object(); |
206
|
1 |
|
break; |
207
|
2 |
|
case 'both': |
208
|
1 |
|
$out = $result->fetch_array(MYSQLI_BOTH); |
209
|
1 |
|
break; |
210
|
|
|
default: |
211
|
1 |
|
throw new Exceptions\UnknownFetchTypeException( |
212
|
1 |
|
"Unknown get type ($mode) specified for fetchRow - must be empty, 'assoc', 'num' or 'both'." |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
13 |
|
return $out; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritDoc} |
221
|
|
|
*/ |
222
|
2 |
|
public function getVersion() |
223
|
|
|
{ |
224
|
2 |
|
return $this->getConnect()->server_info; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritDoc} |
229
|
|
|
*/ |
230
|
7 |
|
public function getInsertId() |
231
|
|
|
{ |
232
|
7 |
|
return $this->getConnect()->insert_id; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritDoc} |
237
|
|
|
*/ |
238
|
11 |
|
public function getAffectedRows() |
239
|
|
|
{ |
240
|
11 |
|
return $this->getConnect()->affected_rows; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param mysqli_result $result |
245
|
|
|
* @param int $position |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
1 |
|
public function dataSeek(&$result, $position) |
249
|
|
|
{ |
250
|
1 |
|
return $this->isResult($result) ? $result->data_seek($position) : false; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* {@inheritDoc} |
255
|
|
|
*/ |
256
|
|
|
public function begin ($flag = 0, $name = null) |
257
|
|
|
{ |
258
|
|
|
return $this->getConnect()->begin_transaction($flag, $name); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* {@inheritDoc} |
263
|
|
|
*/ |
264
|
|
|
public function commit ($flag = 0, $name = null) |
265
|
|
|
{ |
266
|
|
|
return $this->getConnect()->commit($flag, $name); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritDoc} |
271
|
|
|
*/ |
272
|
|
|
public function rollback ($flag = 0, $name = null) |
273
|
|
|
{ |
274
|
|
|
return $this->getConnect()->rollback($flag, $name); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|