Repository navigation
Expand file tree
/
Copy pathbuilder.go
More file actions
335 lines (283 loc) · 9.37 KB
/
Copy pathbuilder.go
File metadata and controls
335 lines (283 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package gpa
import "context"
// =====================================
// Query Builder
// =====================================
// QueryBuilder provides a fluent interface for building type-safe database queries.
// Supports method chaining for convenient query construction.
type QueryBuilder[T any] struct {
conditions []Condition
orders []Order
limit *int
offset *int
fields []string
joins []JoinClause
groups []string
having []Condition
preloads []string
distinct bool
lock LockType
}
// NewQueryBuilder creates a new type-safe query builder for entity type T.
// Example: qb := NewQueryBuilder[User]()
func NewQueryBuilder[T any]() *QueryBuilder[T] {
return &QueryBuilder[T]{
conditions: make([]Condition, 0),
orders: make([]Order, 0),
fields: make([]string, 0),
joins: make([]JoinClause, 0),
groups: make([]string, 0),
having: make([]Condition, 0),
preloads: make([]string, 0),
lock: LockNone,
}
}
// Where adds a WHERE condition to the query.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Where("age", ">", 18).Where("status", "=", "active")
func (qb *QueryBuilder[T]) Where(field string, operator Operator, value interface{}) *QueryBuilder[T] {
qb.conditions = append(qb.conditions, BasicCondition{
FieldName: field,
Op: operator,
Val: value,
})
return qb
}
// WhereCondition adds a custom condition to the query.
// Useful for complex conditions that can't be expressed with simple field/operator/value.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.WhereCondition(customCondition)
func (qb *QueryBuilder[T]) WhereCondition(condition Condition) *QueryBuilder[T] {
qb.conditions = append(qb.conditions, condition)
return qb
}
// OrderBy adds an ORDER BY clause to the query.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.OrderBy("name", OrderAsc).OrderBy("created_at", OrderDesc)
func (qb *QueryBuilder[T]) OrderBy(field string, direction OrderDirection) *QueryBuilder[T] {
qb.orders = append(qb.orders, Order{
Field: field,
Direction: direction,
})
return qb
}
// Limit sets the maximum number of results to return.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Limit(10)
func (qb *QueryBuilder[T]) Limit(count int) *QueryBuilder[T] {
qb.limit = &count
return qb
}
// Offset sets the number of results to skip.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Offset(20)
func (qb *QueryBuilder[T]) Offset(count int) *QueryBuilder[T] {
qb.offset = &count
return qb
}
// Select specifies which fields to include in the results.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Select("id", "name", "email")
func (qb *QueryBuilder[T]) Select(fields ...string) *QueryBuilder[T] {
qb.fields = append(qb.fields, fields...)
return qb
}
// Join adds a JOIN clause to the query.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Join(JoinLeft, "orders", "users.id = orders.user_id")
func (qb *QueryBuilder[T]) Join(joinType JoinType, table string, condition string) *QueryBuilder[T] {
qb.joins = append(qb.joins, JoinClause{
Type: joinType,
Table: table,
Condition: condition,
})
return qb
}
// GroupBy adds a GROUP BY clause to the query.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.GroupBy("status", "department")
func (qb *QueryBuilder[T]) GroupBy(fields ...string) *QueryBuilder[T] {
qb.groups = append(qb.groups, fields...)
return qb
}
// Having adds a HAVING condition to the query.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Having("COUNT(*)", ">", 5)
func (qb *QueryBuilder[T]) Having(field string, operator Operator, value interface{}) *QueryBuilder[T] {
qb.having = append(qb.having, BasicCondition{
FieldName: field,
Op: operator,
Val: value,
})
return qb
}
// Distinct adds a DISTINCT clause to the query.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Distinct()
func (qb *QueryBuilder[T]) Distinct() *QueryBuilder[T] {
qb.distinct = true
return qb
}
// Lock adds a locking clause to the query.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Lock(LockExclusive)
func (qb *QueryBuilder[T]) Lock(lockType LockType) *QueryBuilder[T] {
qb.lock = lockType
return qb
}
// Preload specifies rel to eagerly load.
// Returns the same QueryBuilder instance for method chaining.
// Example: qb.Preload("Orders", "Profile")
func (qb *QueryBuilder[T]) Preload(relations ...string) *QueryBuilder[T] {
qb.preloads = append(qb.preloads, relations...)
return qb
}
// Build converts the QueryBuilder to a Query struct.
// Returns a Query that can be used with repository methods.
// Example: query := qb.Build()
func (qb *QueryBuilder[T]) Build() *Query {
return &Query{
Conditions: qb.conditions,
Orders: qb.orders,
Limit: qb.limit,
Offset: qb.offset,
Fields: qb.fields,
Joins: qb.joins,
Groups: qb.groups,
Having: qb.having,
Distinct: qb.distinct,
Lock: qb.lock,
Preloads: qb.preloads,
SubQueries: make([]SubQuery, 0),
}
}
// Execute executes the query using the provided repository.
// Returns a slice of entity pointers with compile-time type safety.
// Example: users, err := qb.Execute(ctx, repo)
func (qb *QueryBuilder[T]) Execute(ctx context.Context, repo Repository[T]) ([]*T, error) {
query := qb.Build()
// Convert Query to QueryOptions
var options []QueryOption
// Add conditions
for _, condition := range query.Conditions {
options = append(options, ConditionOption{Condition: condition})
}
// Add orders
for _, order := range query.Orders {
options = append(options, OrderOption{Order: order})
}
// Add limit
if query.Limit != nil {
options = append(options, LimitOption{Count: *query.Limit})
}
// Add offset
if query.Offset != nil {
options = append(options, OffsetOption{Count: *query.Offset})
}
// Add fields
if len(query.Fields) > 0 {
options = append(options, FieldsOption{Fields: query.Fields})
}
// Add joins
for _, join := range query.Joins {
options = append(options, JoinOption{Join: join})
}
// Add group by
if len(query.Groups) > 0 {
options = append(options, GroupByOption{Fields: query.Groups})
}
// Add having
for _, having := range query.Having {
options = append(options, HavingOption{Condition: having})
}
// Add distinct
if query.Distinct {
options = append(options, DistinctOption{})
}
// Add lock
if query.Lock != LockNone {
options = append(options, LockOption{Type: query.Lock})
}
// Add preloads
if len(query.Preloads) > 0 {
options = append(options, PreloadOption{Relations: query.Preloads})
}
return repo.Query(ctx, options...)
}
// ExecuteOne executes the query and returns a single result.
// Returns the entity directly with compile-time type safety.
// Returns ErrorTypeNotFound if no entity matches the query.
// Example: user, err := qb.ExecuteOne(ctx, repo)
func (qb *QueryBuilder[T]) ExecuteOne(ctx context.Context, repo Repository[T]) (*T, error) {
query := qb.Build()
// Convert Query to QueryOptions (same as Execute)
var options []QueryOption
// Add conditions
for _, condition := range query.Conditions {
options = append(options, ConditionOption{Condition: condition})
}
// Add orders
for _, order := range query.Orders {
options = append(options, OrderOption{Order: order})
}
// Add limit (force to 1 for single result)
options = append(options, LimitOption{Count: 1})
// Add offset
if query.Offset != nil {
options = append(options, OffsetOption{Count: *query.Offset})
}
// Add fields
if len(query.Fields) > 0 {
options = append(options, FieldsOption{Fields: query.Fields})
}
// Add joins
for _, join := range query.Joins {
options = append(options, JoinOption{Join: join})
}
// Add group by
if len(query.Groups) > 0 {
options = append(options, GroupByOption{Fields: query.Groups})
}
// Add having
for _, having := range query.Having {
options = append(options, HavingOption{Condition: having})
}
// Add distinct
if query.Distinct {
options = append(options, DistinctOption{})
}
// Add lock
if query.Lock != LockNone {
options = append(options, LockOption{Type: query.Lock})
}
// Add preloads
if len(query.Preloads) > 0 {
options = append(options, PreloadOption{Relations: query.Preloads})
}
return repo.QueryOne(ctx, options...)
}
// Count executes the query and returns the count of matching entities.
// Returns the count as an int64.
// Example: count, err := qb.Count(ctx, repo)
func (qb *QueryBuilder[T]) Count(ctx context.Context, repo Repository[T]) (int64, error) {
query := qb.Build()
// Convert Query to QueryOptions (only conditions matter for count)
var options []QueryOption
// Add conditions
for _, condition := range query.Conditions {
options = append(options, ConditionOption{Condition: condition})
}
// Add joins if they affect the count
for _, join := range query.Joins {
options = append(options, JoinOption{Join: join})
}
// Add group by if it affects the count
if len(query.Groups) > 0 {
options = append(options, GroupByOption{Fields: query.Groups})
}
// Add having if it affects the count
for _, having := range query.Having {
options = append(options, HavingOption{Condition: having})
}
return repo.Count(ctx, options...)
}