@@ -10,24 +10,119 @@ func (qb *QueryBuilder) Where(conditions ...QueryCondition) *QueryBuilder {
1010 if cond .isGroup {
1111 var groupParts []string
1212 for _ , groupCond := range cond .groupConds {
13- tempPlaceholder := fmt .Sprintf ("$%d" , qb .argCounter )
14- condition := strings .Replace (groupCond .condition , "$1" , tempPlaceholder , 1 )
15- groupParts = append (groupParts , condition )
16- qb .args = append (qb .args , groupCond .placeholder )
17- qb .values = append (qb .values , groupCond .value )
18- qb .argCounter ++
13+ // Expand slice values for IN clauses; otherwise single placeholder
14+ if strings .Contains (groupCond .condition , "IN $1" ) {
15+ switch v := groupCond .value .(type ) {
16+ case []int :
17+ placeholders := make ([]string , len (v ))
18+ for i := range v {
19+ placeholders [i ] = fmt .Sprintf ("$%d" , qb .argCounter + i )
20+ }
21+ expanded := strings .Replace (groupCond .condition , "IN $1" , "IN (" + strings .Join (placeholders , ", " )+ ")" , 1 )
22+ groupParts = append (groupParts , expanded )
23+ for _ , item := range v {
24+ qb .values = append (qb .values , item )
25+ }
26+ qb .argCounter += len (v )
27+ case []string :
28+ placeholders := make ([]string , len (v ))
29+ for i := range v {
30+ placeholders [i ] = fmt .Sprintf ("$%d" , qb .argCounter + i )
31+ }
32+ expanded := strings .Replace (groupCond .condition , "IN $1" , "IN (" + strings .Join (placeholders , ", " )+ ")" , 1 )
33+ groupParts = append (groupParts , expanded )
34+ for _ , item := range v {
35+ qb .values = append (qb .values , item )
36+ }
37+ qb .argCounter += len (v )
38+ default :
39+ tempPlaceholder := fmt .Sprintf ("$%d" , qb .argCounter )
40+ condition := strings .Replace (groupCond .condition , "$1" , tempPlaceholder , 1 )
41+ groupParts = append (groupParts , condition )
42+ qb .values = append (qb .values , groupCond .value )
43+ qb .argCounter ++
44+ }
45+ } else {
46+ // Handle date range queries with []string values (two placeholders)
47+ if v , ok := groupCond .value .([]string ); ok && (strings .Contains (groupCond .condition , "$1" ) && strings .Contains (groupCond .condition , "$2" )) {
48+ placeholder1 := fmt .Sprintf ("$%d" , qb .argCounter )
49+ placeholder2 := fmt .Sprintf ("$%d" , qb .argCounter + 1 )
50+ condition := strings .Replace (groupCond .condition , "$1" , placeholder1 , 1 )
51+ condition = strings .Replace (condition , "$2" , placeholder2 , 1 )
52+ groupParts = append (groupParts , condition )
53+ for _ , item := range v {
54+ qb .values = append (qb .values , item )
55+ }
56+ qb .argCounter += len (v )
57+ } else {
58+ tempPlaceholder := fmt .Sprintf ("$%d" , qb .argCounter )
59+ condition := strings .Replace (groupCond .condition , "$1" , tempPlaceholder , 1 )
60+ groupParts = append (groupParts , condition )
61+ qb .values = append (qb .values , groupCond .value )
62+ qb .argCounter ++
63+ }
64+ }
1965 }
2066 groupCondition := "(" + strings .Join (groupParts , " " + cond .groupOp + " " ) + ")"
2167 qb .conditions = append (qb .conditions , groupCondition )
2268 qb .operators = append (qb .operators , "AND" )
2369 } else {
24- tempPlaceholder := fmt .Sprintf ("$%d" , qb .argCounter )
25- condition := strings .Replace (cond .condition , "$1" , tempPlaceholder , 1 )
26- qb .conditions = append (qb .conditions , condition )
27- qb .operators = append (qb .operators , "AND" )
28- qb .args = append (qb .args , cond .placeholder )
29- qb .values = append (qb .values , cond .value )
30- qb .argCounter ++
70+ if strings .Contains (cond .condition , "IN $1" ) {
71+ switch v := cond .value .(type ) {
72+ case []int :
73+ placeholders := make ([]string , len (v ))
74+ for i := range v {
75+ placeholders [i ] = fmt .Sprintf ("$%d" , qb .argCounter + i )
76+ }
77+ condition := strings .Replace (cond .condition , "IN $1" , "IN (" + strings .Join (placeholders , ", " )+ ")" , 1 )
78+ qb .conditions = append (qb .conditions , condition )
79+ qb .operators = append (qb .operators , "AND" )
80+ for _ , item := range v {
81+ qb .values = append (qb .values , item )
82+ }
83+ qb .argCounter += len (v )
84+ case []string :
85+ placeholders := make ([]string , len (v ))
86+ for i := range v {
87+ placeholders [i ] = fmt .Sprintf ("$%d" , qb .argCounter + i )
88+ }
89+ condition := strings .Replace (cond .condition , "IN $1" , "IN (" + strings .Join (placeholders , ", " )+ ")" , 1 )
90+ qb .conditions = append (qb .conditions , condition )
91+ qb .operators = append (qb .operators , "AND" )
92+ for _ , item := range v {
93+ qb .values = append (qb .values , item )
94+ }
95+ qb .argCounter += len (v )
96+ default :
97+ tempPlaceholder := fmt .Sprintf ("$%d" , qb .argCounter )
98+ condition := strings .Replace (cond .condition , "$1" , tempPlaceholder , 1 )
99+ qb .conditions = append (qb .conditions , condition )
100+ qb .operators = append (qb .operators , "AND" )
101+ qb .values = append (qb .values , cond .value )
102+ qb .argCounter ++
103+ }
104+ } else {
105+ // Handle date range queries with []string values (two placeholders)
106+ if v , ok := cond .value .([]string ); ok && (strings .Contains (cond .condition , "$1" ) && strings .Contains (cond .condition , "$2" )) {
107+ placeholder1 := fmt .Sprintf ("$%d" , qb .argCounter )
108+ placeholder2 := fmt .Sprintf ("$%d" , qb .argCounter + 1 )
109+ condition := strings .Replace (cond .condition , "$1" , placeholder1 , 1 )
110+ condition = strings .Replace (condition , "$2" , placeholder2 , 1 )
111+ qb .conditions = append (qb .conditions , condition )
112+ qb .operators = append (qb .operators , "AND" )
113+ for _ , item := range v {
114+ qb .values = append (qb .values , item )
115+ }
116+ qb .argCounter += len (v )
117+ } else {
118+ tempPlaceholder := fmt .Sprintf ("$%d" , qb .argCounter )
119+ condition := strings .Replace (cond .condition , "$1" , tempPlaceholder , 1 )
120+ qb .conditions = append (qb .conditions , condition )
121+ qb .operators = append (qb .operators , "AND" )
122+ qb .values = append (qb .values , cond .value )
123+ qb .argCounter ++
124+ }
125+ }
31126 }
32127 }
33128 return qb
0 commit comments