Skip to content

Commit 1463b4d

Browse files
committed
refactor query, and bug fix query_prepared methods, now queryResult correctly returns last result objects under all SQL vendors., update tests
1 parent a166df6 commit 1463b4d

17 files changed

+827
-472
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ foreach ($result as $row) {
207207
}
208208
```
209209

210-
>Note: `query_prepared()` example works correctly for MySQL, the other drivers will return full query result statement. Will not be stored in `last_result`.
211-
212210
## For Authors and **[Contributors](https://github.com/ezSQL/ezsql/blob/master/CONTRIBUTORS.md)**
213211

214212
## Contributing

lib/Constants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* ezsqlModel Constants
77
*/
8-
\defined('EZSQL_VERSION') or \define('EZSQL_VERSION', '4.0.0');
8+
\defined('EZSQL_VERSION') or \define('EZSQL_VERSION', '4.0.4');
99
\defined('OBJECT') or \define('OBJECT', 'OBJECT');
1010
\defined('ARRAY_A') or \define('ARRAY_A', 'ARRAY_A');
1111
\defined('ARRAY_N') or \define('ARRAY_N', 'ARRAY_N');

lib/Database/ez_mysqli.php

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
use ezsql\DatabaseInterface;
1010

1111
class ez_mysqli extends ezsqlModel implements DatabaseInterface
12-
{
12+
{
13+
private $return_val = 0;
14+
private $is_insert = false;
15+
private $shortcutUsed = false;
16+
1317
/**
1418
* Database connection handle
1519
* @var resource
@@ -297,62 +301,23 @@ function ($string, &$arg) use (&$params) {
297301

298302
return $result;
299303
}
300-
304+
301305
/**
302-
* Perform mySQL query and try to determine result value
306+
* Perform post processing on SQL query call
303307
*
304308
* @param string $query
305-
* @param bool $use_prepare
306-
* @return mixed|bool
309+
* @param mixed $result
310+
* @return bool|void
307311
*/
308-
public function query(string $query, bool $use_prepare = false)
312+
private function processQueryResult(string $query, $result = null)
309313
{
310-
$param = [];
311-
if ($use_prepare)
312-
$param = $this->prepareValues();
313-
314-
// check for ezQuery placeholder tag and replace tags with proper prepare tag
315-
$query = \str_replace(\_TAG, '?', $query);
316-
317-
// Initialize return
318-
$return_val = 0;
319-
320-
// Flush cached values..
321-
$this->flush();
322-
323-
// For reg expressions
324-
$query = \trim($query);
325-
326-
// Log how the function was called
327-
$this->log_query("\$db->query(\"$query\")");
328-
329-
// Keep track of the last query for debug..
330-
$this->last_query = $query;
314+
$this->shortcutUsed = false;
331315

332-
// Count how many queries there have been
333-
$this->num_queries++;
334-
335-
// Use core file cache function
336-
if ( $cache = $this->get_cache($query) ) {
337-
return $cache;
338-
}
339-
340-
// If there is no existing database connection then try to connect
341-
if ( ! isset($this->dbh) || ! $this->dbh ) {
342-
$this->connect($this->database->getUser(), $this->database->getPassword(), $this->database->getHost());
343-
$this->select($this->database->getName());
344-
}
345-
346-
// Perform the query via std mysql_query function..
347-
if (!empty($param) && \is_array($param) && ($this->isPrepareOn())) {
348-
return $this->query_prepared($query, $param);
349-
}
350-
351-
$this->result = \mysqli_query($this->dbh, $query);
316+
if (!empty($result))
317+
$this->result = $result;
352318

353319
// If there is an error then take note of it..
354320
if ( $str = \mysqli_error($this->dbh) ) {
355-
$is_insert = true;
356321
$this->register_error($str);
357322

358323
// If debug ALL queries
@@ -361,8 +326,9 @@ public function query(string $query, bool $use_prepare = false)
361326
}
362327

363328
// Query was an insert, delete, update, replace
364-
$is_insert = false;
329+
$this->is_insert = false;
365330
if ( \preg_match("/^(insert|delete|update|replace)\s+/i", $query) ) {
331+
$this->is_insert = true;
366332
$this->_affectedRows = \mysqli_affected_rows($this->dbh);
367333

368334
// Take note of the insert_id
@@ -371,7 +337,7 @@ public function query(string $query, bool $use_prepare = false)
371337
}
372338

373339
// Return number of rows affected
374-
$return_val = $this->_affectedRows;
340+
$this->return_val = $this->_affectedRows;
375341
} else {
376342
// Query was a select
377343
if ( !\is_numeric($this->result) && !\is_bool($this->result)) {
@@ -397,17 +363,74 @@ public function query(string $query, bool $use_prepare = false)
397363
$this->num_rows = $num_rows;
398364

399365
// Return number of rows selected
400-
$return_val = $this->num_rows;
366+
$this->return_val = $this->num_rows;
401367
}
402368
}
369+
}
370+
371+
/**
372+
* Perform mySQL query and try to determine result value
373+
*
374+
* @param string $query
375+
* @param bool $use_prepare
376+
* @return bool|mixed
377+
*/
378+
public function query(string $query, bool $use_prepare = false)
379+
{
380+
$param = [];
381+
if ($use_prepare)
382+
$param = $this->prepareValues();
383+
384+
// check for ezQuery placeholder tag and replace tags with proper prepare tag
385+
$query = \str_replace(\_TAG, '?', $query);
386+
387+
// Initialize return
388+
$this->return_val = 0;
389+
390+
// Flush cached values..
391+
$this->flush();
392+
393+
// For reg expressions
394+
$query = \trim($query);
395+
396+
// Log how the function was called
397+
$this->log_query("\$db->query(\"$query\")");
398+
399+
// Keep track of the last query for debug..
400+
$this->last_query = $query;
401+
402+
// Count how many queries there have been
403+
$this->num_queries++;
404+
405+
// Use core file cache function
406+
if ( $cache = $this->get_cache($query) ) {
407+
return $cache;
408+
}
409+
410+
// If there is no existing database connection then try to connect
411+
if ( ! isset($this->dbh) || ! $this->dbh ) {
412+
$this->connect($this->database->getUser(), $this->database->getPassword(), $this->database->getHost());
413+
$this->select($this->database->getName());
414+
}
415+
416+
// Perform the query via std mysql_query function..
417+
if (!empty($param) && \is_array($param) && ($this->isPrepareOn())) {
418+
$this->shortcutUsed = true;
419+
return $this->query_prepared($query, $param);
420+
}
421+
422+
$this->result = \mysqli_query($this->dbh, $query);
423+
424+
if ($this->processQueryResult($query) === false)
425+
return false;
403426

404427
// disk caching of queries
405-
$this->store_cache($query, $is_insert);
428+
$this->store_cache($query, $this->is_insert);
406429

407430
// If debug ALL queries
408431
$this->trace || $this->debug_all ? $this->debug() : null ;
409432

410-
return $return_val;
433+
return $this->return_val;
411434
} // query
412435

413436
/**

0 commit comments

Comments
 (0)