-
Notifications
You must be signed in to change notification settings - Fork 0
/
q_simple.go
95 lines (79 loc) · 2.1 KB
/
q_simple.go
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
package dal
import "reflect"
type SingleSource interface {
Where(conditions ...Condition) QueryBuilder
}
type Cursor string
type QueryBuilder interface {
Offset(int) QueryBuilder
Limit(int) QueryBuilder
Where(conditions ...Condition) QueryBuilder
WhereField(name string, operator Operator, v any) QueryBuilder
OrderBy(expressions ...OrderExpression) QueryBuilder
SelectInto(func() Record) Query
SelectKeysOnly(idKind reflect.Kind) Query
StartFrom(cursor Cursor) QueryBuilder
}
var _ QueryBuilder = (*queryBuilder)(nil)
func From(collection string, conditions ...Condition) QueryBuilder {
return &queryBuilder{collection: collection, conditions: conditions}
}
type queryBuilder struct {
collection string
offset int
limit int
conditions []Condition
orderBy []OrderExpression
startCursor Cursor
}
func (s queryBuilder) StartFrom(cursor Cursor) QueryBuilder {
s.startCursor = cursor
return s
}
func (s queryBuilder) Offset(i int) QueryBuilder {
s.offset = i
return s
}
func (s queryBuilder) Limit(i int) QueryBuilder {
s.limit = i
return s
}
func (s queryBuilder) OrderBy(expressions ...OrderExpression) QueryBuilder {
s.orderBy = append(s.orderBy, expressions...)
return s
}
func (s queryBuilder) Where(conditions ...Condition) QueryBuilder {
s.conditions = append(s.conditions, conditions...)
return s
}
func (s queryBuilder) WhereField(name string, operator Operator, v any) QueryBuilder {
s.conditions = append(s.conditions, WhereField(name, operator, v))
return s
}
func (s queryBuilder) SelectInto(into func() Record) Query {
q := s.newQuery()
q.into = into
return q
}
func (s queryBuilder) SelectKeysOnly(idKind reflect.Kind) Query {
q := s.newQuery()
q.idKind = idKind
return q
}
func (s queryBuilder) newQuery() theQuery {
q := theQuery{
from: &CollectionRef{Name: s.collection},
limit: s.limit,
orderBy: s.orderBy,
offset: s.offset,
startCursor: s.startCursor,
}
switch len(s.conditions) {
case 0: // no conditions
case 1:
q.where = s.conditions[0]
default:
q.where = GroupCondition{conditions: s.conditions, operator: And}
}
return q
}