-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpudlMySqli.php
More file actions
291 lines (206 loc) · 7.99 KB
/
pudlMySqli.php
File metadata and controls
291 lines (206 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
if (!class_exists('pudl',false)) require_once(__DIR__.'/../pudl.php');
require_once(is_owner(__DIR__.'/pudlMyShared.php'));
require_once(is_owner(__DIR__.'/pudlMySqliResult.php'));
class pudlMySqli
extends pudlMyShared {
////////////////////////////////////////////////////////////////////////////
// OPENS A CONNECTION TO A MYSQL SERVER
// http://php.net/manual/en/function.mysqli-connect.php
////////////////////////////////////////////////////////////////////////////
public function connect() {
$auth = $this->auth();
pudl_require_extension('mysqli');
//USE EXISTING CONNECTION IF AVAILABLE
if ($auth['server'] instanceof mysqli){
$this->connection = $auth['server'];
$ok = true;
//CREATE A NEW CONNECTION OTHERWISE
} else {
$this->connection = mysqli_init();
$this->connection->options(MYSQLI_OPT_CONNECT_TIMEOUT, $auth['timeout']);
$this->connection->options(MYSQLI_OPT_READ_TIMEOUT, $auth['timeout']);
//ATTEMPT TO CREATE A CONNECTION
try {
$ok = @$this->connection->real_connect(
(empty($auth['persistent']) ? '' : 'p:') . $auth['server'],
$auth['username'],
$auth['password'],
$auth['database']
);
} catch (Exception $e) {
$ok = false;
}
}
//VERIFY WE CONNECTED OKAY!
if ($ok) $ok = ($this->connectErrno() === 0);
//ATTEMPT TO SET UTF8 CHARACTER SET
if ($ok) $ok = @$this->connection->set_charset('utf8mb4');
//CONNECTION IS GOOD!
if (!empty($ok)) {
$this->connected = $auth['server'];
$this->connection->options(
MYSQLI_OPT_READ_TIMEOUT,
ini_get('mysqlnd.net_read_timeout')
);
$this->strict()->timeout($auth);
return true;
}
//CANNOT CONNECT - ERROR OUT
throw new pudlConnectionException(
$this,
pudl::jsonEncode([
'message' => 'Database connection error',
'code' => $this->connectErrno(),
'error' => $this->connectError(),
'server' => $auth['server'],
'user' => $auth['username'],
],
$this->connectErrno())
);
}
////////////////////////////////////////////////////////////////////////////
// RECONNECT TO THE DATABASE SERVER
////////////////////////////////////////////////////////////////////////////
public function reconnect() {
if (empty($this->connected)) return false;
if (++$this->_depth > PUDL_RECURSION) {
throw new pudlRecursionException($this,
'Recursion limit reached in ' . __METHOD__
);
}
$this->disconnect(false);
$connect = $this->connect();
$this->trigger('reconnect', $connect);
//TODO: move this to a reconnect callback method
if ($connect) {
if ($this->inTransaction()) {
$result = $this->retryTransaction();
} else {
$result = $this->process($this->query());
}
}
$this->_depth--;
return $connect;
}
////////////////////////////////////////////////////////////////////////////
// CLOSES THE DATABASE CONNECTION
// http://php.net/manual/en/mysqli.close.php
////////////////////////////////////////////////////////////////////////////
public function disconnect($trigger=true) {
parent::disconnect($trigger);
if (!$this->connection) return;
@$this->connection->close();
$this->connection = NULL;
$this->connected = NULL;
}
////////////////////////////////////////////////////////////////////////////
// ESCAPES SPECIAL CHARACTERS IN A STRING FOR USE IN AN SQL STATEMENT
// http://php.net/manual/en/mysqli.real-escape-string.php
////////////////////////////////////////////////////////////////////////////
public function escape($value) {
if (!$this->connection) return false;
return @$this->connection->real_escape_string($value);
}
////////////////////////////////////////////////////////////////////////////
// PERFORMS A QUERY ON THE DATABASE AND RETURNS A PUDLRESULT
// http://php.net/manual/en/mysqli.query.php
////////////////////////////////////////////////////////////////////////////
protected function process($query) {
$result = $this->_query($query);
switch ($this->errno()) {
case 2006: // "MYSQL SERVER HAS GONE AWAY"
case 2013: // "LOST CONNECTION TO MYSQL SERVER DURING QUERY"
case 2062: // "READ TIMEOUT IS REACHED"
if ($result) $result->free();
$result = $this->reconnect();
break;
}
return new pudlMySqliResult($this,
$result instanceof mysqli_result
? $result
: NULL
);
}
////////////////////////////////////////////////////////////////////////////
// PERFORMS A QUERY ON THE DATABASE BYPASSING PUDL CALLS
// http://php.net/manual/en/mysqli.query.php
////////////////////////////////////////////////////////////////////////////
protected function _query($query) {
if (!$this->connection) return false;
if ($this->connection->connect_errno) return false;
return @$this->connection->query($query);
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE AUTO GENERATED ID USED IN THE LATEST QUERY
// http://php.net/manual/en/mysqli.insert-id.php
////////////////////////////////////////////////////////////////////////////
public function insertId() {
if (!$this->connection) return 0;
return $this->connection->insert_id;
}
////////////////////////////////////////////////////////////////////////////
// GETS THE NUMBER OF AFFECTED ROWS IN A PREVIOUS MYSQL OPERATION
// http://php.net/manual/en/mysqli.affected-rows.php
////////////////////////////////////////////////////////////////////////////
public function updated() {
if (!$this->connection) return 0;
return $this->connection->affected_rows;
}
////////////////////////////////////////////////////////////////////////////
// RETRIEVES INFORMATION ABOUT THE MOST RECENTLY EXECUTED QUERY
// http://php.net/manual/en/mysqli.info.php
////////////////////////////////////////////////////////////////////////////
public function queryinfo() {
if (!$this->connection) return [];
$info = explode(' ', $this->connection->info);
$array = [];
foreach ($info as $item) {
$parts = explode(': ', $item);
if (count($parts) < 2) continue;
$parts[0] = str_replace(' ', '_', strtolower($parts[0]));
$array[$parts[0]] = (int)$parts[1];
}
return $array;
}
////////////////////////////////////////////////////////////////////////////
// GET THE VERSION NUMBER INFO OF THE CONNECTED MYSQL/MARIADB SERVER
// http://php.net/manual/en/mysqli.get-server-info.php
////////////////////////////////////////////////////////////////////////////
public function info() {
if (!$this->connection) return NULL;
return $this->connection->server_info;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE ERROR CODE FOR THE MOST RECENT FUNCTION CALL
// http://php.net/manual/en/mysqli.errno.php
////////////////////////////////////////////////////////////////////////////
public function errno() {
if (!$this->connection) return @mysqli_connect_errno();
return $this->connection->errno;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS A STRING DESCRIPTION OF THE LAST ERROR
// http://php.net/manual/en/mysqli.error.php
////////////////////////////////////////////////////////////////////////////
public function error() {
if (!$this->connection) return @mysqli_connect_error();
return $this->connection->error;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS THE ERROR CODE FROM LAST CONNECT CALL
// http://php.net/manual/en/mysqli.connect-errno.php
////////////////////////////////////////////////////////////////////////////
public function connectErrno() {
if (!$this->connection) return @mysqli_connect_errno();
return $this->connection->connect_errno;
}
////////////////////////////////////////////////////////////////////////////
// RETURNS A STRING DESCRIPTION OF THE LAST CONNECT ERROR
// http://php.net/manual/en/mysqli.connect-error.php
////////////////////////////////////////////////////////////////////////////
public function connectError() {
if (!$this->connection) return @mysqli_connect_error();
return $this->connection->connect_error;
}
}