-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfilter.go
159 lines (131 loc) · 3.72 KB
/
filter.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
package yaormfilter
import "github.com/juju/errors"
// Filter is the interface stating the methods to implement to correctly filter on models
type Filter interface {
Subqueryload() Filter
ShouldSubqueryload() bool
AddOption(opt RequestOption) Filter
GetSelectOptions() []RequestOption
OrderBy(field string, way OrderingWay) Filter
GetOrderBy() []*OrderBy
Limit(limit uint64) Filter
Offset(offset uint64) Filter
GetLimit() (bool, uint64)
GetOffset() (bool, uint64)
LoadColumns(columns ...string)
GetLoadColumns() []string
DontLoadColumns(columns ...string)
GetDontLoadColumns() []string
}
// OrderingWay is a custom type to have ordering
type OrderingWay string
// OrderingWays represents the Enum to have ordering
var OrderingWays = struct {
Asc OrderingWay
Desc OrderingWay
}{
Asc: "ASC",
Desc: "DESC",
}
// RequestOption is a custom type to have request options
type RequestOption string
// RequestOptions represents the Enum of Request options
var RequestOptions = struct {
SelectForUpdate RequestOption
SelectDistinct RequestOption
LeftJoin RequestOption
}{
SelectForUpdate: "SelectForUpdate",
SelectDistinct: "SelectDistinct",
LeftJoin: "LeftJoin",
}
// ModelFilter is the struct every filter should compose
type ModelFilter struct {
subqueryload bool
options []RequestOption
orderBy []*OrderBy
shouldLimit bool
limit uint64
shouldOffset bool
offset uint64
loadColumns []string
dontLoadColumns []string
}
type OrderBy struct {
Field string
Way OrderingWay
}
func (mf *ModelFilter) Subqueryload() Filter {
panic(errors.NotImplementedf("Subqueryload"))
}
func (mf *ModelFilter) AllowSubqueryload() Filter {
mf.subqueryload = true
return mf
}
func (mf *ModelFilter) ShouldSubqueryload() bool {
return mf.subqueryload
}
func (mf *ModelFilter) AddOption(opt RequestOption) Filter {
panic(errors.NotImplementedf("AddOption"))
}
func (mf *ModelFilter) OrderBy(field string, way OrderingWay) Filter {
panic(errors.NotImplementedf("OrderBy"))
}
func (mf *ModelFilter) SetOrderBy(field string, way OrderingWay) Filter {
if mf.orderBy == nil {
mf.orderBy = []*OrderBy{}
}
mf.orderBy = append(mf.orderBy, &OrderBy{field, way})
return mf
}
func (mf *ModelFilter) GetOrderBy() []*OrderBy {
return mf.orderBy[:]
}
func (mf *ModelFilter) AddOption_(opt RequestOption) {
mf.options = append(mf.options, opt)
}
func (mf *ModelFilter) GetSelectOptions() []RequestOption {
opts := []RequestOption{}
for _, opt := range mf.options {
switch opt {
case RequestOptions.SelectForUpdate, RequestOptions.SelectDistinct:
opts = append(opts, opt)
}
}
return opts
}
func (mf *ModelFilter) Distinct() {
mf.AddOption_("SelectDistinct")
}
func (mf *ModelFilter) Limit(limit uint64) Filter {
panic(errors.NotImplementedf("Limit"))
}
func (mf *ModelFilter) Offset(limit uint64) Filter {
panic(errors.NotImplementedf("Offset"))
}
func (mf *ModelFilter) SetLimit(limit uint64) {
mf.shouldLimit = true
mf.limit = limit
}
func (mf *ModelFilter) SetOffset(offset uint64) {
mf.shouldOffset = true
mf.offset = offset
}
func (mf *ModelFilter) GetLimit() (bool, uint64) {
return mf.shouldLimit, mf.limit
}
func (mf *ModelFilter) GetOffset() (bool, uint64) {
return mf.shouldOffset, mf.offset
}
func (mf *ModelFilter) LoadColumns(columns ...string) {
mf.loadColumns = append(mf.loadColumns, columns...)
}
func (mf *ModelFilter) GetLoadColumns() []string {
return mf.loadColumns[:len(mf.loadColumns):len(mf.loadColumns)]
}
func (mf *ModelFilter) DontLoadColumns(columns ...string) {
mf.dontLoadColumns = append(mf.dontLoadColumns, columns...)
}
func (mf *ModelFilter) GetDontLoadColumns() []string {
return mf.dontLoadColumns[:len(mf.dontLoadColumns):len(mf.dontLoadColumns)]
}