forked from ezSQL/ezsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathez_mysqli.php
More file actions
551 lines (464 loc) · 15.9 KB
/
ez_mysqli.php
File metadata and controls
551 lines (464 loc) · 15.9 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
declare(strict_types=1);
namespace ezsql\Database;
use Exception;
use ezsql\Db;
use ezsql\ezsqlModel;
use ezsql\ConfigInterface;
use ezsql\DatabaseInterface;
class ez_mysqli extends ezsqlModel implements DatabaseInterface
{
private $return_val = 0;
private $is_insert = false;
private $isTransactional = false;
/**
* Database connection handle
* @var \mysqli
*/
private $dbh;
/**
* Query result
* @var mixed
*/
private $result;
/**
* Database configuration setting
* @var ConfigInterface
*/
private $database;
protected $shortcutUsed = false;
public function __construct(ConfigInterface $settings = null)
{
if (empty($settings)) {
throw new Exception(\MISSING_CONFIGURATION);
}
parent::__construct();
$this->database = $settings;
if (!Db::has('ez' . \MYSQLI))
Db::set('ez' . \MYSQLI, $this);
Db::set('global', $this);
} // __construct
public function settings()
{
return $this->database;
}
/**
* Short hand way to connect to mysql database server and select a mysql
* database at the same time
*
* @param string $user The database user name
* @param string $password The database users password
* @param string $name The name of the database
* @param string $host The host name or IP address of the database server.
* Default is localhost
* @param string $charset Encoding of the database
* @return boolean
*/
public function quick_connect(
string $user = '',
string $password = '',
string $name = '',
string $host = '',
$port = '',
string $charset = ''
) {
$user = empty($user) ? $this->database->getUser() : $user;
$password = empty($password) ? $this->database->getPassword() : $password;
$name = empty($name) ? $this->database->getName() : $name;
$host = empty($host) ? $this->database->getHost() : $host;
$port = empty($port) ? $this->database->getPort() : $port;
$charset = empty($charset) ? $this->database->getCharset() : $charset;
if (!$this->connect($user, $password, $host, (int) $port, $charset));
else if (!$this->dbSelect($name, $charset));
return $this->_connected;
} // quick_connect
/**
* Try to connect to mySQLi database server
*
* @param string $user The database user name
* @param string $password The database users password
* @param string $host The host name or IP address of the database server.
* Default is localhost
* @param string $charset The database charset
* Default is empty string
* @return boolean
*/
public function connect(
string $user = '',
string $password = '',
string $host = '',
$port = '',
string $charset = ''
) {
$this->_connected = false;
$user = empty($user) ? $this->database->getUser() : $user;
$password = empty($password) ? $this->database->getPassword() : $password;
$host = empty($host) ? $this->database->getHost() : $host;
$port = empty($port) ? $this->database->getPort() : $port;
$charset = empty($charset) ? $this->database->getCharset() : $charset;
// Try to establish the server database handle
if (!$this->dbh = \mysqli_connect($host, $user, $password, $this->database->getName(), (int) $port)) {
$this->register_error(\FAILED_CONNECTION . ' in ' . __FILE__ . ' on line ' . __LINE__);
} else {
\mysqli_set_charset($this->dbh, $charset);
$this->_connected = true;
}
return $this->_connected;
} // connect
/**
* Try to select the default database for mySQL
*
* @param string $name The name of the database
* @param string $charset Encoding of the database
* @return boolean
*/
public function dbSelect($name = '', $charset = '')
{
$name = empty($name) ? $this->database->getName() : $name;
try {
// Try to connect to the database
if (($this->dbh === null) || ($this->_connected === false) || !\mysqli_select_db($this->dbh, $name)) {
throw new Exception("Error Processing Request", 1);
}
$this->database->setName($name);
if ($charset == '') {
$charset = $this->database->getCharset();
}
if ($charset != '') {
$encoding = \strtolower(\str_replace('-', '', $charset));
$charsetArray = array();
$recordSet = \mysqli_query($this->dbh, 'SHOW CHARACTER SET');
while ($row = \mysqli_fetch_array($recordSet, \MYSQLI_ASSOC)) {
$charsetArray[] = $row['Charset'];
}
if (\in_array($charset, $charsetArray)) {
\mysqli_query($this->dbh, 'SET NAMES \'' . $encoding . '\'');
}
}
return true;
} catch (\Throwable $e) {
$str = \FAILED_CONNECTION;
// Must have an active database connection
if ($this->dbh && $this->_connected) {
// Try to get error supplied by mysql if not use our own
if (!$str = \mysqli_error($this->dbh)) {
$str = 'Unexpected error while trying to select database';
}
}
$this->register_error($str . ' in ' . __FILE__ . ' on line ' . __LINE__);
return false;
}
} // select
/**
* Format a mySQL string correctly for safe mySQL insert
* (no matter if magic quotes are on or not)
*
* @param string $str
* @return string
*/
public function escape(string $str)
{
return \mysqli_real_escape_string($this->dbh, \stripslashes($str));
} // escape
/**
* Return mySQLi specific system date syntax
* i.e. Oracle: SYSDATE Mysql: NOW()
*
* @return string
*/
public function sysDate()
{
return 'NOW()';
}
/**
* Helper fetches rows from a prepared result set
* @param \mysqli_stmt $stmt
* @param string $query
* @return bool|\mysqli_result
*/
private function fetch_prepared_result(&$stmt, $query)
{
if ($stmt instanceof \mysqli_stmt) {
$stmt->store_result();
$variables = array();
$is_insert = false;
$col_info = array();
if (\preg_match("/^(insert|delete|update|replace)\s+/i", $query)) {
$this->_affectedRows = \mysqli_stmt_affected_rows($stmt);
// Take note of the insert id
if (\preg_match("/^(insert|replace)\s+/i", $query)) {
$this->insertId = $stmt->insert_id;
}
} else {
$this->_affectedRows = $stmt->num_rows;
$meta = $stmt->result_metadata();
$x = 0;
// Take note of column info
while ($field = $meta->fetch_field()) {
$col_info[$field->name] = "";
$variables[$field->name] = &$col_info[$field->name];
$this->colInfo[$x] = $field;
$x++;
}
// Binds variables to a prepared statement for result storage
\call_user_func_array([$stmt, 'bind_result'], \array_values($variables));
$i = 0;
// Store Query Results
while ($stmt->fetch()) {
// Store results as an objects within main array
$resultObject = new \stdClass();
foreach ($variables as $key => $value) {
$resultObject->$key = $value;
}
$this->lastResult[$i] = $resultObject;
$i++;
}
}
// If there is an error then take note of it..
if ($str = $stmt->error) {
$is_insert = true;
$this->register_error($str);
// If debug ALL queries
$this->trace || $this->debugAll ? $this->debug() : null;
return false;
}
// Return number of rows affected
$return_val = $this->_affectedRows;
// disk caching of queries
$this->store_cache($query, $is_insert);
// If debug ALL queries
$this->trace || $this->debugAll ? $this->debug() : null;
return $return_val;
}
return false;
}
/**
* Creates a prepared query, binds the given parameters and returns the result of the executed
* {@link mysqli_stmt}.
* @param string $query
* @param array $param
* @return bool|\mysqli_result
*/
public function query_prepared(string $query, array $param = null)
{
$stmt = $this->dbh->prepare($query);
if (!$stmt instanceof \mysqli_stmt) {
if ($this->isTransactional)
throw new \Exception($this->lastError);
return false;
}
$params = [];
$types = \array_reduce(
$param,
function ($string, $arg) use (&$params) {
$params[] = &$arg;
if (\is_float($arg))
$string .= 'd';
elseif (\is_integer($arg))
$string .= 'i';
elseif (\is_string($arg))
$string .= 's';
else
$string .= 'b';
return $string;
},
''
);
\array_unshift($params, $types);
\call_user_func_array([$stmt, 'bind_param'], $params);
$result = ($stmt->execute()) ? $this->fetch_prepared_result($stmt, $query) : false;
// free and closes a prepared statement
$stmt->free_result();
$stmt->close();
return $result;
}
/**
* Perform post processing on SQL query call
*
* @param string $query
* @param mixed $result
* @return bool|void
*/
private function processQueryResult(string $query, $result = null)
{
$this->shortcutUsed = false;
if (!empty($result))
$this->result = $result;
// If there is an error then take note of it..
if ($str = \mysqli_error($this->dbh)) {
$this->register_error($str);
// If debug ALL queries
$this->trace || $this->debugAll ? $this->debug() : null;
return false;
}
// Query was an insert, delete, update, replace
$this->is_insert = false;
if (\preg_match("/^(insert|delete|update|replace)\s+/i", $query)) {
$this->is_insert = true;
$this->_affectedRows = \mysqli_affected_rows($this->dbh);
// Take note of the insert_id
if (\preg_match("/^(insert|replace)\s+/i", $query)) {
$this->insertId = \mysqli_insert_id($this->dbh);
}
// Return number of rows affected
$this->return_val = $this->_affectedRows;
} else {
// Query was a select
if (!\is_numeric($this->result) && !\is_bool($this->result)) {
// Take note of column info
$i = 0;
while ($i < \mysqli_num_fields($this->result)) {
$this->colInfo[$i] = \mysqli_fetch_field($this->result);
$i++;
}
// Store Query Results
$num_rows = 0;
while ($row = \mysqli_fetch_object($this->result)) {
// Store results as an objects within main array
$this->lastResult[$num_rows] = $row;
$num_rows++;
}
\mysqli_free_result($this->result);
// Log number of rows the query returned
$this->numRows = $num_rows;
// Return number of rows selected
$this->return_val = $this->numRows;
}
}
}
/**
* Perform mySQL query and try to determine result value
*
* @param string $query
* @param bool $use_prepare
* @return bool|mixed
*/
public function query(string $query, bool $use_prepare = false)
{
$param = [];
if ($use_prepare)
$param = $this->prepareValues();
// check for ezQuery placeholder tag and replace tags with proper prepare tag
$query = \str_replace(\_TAG, '?', $query);
// Initialize return
$this->return_val = 0;
// Flush cached values..
$this->flush();
// For reg expressions
$query = \trim($query);
// Log how the function was called
$this->log_query("\$db->query(\"$query\")");
// Keep track of the last query for debug..
$this->lastQuery = $query;
// Count how many queries there have been
$this->numQueries++;
// Use core file cache function
if ($cache = $this->get_cache($query)) {
return $cache;
}
// If there is no existing database connection then try to connect
if (!isset($this->dbh) || !$this->dbh) {
$this->connect($this->database->getUser(), $this->database->getPassword(), $this->database->getHost());
$this->dbSelect($this->database->getName());
}
// Perform the query via std mysql_query function..
if (!empty($param) && \is_array($param) && ($this->isPrepareOn())) {
$this->shortcutUsed = true;
return $this->query_prepared($query, $param);
}
$this->result = \mysqli_query($this->dbh, $query);
if ($this->processQueryResult($query) === false) {
if ($this->isTransactional)
throw new \Exception($this->lastError);
return false;
}
// disk caching of queries
$this->store_cache($query, $this->is_insert);
// If debug ALL queries
$this->trace || $this->debugAll ? $this->debug() : null;
return $this->return_val;
} // query
/**
* Close the database connection
*/
public function disconnect()
{
if ($this->dbh) {
\mysqli_close($this->dbh);
$this->_connected = false;
}
$this->_connected = false;
}
/**
* Reset database handle
*/
public function reset()
{
$this->dbh = null;
}
/**
* Get connection handle
*/
public function handle()
{
return $this->dbh;
}
/**
* Returns the current database server host
*
* @return string
*/
public function getHost()
{
return $this->database->getHost();
}
/**
* Returns the current database server port
*
* @return string
*/
public function getPort()
{
return $this->database->getPort();
}
/**
* Returns the current connection charset
*
* @return string
*/
public function getCharset()
{
return $this->database->getCharset();
}
/**
* Returns the last inserted Id - auto generated
*
* @return int
*/
public function getInsertId()
{
return \mysqli_insert_id($this->dbh);
} // getInsertId
/**
* Begin Mysql Transaction
*/
public function beginTransaction()
{
/* turn autocommit off */
$this->dbh->autocommit(false);
$this->dbh->begin_transaction(\MYSQLI_TRANS_START_READ_WRITE);
$this->isTransactional = true;
}
public function commit()
{
$this->dbh->commit();
$this->dbh->autocommit(true);
$this->isTransactional = false;
}
public function rollback()
{
$this->dbh->rollBack();
$this->dbh->autocommit(true);
$this->isTransactional = false;
}
} // ez_mysqli