Skip to content

Commit f07da2a

Browse files
committed
Merge pull request #236 from avbdr/master
buildWhere refactoring and versioning
2 parents baf4d2a + 9aceb94 commit f07da2a

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

MysqliDb.php

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Alexander V. Butenko <[email protected]>
1010
* @copyright Copyright (c) 2010
1111
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
12-
* @version 2.0
12+
* @version 2.1
1313
**/
1414
class MysqliDb
1515
{
@@ -484,12 +484,16 @@ public function delete($tableName, $numRows = null)
484484
*
485485
* @return MysqliDb
486486
*/
487-
public function where($whereProp, $whereValue = null, $operator = null)
487+
public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
488488
{
489-
if ($operator)
490-
$whereValue = Array ($operator => $whereValue);
491-
492-
$this->_where[] = Array ("AND", $whereValue, $whereProp);
489+
// forkaround for an old operation api
490+
if (is_array ($whereValue) && ($key = key ($whereValue)) != "0") {
491+
$operator = $key;
492+
$whereValue = $whereValue[$key];
493+
}
494+
if (count ($this->_where) == 0)
495+
$cond = '';
496+
$this->_where[] = Array ($cond, $whereProp, $operator, $whereValue);
493497
return $this;
494498
}
495499

@@ -503,13 +507,9 @@ public function where($whereProp, $whereValue = null, $operator = null)
503507
*
504508
* @return MysqliDb
505509
*/
506-
public function orWhere($whereProp, $whereValue = null, $operator = null)
510+
public function orWhere($whereProp, $whereValue = 'DBNULL', $operator = '=')
507511
{
508-
if ($operator)
509-
$whereValue = Array ($operator => $whereValue);
510-
511-
$this->_where[] = Array ("OR", $whereValue, $whereProp);
512-
return $this;
512+
return $this->where ($whereProp, $whereValue, $operator, 'OR');
513513
}
514514
/**
515515
* This method allows you to concatenate joins for the final SQL statement.
@@ -870,33 +870,17 @@ protected function _buildWhere () {
870870
if (empty ($this->_where))
871871
return;
872872

873-
//Prepair the where portion of the query
873+
//Prepare the where portion of the query
874874
$this->_query .= ' WHERE';
875875

876-
// Remove first AND/OR concatenator
877-
$this->_where[0][0] = '';
878876
foreach ($this->_where as $cond) {
879-
list ($concat, $wValue, $wKey) = $cond;
880-
881-
$this->_query .= " " . $concat ." " . $wKey;
877+
list ($concat, $varName, $operator, $val) = $cond;
878+
$this->_query .= " " . $concat ." " . $varName;
882879

883-
// Empty value (raw where condition in wKey)
884-
if ($wValue === null)
885-
continue;
886-
887-
// Simple = comparison
888-
if (!is_array ($wValue))
889-
$wValue = Array ('=' => $wValue);
890-
891-
$key = key ($wValue);
892-
$val = $wValue[$key];
893-
switch (strtolower ($key)) {
894-
case '0':
895-
$this->_bindParams ($wValue);
896-
break;
880+
switch (strtolower ($operator)) {
897881
case 'not in':
898882
case 'in':
899-
$comparison = ' ' . $key . ' (';
883+
$comparison = ' ' . $operator. ' (';
900884
if (is_object ($val)) {
901885
$comparison .= $this->_buildPair ("", $val);
902886
} else {
@@ -909,15 +893,20 @@ protected function _buildWhere () {
909893
break;
910894
case 'not between':
911895
case 'between':
912-
$this->_query .= " $key ? AND ? ";
896+
$this->_query .= " $operator ? AND ? ";
913897
$this->_bindParams ($val);
914898
break;
915899
case 'not exists':
916900
case 'exists':
917-
$this->_query.= $key . $this->_buildPair ("", $val);
901+
$this->_query.= $operator . $this->_buildPair ("", $val);
918902
break;
919903
default:
920-
$this->_query .= $this->_buildPair ($key, $val);
904+
if (is_array ($val))
905+
$this->_bindParams ($val);
906+
else if ($val === null)
907+
$this->_query .= $operator . " NULL";
908+
else if ($val != 'DBNULL')
909+
$this->_query .= $this->_buildPair ($operator, $val);
921910
}
922911
}
923912
}

dbObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Alexander V. Butenko <[email protected]>
88
* @copyright Copyright (c) 2015
99
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
10-
* @version 2.0
10+
* @version 2.1
1111
*
1212
* @method int count ()
1313
* @method mixed byId (string $id, mixed $fields)

0 commit comments

Comments
 (0)