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