Commit 7429e5a8 authored by Alexander Butenko's avatar Alexander Butenko

Added setQueryOption method

parent a204875d
...@@ -43,6 +43,12 @@ class MysqliDb ...@@ -43,6 +43,12 @@ class MysqliDb
* @var string * @var string
*/ */
protected $_lastQuery; protected $_lastQuery;
/**
* The SQL query options required after SELECT, INSERT, UPDATE or DELETE
*
* @var string
*/
protected $_queryOptions = array();
/** /**
* An array that holds where joins * An array that holds where joins
* *
...@@ -81,7 +87,6 @@ class MysqliDb ...@@ -81,7 +87,6 @@ class MysqliDb
* @var string * @var string
*/ */
public $totalCount = 0; public $totalCount = 0;
protected $fetchTotalCount = false;
/** /**
* Variable which holds last statement error * Variable which holds last statement error
* *
...@@ -193,7 +198,7 @@ class MysqliDb ...@@ -193,7 +198,7 @@ class MysqliDb
$this->_groupBy = array(); $this->_groupBy = array();
$this->_bindParams = array(''); // Create the empty 0 index $this->_bindParams = array(''); // Create the empty 0 index
$this->_query = null; $this->_query = null;
$this->count = 0; $this->_queryOptions = array();
} }
/** /**
...@@ -238,9 +243,10 @@ class MysqliDb ...@@ -238,9 +243,10 @@ class MysqliDb
$stmt->execute(); $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$this->_lastQuery = $this->replacePlaceHolders ($this->_query, $params); $this->_lastQuery = $this->replacePlaceHolders ($this->_query, $params);
$res = $this->_dynamicBindResults($stmt);
$this->reset(); $this->reset();
return $this->_dynamicBindResults($stmt); return $res;
} }
/** /**
...@@ -256,9 +262,28 @@ class MysqliDb ...@@ -256,9 +262,28 @@ class MysqliDb
$stmt = $this->_buildQuery($numRows); $stmt = $this->_buildQuery($numRows);
$stmt->execute(); $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$res = $this->_dynamicBindResults($stmt);
$this->reset(); $this->reset();
return $this->_dynamicBindResults($stmt); return $res;
}
/**
* This method allows you to specify multiple (method chaining optional) options for SQL queries.
*
* @uses $MySqliDb->setQueryOption('name');
*
* @param string/array $options The optons name of the query.
*
* @return MysqliDb
*/
public function setQueryOption ($options) {
if (is_array ($options))
$this->_queryOptions = array_merge ($this->_queryOptions, $options);
else
$this->_queryOptions[] = $options;
return $this;
} }
/** /**
...@@ -267,7 +292,7 @@ class MysqliDb ...@@ -267,7 +292,7 @@ class MysqliDb
* @return MysqliDb * @return MysqliDb
*/ */
public function withTotalCount () { public function withTotalCount () {
$this->fetchTotalCount = true; $this->setQueryOption ('SQL_CALC_FOUND_ROWS');
return $this; return $this;
} }
...@@ -284,9 +309,9 @@ class MysqliDb ...@@ -284,9 +309,9 @@ class MysqliDb
if (empty ($columns)) if (empty ($columns))
$columns = '*'; $columns = '*';
$this->_query = $this->fetchTotalCount == true ? 'SELECT SQL_CALC_FOUND_ROWS ' : 'SELECT ';
$column = is_array($columns) ? implode(', ', $columns) : $columns; $column = is_array($columns) ? implode(', ', $columns) : $columns;
$this->_query .= "$column FROM " .self::$_prefix . $tableName; $this->_query = 'SELECT ' . implode(' ', $this->_queryOptions) . ' ' .
$column . " FROM " .self::$_prefix . $tableName;
$stmt = $this->_buildQuery($numRows); $stmt = $this->_buildQuery($numRows);
if ($this->isSubQuery) if ($this->isSubQuery)
...@@ -294,9 +319,10 @@ class MysqliDb ...@@ -294,9 +319,10 @@ class MysqliDb
$stmt->execute(); $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$res = $this->_dynamicBindResults($stmt);
$this->reset(); $this->reset();
return $this->_dynamicBindResults($stmt); return $res;
} }
/** /**
...@@ -724,9 +750,8 @@ class MysqliDb ...@@ -724,9 +750,8 @@ class MysqliDb
if ($this->_mysqli->more_results()) if ($this->_mysqli->more_results())
$this->_mysqli->next_result(); $this->_mysqli->next_result();
if ($this->fetchTotalCount === true) { if (in_array ('SQL_CALC_FOUND_ROWS', $this->_queryOptions)) {
$this->fetchTotalCount = false; $stmt = $this->_mysqli->query ('SELECT FOUND_ROWS()');
$stmt = $this->_mysqli->query ('SELECT FOUND_ROWS();');
$totalCount = $stmt->fetch_row(); $totalCount = $stmt->fetch_row();
$this->totalCount = $totalCount[0]; $this->totalCount = $totalCount[0];
} }
......
...@@ -7,6 +7,7 @@ MysqliDb -- Simple MySQLi wrapper with prepared statements ...@@ -7,6 +7,7 @@ MysqliDb -- Simple MySQLi wrapper with prepared statements
**[Select Query](#select-query)** **[Select Query](#select-query)**
**[Delete Query](#delete-query)** **[Delete Query](#delete-query)**
**[Generic Query](#generic-query-method)** **[Generic Query](#generic-query-method)**
**[Query Keywords](#query-keywords)**
**[Raw Query](#raw-query-method)** **[Raw Query](#raw-query-method)**
**[Where Conditions](#where-method)** **[Where Conditions](#where-method)**
**[Order Conditions](#ordering-method)** **[Order Conditions](#ordering-method)**
...@@ -287,6 +288,28 @@ $users = $db->withTotalCount()->get('users', Array ($offset, $count)); ...@@ -287,6 +288,28 @@ $users = $db->withTotalCount()->get('users', Array ($offset, $count));
echo "Showing {$count} from {$db->totalCount}"; echo "Showing {$count} from {$db->totalCount}";
``` ```
### Query Keywords
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of mysql keywords to INSERT , SELECT , UPDATE, DELETE query:
```php
$db->setQueryOption('LOW_PRIORITY');
$db->insert($table,$param);
// GIVES: INSERT LOW_PRIORITY INTO table ...
```
Also you can use an array of keywords:
```php
$db->setQueryOption(Array('LOW_PRIORITY', 'IGNORE'));
$db->insert($table,$param);
// GIVES: INSERT LOW_PRIORITY IGNORE INTO table ...
```
Same way keywords could be used in SELECT queries as well:
```php
$db->setQueryOption('SQL_NO_CACHE');
$db->get("users");
// GIVES: SELECT SQL_NO_CACHE * FROM USERS;
```
Optionally you can use method chaining to call where multiple times without referencing your object over an over: Optionally you can use method chaining to call where multiple times without referencing your object over an over:
```php ```php
...@@ -418,7 +441,6 @@ if($db->has("users")) { ...@@ -418,7 +441,6 @@ if($db->has("users")) {
return "Wrong user/password"; return "Wrong user/password";
} }
``` ```
### Helper commands ### Helper commands
Reconnect in case mysql connection died Reconnect in case mysql connection died
```php ```php
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment