@@ -15,6 +15,8 @@ class ezQuery implements ezQueryInterface
1515 private $ fromTable = null ;
1616 private $ isWhere = true ;
1717 private $ isInto = false ;
18+ private $ whereSQL = null ;
19+ private $ combineWith = null ;
1820
1921 public function __construct ()
2022 {
@@ -292,6 +294,52 @@ public function limit($numberOf, $offset = null)
292294 return 'LIMIT ' .$ rows .$ value ;
293295 }
294296
297+ private function conditions ($ key , $ condition , $ value , $ combine )
298+ {
299+ if ($ this ->isPrepareOn ()) {
300+ $ this ->whereSQL .= "$ key $ condition " .\_TAG ." $ combine " ;
301+ $ this ->addPrepare ($ value );
302+ } else
303+ $ this ->whereSQL .= "$ key $ condition ' " .$ this ->escape ($ value )."' $ combine " ;
304+ }
305+
306+ private function conditionBetween ($ key , $ condition , $ valueOne , $ valueTwo , $ combine )
307+ {
308+ $ _valueTwo = $ this ->escape ($ valueTwo );
309+ $ andCombineWith = \_AND ;
310+ if (\in_array (\strtoupper ($ combine ), \_COMBINERS ))
311+ $ andCombineWith = \strtoupper ($ combine );
312+
313+ if ($ this ->isPrepareOn ()) {
314+ $ this ->whereSQL .= "$ key " .$ condition .' ' .\_TAG ." AND " .\_TAG ." $ andCombineWith " ;
315+ $ this ->addPrepare ($ valueOne );
316+ $ this ->addPrepare ($ valueTwo );
317+ } else
318+ $ this ->whereSQL .= "$ key $ condition ' " .$ this ->escape ($ valueOne )."' AND ' " .$ _valueTwo ."' $ andCombineWith " ;
319+
320+ $ this ->combineWith = $ andCombineWith ;
321+ }
322+
323+ private function conditionIn ($ key , $ condition , $ valueRow , $ combine )
324+ {
325+ $ value = '' ;
326+ foreach ($ valueRow as $ splitValue ) {
327+ if ($ this ->isPrepareOn ()) {
328+ $ value .= \_TAG .', ' ;
329+ $ this ->addPrepare ($ splitValue );
330+ } else
331+ $ value .= "' " .$ this ->escape ($ splitValue )."', " ;
332+ }
333+
334+ $ this ->whereSQL .= "$ key $ condition ( " .\rtrim ($ value , ', ' )." ) $ combine " ;
335+ }
336+
337+ private function conditionIs ($ key , $ condition , $ combine )
338+ {
339+ $ isCondition = (($ condition == 'IS ' ) || ($ condition == 'IS NOT ' )) ? $ condition : 'IS ' ;
340+ $ this ->whereSQL .= "$ key $ isCondition NULL $ combine " ;
341+ }
342+
295343 public function where ( ...$ whereConditions )
296344 {
297345 if (empty ($ whereConditions ))
@@ -304,7 +352,7 @@ public function where( ...$whereConditions)
304352 $ operator = [];
305353 $ extra = [];
306354 $ combiner = [];
307- $ combineWith = '' ;
355+ $ this -> combineWith = '' ;
308356
309357 $ storeWhereConditions = $ whereConditions ;
310358 if (\is_string ($ whereConditions [0 ])) {
@@ -336,63 +384,41 @@ public function where( ...$whereConditions)
336384
337385 $ where = '1 ' ;
338386 if (! isset ($ whereClause ['1 ' ])) {
339- $ where = '' ;
387+ $ this -> whereSQL = '' ;
340388 $ i = 0 ;
341389 foreach ($ whereClause as $ key => $ val ) {
342390 $ isCondition = \strtoupper ($ operator [$ i ]);
343391 $ combine = $ combiner [$ i ];
344- $ combineWith = \_AND ;
345- if ( \in_array (\strtoupper ($ combine ), \_COMBINERS ) || isset ($ extra [$ i ]))
346- $ combineWith = ( isset ($ extra [$ i ]) ) ? $ combine : \strtoupper ($ combine );
392+ $ this -> combineWith = \_AND ;
393+ if ( \in_array (\strtoupper ($ combine ), \_COMBINERS ) || isset ($ extra [$ i ]))
394+ $ this -> combineWith = isset ($ extra [$ i ]) ? $ combine : \strtoupper ($ combine );
347395
348396 if (! \in_array ( $ isCondition , \_BOOLEAN_OPERATORS ))
349397 return $ this ->clearPrepare ();
350398
351399 if (($ isCondition == \_BETWEEN ) || ($ isCondition == \_notBETWEEN)) {
352- $ value = $ this ->escape ($ combineWith );
353- $ myCombineWith = \_AND ;
354- if (\in_array (\strtoupper ($ extra [$ i ]), \_COMBINERS ))
355- $ myCombineWith = \strtoupper ($ extra [$ i ]);
356-
357- if ($ this ->isPrepareOn ()) {
358- $ where .= "$ key " .$ isCondition .' ' .\_TAG ." AND " .\_TAG ." $ myCombineWith " ;
359- $ this ->addPrepare ($ val );
360- $ this ->addPrepare ($ combineWith );
361- } else
362- $ where .= "$ key " .$ isCondition ." ' " .$ this ->escape ($ val )."' AND ' " .$ value ."' $ myCombineWith " ;
363-
364- $ combineWith = $ myCombineWith ;
400+ $ this ->conditionBetween ($ key , $ isCondition , $ val , $ this ->combineWith , $ extra [$ i ]);
365401 } elseif ($ isCondition == \_IN ) {
366- $ value = '' ;
367- foreach ($ val as $ inValues ) {
368- if ($ this ->isPrepareOn ()) {
369- $ value .= \_TAG .', ' ;
370- $ this ->addPrepare ($ inValues );
371- } else
372- $ value .= "' " .$ this ->escape ($ inValues )."', " ;
373- }
374- $ where .= "$ key " .$ isCondition ." ( " .\rtrim ($ value , ', ' )." ) $ combineWith " ;
402+ $ this ->conditionIn ($ key , $ isCondition , $ val , $ this ->combineWith );
375403 } elseif (((\strtolower ($ val ) == 'null ' ) || ($ isCondition == 'IS ' ) || ($ isCondition == 'IS NOT ' ))) {
376- $ isCondition = (($ isCondition == 'IS ' ) || ($ isCondition == 'IS NOT ' )) ? $ isCondition : 'IS ' ;
377- $ where .= "$ key " .$ isCondition ." NULL $ combineWith " ;
404+ $ this ->conditionIs ($ key , $ isCondition , $ this ->combineWith );
378405 } elseif ((($ isCondition == \_LIKE ) || ($ isCondition == \_notLIKE)) && ! \preg_match ('/[_%?]/ ' , $ val )) {
379406 return $ this ->clearPrepare ();
380407 } else {
381- if ($ this ->isPrepareOn ()) {
382- $ where .= "$ key " .$ isCondition .' ' .\_TAG ." $ combineWith " ;
383- $ this ->addPrepare ($ val );
384- } else
385- $ where .= "$ key " .$ isCondition ." ' " .$ this ->escape ($ val )."' $ combineWith " ;
386- }
408+ $ this ->conditions ($ key , $ isCondition , $ val , $ this ->combineWith );
409+ }
410+
387411 $ i ++;
388412 }
389- $ where = \rtrim ($ where , " $ combineWith " );
413+
414+ $ where = \rtrim ($ this ->whereSQL , " $ this ->combineWith " );
415+ $ this ->whereSQL = null ;
390416 }
391417
392418 if (($ this ->isPrepareOn ()) && !empty ($ this ->prepareValues ()) && ($ where != '1 ' ))
393- return " $ whereOrHaving " . $ where. ' ' ;
419+ return " $ whereOrHaving $ where " ;
394420
395- return ($ where != '1 ' ) ? " $ whereOrHaving " . $ where. ' ' : ' ' ;
421+ return ($ where != '1 ' ) ? " $ whereOrHaving $ where " : ' ' ;
396422 }
397423
398424 public function selecting (string $ table = null , $ columnFields = '* ' , ...$ conditions )
0 commit comments