Skip to content

Commit f521f06

Browse files
committed
add missing methods in cond.go
1 parent 0aa1ca2 commit f521f06

File tree

8 files changed

+900
-110
lines changed

8 files changed

+900
-110
lines changed

builder.go

Lines changed: 268 additions & 51 deletions
Large diffs are not rendered by default.

cond.go

Lines changed: 231 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
1+
// Package builder provides a fluent SQL query builder with support for multiple SQL dialects.
12
package builder
23

3-
// type Fields []string
4-
5-
// Values ...
4+
// Values represents a slice of interface{} used for storing query parameter values.
5+
// It is used throughout the package to handle various types of SQL parameter values
6+
// in a type-safe manner. This type provides a convenient way to pass multiple
7+
// values to SQL conditions while maintaining type flexibility.
8+
//
9+
// Example usage:
10+
//
11+
// values := Values{1, "active", true}
12+
// // Can be used with IN conditions
13+
// b.Select("*").From("users").Where(builder.In("status", values...))
614
type Values []interface{}
715

8-
// Condition for Where & Order Cluse
16+
// Condition represents a SQL condition that can be used in WHERE clauses or ORDER BY statements.
17+
// It supports various SQL operators and can be combined using AND/OR logic.
18+
//
19+
// Conditions can be created using helper functions like And(), Or(), Between(), In(), etc.
20+
// Multiple conditions can be combined to create complex WHERE clauses with proper
21+
// operator precedence and grouping.
22+
//
23+
// Example usage:
24+
//
25+
// // Simple condition
26+
// cond1 := builder.And("status", "=", "active")
27+
//
28+
// // Multiple conditions combined
29+
// cond2 := builder.Or("role", "IN", "admin", "moderator")
30+
// b.Select("*").From("users").Where(cond1, cond2)
931
type Condition struct {
10-
Field string // field name
11-
Asc bool // true for `ASC`, false for `DESC`
12-
AndOr bool // true for `AND`, false for `OR`
13-
Operator string // where operator
14-
Values []interface{} // query args
32+
Field string // The name of the database field or column
33+
Asc bool // Sort direction: true for ASC, false for DESC
34+
AndOr bool // Logical operator: true for AND, false for OR
35+
Operator string // SQL operator (e.g., =, >, LIKE, IN, etc.)
36+
Values []interface{} // The values to compare against the field
1537
}
1638

17-
// newCondition : get new Condition
39+
// newCondition creates a new Condition with the specified parameters.
40+
// It is an internal helper function used by the public condition constructors.
1841
func newCondition(andOr bool, field string, op string, values []interface{}) *Condition {
1942
return &Condition{
2043
AndOr: andOr,
@@ -24,39 +47,221 @@ func newCondition(andOr bool, field string, op string, values []interface{}) *Co
2447
}
2548
}
2649

27-
// And return "and" condition
50+
// And creates a new condition that will be combined with AND logic.
51+
// It takes a field name, operator, and values to construct the condition.
52+
//
53+
// Parameters:
54+
// - field: The database column or field name
55+
// - op: The SQL operator (e.g., "=", ">", "LIKE", etc.)
56+
// - values: The values to compare against the field
57+
//
58+
// Example:
59+
//
60+
// // Creates: WHERE age >= 18 AND status = 'active'
61+
// b.Where(
62+
// builder.And("age", ">=", 18),
63+
// builder.And("status", "=", "active"),
64+
// )
2865
func And(field string, op string, values ...interface{}) *Condition {
2966
return newCondition(true, field, op, values)
3067
}
3168

32-
// Or return "or" condition
69+
// Or creates a new condition that will be combined with OR logic.
70+
// It takes a field name, operator, and values to construct the condition.
71+
//
72+
// Parameters:
73+
// - field: The database column or field name
74+
// - op: The SQL operator (e.g., "=", ">", "LIKE", etc.)
75+
// - values: The values to compare against the field
76+
//
77+
// Example:
78+
//
79+
// // Creates: WHERE role = 'admin' OR role = 'moderator'
80+
// b.Where(
81+
// builder.Or("role", "=", "admin"),
82+
// builder.Or("role", "=", "moderator"),
83+
// )
3384
func Or(field string, op string, values ...interface{}) *Condition {
3485
return newCondition(false, field, op, values)
3586
}
3687

37-
// Between return "and `field` between" condition
88+
// Between creates a new BETWEEN condition for the specified field.
89+
// The values parameter should contain exactly two values defining the range.
90+
//
91+
// Parameters:
92+
// - field: The database column or field name
93+
// - values: Exactly two values defining the range (start and end)
94+
//
95+
// Example:
96+
//
97+
// // Creates: WHERE price BETWEEN 10 AND 100
98+
// b.Where(builder.Between("price", 10, 100))
3899
func Between(field string, values ...interface{}) *Condition {
39100
return newCondition(false, field, "BETWEEN", values)
40101
}
41102

42-
// NotBetween return "and `field` not between" condition
103+
// NotBetween creates a new NOT BETWEEN condition for the specified field.
104+
// The values parameter should contain exactly two values defining the range.
105+
//
106+
// Parameters:
107+
// - field: The database column or field name
108+
// - values: Exactly two values defining the range (start and end)
109+
//
110+
// Example:
111+
//
112+
// // Creates: WHERE age NOT BETWEEN 0 AND 18
113+
// b.Where(builder.NotBetween("age", 0, 18))
43114
func NotBetween(field string, values ...interface{}) *Condition {
44115
return newCondition(false, field, "NOT BETWEEN", values)
45116
}
46117

47-
// In return "and `field` in" condition
118+
// Eq creates a new equality condition for the specified field and value.
119+
//
120+
// Parameters:
121+
// - field: The database column or field name
122+
// - value: The value to compare against the field
123+
//
124+
// Example:
125+
//
126+
// // Creates: WHERE status = 'active'
127+
// b.Where(builder.Eq("status", "active"))
128+
func Eq(field string, value interface{}) *Condition {
129+
return newCondition(false, field, "=", []interface{}{value})
130+
}
131+
132+
// NotEq creates a new inequality condition for the specified field and value.
133+
//
134+
// Parameters:
135+
// - field: The database column or field name
136+
// - value: The value to compare against the field
137+
//
138+
// Example:
139+
//
140+
// // Creates: WHERE status != 'active'
141+
// b.Where(builder.NotEq("status", "active"))
142+
func NotEq(field string, value interface{}) *Condition {
143+
return newCondition(false, field, "!=", []interface{}{value})
144+
}
145+
146+
// Gt creates a new greater than condition for the specified field and value.
147+
//
148+
// Parameters:
149+
// - field: The database column or field name
150+
// - value: The value to compare against the field
151+
//
152+
// Example:
153+
//
154+
// // Creates: WHERE age > 18
155+
// b.Where(builder.Gt("age", 18))
156+
func Gt(field string, value interface{}) *Condition {
157+
return newCondition(false, field, ">", []interface{}{value})
158+
}
159+
160+
// Gte creates a new greater than or equal to condition for the specified field and value.
161+
//
162+
// Parameters:
163+
// - field: The database column or field name
164+
// - value: The value to compare against the field
165+
//
166+
// Example:
167+
//
168+
// // Creates: WHERE age >= 18
169+
// b.Where(builder.Gte("age", 18))
170+
func Gte(field string, value interface{}) *Condition {
171+
return newCondition(false, field, ">=", []interface{}{value})
172+
}
173+
174+
// Lt creates a new less than condition for the specified field and value.
175+
//
176+
// Parameters:
177+
// - field: The database column or field name
178+
// - value: The value to compare against the field
179+
//
180+
// Example:
181+
//
182+
// // Creates: WHERE age < 18
183+
// b.Where(builder.Lt("age", 18))
184+
func Lt(field string, value interface{}) *Condition {
185+
return newCondition(false, field, "<", []interface{}{value})
186+
}
187+
188+
// Lte creates a new less than or equal to condition for the specified field and value.
189+
//
190+
// Parameters:
191+
// - field: The database column or field name
192+
// - value: The value to compare against the field
193+
//
194+
// Example:
195+
//
196+
// // Creates: WHERE age <= 18
197+
// b.Where(builder.Lte("age", 18))
198+
func Lte(field string, value interface{}) *Condition {
199+
return newCondition(false, field, "<=", []interface{}{value})
200+
}
201+
202+
// Like creates a new LIKE condition for the specified field and value.
203+
//
204+
// Parameters:
205+
// - field: The database column or field name
206+
// - value: The value to compare against the field
207+
//
208+
// Example:
209+
//
210+
// // Creates: WHERE name LIKE '%John%'
211+
// b.Where(builder.Like("name", "%John%"))
212+
func Like(field string, value interface{}) *Condition {
213+
return newCondition(false, field, "LIKE", []interface{}{value})
214+
}
215+
216+
// NotLike creates a new NOT LIKE condition for the specified field and value.
217+
//
218+
// Parameters:
219+
// - field: The database column or field name
220+
// - value: The value to compare against the field
221+
//
222+
// Example:
223+
//
224+
// // Creates: WHERE name NOT LIKE '%John%'
225+
// b.Where(builder.NotLike("name", "%John%"))
226+
func NotLike(field string, value interface{}) *Condition {
227+
return newCondition(false, field, "NOT LIKE", []interface{}{value})
228+
}
229+
230+
// In creates a new IN condition for the specified field.
231+
// The values parameter contains the list of values to match against.
232+
//
233+
// Parameters:
234+
// - field: The database column or field name
235+
// - values: One or more values to include in the IN clause
236+
//
237+
// Example:
238+
//
239+
// // Creates: WHERE status IN ('pending', 'processing', 'completed')
240+
// b.Where(builder.In("status", "pending", "processing", "completed"))
48241
func In(field string, values ...interface{}) *Condition {
49242
return newCondition(false, field, "IN", values)
50243
}
51244

52-
// NotIn return "and `field` not in" condition
245+
// NotIn creates a new NOT IN condition for the specified field.
246+
// The values parameter contains the list of values to exclude.
247+
//
248+
// Parameters:
249+
// - field: The database column or field name
250+
// - values: One or more values to exclude in the NOT IN clause
251+
//
252+
// Example:
253+
//
254+
// // Creates: WHERE status NOT IN ('deleted', 'banned')
255+
// b.Where(builder.NotIn("status", "deleted", "banned"))
53256
func NotIn(field string, values ...interface{}) *Condition {
54257
return newCondition(false, field, "NOT IN", values)
55258
}
56259

57-
// NewConditionGroup reutrn condition group
260+
// NewConditionGroup creates a group of conditions that can be used together.
261+
// It accepts multiple conditions and returns them as a slice.
58262
//
59-
// @Warning: don't mix `OrderBy` and `Where` conditions...
263+
// Warning: Do not mix OrderBy and Where conditions in the same group as they
264+
// serve different purposes and may lead to unexpected behavior.
60265
func NewConditionGroup(conds ...*Condition) (cg []*Condition) {
61266
if len(conds) > 0 {
62267
cg = append(cg, conds...)
@@ -74,28 +279,32 @@ func NewConditionGroup(conds ...*Condition) (cg []*Condition) {
74279
// return newCondition(false, field, op, values)
75280
// }
76281

77-
// newOrderCondition : get new Order Condition
282+
// newOrderCondition creates a new Condition for ORDER BY clauses.
283+
// It is an internal helper function used by Asc and Desc functions.
78284
func newOrderCondition(field string, asc bool) *Condition {
79285
return &Condition{
80286
Field: field,
81287
Asc: asc,
82288
}
83289
}
84290

85-
// OrderBy new orderby conditions
291+
// OrderBy creates a new slice of ordering conditions.
292+
// It accepts multiple order conditions and combines them into a single ordering clause.
86293
func OrderBy(conds ...*Condition) (by []*Condition) {
87294
if len(conds) > 0 {
88295
by = append(by, conds...)
89296
}
90297
return
91298
}
92299

93-
// Desc return `OrderBy` condition with descending order by `field`
300+
// Desc creates a new descending ORDER BY condition for the specified field.
301+
// It is used to sort results in descending order.
94302
func Desc(field string) *Condition {
95303
return newOrderCondition(field, false)
96304
}
97305

98-
// Asc return `OrderBy` condition with ascending order by `field`
306+
// Asc creates a new ascending ORDER BY condition for the specified field.
307+
// It is used to sort results in ascending order.
99308
func Asc(field string) *Condition {
100309
return newOrderCondition(field, true)
101310
}

0 commit comments

Comments
 (0)