1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Connector for MySQL using the MySQLi method |
5
|
|
|
* @package framework |
6
|
|
|
* @subpackage model |
7
|
|
|
*/ |
8
|
|
|
class MySQLiConnector extends DBConnector { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Connection to the MySQL database |
12
|
|
|
* |
13
|
|
|
* @var MySQLi |
14
|
|
|
*/ |
15
|
|
|
protected $dbConn = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Name of the currently selected database |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $databaseName = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The most recent statement returned from MySQLiConnector->preparedQuery |
26
|
|
|
* |
27
|
|
|
* @var mysqli_stmt |
28
|
|
|
*/ |
29
|
|
|
protected $lastStatement = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Store the most recent statement for later use |
33
|
|
|
* |
34
|
|
|
* @param mysqli_stmt $statement |
35
|
|
|
*/ |
36
|
|
|
protected function setLastStatement($statement) { |
37
|
|
|
$this->lastStatement = $statement; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve a prepared statement for a given SQL string |
42
|
|
|
* |
43
|
|
|
* @param string $sql |
44
|
|
|
* @param boolean &$success |
45
|
|
|
* @return mysqli_stmt |
46
|
|
|
*/ |
47
|
|
|
public function prepareStatement($sql, &$success) { |
48
|
|
|
// Record last statement for error reporting |
49
|
|
|
$statement = $this->dbConn->stmt_init(); |
50
|
|
|
$this->setLastStatement($statement); |
51
|
|
|
$success = $statement->prepare($sql); |
52
|
|
|
return $statement; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function connect($parameters, $selectDB = false) { |
56
|
|
|
// Normally $selectDB is set to false by the MySQLDatabase controller, as per convention |
57
|
|
|
$selectedDB = ($selectDB && !empty($parameters['database'])) ? $parameters['database'] : null; |
58
|
|
|
|
59
|
|
|
// Connection charset and collation |
60
|
|
|
$connCharset = Config::inst()->get('MySQLDatabase', 'connection_charset'); |
61
|
|
|
$connCollation = Config::inst()->get('MySQLDatabase', 'connection_collation'); |
62
|
|
|
|
63
|
|
|
if(!empty($parameters['port'])) { |
64
|
|
|
$this->dbConn = new MySQLi( |
65
|
|
|
$parameters['server'], |
66
|
|
|
$parameters['username'], |
67
|
|
|
$parameters['password'], |
68
|
|
|
$selectedDB, |
69
|
|
|
$parameters['port'] |
70
|
|
|
); |
71
|
|
|
} else { |
72
|
|
|
$this->dbConn = new MySQLi( |
73
|
|
|
$parameters['server'], |
74
|
|
|
$parameters['username'], |
75
|
|
|
$parameters['password'], |
76
|
|
|
$selectedDB |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->dbConn->connect_error) { |
81
|
|
|
$this->databaseError("Couldn't connect to MySQL database | " . $this->dbConn->connect_error); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Set charset and collation if given and not null. Can explicitly set to empty string to omit |
85
|
|
|
$charset = isset($parameters['charset']) |
86
|
|
|
? $parameters['charset'] |
87
|
|
|
: $connCharset; |
88
|
|
|
|
89
|
|
|
if (!empty($charset)) $this->dbConn->set_charset($charset); |
90
|
|
|
|
91
|
|
|
$collation = isset($parameters['collation']) |
92
|
|
|
? $parameters['collation'] |
93
|
|
|
: $connCollation; |
94
|
|
|
|
95
|
|
|
if (!empty($collation)) $this->dbConn->query("SET collation_connection = {$collation}"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function __destruct() { |
99
|
|
|
if ($this->dbConn) { |
100
|
|
|
mysqli_close($this->dbConn); |
101
|
|
|
$this->dbConn = null; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function escapeString($value) { |
106
|
|
|
return $this->dbConn->real_escape_string($value); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function quoteString($value) { |
110
|
|
|
$value = $this->escapeString($value); |
111
|
|
|
return "'$value'"; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getVersion() { |
115
|
|
|
return $this->dbConn->server_info; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Invoked before any query is executed |
120
|
|
|
* |
121
|
|
|
* @param string $sql |
122
|
|
|
*/ |
123
|
|
|
protected function beforeQuery($sql) { |
|
|
|
|
124
|
|
|
// Clear the last statement |
125
|
|
|
$this->setLastStatement(null); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function query($sql, $errorLevel = E_USER_ERROR) { |
129
|
|
|
$this->beforeQuery($sql); |
130
|
|
|
|
131
|
|
|
// Benchmark query |
132
|
|
|
$handle = $this->dbConn->query($sql, MYSQLI_STORE_RESULT); |
133
|
|
|
|
134
|
|
|
if (!$handle || $this->dbConn->error) { |
135
|
|
|
$this->databaseError($this->getLastError(), $errorLevel, $sql); |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Some non-select queries return true on success |
140
|
|
|
return new MySQLQuery($this, $handle); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Prepares the list of parameters in preparation for passing to mysqli_stmt_bind_param |
145
|
|
|
* |
146
|
|
|
* @param array $parameters List of parameters |
147
|
|
|
* @param array &$blobs Out parameter for list of blobs to bind separately |
148
|
|
|
* @return array List of parameters appropriate for mysqli_stmt_bind_param function |
149
|
|
|
*/ |
150
|
|
|
public function parsePreparedParameters($parameters, &$blobs) { |
151
|
|
|
$types = ''; |
152
|
|
|
$values = array(); |
153
|
|
|
$blobs = array(); |
154
|
|
|
for($index = 0; $index < count($parameters); $index++) { |
|
|
|
|
155
|
|
|
$value = $parameters[$index]; |
156
|
|
|
$phpType = gettype($value); |
157
|
|
|
|
158
|
|
|
// Allow overriding of parameter type using an associative array |
159
|
|
|
if($phpType === 'array') { |
160
|
|
|
$phpType = $value['type']; |
161
|
|
|
$value = $value['value']; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// Convert php variable type to one that makes mysqli_stmt_bind_param happy |
165
|
|
|
// @see http://www.php.net/manual/en/mysqli-stmt.bind-param.php |
166
|
|
|
switch($phpType) { |
167
|
|
|
case 'boolean': |
168
|
|
|
case 'integer': |
169
|
|
|
$types .= 'i'; |
170
|
|
|
break; |
171
|
|
|
case 'float': // Not actually returnable from gettype |
172
|
|
|
case 'double': |
173
|
|
|
$types .= 'd'; |
174
|
|
|
break; |
175
|
|
|
case 'object': // Allowed if the object or resource has a __toString method |
176
|
|
|
case 'resource': |
177
|
|
|
case 'string': |
178
|
|
|
case 'NULL': // Take care that a where clause should use "where XX is null" not "where XX = null" |
179
|
|
|
$types .= 's'; |
180
|
|
|
break; |
181
|
|
|
case 'blob': |
182
|
|
|
$types .= 'b'; |
183
|
|
|
// Blobs must be sent via send_long_data and set to null here |
184
|
|
|
$blobs[] = array( |
185
|
|
|
'index' => $index, |
186
|
|
|
'value' => $value |
187
|
|
|
); |
188
|
|
|
$value = null; |
189
|
|
|
break; |
190
|
|
|
case 'array': |
191
|
|
|
case 'unknown type': |
192
|
|
|
default: |
193
|
|
|
user_error("Cannot bind parameter \"$value\" as it is an unsupported type ($phpType)", |
194
|
|
|
E_USER_ERROR); |
195
|
|
|
break; |
196
|
|
|
} |
197
|
|
|
$values[] = $value; |
198
|
|
|
} |
199
|
|
|
return array_merge(array($types), $values); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Binds a list of parameters to a statement |
204
|
|
|
* |
205
|
|
|
* @param mysqli_stmt $statement MySQLi statement |
206
|
|
|
* @param array $parameters List of parameters to pass to bind_param |
207
|
|
|
*/ |
208
|
|
|
public function bindParameters(mysqli_stmt $statement, array $parameters) { |
209
|
|
|
// Because mysqli_stmt::bind_param arguments must be passed by reference |
210
|
|
|
// we need to do a bit of hackery |
211
|
|
|
for ($i = 0; $i < count($parameters); $i++) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$boundName = "param$i"; |
214
|
|
|
$$boundName = $parameters[$i]; |
215
|
|
|
$boundNames[] = &$$boundName; |
|
|
|
|
216
|
|
|
} |
217
|
|
|
call_user_func_array( array($statement, 'bind_param'), $boundNames); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR) { |
221
|
|
|
// Shortcut to basic query when not given parameters |
222
|
|
|
if(empty($parameters)) { |
223
|
|
|
return $this->query($sql, $errorLevel); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->beforeQuery($sql); |
227
|
|
|
|
228
|
|
|
// Type check, identify, and prepare parameters for passing to the statement bind function |
229
|
|
|
$parsedParameters = $this->parsePreparedParameters($parameters, $blobs); |
230
|
|
|
|
231
|
|
|
// Benchmark query |
232
|
|
|
$statement = $this->prepareStatement($sql, $success); |
233
|
|
|
if($success) { |
234
|
|
|
if($parsedParameters) { |
|
|
|
|
235
|
|
|
$this->bindParameters($statement, $parsedParameters); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// Bind any blobs given |
239
|
|
|
foreach($blobs as $blob) { |
240
|
|
|
$statement->send_long_data($blob['index'], $blob['value']); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// Safely execute the statement |
244
|
|
|
$statement->execute(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if (!$success || $statement->error) { |
248
|
|
|
$values = $this->parameterValues($parameters); |
249
|
|
|
$this->databaseError($this->getLastError(), $errorLevel, $sql, $values); |
250
|
|
|
return null; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
// Non-select queries will have no result data |
254
|
|
|
$metaData = $statement->result_metadata(); |
255
|
|
|
if($metaData) { |
256
|
|
|
return new MySQLStatement($statement, $metaData); |
257
|
|
|
} else { |
258
|
|
|
// Replicate normal behaviour of ->query() on non-select calls |
259
|
|
|
return new MySQLQuery($this, true); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function selectDatabase($name) { |
264
|
|
|
if ($this->dbConn->select_db($name)) { |
265
|
|
|
$this->databaseName = $name; |
266
|
|
|
return true; |
267
|
|
|
} else { |
268
|
|
|
return false; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function getSelectedDatabase() { |
273
|
|
|
return $this->databaseName; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function unloadDatabase() { |
277
|
|
|
$this->databaseName = null; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function isActive() { |
281
|
|
|
return $this->databaseName && $this->dbConn && empty($this->dbConn->connect_error); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function affectedRows() { |
285
|
|
|
return $this->dbConn->affected_rows; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function getGeneratedID($table) { |
289
|
|
|
return $this->dbConn->insert_id; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function getLastError() { |
293
|
|
|
// Check if a statement was used for the most recent query |
294
|
|
|
if($this->lastStatement && $this->lastStatement->error) { |
295
|
|
|
return $this->lastStatement->error; |
296
|
|
|
} |
297
|
|
|
return $this->dbConn->error; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.