Commit f07da2ac authored by Alexander Butenko's avatar Alexander Butenko

Merge pull request #236 from avbdr/master

buildWhere refactoring and versioning
parents baf4d2a1 9aceb949
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* @author Alexander V. Butenko <a.butenka@gmail.com> * @author Alexander V. Butenko <a.butenka@gmail.com>
* @copyright Copyright (c) 2010 * @copyright Copyright (c) 2010
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @version 2.0 * @version 2.1
**/ **/
class MysqliDb class MysqliDb
{ {
...@@ -484,12 +484,16 @@ class MysqliDb ...@@ -484,12 +484,16 @@ class MysqliDb
* *
* @return MysqliDb * @return MysqliDb
*/ */
public function where($whereProp, $whereValue = null, $operator = null) public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
{ {
if ($operator) // forkaround for an old operation api
$whereValue = Array ($operator => $whereValue); if (is_array ($whereValue) && ($key = key ($whereValue)) != "0") {
$operator = $key;
$this->_where[] = Array ("AND", $whereValue, $whereProp); $whereValue = $whereValue[$key];
}
if (count ($this->_where) == 0)
$cond = '';
$this->_where[] = Array ($cond, $whereProp, $operator, $whereValue);
return $this; return $this;
} }
...@@ -503,13 +507,9 @@ class MysqliDb ...@@ -503,13 +507,9 @@ class MysqliDb
* *
* @return MysqliDb * @return MysqliDb
*/ */
public function orWhere($whereProp, $whereValue = null, $operator = null) public function orWhere($whereProp, $whereValue = 'DBNULL', $operator = '=')
{ {
if ($operator) return $this->where ($whereProp, $whereValue, $operator, 'OR');
$whereValue = Array ($operator => $whereValue);
$this->_where[] = Array ("OR", $whereValue, $whereProp);
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.
...@@ -870,33 +870,17 @@ class MysqliDb ...@@ -870,33 +870,17 @@ class MysqliDb
if (empty ($this->_where)) if (empty ($this->_where))
return; return;
//Prepair the where portion of the query //Prepare the where portion of the query
$this->_query .= ' WHERE'; $this->_query .= ' WHERE';
// Remove first AND/OR concatenator
$this->_where[0][0] = '';
foreach ($this->_where as $cond) { foreach ($this->_where as $cond) {
list ($concat, $wValue, $wKey) = $cond; list ($concat, $varName, $operator, $val) = $cond;
$this->_query .= " " . $concat ." " . $varName;
$this->_query .= " " . $concat ." " . $wKey; switch (strtolower ($operator)) {
// Empty value (raw where condition in wKey)
if ($wValue === null)
continue;
// Simple = comparison
if (!is_array ($wValue))
$wValue = Array ('=' => $wValue);
$key = key ($wValue);
$val = $wValue[$key];
switch (strtolower ($key)) {
case '0':
$this->_bindParams ($wValue);
break;
case 'not in': case 'not in':
case 'in': case 'in':
$comparison = ' ' . $key . ' ('; $comparison = ' ' . $operator. ' (';
if (is_object ($val)) { if (is_object ($val)) {
$comparison .= $this->_buildPair ("", $val); $comparison .= $this->_buildPair ("", $val);
} else { } else {
...@@ -909,15 +893,20 @@ class MysqliDb ...@@ -909,15 +893,20 @@ class MysqliDb
break; break;
case 'not between': case 'not between':
case 'between': case 'between':
$this->_query .= " $key ? AND ? "; $this->_query .= " $operator ? AND ? ";
$this->_bindParams ($val); $this->_bindParams ($val);
break; break;
case 'not exists': case 'not exists':
case 'exists': case 'exists':
$this->_query.= $key . $this->_buildPair ("", $val); $this->_query.= $operator . $this->_buildPair ("", $val);
break; break;
default: default:
$this->_query .= $this->_buildPair ($key, $val); if (is_array ($val))
$this->_bindParams ($val);
else if ($val === null)
$this->_query .= $operator . " NULL";
else if ($val != 'DBNULL')
$this->_query .= $this->_buildPair ($operator, $val);
} }
} }
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* @author Alexander V. Butenko <a.butenka@gmail.com> * @author Alexander V. Butenko <a.butenka@gmail.com>
* @copyright Copyright (c) 2015 * @copyright Copyright (c) 2015
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @version 2.0 * @version 2.1
* *
* @method int count () * @method int count ()
* @method mixed byId (string $id, mixed $fields) * @method mixed byId (string $id, mixed $fields)
......
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