-
Notifications
You must be signed in to change notification settings - Fork 2
/
deep-filtering.go
308 lines (253 loc) · 9.72 KB
/
deep-filtering.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package deepgorm
import (
"fmt"
"github.com/survivorbat/go-tsyncmap"
"gorm.io/gorm/schema"
"reflect"
"sync"
"gorm.io/gorm"
)
var (
// Cache mechanism for reflecting database structs, reflection is slow, so we
// cache results for quick lookups. Just remember to reset it in unit tests ;-)
// cacheDatabaseMap map[string]map[string]*nestedType{}
cacheDatabaseMap = tsyncmap.Map[string, map[string]*nestedType]{}
// schemaCache is for gorm's schema.Parse
schemaCache = sync.Map{}
)
// AddDeepFilters / addDeepFilter godoc
//
// Gorm supports the following filtering:
//
// type Person struct {
// Name string
// }
//
// map[string]any{
// "name": "Jake"
// }
//
// Which will return a list of people that are named 'Jake'. This is great for simple filtering
// but for more nested versions like the following it becomes problematic.
//
// type Group struct {
// IDs int
// Name string
// }
//
// type Person struct {
// Name string
// Group Group
// GroupRef int
// }
//
// // Get all the users belonging to 'some group'
//
// map[string]any{
// "group": map[string]any{
// "name": "some group",
// },
// }
//
// Gorm does not understand that we expected to filter users based on their group, it's
// not capable of doing that automatically. For this we need to use subqueries. Find more info here:
// https://gorm.io/docs/advanced_query.html
//
// This function is constructed to automatically convert those nested maps ("group": map[string]...) into
// subqueries. In order to do this, it takes the following steps:
//
// 1. Get all the struct-type fields from the incoming 'object', ignore all simple types and interfaces
// 2. Loop through all the key/values in the incoming map
// 3. Add all the simple types to a simpleMap, GORM can handle these,
// For all the special (nested) structs, add a subquery that uses WHERE on the subquery.
// 4. Add the simple filters to the query and return it.
func AddDeepFilters(db *gorm.DB, objectType any, filters ...map[string]any) (*gorm.DB, error) {
schemaInfo, err := schema.Parse(objectType, &schemaCache, db.NamingStrategy)
if err != nil {
return nil, err
}
relationalTypesInfo := getDatabaseFieldsOfType(db.NamingStrategy, schemaInfo)
simpleFilter := map[string]any{}
// Go through the filters
for _, filterObject := range filters {
// Go through all the keys of the filters
for fieldName, givenFilter := range filterObject {
switch givenFilter.(type) {
// WithFilters for relational objects
case map[string]any:
fieldInfo, ok := relationalTypesInfo[fieldName]
if !ok {
return nil, fmt.Errorf("field '%s' does not exist", fieldName)
}
// We have 2 db objects because if we use 'result' to create subqueries it will cause a stackoverflow.
query, err := addDeepFilter(db, fieldInfo, givenFilter)
if err != nil {
return nil, err
}
db = query
// Simple filters (string, int, bool etc.)
default:
simpleFilter[schemaInfo.Table+"."+fieldName] = givenFilter
}
}
}
// Add simple filters
db = db.Where(simpleFilter)
return db, nil
}
// nestedType Wrapper object used to create subqueries.
//
// NOTICE: We can only do simple many-to-many's with 2 ids right now, I currently (15-06-2021) see no reason
// to add even more advanced options.
type nestedType struct {
// An empty instance of the object, used in db.Model(...)
fieldStructInstance any
fieldForeignKey string
// Whether this is a manyToOne, oneToMany or manyToMany. oneToOne is taken care of automatically.
relationType string
/////////////////////////
// Many to Many fields //
/////////////////////////
// The name of the join table
manyToManyTable string
// The destination field from destinationManyToManyStructInstance
destinationManyToManyForeignKey string
}
// iKind is an abstraction of reflect.Value and reflect.Type that allows us to make ensureConcrete generic.
type iKind[T any] interface {
Kind() reflect.Kind
Elem() T
}
// ensureConcrete ensures that the given value is a value and not a pointer, if it is, convert it to its element type
func ensureConcrete[T iKind[T]](value T) T {
if value.Kind() == reflect.Ptr {
return ensureConcrete(value.Elem())
}
return value
}
// ensureNotASlice Ensures that the given value is not a slice, if it is a slice, we use Elem()
// For example: Type []*string will return string. This one is not generic because it doesn't work
// well with reflect.Value.
func ensureNotASlice(value reflect.Type) reflect.Type {
result := ensureConcrete(value)
if result.Kind() == reflect.Slice {
return ensureNotASlice(result.Elem())
}
return result
}
// getInstanceAndRelationOfField Since db.Model(...) requires an instance, we use this function to instantiate a field type
// and retrieve what kind of relation we assume the object has.
func getInstanceAndRelationOfField(fieldType reflect.Type) (any, string) {
valueType := ensureConcrete(fieldType)
switch valueType.Kind() {
// If the given field is a struct, we can safely say it's a oneToMany, we instantiate it
// using reflect.New and return it as an object.
case reflect.Struct:
return reflect.New(valueType).Interface(), "oneToMany"
// If the given field is a slice, it can be either manyToOne or manyToMany. We figure out what
// kind of slice it is and use reflect.New to return it as an object
case reflect.Slice:
elementType := ensureNotASlice(valueType)
return reflect.New(elementType).Interface(), "manyToOne"
default:
return nil, ""
}
}
// getNestedType Returns information about the struct field in a nestedType object. Used to figure out
// what database tables need to be queried.
func getNestedType(naming schema.Namer, dbField *schema.Field, ofType reflect.Type) (*nestedType, error) {
// Get empty instance for db.Model() of the given field
sourceStructType, relationType := getInstanceAndRelationOfField(dbField.FieldType)
result := &nestedType{
relationType: relationType,
fieldStructInstance: sourceStructType,
}
sourceForeignKey, ok := dbField.TagSettings["FOREIGNKEY"]
if ok {
result.fieldForeignKey = naming.ColumnName(dbField.Schema.Table, sourceForeignKey)
return result, nil
}
// No foreign key found, then it must be a manyToMany
manyToMany, ok := dbField.TagSettings["MANY2MANY"]
if !ok {
return nil, fmt.Errorf("no 'foreignKey:...' or 'many2many:...' found in field %s", dbField.Name)
}
// Woah it's a many-to-many!
result.relationType = "manyToMany"
result.manyToManyTable = manyToMany
// Based on the type we can just put _id behind it, again this only works with simple many-to-many structs
result.fieldForeignKey = naming.ColumnName(dbField.Schema.Table, ensureNotASlice(dbField.FieldType).Name()) + "_id"
// Now the other table that we're getting information from.
result.destinationManyToManyForeignKey = naming.ColumnName(dbField.Schema.Table, ofType.Name()) + "_id"
return result, nil
}
// getDatabaseFieldsOfType godoc
// Helper method used in AddDeepFilters to get nestedType objects for specific fields.
// For example, the following struct.
//
// type Tag struct {
// IDs uuid.UUID
// }
//
// type SimpleStruct1 struct {
// Name string
// TagRef uuid.UUID
// Tag Tag `gorm:"foreignKey:TagRef"`
// }
//
// Now when we call getDatabaseFieldsOfType(SimpleStruct1{}) it will return the following
// map of items.
//
// {
// "nestedstruct": {
// fieldStructInstance: Tag{},
// fieldForeignKey: "NestedStructRef",
// relationType: "oneToMany"
// }
// }
func getDatabaseFieldsOfType(naming schema.Namer, schemaInfo *schema.Schema) map[string]*nestedType {
// First get all the information of the to-be-reflected object
reflectType := ensureConcrete(schemaInfo.ModelType)
reflectTypeName := reflectType.Name()
if dbFields, ok := cacheDatabaseMap.Load(reflectTypeName); ok {
return dbFields
}
var resultNestedType = map[string]*nestedType{}
for _, fieldInfo := range schemaInfo.FieldsByName {
// Not interested in these
if kind := ensureConcrete(fieldInfo.FieldType).Kind(); kind != reflect.Struct && kind != reflect.Slice {
continue
}
nestedTypeResult, err := getNestedType(naming, fieldInfo, reflectType)
if err != nil {
continue
}
resultNestedType[naming.ColumnName(schemaInfo.Table, fieldInfo.Name)] = nestedTypeResult
}
// Add to cache
cacheDatabaseMap.Store(reflectTypeName, resultNestedType)
return resultNestedType
}
// AddDeepFilters / addDeepFilter godoc
// Refer to AddDeepFilters.
func addDeepFilter(db *gorm.DB, fieldInfo *nestedType, filter any) (*gorm.DB, error) {
cleanDB := db.Session(&gorm.Session{NewDB: true})
switch fieldInfo.relationType {
case "oneToMany":
whereQuery := fmt.Sprintf("%s IN (?)", fieldInfo.fieldForeignKey)
// SELECT * FROM <table> WHERE fieldInfo.fieldForeignKey IN (SELECT id FROM fieldInfo.fieldStructInstance WHERE givenFilter)
return db.Where(whereQuery, cleanDB.Model(fieldInfo.fieldStructInstance).Select("id").Where(filter)), nil
case "manyToOne":
// SELECT * FROM <table> WHERE id IN (SELECT fieldInfo.fieldStructInstance FROM fieldInfo.fieldStructInstance WHERE filter)
return db.Where("id IN (?)", cleanDB.Model(fieldInfo.fieldStructInstance).Select(fieldInfo.fieldForeignKey).Where(filter)), nil
case "manyToMany":
// The one on the 'other' object
subSubQuery := cleanDB.Model(fieldInfo.fieldStructInstance).Select("id").Where(filter)
// The one that connects the objects
subWhere := fmt.Sprintf("%s IN (?)", fieldInfo.fieldForeignKey)
subQuery := cleanDB.Table(fieldInfo.manyToManyTable).Select(fieldInfo.destinationManyToManyForeignKey).Where(subWhere, subSubQuery)
// SELECT * FROM <table> WHERE id IN (SELECT <table>_id FROM fieldInfo.fieldForeignKey WHERE <other_table>_id IN (SELECT id FROM <other_table> WHERE givenFilter))
return db.Where("id IN (?)", subQuery), nil
}
return nil, fmt.Errorf("relationType '%s' unknown", fieldInfo.relationType)
}