-
Notifications
You must be signed in to change notification settings - Fork 0
/
result-type.go
executable file
·194 lines (162 loc) · 4.99 KB
/
result-type.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
package gen
import (
"context"
"fmt"
"strings"
"github.com/vektah/gqlparser/ast"
"github.com/jinzhu/gorm"
)
func GetItem(ctx context.Context, db *gorm.DB, out interface{}, id *string) error {
return db.Find(out, "id = ?", id).Error
}
func GetItemForRelation(ctx context.Context, db *gorm.DB, obj interface{}, relation string, out interface{}) error {
return db.Model(obj).Related(out, relation).Error
}
type EntityFilter interface {
Apply(ctx context.Context, dialect gorm.Dialect, wheres *[]string, whereValues *[]interface{}, havings *[]string, havingValues *[]interface{}, joins *[]string) error
}
type EntityFilterQuery interface {
Apply(ctx context.Context, dialect gorm.Dialect, selectionSet *ast.SelectionSet, wheres *[]string, values *[]interface{}, joins *[]string) error
}
type SortInfo struct {
Field string
Direction string
IsAggregation bool
}
func (si *SortInfo) String() string {
return fmt.Sprintf("%s %s", si.Field, si.Direction)
}
type EntitySort interface {
Apply(ctx context.Context, dialect gorm.Dialect, sorts *[]SortInfo, joins *[]string) error
}
type EntityResultType struct {
Offset *int
Limit *int
Query EntityFilterQuery
Sort []EntitySort
Filter EntityFilter
Fields []*ast.Field
SelectionSet *ast.SelectionSet
}
type GetItemsOptions struct {
Alias string
Preloaders []string
}
// GetResultTypeItems ...
func (r *EntityResultType) GetItems(ctx context.Context, db *gorm.DB, opts GetItemsOptions, out interface{}) error {
subq := db.Model(out)
q := db
subqGroups := []string{opts.Alias + ".id"}
subqFields := []string{"DISTINCT(" + opts.Alias + ".id) as id"}
qSorts := []string{}
subqSorts := []string{}
if r.Limit != nil {
// q = q.Limit(*r.Limit)
subq = subq.Limit(*r.Limit)
}
if r.Offset != nil {
// q = q.Offset(*r.Offset)
subq = subq.Offset(*r.Offset)
}
dialect := q.Dialect()
wheres := []string{}
havings := []string{}
whereValues := []interface{}{}
havingValues := []interface{}{}
joins := []string{}
sorts := []SortInfo{}
err := r.Query.Apply(ctx, dialect, r.SelectionSet, &wheres, &whereValues, &joins)
if err != nil {
return err
}
for _, sort := range r.Sort {
sort.Apply(ctx, dialect, &sorts, &joins)
}
if r.Filter != nil {
err = r.Filter.Apply(ctx, dialect, &wheres, &whereValues, &havings, &havingValues, &joins)
if err != nil {
return err
}
}
if len(sorts) > 0 {
for i, sort := range sorts {
if !sort.IsAggregation {
subqGroups = append(subqGroups, fmt.Sprintf("%s", sort.Field))
}
subqFields = append(subqFields, fmt.Sprintf("%s as "+dialect.Quote("sort_key_%d"), sort.Field, i))
qSorts = append(qSorts, fmt.Sprintf(dialect.Quote("filter_table")+"."+dialect.Quote("sort_key_%d")+" %s", i, sort.Direction))
subqSorts = append(subqSorts, fmt.Sprintf(dialect.Quote("sort_key_%d")+" %s", i, sort.Direction))
}
}
if len(wheres) > 0 {
subq = subq.Where(strings.Join(wheres, " AND "), whereValues...)
}
if len(havings) > 0 {
subq = subq.Having(strings.Join(havings, " AND "), havingValues...)
}
uniqueJoinsMap := map[string]bool{}
uniqueJoins := []string{}
for _, join := range joins {
if !uniqueJoinsMap[join] {
uniqueJoinsMap[join] = true
uniqueJoins = append(uniqueJoins, join)
}
}
for _, join := range uniqueJoins {
q = q.Joins(join)
subq = subq.Joins(join)
}
if len(opts.Preloaders) > 0 {
for _, p := range opts.Preloaders {
q = q.Preload(p)
}
}
subq = subq.Group(strings.Join(subqGroups, ", ")).Select(strings.Join(subqFields, ", "))
subq = subq.Order(strings.Join(subqSorts, ", "))
q = q.Order(strings.Join(qSorts, ", "))
q = q.Joins("INNER JOIN (?) as filter_table ON filter_table.id = "+opts.Alias+".id", subq.QueryExpr())
return q.Find(out).Error
}
// GetCount ...
func (r *EntityResultType) GetCount(ctx context.Context, db *gorm.DB, opts GetItemsOptions, out interface{}) (count int, err error) {
q := db
dialect := q.Dialect()
wheres := []string{}
havings := []string{}
whereValues := []interface{}{}
havingValues := []interface{}{}
joins := []string{}
err = r.Query.Apply(ctx, dialect, r.SelectionSet, &wheres, &whereValues, &joins)
if err != nil {
return 0, err
}
if r.Filter != nil {
err = r.Filter.Apply(ctx, dialect, &wheres, &whereValues, &havings, &havingValues, &joins)
if err != nil {
return 0, err
}
}
if len(wheres) > 0 {
q = q.Where(strings.Join(wheres, " AND "), whereValues...)
}
if len(havings) > 0 {
q = q.Having(strings.Join(havings, " AND "), havingValues...)
}
uniqueJoinsMap := map[string]bool{}
uniqueJoins := []string{}
for _, join := range joins {
if !uniqueJoinsMap[join] {
uniqueJoinsMap[join] = true
uniqueJoins = append(uniqueJoins, join)
}
}
for _, join := range uniqueJoins {
q = q.Joins(join)
}
q = q.Model(out).Group(opts.Alias + ".id")
err = db.Model(out).Joins("INNER JOIN (?) as filter_table ON filter_table.id = "+opts.Alias+".id", q.QueryExpr()).Count(&count).Error
return
}
func (r *EntityResultType) GetSortStrings() []string {
return []string{}
}