@@ -342,13 +342,16 @@ public function limit($numberOf, $offset = null)
342342 return 'LIMIT ' . $ rows . $ value ;
343343 }
344344
345- private function conditions ($ key , $ condition , $ value , $ combine )
345+ private function conditions ($ key , $ condition , $ value , $ combine, $ extra )
346346 {
347+ $ groupStart = (!empty ($ extra ) && $ extra === '( ' ) ? $ extra : '' ;
348+ $ groupEnd = (!empty ($ extra ) && $ extra === ') ' ) ? $ extra : '' ;
349+
347350 if ($ this ->isPrepareOn ()) {
348- $ this ->whereSQL .= "$ key $ condition " . \_TAG . " $ combine " ;
351+ $ this ->whereSQL .= "$ groupStart $ key $ condition " . \_TAG . " $ groupEnd $ combine " ;
349352 $ this ->addPrepare ($ value );
350353 } else
351- $ this ->whereSQL .= "$ key $ condition ' " . $ this ->escape ($ value ) . "' $ combine " ;
354+ $ this ->whereSQL .= "$ groupStart $ key $ condition ' " . $ this ->escape ($ value ) . "' $ groupEnd $ combine " ;
352355 }
353356
354357 private function conditionBetween ($ key , $ condition , $ valueOne , $ valueTwo , $ combine )
@@ -388,8 +391,22 @@ private function conditionIs($key, $condition, $combine)
388391 $ this ->whereSQL .= "$ key $ isCondition NULL $ combine " ;
389392 }
390393
394+ private function flattenWhereConditions ($ whereConditions )
395+ {
396+ $ whereConditionsReturn = [];
397+ foreach ($ whereConditions as $ whereCondition ) {
398+ if (!empty ($ whereCondition [0 ]) && is_array ($ whereCondition [0 ])) {
399+ $ whereConditionsReturn = array_merge ($ whereConditionsReturn , $ this ->flattenWhereConditions ($ whereCondition ));
400+ } else {
401+ $ whereConditionsReturn [] = $ whereCondition ;
402+ }
403+ }
404+ return $ whereConditionsReturn ;
405+ }
406+
391407 private function retrieveConditions ($ whereConditions )
392408 {
409+ $ whereConditions = $ this ->flattenWhereConditions ($ whereConditions );
393410 $ whereKey = [];
394411 $ whereValue = [];
395412 $ operator = [];
@@ -408,7 +425,7 @@ private function retrieveConditions($whereConditions)
408425 $ combiner [] = \_AND ;
409426 $ extra [] = null ;
410427 } else {
411- if (isset ($ checkFields [0 ])) {
428+ if (! empty ($ checkFields [0 ])) {
412429 $ whereKey [] = $ checkFields [0 ];
413430 $ whereValue [] = (isset ($ checkFields [2 ])) ? $ checkFields [2 ] : '' ;
414431 $ combiner [] = (isset ($ checkFields [3 ])) ? $ checkFields [3 ] : \_AND ;
@@ -434,12 +451,37 @@ private function processConditions($column, $condition, $value, $valueOrCombine,
434451 } elseif ((($ condition == \_LIKE ) || ($ condition == \_notLIKE)) && !\preg_match ('/[_%?]/ ' , $ value )) {
435452 return $ this ->clearPrepare ();
436453 } else {
437- $ this ->conditions ($ column , $ condition , $ value , $ valueOrCombine );
454+ $ this ->conditions ($ column , $ condition , $ value , $ valueOrCombine , $ extraCombine );
455+ }
456+ }
457+
458+ public function grouping (...$ whereConditions )
459+ {
460+ if (empty ($ whereConditions ))
461+ return false ;
462+
463+ $ whereOrHaving = ($ this ->isWhere ) ? 'WHERE ' : 'HAVING ' ;
464+
465+ if (\is_string ($ whereConditions [0 ]) && \strpos ($ whereConditions [0 ], $ whereOrHaving ) !== false )
466+ return $ whereConditions [0 ];
467+
468+ $ totalConditions = count ($ whereConditions ) - 1 ;
469+
470+ if ($ totalConditions > 0 ) {
471+
472+ if (!in_array ('( ' , $ whereConditions [0 ]))
473+ $ whereConditions [0 ][count ($ whereConditions [0 ])] = '( ' ;
474+
475+ if (!in_array (') ' , $ whereConditions [$ totalConditions ]))
476+ $ whereConditions [$ totalConditions ][count ($ whereConditions [$ totalConditions ])] = ') ' ;
438477 }
478+
479+ return $ whereConditions ;
439480 }
440481
441482 public function where (...$ whereConditions )
442483 {
484+
443485 if (empty ($ whereConditions ))
444486 return false ;
445487
0 commit comments