@@ -764,14 +764,18 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type
764764 $ keyValue = $ key ;
765765 }
766766
767+ if ($ keyValue === []) {
768+ return $ this ;
769+ }
770+
767771 // If the escape value was not set will base it on the global setting
768772 if (! is_bool ($ escape )) {
769773 $ escape = $ this ->db ->protectIdentifiers ;
770774 }
771775
772- foreach ($ keyValue as $ k => $ v ) {
773- $ prefix = empty ($ this ->{$ qbKey }) ? $ this ->groupGetType ('' ) : $ this ->groupGetType ($ type );
776+ $ prefix = empty ($ this ->{$ qbKey }) ? $ this ->groupGetType ('' ) : $ this ->groupGetType ($ type );
774777
778+ foreach ($ keyValue as $ k => $ v ) {
775779 if ($ rawSqlOnly ) {
776780 $ k = '' ;
777781 $ op = '' ;
@@ -830,6 +834,8 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type
830834 'escape ' => $ escape ,
831835 ];
832836 }
837+
838+ $ prefix = $ type ;
833839 }
834840
835841 return $ this ;
@@ -1115,71 +1121,55 @@ public function orNotHavingLike($field, string $match = '', string $side = 'both
11151121 */
11161122 protected function _like ($ field , string $ match = '' , string $ type = 'AND ' , string $ side = 'both ' , string $ not = '' , ?bool $ escape = null , bool $ insensitiveSearch = false , string $ clause = 'QBWhere ' )
11171123 {
1118- $ escape = is_bool ( $ escape ) ? $ escape : $ this ->db ->protectIdentifiers ;
1119- $ side = strtolower ($ side );
1124+ $ escape ??= $ this ->db ->protectIdentifiers ;
1125+ $ side = strtolower ($ side );
11201126
1127+ $ keyValue = [];
11211128 if ($ field instanceof RawSql) {
1122- $ k = (string ) $ field ;
1123- $ v = $ match ;
1124- $ insensitiveSearch = false ;
1125-
1126- $ prefix = empty ($ this ->{$ clause }) ? $ this ->groupGetType ('' ) : $ this ->groupGetType ($ type );
1127-
1128- if ($ side === 'none ' ) {
1129- $ bind = $ this ->setBind ($ field ->getBindingKey (), $ v , $ escape );
1130- } elseif ($ side === 'before ' ) {
1131- $ bind = $ this ->setBind ($ field ->getBindingKey (), "% {$ v }" , $ escape );
1132- } elseif ($ side === 'after ' ) {
1133- $ bind = $ this ->setBind ($ field ->getBindingKey (), "{$ v }% " , $ escape );
1134- } else {
1135- $ bind = $ this ->setBind ($ field ->getBindingKey (), "% {$ v }% " , $ escape );
1136- }
1137-
1138- $ likeStatement = $ this ->_like_statement ($ prefix , $ k , $ not , $ bind , $ insensitiveSearch );
1139-
1140- // some platforms require an escape sequence definition for LIKE wildcards
1141- if ($ escape === true && $ this ->db ->likeEscapeStr !== '' ) {
1142- $ likeStatement .= sprintf ($ this ->db ->likeEscapeStr , $ this ->db ->likeEscapeChar );
1129+ $ keyValue [] = ['field ' => $ field , 'match ' => $ match ];
1130+ } else {
1131+ foreach (is_array ($ field ) ? $ field : [$ field => $ match ] as $ k => $ v ) {
1132+ $ keyValue [] = ['field ' => $ k , 'match ' => $ v ];
11431133 }
1134+ }
11441135
1145- $ this ->{$ clause }[] = [
1146- 'condition ' => $ field ->with ($ likeStatement ),
1147- 'escape ' => $ escape ,
1148- ];
1149-
1136+ if ($ keyValue === []) {
11501137 return $ this ;
11511138 }
11521139
1153- $ keyValue = is_array ( $ field ) ? $ field : [ $ field => $ match ] ;
1140+ $ prefix = empty ( $ this ->{ $ clause } ) ? $ this -> groupGetType ( '' ) : $ this -> groupGetType ( $ type ) ;
11541141
1155- foreach ($ keyValue as $ k => $ v ) {
1156- if ($ insensitiveSearch ) {
1142+ foreach ($ keyValue as $ item ) {
1143+ $ k = $ item ['field ' ];
1144+ $ v = $ item ['match ' ];
1145+
1146+ if ($ insensitiveSearch && ! ($ k instanceof RawSql)) {
11571147 $ v = mb_strtolower ($ v , 'UTF-8 ' );
11581148 }
11591149
1160- $ prefix = empty ($ this ->{$ clause }) ? $ this ->groupGetType ('' ) : $ this ->groupGetType ($ type );
1150+ $ val = match ($ side ) {
1151+ 'none ' => $ v ,
1152+ 'before ' => "% {$ v }" ,
1153+ 'after ' => "{$ v }% " ,
1154+ default => "% {$ v }% " ,
1155+ };
11611156
1162- if ($ side === 'none ' ) {
1163- $ bind = $ this ->setBind ($ k , $ v , $ escape );
1164- } elseif ($ side === 'before ' ) {
1165- $ bind = $ this ->setBind ($ k , "% {$ v }" , $ escape );
1166- } elseif ($ side === 'after ' ) {
1167- $ bind = $ this ->setBind ($ k , "{$ v }% " , $ escape );
1168- } else {
1169- $ bind = $ this ->setBind ($ k , "% {$ v }% " , $ escape );
1170- }
1157+ $ bind = ($ k instanceof RawSql)
1158+ ? $ this ->setBind ($ k ->getBindingKey (), $ val , $ escape )
1159+ : $ this ->setBind ($ k , $ val , $ escape );
11711160
1172- $ likeStatement = $ this ->_like_statement ($ prefix , $ k , $ not , $ bind , $ insensitiveSearch );
1161+ $ likeStatement = $ this ->_like_statement ($ prefix , ( string ) $ k , $ not , $ bind , $ insensitiveSearch );
11731162
1174- // some platforms require an escape sequence definition for LIKE wildcards
11751163 if ($ escape === true && $ this ->db ->likeEscapeStr !== '' ) {
11761164 $ likeStatement .= sprintf ($ this ->db ->likeEscapeStr , $ this ->db ->likeEscapeChar );
11771165 }
11781166
11791167 $ this ->{$ clause }[] = [
1180- 'condition ' => $ likeStatement ,
1168+ 'condition ' => ( $ k instanceof RawSql) ? $ k -> with ( $ likeStatement ) : $ likeStatement ,
11811169 'escape ' => $ escape ,
11821170 ];
1171+
1172+ $ prefix = $ type ;
11831173 }
11841174
11851175 return $ this ;
0 commit comments