-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
213 lines (191 loc) · 7.5 KB
/
options.go
File metadata and controls
213 lines (191 loc) · 7.5 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
package sqlquery
// Supported SQL database flavors for query compilation.
const (
MySQLFlavor Flavor = iota + 1
PostgreSQLFlavor
SQLiteFlavor
)
// Flavor represents the SQL dialect used for query compilation.
// It controls the format of compiled SQL to match database-specific syntax.
type Flavor int
// FindOptions configures the behavior of FindQuery for building SELECT queries.
//
// Fields:
// - Flavor: The SQL dialect (MySQLFlavor, PostgreSQLFlavor, or SQLiteFlavor)
// - Fields: Column names to select (defaults to "*" for all columns)
// - Filters: WHERE conditions using field names and optional operators (see package docs for filter syntax)
// - ForUpdate: Whether to add FOR UPDATE clause for row locking
// - ForUpdateMode: Optional mode for FOR UPDATE (e.g., "NOWAIT", "SKIP LOCKED")
type FindOptions struct {
Flavor Flavor
Fields []string
Filters map[string]interface{}
ForUpdate bool
ForUpdateMode string
}
// WithFields returns a new FindOptions with the specified field list.
// Use this to select specific columns instead of "*".
func (f *FindOptions) WithFields(fields []string) *FindOptions {
copy := *f
copy.Fields = fields
return ©
}
// WithFilter returns a new FindOptions with an additional filter condition.
// Supports special operators via dot notation (e.g., "age.gte", "status.in").
// See package documentation for complete filter syntax.
func (f *FindOptions) WithFilter(field string, value interface{}) *FindOptions {
copy := *f
copy.Filters[field] = value
return ©
}
// WithForUpdate returns a new FindOptions with FOR UPDATE clause enabled.
// The mode parameter can specify locking behavior (e.g., "NOWAIT", "SKIP LOCKED").
// Pass an empty string for default FOR UPDATE behavior.
func (f *FindOptions) WithForUpdate(mode string) *FindOptions {
copy := *f
copy.ForUpdate = true
copy.ForUpdateMode = mode
return ©
}
// NewFindOptions creates a new FindOptions with default values.
// The fields list defaults to ["*"] (all columns), and an empty filters map is initialized.
func NewFindOptions(flavor Flavor) *FindOptions {
return &FindOptions{
Fields: []string{"*"},
Flavor: flavor,
Filters: make(map[string]interface{}),
}
}
// FindAllOptions configures the behavior of FindAllQuery for building paginated SELECT queries.
//
// Fields:
// - Flavor: The SQL dialect (MySQLFlavor, PostgreSQLFlavor, or SQLiteFlavor)
// - Fields: Column names to select (defaults to "*" for all columns)
// - Filters: WHERE conditions using field names and optional operators (see package docs for filter syntax)
// - Limit: Maximum number of rows to return
// - Offset: Number of rows to skip before returning results
// - OrderBy: ORDER BY clause (e.g., "created_at DESC", "name ASC, id DESC")
// - ForUpdate: Whether to add FOR UPDATE clause for row locking
// - ForUpdateMode: Optional mode for FOR UPDATE (e.g., "NOWAIT", "SKIP LOCKED")
type FindAllOptions struct {
Flavor Flavor
Fields []string
Filters map[string]interface{}
Limit int
Offset int
OrderBy string
ForUpdate bool
ForUpdateMode string
}
// WithFields returns a new FindAllOptions with the specified field list.
// Use this to select specific columns instead of "*".
func (f *FindAllOptions) WithFields(fields []string) *FindAllOptions {
copy := *f
copy.Fields = fields
return ©
}
// WithFilter returns a new FindAllOptions with an additional filter condition.
// Supports special operators via dot notation (e.g., "age.gte", "status.in").
// See package documentation for complete filter syntax.
func (f *FindAllOptions) WithFilter(field string, value interface{}) *FindAllOptions {
copy := *f
copy.Filters[field] = value
return ©
}
// WithLimit returns a new FindAllOptions with the specified LIMIT value.
func (f *FindAllOptions) WithLimit(limit int) *FindAllOptions {
copy := *f
copy.Limit = limit
return ©
}
// WithOffset returns a new FindAllOptions with the specified OFFSET value.
func (f *FindAllOptions) WithOffset(offset int) *FindAllOptions {
copy := *f
copy.Offset = offset
return ©
}
// WithOrderBy returns a new FindAllOptions with the specified ORDER BY clause.
// The orderBy parameter should be a valid SQL ORDER BY expression (e.g., "created_at DESC").
func (f *FindAllOptions) WithOrderBy(orderBy string) *FindAllOptions {
copy := *f
copy.OrderBy = orderBy
return ©
}
// WithForUpdate returns a new FindAllOptions with FOR UPDATE clause enabled.
// The mode parameter can specify locking behavior (e.g., "NOWAIT", "SKIP LOCKED").
// Pass an empty string for default FOR UPDATE behavior.
func (f *FindAllOptions) WithForUpdate(mode string) *FindAllOptions {
copy := *f
copy.ForUpdate = true
copy.ForUpdateMode = mode
return ©
}
// NewFindAllOptions creates a new FindAllOptions with default values.
// The fields list defaults to ["*"] (all columns), and an empty filters map is initialized.
func NewFindAllOptions(flavor Flavor) *FindAllOptions {
return &FindAllOptions{
Fields: []string{"*"},
Flavor: flavor,
Filters: make(map[string]interface{}),
}
}
// UpdateOptions configures the behavior of UpdateWithOptionsQuery for building UPDATE queries.
//
// Fields:
// - Flavor: The SQL dialect (MySQLFlavor, PostgreSQLFlavor, or SQLiteFlavor)
// - Assignments: Field-value pairs to set (e.g., {"status": "active", "updated_at": time.Now()})
// - Filters: WHERE conditions using field names and optional operators (see package docs for filter syntax)
type UpdateOptions struct {
Flavor Flavor
Assignments map[string]interface{}
Filters map[string]interface{}
}
// WithAssignment returns a new UpdateOptions with an additional field assignment.
// Use this to specify which fields to update and their new values.
func (u *UpdateOptions) WithAssignment(field string, value interface{}) *UpdateOptions {
copy := *u
copy.Assignments[field] = value
return ©
}
// WithFilter returns a new UpdateOptions with an additional filter condition.
// Supports special operators via dot notation (e.g., "age.gte", "status.in").
// See package documentation for complete filter syntax.
func (u *UpdateOptions) WithFilter(field string, value interface{}) *UpdateOptions {
copy := *u
copy.Filters[field] = value
return ©
}
// NewUpdateOptions creates a new UpdateOptions with default values.
// Empty maps are initialized for both assignments and filters.
func NewUpdateOptions(flavor Flavor) *UpdateOptions {
return &UpdateOptions{
Flavor: flavor,
Assignments: make(map[string]interface{}),
Filters: make(map[string]interface{}),
}
}
// DeleteOptions configures the behavior of DeleteWithOptionsQuery for building DELETE queries.
//
// Fields:
// - Flavor: The SQL dialect (MySQLFlavor, PostgreSQLFlavor, or SQLiteFlavor)
// - Filters: WHERE conditions using field names and optional operators (see package docs for filter syntax)
type DeleteOptions struct {
Flavor Flavor
Filters map[string]interface{}
}
// WithFilter returns a new DeleteOptions with an additional filter condition.
// Supports special operators via dot notation (e.g., "age.gte", "status.in").
// See package documentation for complete filter syntax.
func (d *DeleteOptions) WithFilter(field string, value interface{}) *DeleteOptions {
copy := *d
copy.Filters[field] = value
return ©
}
// NewDeleteOptions creates a new DeleteOptions with default values.
// An empty filters map is initialized.
func NewDeleteOptions(flavor Flavor) *DeleteOptions {
return &DeleteOptions{
Flavor: flavor,
Filters: make(map[string]interface{}),
}
}