9
9
* @author Alexander V. Butenko <[email protected] >
10
10
* @copyright Copyright (c) 2010
11
11
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
12
- * @version 2.0
12
+ * @version 2.1
13
13
**/
14
14
class MysqliDb
15
15
{
@@ -484,12 +484,16 @@ public function delete($tableName, $numRows = null)
484
484
*
485
485
* @return MysqliDb
486
486
*/
487
- public function where ($ whereProp , $ whereValue = null , $ operator = null )
487
+ public function where ($ whereProp , $ whereValue = ' DBNULL ' , $ operator = ' = ' , $ cond = ' AND ' )
488
488
{
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 );
493
497
return $ this ;
494
498
}
495
499
@@ -503,13 +507,9 @@ public function where($whereProp, $whereValue = null, $operator = null)
503
507
*
504
508
* @return MysqliDb
505
509
*/
506
- public function orWhere ($ whereProp , $ whereValue = null , $ operator = null )
510
+ public function orWhere ($ whereProp , $ whereValue = ' DBNULL ' , $ operator = ' = ' )
507
511
{
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 ' );
513
513
}
514
514
/**
515
515
* This method allows you to concatenate joins for the final SQL statement.
@@ -870,33 +870,17 @@ protected function _buildWhere () {
870
870
if (empty ($ this ->_where ))
871
871
return ;
872
872
873
- //Prepair the where portion of the query
873
+ //Prepare the where portion of the query
874
874
$ this ->_query .= ' WHERE ' ;
875
875
876
- // Remove first AND/OR concatenator
877
- $ this ->_where [0 ][0 ] = '' ;
878
876
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 ;
882
879
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 )) {
897
881
case 'not in ' :
898
882
case 'in ' :
899
- $ comparison = ' ' . $ key . ' ( ' ;
883
+ $ comparison = ' ' . $ operator . ' ( ' ;
900
884
if (is_object ($ val )) {
901
885
$ comparison .= $ this ->_buildPair ("" , $ val );
902
886
} else {
@@ -909,15 +893,20 @@ protected function _buildWhere () {
909
893
break ;
910
894
case 'not between ' :
911
895
case 'between ' :
912
- $ this ->_query .= " $ key ? AND ? " ;
896
+ $ this ->_query .= " $ operator ? AND ? " ;
913
897
$ this ->_bindParams ($ val );
914
898
break ;
915
899
case 'not exists ' :
916
900
case 'exists ' :
917
- $ this ->_query .= $ key . $ this ->_buildPair ("" , $ val );
901
+ $ this ->_query .= $ operator . $ this ->_buildPair ("" , $ val );
918
902
break ;
919
903
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 );
921
910
}
922
911
}
923
912
}
0 commit comments