Commit 8abb2a26 authored by Alexander Butenko's avatar Alexander Butenko

added having support

parent 23f6f776
...@@ -57,11 +57,17 @@ class MysqliDb ...@@ -57,11 +57,17 @@ class MysqliDb
*/ */
protected $_join = array(); protected $_join = array();
/** /**
* An array that holds where conditions 'fieldname' => 'value' * An array that holds where conditions
* *
* @var array * @var array
*/ */
protected $_where = array(); protected $_where = array();
/**
* An array that holds having conditions
*
* @var array
*/
protected $_having = array();
/** /**
* Dynamic type list for order by condition value * Dynamic type list for order by condition value
*/ */
...@@ -259,6 +265,7 @@ class MysqliDb ...@@ -259,6 +265,7 @@ class MysqliDb
$this->trace[] = array ($this->_lastQuery, (microtime(true) - $this->traceStartQ) , $this->_traceGetCaller()); $this->trace[] = array ($this->_lastQuery, (microtime(true) - $this->traceStartQ) , $this->_traceGetCaller());
$this->_where = array(); $this->_where = array();
$this->_having = array();
$this->_join = array(); $this->_join = array();
$this->_orderBy = array(); $this->_orderBy = array();
$this->_groupBy = array(); $this->_groupBy = array();
...@@ -681,6 +688,45 @@ class MysqliDb ...@@ -681,6 +688,45 @@ class MysqliDb
{ {
return $this->where ($whereProp, $whereValue, $operator, 'OR'); return $this->where ($whereProp, $whereValue, $operator, 'OR');
} }
/*
* This method allows you to specify multiple (method chaining optional) AND HAVING statements for SQL queries.
*
* @uses $MySqliDb->having('SUM(tags) > 10')
*
* @param string $havingProp The name of the database field.
* @param mixed $havingValue The value of the database field.
*
* @return MysqliDb
*/
public function having($havingProp, $havingValue = null, $operator = null)
{
if ($operator)
$havingValue = Array ($operator => $havingValue);
$this->_having[] = Array ("AND", $havingValue, $havingProp);
return $this;
}
/**
* This method allows you to specify multiple (method chaining optional) OR HAVING statements for SQL queries.
*
* @uses $MySqliDb->orHaving('SUM(tags) > 10')
*
* @param string $havingProp The name of the database field.
* @param mixed $havingValue The value of the database field.
*
* @return MysqliDb
*/
public function orHaving($havingProp, $havingValue = null, $operator = null)
{
if ($operator)
$havingValue = Array ($operator => $havingValue);
$this->_having[] = Array ("OR", $havingValue, $havingProp);
return $this;
}
/** /**
* This method allows you to concatenate joins for the final SQL statement. * This method allows you to concatenate joins for the final SQL statement.
* *
...@@ -911,8 +957,9 @@ class MysqliDb ...@@ -911,8 +957,9 @@ class MysqliDb
{ {
$this->_buildJoin(); $this->_buildJoin();
$this->_buildInsertQuery ($tableData); $this->_buildInsertQuery ($tableData);
$this->_buildWhere(); $this->_buildCondition('WHERE', $this->_where);
$this->_buildGroupBy(); $this->_buildGroupBy();
$this->_buildCondition('HAVING', $this->_having);
$this->_buildOrderBy(); $this->_buildOrderBy();
$this->_buildLimit ($numRows); $this->_buildLimit ($numRows);
$this->_buildOnDuplicate($tableData); $this->_buildOnDuplicate($tableData);
...@@ -1140,14 +1187,14 @@ class MysqliDb ...@@ -1140,14 +1187,14 @@ class MysqliDb
/** /**
* Abstraction method that will build the part of the WHERE conditions * Abstraction method that will build the part of the WHERE conditions
*/ */
protected function _buildWhere () { protected function _buildCondition ($operator, &$conditions) {
if (empty ($this->_where)) if (empty ($conditions))
return; return;
//Prepare the where portion of the query //Prepare the where portion of the query
$this->_query .= ' WHERE'; $this->_query .= ' ' . $operator;
foreach ($this->_where as $cond) { foreach ($conditions as $cond) {
list ($concat, $varName, $operator, $val) = $cond; list ($concat, $varName, $operator, $val) = $cond;
$this->_query .= " " . $concat ." " . $varName; $this->_query .= " " . $concat ." " . $varName;
...@@ -1258,6 +1305,7 @@ class MysqliDb ...@@ -1258,6 +1305,7 @@ class MysqliDb
{ {
if ($this->isSubQuery) if ($this->isSubQuery)
return; return;
print_r ($this);
if ($this->_mysqli) { if ($this->_mysqli) {
$this->_mysqli->close(); $this->_mysqli->close();
$this->_mysqli = null; $this->_mysqli = null;
......
...@@ -286,8 +286,8 @@ $resutls = $db->rawQuery ($q, $params); ...@@ -286,8 +286,8 @@ $resutls = $db->rawQuery ($q, $params);
print_r ($results); // contains Array of returned rows print_r ($results); // contains Array of returned rows
``` ```
### Where Method ### Where / Having Methods
This method allows you to specify where parameters of the query. `where()`, `orWhere()`, `having()` and `orHaving()` methods allows you to specify where and having conditions of the query. All conditions supported by where() are supported by having() as well.
WARNING: In order to use column to column comparisons only raw where conditions should be used as column name or functions cant be passed as a bind variable. WARNING: In order to use column to column comparisons only raw where conditions should be used as column name or functions cant be passed as a bind variable.
...@@ -299,6 +299,14 @@ $results = $db->get ('users'); ...@@ -299,6 +299,14 @@ $results = $db->get ('users');
// Gives: SELECT * FROM users WHERE id=1 AND login='admin'; // Gives: SELECT * FROM users WHERE id=1 AND login='admin';
``` ```
```php
$db->where ('id', 1);
$db->having ('login', 'admin');
$results = $db->get ('users');
// Gives: SELECT * FROM users WHERE id=1 HAVING login='admin';
```
Regular == operator with column to column comparison: Regular == operator with column to column comparison:
```php ```php
// WRONG // WRONG
...@@ -342,6 +350,14 @@ $results = $db->get ('users'); ...@@ -342,6 +350,14 @@ $results = $db->get ('users');
// Gives: SELECT * FROM users WHERE firstName='John' OR firstName='peter' // Gives: SELECT * FROM users WHERE firstName='John' OR firstName='peter'
``` ```
```php
$db->where ('firstName', 'John');
$db->orWhere ('firstName', 'Peter');
$results = $db->get ('users');
// Gives: SELECT * FROM users WHERE firstName='John' OR firstName='peter'
```
NULL comparison: NULL comparison:
```php ```php
$db->where ("lastName", NULL, '<=>'); $db->where ("lastName", NULL, '<=>');
......
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