-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.go
More file actions
323 lines (269 loc) Β· 9.57 KB
/
Copy pathexpression.go
File metadata and controls
323 lines (269 loc) Β· 9.57 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
package oro
import "strings"
// FieldExpr describes a structured field condition target.
type FieldExpr struct {
Name string
Alias string
}
// Field creates a field expression. Model queries resolve name as a Go field;
// table queries resolve name as a database column.
func Field(name string) FieldExpr {
return FieldExpr{Name: name}
}
// Column is a semantic alias for Field when the call site wants column wording.
func Column(name string) FieldExpr {
return FieldExpr{Name: name}
}
// Eq returns a field = value condition.
func (field FieldExpr) Eq(value any) Condition {
return Condition{Field: field.Name, Op: "=", Value: value}
}
// NotEq returns a field != value condition.
func (field FieldExpr) NotEq(value any) Condition {
return Condition{Field: field.Name, Op: "!=", Value: value}
}
// Gt returns a field > value condition.
func (field FieldExpr) Gt(value any) Condition {
return Condition{Field: field.Name, Op: ">", Value: value}
}
// Gte returns a field >= value condition.
func (field FieldExpr) Gte(value any) Condition {
return Condition{Field: field.Name, Op: ">=", Value: value}
}
// Lt returns a field < value condition.
func (field FieldExpr) Lt(value any) Condition {
return Condition{Field: field.Name, Op: "<", Value: value}
}
// Lte returns a field <= value condition.
func (field FieldExpr) Lte(value any) Condition {
return Condition{Field: field.Name, Op: "<=", Value: value}
}
// Like returns a LIKE condition without escaping wildcard characters.
func (field FieldExpr) Like(value any) Condition {
return Condition{Field: field.Name, Op: "like", Value: value}
}
// NotLike returns a NOT LIKE condition without escaping wildcard characters.
func (field FieldExpr) NotLike(value any) Condition {
return Condition{Field: field.Name, Op: "not like", Value: value}
}
// Contains returns an escaped literal substring LIKE condition.
func (field FieldExpr) Contains(value string) Condition {
return Condition{Field: field.Name, Op: "like", Value: "%" + EscapeLike(value) + "%", Escape: `\`}
}
// StartsWith returns an escaped literal prefix LIKE condition.
func (field FieldExpr) StartsWith(value string) Condition {
return Condition{Field: field.Name, Op: "like", Value: EscapeLike(value) + "%", Escape: `\`}
}
// EndsWith returns an escaped literal suffix LIKE condition.
func (field FieldExpr) EndsWith(value string) Condition {
return Condition{Field: field.Name, Op: "like", Value: "%" + EscapeLike(value), Escape: `\`}
}
// In returns an IN condition for a list of values.
func (field FieldExpr) In(values ...any) Condition {
return Condition{Field: field.Name, Op: "in_values", Value: append([]any(nil), values...)}
}
// NotIn returns a NOT IN condition for a list of values.
func (field FieldExpr) NotIn(values ...any) Condition {
return Condition{Field: field.Name, Op: "not_in_values", Value: append([]any(nil), values...)}
}
// Between returns a closed BETWEEN condition.
func (field FieldExpr) Between(start any, end any) Condition {
return Condition{Field: field.Name, Op: "between", Value: []any{start, end}}
}
// NotBetween returns the negation of a closed BETWEEN condition.
func (field FieldExpr) NotBetween(start any, end any) Condition {
return Not(field.Between(start, end))
}
// IsNull returns an IS NULL condition.
func (field FieldExpr) IsNull() Condition {
return isNullCondition(field.Name)
}
// IsNotNull returns an IS NOT NULL condition.
func (field FieldExpr) IsNotNull() Condition {
return isNotNullCondition(field.Name)
}
// EqCol returns a field = right-column condition.
func (field FieldExpr) EqCol(right string) Condition {
return buildColumnCondition(field.Name, "=", right)
}
// NotEqCol returns a field != right-column condition.
func (field FieldExpr) NotEqCol(right string) Condition {
return buildColumnCondition(field.Name, "!=", right)
}
// GtCol returns a field > right-column condition.
func (field FieldExpr) GtCol(right string) Condition {
return buildColumnCondition(field.Name, ">", right)
}
// GteCol returns a field >= right-column condition.
func (field FieldExpr) GteCol(right string) Condition {
return buildColumnCondition(field.Name, ">=", right)
}
// LtCol returns a field < right-column condition.
func (field FieldExpr) LtCol(right string) Condition {
return buildColumnCondition(field.Name, "<", right)
}
// LteCol returns a field <= right-column condition.
func (field FieldExpr) LteCol(right string) Condition {
return buildColumnCondition(field.Name, "<=", right)
}
// RawExpr is a raw SQL expression with bound arguments.
type RawExpr struct {
SQL string
Args []any
}
// Raw creates a raw SQL expression. When used as db.Raw it starts a raw query;
// when used in Select or Where it acts as a structured raw expression.
func Raw(sql string, args ...any) RawExpr {
return RawExpr{SQL: sql, Args: args}
}
// EscapeLike escapes \, %, and _ for literal LIKE matching.
func EscapeLike(value string) string {
value = strings.ReplaceAll(value, `\`, `\\`)
value = strings.ReplaceAll(value, `%`, `\%`)
value = strings.ReplaceAll(value, `_`, `\_`)
return value
}
// IncrementExpr represents an arithmetic increment write expression.
type IncrementExpr struct {
Value any
}
// Increment creates an increment write expression.
func Increment(value any) IncrementExpr {
return IncrementExpr{Value: value}
}
// DecrementExpr represents an arithmetic decrement write expression.
type DecrementExpr struct {
Value any
}
// Decrement creates a decrement write expression.
func Decrement(value any) DecrementExpr {
return DecrementExpr{Value: value}
}
func selectExprs(items []any) ([]SelectExpr, error) {
exprs := make([]SelectExpr, 0, len(items))
for _, item := range items {
switch typedItem := item.(type) {
case string:
exprs = append(exprs, SelectExpr{Expr: typedItem})
case FieldExpr:
exprs = append(exprs, SelectExpr{Expr: typedItem.Name, Alias: typedItem.Alias})
case RawExpr:
exprs = append(exprs, SelectExpr{Expr: typedItem.SQL, Raw: true, Args: typedItem.Args})
case AggregateExpr:
exprs = append(exprs, SelectExpr{Expr: "__oro_aggregate__", Alias: typedItem.Alias, Args: []any{typedItem}})
case RelationAggregateExpr:
exprs = append(exprs, SelectExpr{Expr: "__oro_relation_aggregate__", Alias: typedItem.Alias, Raw: true, Args: []any{typedItem}})
case FullTextExpr:
exprs = append(exprs, SelectExpr{Expr: "__oro_fulltext_score__", Alias: typedItem.Alias, Raw: true, Args: []any{typedItem}})
case QuerySource:
source := typedItem.sourceAST()
exprs = append(exprs, SelectExpr{Alias: source.Alias, Source: &source})
default:
return nil, &Error{Op: "select", Kind: ErrInvalidArgument}
}
}
return exprs, nil
}
func orderExprs(desc bool, fields []string) []OrderExpr {
exprs := make([]OrderExpr, 0, len(fields))
for _, field := range fields {
exprs = append(exprs, OrderExpr{Expr: field, Desc: desc})
}
return exprs
}
// JSONField describes a JSON column for path conditions.
type JSONField struct {
Field string
}
// JSONPath describes a JSON column path condition target.
type JSONPath struct {
Field string
Parts []string
}
// JSONCondition is the structured payload for JSON path comparisons.
type JSONCondition struct {
Field string
Parts []string
Op string
Value any
}
// JSON creates a JSON field expression for path-based conditions.
func JSON(field string) JSONField {
return JSONField{Field: field}
}
// Path selects a nested JSON path under the JSON field.
func (field JSONField) Path(parts ...string) JSONPath {
return JSONPath{Field: field.Field, Parts: append([]string(nil), parts...)}
}
// Eq returns a JSON path equality condition.
func (path JSONPath) Eq(value any) Condition {
return jsonCondition(path, "=", value)
}
// NotEq returns a JSON path inequality condition.
func (path JSONPath) NotEq(value any) Condition {
return jsonCondition(path, "!=", value)
}
// IsNull returns a JSON path IS NULL condition.
func (path JSONPath) IsNull() Condition {
return jsonCondition(path, "is null", nil)
}
// IsNotNull returns a JSON path IS NOT NULL condition.
func (path JSONPath) IsNotNull() Condition {
return jsonCondition(path, "is not null", nil)
}
// Exists returns a JSON path existence condition.
func (path JSONPath) Exists() Condition {
return jsonCondition(path, "exists", nil)
}
// Contains returns a JSON path containment condition.
func (path JSONPath) Contains(value any) Condition {
return jsonCondition(path, "contains", value)
}
// Like returns a JSON path LIKE condition.
func (path JSONPath) Like(value any) Condition {
return jsonCondition(path, "like", value)
}
func jsonCondition(path JSONPath, op string, value any) Condition {
return Condition{
Field: path.Field,
Op: "json",
Value: JSONCondition{
Field: path.Field,
Parts: append([]string(nil), path.Parts...),
Op: op,
Value: value,
},
}
}
// FullTextExpr describes a full-text match condition over one or more fields.
type FullTextExpr struct {
Fields []string
Query string
Alias string
IsScore bool
}
// FullText creates a full-text expression over fields.
func FullText(fields ...string) FullTextExpr {
return FullTextExpr{Fields: append([]string(nil), fields...)}
}
// Match returns a full-text match condition.
func (expr FullTextExpr) Match(query string) Condition {
return Condition{
Op: "fulltext",
Value: FullTextExpr{
Fields: append([]string(nil), expr.Fields...),
Query: query,
},
}
}
// Score returns a full-text score expression for SELECT lists.
func (expr FullTextExpr) Score(query string) FullTextExpr {
expr.Query = query
expr.IsScore = true
return expr
}
// As aliases a full-text score expression.
func (expr FullTextExpr) As(alias string) FullTextExpr {
expr.Alias = alias
return expr
}