-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery.go
More file actions
173 lines (162 loc) · 4.84 KB
/
Copy pathquery.go
File metadata and controls
173 lines (162 loc) · 4.84 KB
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
package repository
import (
"fmt"
"reflect"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
// Queryable objects will be treated as special objects during the query
// building.
type QueryBuilder interface {
// `GetQuery` is used for filtering information. It is the object that
// would be passed to the `mgo.Collection.Find` method to return a
// `mgo.Query`. But it returns just a piece of it for further aggregation
// inside of the `Query` method.
GetQuery() (bson.D, error)
}
// QueryModifier is the interface that describes an object that can modify one
// `mgo.Query`
type QueryModifier interface {
// Apply will
Apply(query *mgo.Query) (*mgo.Query, error)
}
func genConditions(conditions bson.D, params ...interface{}) (bson.D, error) {
if conditions == nil {
conditions = make(bson.D, 0, len(params))
}
for _, param := range params {
switch v := param.(type) {
case bson.D:
// Appends the conditions directly to the result.
conditions = append(conditions, v...)
break
case bson.DocElem:
// Appends the condition directly to the result.
conditions = append(conditions, v)
break
case *bson.DocElem:
// Appends the condition directly to the result.
conditions = append(conditions, *v)
break
case BinaryOperator:
// If a `BinaryOperator` is passed.
// Get the conditions
cond, err := v.GetCondition()
if err != nil {
return nil, err
}
// Applies it to the result
conditions = append(conditions, cond)
case QueryBuilder:
cond, err := v.GetQuery()
if err != nil {
return nil, err
}
if cond != nil {
conditions = append(conditions, cond...)
}
}
}
return conditions, nil
}
// GetQueryCriteria builds the criteria that will be performed in the Find
// method of a `mgo.Collection.Find` method.
//
// The parameter `defaultCriteria` represents an initial set of criterias that
// be mixed with the passed parameters.
//
// An error can be returned when building the criteria, due to a non supported
// datatype being used. Otherwise, a ready for use criteria object is returned.
func GetQueryCriteria(defaultCriteria interface{}, params ...interface{}) (bson.D, error) {
var conditions bson.D
if defaultCriteria == nil {
conditions = make(bson.D, 0, len(params))
} else {
ccc, err := genConditions(conditions, defaultCriteria)
if err != nil {
return nil, err
}
conditions = ccc
}
ccc, err := genConditions(conditions, params...)
if err != nil {
return nil, err
}
return ccc, nil
}
func ApplyQueryModifiers(r RepositoryProvider, query *mgo.Query, params ...interface{}) (*mgo.Query, error) {
var sorting = r.GetDefaultSorting()
for _, param := range params {
switch v := param.(type) {
case bson.D:
case bson.DocElem:
case *bson.DocElem:
case BinaryOperator:
case QueryBuilder:
// Ignoring those types because they are exclusively used for
// building the conditions to bootstrap the `mgo.Query` object.
break
case *Sort:
// For sorting it has a special case: it will replace the default sorting.
sorting = v.Fields
break
case QueryModifier:
// Queryable objects will apply some transformation to the query.
var err error
query, err = v.Apply(query)
if err != nil {
return nil, err
}
break
default:
// This type is not supported.
return nil, NewErrTypeNotSupported(v)
}
}
if len(sorting) > 0 {
// If any sorting defined, applies it to the query.
query = query.Sort(sorting...)
}
return query, nil
}
// Query builds the criteria, using `GetQueryCriteria`, and performs the
// `mgo.Collection.Find` for building the `mgo.Query`.
//
// Then, the generated `mgo.Query` will be passed through each `QueryModifier`
// passed as param.
//
// Finally, it will return the `mgo.Query` prepared for execution.
func Query(r RepositoryProvider, c *mgo.Collection, params ...interface{}) (*mgo.Query, error) {
conditions, err := GetQueryCriteria(r.GetDefaultCriteria(), params...)
if err != nil {
return nil, err
}
query := c.Find(conditions)
query, err = ApplyQueryModifiers(r, query, params...)
if err != nil {
return nil, err
}
return query, nil
}
// CountAndQuery will use the same idea as `Query`. However, before applying the
// modifications from `QueryModifier` it saves the count and returns it with the
// reference of the `mgo.Query` obtained.
func CountAndQuery(r RepositoryProvider, c *mgo.Collection, params ...interface{}) (*mgo.Query, int, error) {
conditions, err := GetQueryCriteria(r.GetDefaultCriteria(), params...)
if err != nil {
return nil, 0, err
}
query := c.Find(conditions)
count, err := query.Count()
if err != nil {
return nil, 0, err
}
query, err = ApplyQueryModifiers(r, query, params...)
if err != nil {
return nil, 0, err
}
return query, count, nil
}
func NewErrTypeNotSupported(v interface{}) error {
return fmt.Errorf("data type not supported: %s", reflect.TypeOf(v).String())
}