-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgo.go
214 lines (175 loc) · 4.11 KB
/
mgo.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
package dal
import (
"encoding/gob"
"log"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func init() {
gob.Register(ObjectID(""))
}
type iter struct {
Iter
iter *mgo.Iter
}
func (i *iter) Next(inter interface{}) bool {
return i.iter.Next(inter)
}
type query struct {
query *mgo.Query
}
func (q *query) One(i interface{}) error {
err := q.query.One(i)
if err == mgo.ErrNotFound {
err = ErrNotFound
}
return err
}
func (q *query) All(i interface{}) error {
err := q.query.All(i)
if err == mgo.ErrNotFound {
err = ErrNotFound
}
return err
}
func (q *query) Iter() Iter {
i := q.query.Iter()
return &iter{iter: i}
}
func (q *query) Sort(s ...string) Query {
q2 := q.query.Sort(s...)
return &query{query: q2}
}
func (q *query) Apply(change Change, result interface{}) (info *ChangeInfo, err error) {
c := mgo.Change{
Update: change.Update,
Upsert: change.Upsert,
Remove: change.Remove,
ReturnNew: change.ReturnNew,
}
mci, err := q.query.Apply(c, result)
info = &ChangeInfo{}
if mci != nil {
info.Updated = mci.Updated
info.Removed = mci.Removed
info.UpsertedId = mci.UpsertedId
}
return
}
type collection struct {
col *mgo.Collection
}
func (c *collection) Find(q Q) Query {
bsonQ := c.col.Find(q)
return &query{query: bsonQ}
}
func (c *collection) EnsureIndex(index Index) error {
i := mgo.Index{
Key: index.Key,
Background: index.Background,
Sparse: index.Sparse,
ExpireAfter: index.ExpireAfter,
}
return c.col.EnsureIndex(i)
}
func (c *collection) FindID(id interface{}) Query {
q := c.col.FindId(id)
return &query{query: q}
}
func (c *collection) RemoveID(id interface{}) error {
return c.col.RemoveId(id)
}
func (c *collection) UpsertID(id interface{}, update interface{}) (*ChangeInfo, error) {
return c.Upsert(bson.M{"_id": id}, update)
}
func (c *collection) SaveID(id interface{}, item interface{}) (*ChangeInfo, error) {
return c.Upsert(bson.M{"_id": id}, bson.M{"$set": item})
}
func (c *collection) Save(selector interface{}, update interface{}) (*ChangeInfo, error) {
return c.Upsert(selector, bson.M{"$set": update})
}
func (c *collection) Upsert(selector interface{}, update interface{}) (*ChangeInfo, error) {
mci, err := c.col.Upsert(selector, update)
if err != nil {
log.Printf("Error upserting: %s\n", err)
}
ci := &ChangeInfo{}
if mci != nil {
ci.Updated = mci.Updated
ci.Removed = mci.Removed
ci.UpsertedId = mci.UpsertedId
}
log.Printf("change info %s", ci)
return ci, err
}
func (c *collection) Insert(docs ...interface{}) error {
return c.col.Insert(docs)
}
type database struct {
Database
db *mgo.Database
}
func (d *database) C(name string) Collection {
col := d.db.C(name)
return &collection{col: col}
}
type connection struct {
Connection
mgoSession *mgo.Session
}
func (c *connection) Clone() Connection {
a := c.mgoSession.Clone()
return &connection{mgoSession: a}
}
func (c *connection) Close() {
c.mgoSession.Close()
}
func (c *connection) DB(name string) Database {
db := c.mgoSession.DB(name)
return &database{db: db}
}
type dal struct {
DAL
}
func (d *dal) Connect(s string) (Connection, error) {
log.Printf("Connect: %s\n", s)
mgoSession, err := mgo.Dial(s)
return &connection{mgoSession: mgoSession}, err
}
func NewDAL() DAL {
return &dal{}
}
type ObjectID string
func (id ObjectID) GetBSON() (interface{}, error) {
return bson.ObjectId(id), nil
}
func (id ObjectID) Hex() string {
return bson.ObjectId(id).Hex()
}
func (id ObjectID) Valid() bool {
return bson.ObjectId(id).Valid()
}
// MarshalJSON turns a dal.ObjectId into a json.Marshaller.
func (id ObjectID) MarshalJSON() ([]byte, error) {
return bson.ObjectId(id).MarshalJSON()
}
// UnmarshalJSON turns *dal.ObjectId into a json.Unmarshaller.
func (id *ObjectID) UnmarshalJSON(data []byte) (err error) {
a := bson.NewObjectId()
err = a.UnmarshalJSON(data)
if err != nil {
log.Printf("ERROR: %s\n", err)
return
}
*id = ObjectID(a)
return
}
func ObjectIDHex(s string) ObjectID {
return ObjectID(bson.ObjectIdHex(s))
}
func IsObjectIDHex(s string) bool {
return bson.IsObjectIdHex(s)
}
func NewObjectID() ObjectID {
return ObjectID(bson.NewObjectId())
}