-
Notifications
You must be signed in to change notification settings - Fork 1
/
clause.go
246 lines (191 loc) · 4.65 KB
/
clause.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
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
package genorm
import (
"errors"
"fmt"
"strings"
)
type whereConditionClause[T Table] struct {
condition TypedTableExpr[T, WrappedPrimitive[bool]]
}
func (c *whereConditionClause[T]) set(condition TypedTableExpr[T, WrappedPrimitive[bool]]) error {
if c.condition != nil {
return errors.New("where conditions already set")
}
if condition == nil {
return errors.New("empty where condition")
}
c.condition = condition
return nil
}
func (c *whereConditionClause[T]) exists() bool {
return c.condition != nil
}
func (c *whereConditionClause[T]) getExpr() (string, []ExprType, error) {
if c.condition == nil {
return "", nil, errors.New("empty where condition")
}
query, args, errs := c.condition.Expr()
if len(errs) != 0 {
return "", nil, errs[0]
}
return query, args, nil
}
type groupClause[T Table] struct {
exprs []TableExpr[T]
}
func (c *groupClause[T]) set(exprs []TableExpr[T]) error {
if len(c.exprs) != 0 {
return errors.New("group by already set")
}
if len(exprs) == 0 {
return errors.New("empty group by")
}
c.exprs = exprs
return nil
}
func (c *groupClause[_]) exists() bool {
return len(c.exprs) != 0
}
func (c *groupClause[T]) getExpr() (string, []ExprType, error) {
if len(c.exprs) == 0 {
return "", nil, errors.New("empty group by")
}
queries := make([]string, 0, len(c.exprs))
args := []ExprType{}
for _, expr := range c.exprs {
groupQuery, groupArgs, errs := expr.Expr()
if len(errs) != 0 {
return "", nil, errs[0]
}
queries = append(queries, groupQuery)
args = append(args, groupArgs...)
}
return "GROUP BY " + strings.Join(queries, ", "), args, nil
}
type orderClause[T Table] struct {
orderExprs []orderItem[T]
}
type orderItem[T Table] struct {
expr TableExpr[T]
direction OrderDirection
}
type OrderDirection uint8
const (
Asc OrderDirection = iota + 1
Desc
)
func (c *orderClause[T]) add(item orderItem[T]) error {
if item.expr == nil {
return errors.New("empty order expr")
}
if item.direction != Asc && item.direction != Desc {
return errors.New("invalid order direction")
}
c.orderExprs = append(c.orderExprs, item)
return nil
}
func (c *orderClause[T]) exists() bool {
return len(c.orderExprs) != 0
}
func (c *orderClause[T]) getExpr() (string, []ExprType, error) {
if len(c.orderExprs) == 0 {
return "", nil, errors.New("empty order by")
}
args := []ExprType{}
orderQueries := make([]string, 0, len(c.orderExprs))
for _, orderItem := range c.orderExprs {
orderQuery, orderArgs, errs := orderItem.expr.Expr()
if len(errs) != 0 {
return "", nil, errs[0]
}
var directionQuery string
switch orderItem.direction {
case Asc:
directionQuery = "ASC"
case Desc:
directionQuery = "DESC"
default:
return "", nil, fmt.Errorf("invalid order direction: %d", orderItem.direction)
}
orderQueries = append(orderQueries, fmt.Sprintf("%s %s", orderQuery, directionQuery))
args = append(args, orderArgs...)
}
return fmt.Sprintf("ORDER BY %s", strings.Join(orderQueries, ", ")), args, nil
}
type limitClause struct {
limit uint64
}
func (l *limitClause) set(limit uint64) error {
if l.limit != 0 {
return errors.New("limit already set")
}
if limit == 0 {
return errors.New("invalid limit")
}
l.limit = limit
return nil
}
func (l *limitClause) exists() bool {
return l.limit != 0
}
func (l *limitClause) getExpr() (string, []ExprType, error) {
if l.limit == 0 {
return "", nil, errors.New("empty limit")
}
return fmt.Sprintf("LIMIT %d", l.limit), nil, nil
}
type offsetClause struct {
offset uint64
}
func (o *offsetClause) set(offset uint64) error {
if o.offset != 0 {
return errors.New("offset already set")
}
if offset == 0 {
return errors.New("invalid offset")
}
o.offset = offset
return nil
}
func (o *offsetClause) exists() bool {
return o.offset != 0
}
func (o *offsetClause) getExpr() (string, []ExprType, error) {
if o.offset == 0 {
return "", nil, errors.New("empty offset")
}
return fmt.Sprintf("OFFSET %d", o.offset), nil, nil
}
type lockClause struct {
lockType LockType
}
type LockType uint8
const (
none LockType = iota
ForUpdate
ForShare
)
func (l *lockClause) set(lockType LockType) error {
if l.lockType != none {
return errors.New("lock type already set")
}
if lockType != ForUpdate && lockType != ForShare {
return errors.New("invalid lock type")
}
l.lockType = lockType
return nil
}
func (l *lockClause) exists() bool {
return l.lockType != none
}
func (l *lockClause) getExpr() (string, []ExprType, error) {
switch l.lockType {
case ForUpdate:
return "FOR UPDATE", nil, nil
case ForShare:
return "FOR SHARE", nil, nil
case none:
return "", nil, nil
}
return "", nil, errors.New("invalid lock type")
}