forked from ti/mdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
executable file
·162 lines (128 loc) · 3.2 KB
/
query.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
package mdb
import (
"time"
"github.com/globalsign/mgo"
)
type Query struct {
originQuery *mgo.Query
session *Session
}
//Origin returns origin mgo query
func (q *Query) Origin() *mgo.Query {
return q.originQuery
}
func (q *Query) Batch(n int) *Query {
q.originQuery = q.originQuery.Batch(n)
return q
}
func (q *Query) Prefetch(p float64) *Query {
q.originQuery = q.originQuery.Prefetch(p)
return q
}
func (q *Query) Skip(n int) *Query {
q.originQuery = q.originQuery.Skip(n)
return q
}
func (q *Query) Limit(n int) *Query {
q.originQuery = q.originQuery.Limit(n)
return q
}
func (q *Query) Select(selector interface{}) *Query {
q.originQuery = q.originQuery.Select(selector)
return q
}
func (q *Query) Sort(fields ...string) *Query {
q.originQuery = q.originQuery.Sort(fields...)
return q
}
func (q *Query) Explain(result interface{}) error {
lastErr := q.session.execWithRetry(func() error {
return q.originQuery.Explain(result)
})
return lastErr
}
func (q *Query) Hint(indexKey ...string) *Query {
q.originQuery = q.originQuery.Hint(indexKey...)
return q
}
func (q *Query) SetMaxScan(n int) *Query {
q.originQuery = q.originQuery.SetMaxScan(n)
return q
}
func (q *Query) SetMaxTime(d time.Duration) *Query {
q.originQuery = q.originQuery.SetMaxTime(d)
return q
}
func (q *Query) Snapshot() *Query {
q.originQuery = q.originQuery.Snapshot()
return q
}
func (q *Query) Comment(comment string) *Query {
q.originQuery = q.originQuery.Comment(comment)
return q
}
func (q *Query) LogReplay() *Query {
q.originQuery = q.originQuery.LogReplay()
return q
}
func (q *Query) One(result interface{}) error {
err := q.session.execWithRetry(func() error {
return q.originQuery.One(result)
})
return err
}
func (q *Query) Count() (int, error) {
var n int
lastErr := q.session.execWithRetry(func() error {
var err error
n, err = q.originQuery.Count()
return err
})
return n, lastErr
}
func (q *Query) Iter() *Iter {
i := &Iter{session: q.session}
q.session.execWithRetry(func() error {
i.originIter = q.originQuery.Iter()
return i.originIter.Err()
})
return i
}
func (q *Query) Tail(timeout time.Duration) *Iter {
i := &Iter{session: q.session}
q.session.execWithRetry(func() error {
i.originIter = q.originQuery.Tail(timeout)
return i.originIter.Err()
})
return i
}
func (q *Query) Distinct(key string, result interface{}) error {
lastErr := q.session.execWithRetry(func() error {
return q.originQuery.Distinct(key, result)
})
return lastErr
}
func (q *Query) MapReduce(job *mgo.MapReduce, result interface{}) (*mgo.MapReduceInfo, error) {
var info *mgo.MapReduceInfo
lastErr := q.session.execWithRetry(func() error {
var err error
info, err = q.originQuery.MapReduce(job, result)
return err
})
return info, lastErr
}
func (q *Query) Apply(change mgo.Change, result interface{}) (*mgo.ChangeInfo, error) {
var info *mgo.ChangeInfo
lastErr := q.session.execWithRetry(func() error {
var err error
info, err = q.originQuery.Apply(change, result)
return err
})
return info, lastErr
}
func (q *Query) All(result interface{}) error {
return q.Iter().All(result)
}
func (q *Query) For(result interface{}, f func() error) error {
return q.Iter().For(result, f)
}