forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_operators.go
221 lines (190 loc) · 7.2 KB
/
collection_operators.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
package gorm
import (
"context"
"fmt"
"strings"
"github.com/golang/protobuf/proto"
"github.com/jinzhu/gorm"
"github.com/infobloxopen/atlas-app-toolkit/v2/query"
)
type SortingCriteriaConverter interface {
SortingCriteriaToGorm(ctx context.Context, cr *query.SortCriteria, obj interface{}) (string, string, error)
}
type FieldSelectionConverter interface {
FieldSelectionToGorm(ctx context.Context, fs *query.FieldSelection, obj interface{}) ([]string, error)
}
type PaginationConverter interface {
PaginationToGorm(ctx context.Context, p *query.Pagination) (offset, limit int32)
}
type SearchingConverter interface {
SearchingToGorm(ctx context.Context, s *query.Searching, fieldsForFTS []string, obj interface{}) (string, error)
}
type CollectionOperatorsConverter interface {
FilteringConditionConverter
SortingCriteriaConverter
FieldSelectionConverter
PaginationConverter
SearchingConverter
}
func ApplyCollectionOperatorsEx(ctx context.Context, db *gorm.DB, obj interface{}, c CollectionOperatorsConverter, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection) (*gorm.DB, error) {
db, fAssocToJoin, err := ApplyFilteringEx(ctx, db, f, obj, c)
if err != nil {
return nil, err
}
db, sAssocToJoin, err := ApplySortingEx(ctx, db, s, obj, c)
if err != nil {
return nil, err
}
if fAssocToJoin == nil && sAssocToJoin != nil {
fAssocToJoin = make(map[string]struct{})
}
for k := range sAssocToJoin {
fAssocToJoin[k] = struct{}{}
}
db, err = JoinAssociations(ctx, db, fAssocToJoin, obj)
if err != nil {
return nil, err
}
db = ApplyPaginationEx(ctx, db, p, c)
db, err = ApplyFieldSelectionEx(ctx, db, fs, obj, c)
if err != nil {
return nil, err
}
return db, nil
}
func ApplyCollectionOperatorsWithSearchingEx(ctx context.Context, db *gorm.DB, obj interface{}, c CollectionOperatorsConverter, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection, sc *query.Searching, fieldsForFTS []string) (*gorm.DB, error) {
db, err := ApplyCollectionOperatorsEx(ctx, db, obj, c, f, s, p, fs)
if err != nil {
return nil, err
}
db, err = ApplySearchingEx(ctx, db, sc, obj, fieldsForFTS, c)
if err != nil {
return nil, err
}
return db, nil
}
// ApplySearchingEx applies searching operator s to gorm instance db.
func ApplySearchingEx(ctx context.Context, db *gorm.DB, s *query.Searching, obj interface{}, fieldsForFTS []string, c SearchingConverter) (*gorm.DB, error) {
str, err := c.SearchingToGorm(ctx, s, fieldsForFTS, obj)
if err != nil {
return nil, err
}
if s != nil && s.Query != "" {
s.Query = strings.TrimSpace(s.Query)
s.Query = strings.ReplaceAll(s.Query, ":", " ")
splChar := []string{"(", ")", "|", "+", "<", "'", "&", "!", "%", ";"}
for _, spl := range splChar {
if strings.Contains(s.Query, spl) {
s.Query = ""
return db.Where(str, s.Query), nil
}
}
s.Query = strings.Join(strings.Fields(s.Query), " ")
if s.Query != "" {
s.Query = strings.ReplaceAll(s.Query, " ", " & ")
s.Query = s.Query + ":*"
}
return db.Where(str, s.Query), nil
}
return db, nil
}
// ApplyFiltering applies filtering operator f to gorm instance db.
func ApplyFilteringEx(ctx context.Context, db *gorm.DB, f *query.Filtering, obj interface{}, c FilteringConditionConverter) (*gorm.DB, map[string]struct{}, error) {
str, args, assocToJoin, err := FilteringToGormEx(ctx, f, obj, c)
if err != nil {
return nil, nil, err
}
if str != "" {
return db.Where(str, args...), assocToJoin, nil
}
return db, nil, nil
}
// ApplySorting applies sorting operator s to gorm instance db.
func ApplySortingEx(ctx context.Context, db *gorm.DB, s *query.Sorting, obj interface{}, c SortingCriteriaConverter) (*gorm.DB, map[string]struct{}, error) {
var crs []string
var assocToJoin map[string]struct{}
for _, cr := range s.GetCriterias() {
dbCr, assoc, err := c.SortingCriteriaToGorm(ctx, cr, obj)
if err != nil {
return nil, nil, err
}
if assoc != "" {
if assocToJoin == nil {
assocToJoin = make(map[string]struct{})
}
assocToJoin[assoc] = struct{}{}
}
crs = append(crs, dbCr)
}
if len(crs) == 0 {
return db, nil, nil
}
return db.Order(strings.Join(crs, ",")), assocToJoin, nil
}
// Deprecated: use ApplyCollectionOperatorsEx instead
// ApplyCollectionOperators applies collection operators to gorm instance db.
func ApplyCollectionOperators(ctx context.Context, db *gorm.DB, obj interface{}, pb proto.Message, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection) (*gorm.DB, error) {
return ApplyCollectionOperatorsEx(ctx, db, obj, NewDefaultPbToOrmConverter(pb), f, s, p, fs)
}
// Deprecated: use ApplyFilteringEx instead
// ApplyFiltering applies filtering operator f to gorm instance db.
func ApplyFiltering(ctx context.Context, db *gorm.DB, f *query.Filtering, obj interface{}, pb proto.Message) (*gorm.DB, map[string]struct{}, error) {
c := &DefaultFilteringConditionConverter{&DefaultFilteringConditionProcessor{pb}}
return ApplyFilteringEx(ctx, db, f, obj, c)
}
// Deprecated: use ApplySortingEx instead
// ApplySorting applies sorting operator s to gorm instance db.
func ApplySorting(ctx context.Context, db *gorm.DB, s *query.Sorting, obj interface{}) (*gorm.DB, map[string]struct{}, error) {
return ApplySortingEx(ctx, db, s, obj, &DefaultSortingCriteriaConverter{})
}
// JoinAssociations joins obj's associations from assoc to the current gorm query.
func JoinAssociations(ctx context.Context, db *gorm.DB, assoc map[string]struct{}, obj interface{}) (*gorm.DB, error) {
for k := range assoc {
tableName, sourceKeys, targetKeys, err := JoinInfo(ctx, obj, k)
if err != nil {
return nil, err
}
var keyPairs []string
for i, k := range sourceKeys {
keyPairs = append(keyPairs, k+" = "+targetKeys[i])
}
alias := gorm.ToDBName(k)
join := fmt.Sprintf("LEFT JOIN %s %s ON %s", tableName, alias, strings.Join(keyPairs, " AND "))
db = db.Joins(join)
}
return db, nil
}
// ApplyPaginationEx applies pagination operator p to gorm instance db.
func ApplyPaginationEx(ctx context.Context, db *gorm.DB, p *query.Pagination, c PaginationConverter) *gorm.DB {
offset, limit := c.PaginationToGorm(ctx, p)
if offset > 0 {
db = db.Offset(offset)
}
if limit > 0 {
db = db.Limit(limit)
}
return db
}
// ApplyPagination applies pagination operator p to gorm instance db.
func ApplyPagination(ctx context.Context, db *gorm.DB, p *query.Pagination) *gorm.DB {
return ApplyPaginationEx(ctx, db, p, &DefaultPaginationConverter{})
}
// ApplyFieldSelectionEx applies field selection operator fs to gorm instance db.
func ApplyFieldSelectionEx(ctx context.Context, db *gorm.DB, fs *query.FieldSelection, obj interface{}, c FieldSelectionConverter) (*gorm.DB, error) {
toPreload, err := c.FieldSelectionToGorm(ctx, fs, obj)
if err != nil {
return nil, err
}
for _, assoc := range toPreload {
db, err = preload(db, obj, assoc)
if err != nil {
return nil, err
}
}
return db, nil
}
// Deprecated: use ApplyFieldSelectionEx instead
// ApplyFieldSelection applies field selection operator fs to gorm instance db.
func ApplyFieldSelection(ctx context.Context, db *gorm.DB, fs *query.FieldSelection, obj interface{}) (*gorm.DB, error) {
return ApplyFieldSelectionEx(ctx, db, fs, obj, &DefaultFieldSelectionConverter{})
}