forked from tqyq/thinkgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
171 lines (151 loc) · 2.81 KB
/
mongo.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
package main
import (
. "github.com/astaxie/beego"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
var (
session *mgo.Session
)
func Session() *mgo.Session {
if session == nil {
var err error
session, err = mgo.Dial(DbHost)
if err != nil {
panic(err) // no, not really
}
}
return session.Clone()
}
func Mgo(collection string, f func(*mgo.Collection)) {
session := Session()
defer func() {
if err := recover(); err != nil {
Error("Mgo", err)
}
session.Close()
}()
c := session.DB(DbName).C(collection)
f(c)
}
func MgoLike(v string) (result interface{}) {
return &bson.RegEx{Pattern: v, Options: "i"}
}
type MongoModel struct {
Cname string
F *P // find/query condition
Start int // query start at
Rows int // query max rows
S string // sort
Select *P // select field
}
func (m MongoModel) Find(p P) DbModel {
m.F = &p
return m
}
func (m MongoModel) Field(s ...string) DbModel {
if m.Select == nil {
m.Select = &P{}
}
for _, k := range s {
(*m.Select)[k] = 1
}
return m
}
func (m MongoModel) Skip(start int) DbModel {
m.Start = start
return m
}
func (m MongoModel) Limit(rows int) DbModel {
m.Rows = rows
return m
}
func (m MongoModel) Sort(s string) DbModel {
m.S = s
return m
}
func (m MongoModel) All() *[]P {
ps := []P{}
Mgo(m.Cname, func(c *mgo.Collection) {
q := m.query(c)
q.All(&ps)
})
return &ps
}
func (m MongoModel) One() (r interface{}) {
p := P{}
Mgo(m.Cname, func(c *mgo.Collection) {
q := m.query(c)
q.One(&p)
})
return p
}
func (m MongoModel) Count() int {
total := 0
Mgo(m.Cname, func(c *mgo.Collection) {
q := m.query(c)
total, _ = q.Count()
})
return total
}
func (m MongoModel) Add(docs ...interface{}) error {
var err error
Mgo(m.Cname, func(c *mgo.Collection) {
if len(docs) == 1 {
c.Insert(docs[0])
} else {
err = c.Insert(docs)
}
})
return err
}
func (m MongoModel) Save(p P) error {
var err error
Mgo(m.Cname, func(c *mgo.Collection) {
id := p["_id"]
var oid bson.ObjectId
switch id.(type) {
case string:
oid = bson.ObjectIdHex(id.(string))
case bson.ObjectId:
oid = id.(bson.ObjectId)
}
p["_id"] = oid
err = c.UpdateId(oid, p)
if err != nil {
Error(err)
}
})
return err
}
func (m MongoModel) RemoveId(id string) {
Mgo(m.Cname, func(c *mgo.Collection) {
err := c.RemoveId(bson.ObjectIdHex(id))
if err != nil {
Error(err)
}
})
}
func (m MongoModel) Remove(selector interface{}) {
}
func (m MongoModel) Explain() (result interface{}) {
p := P{}
Mgo(m.Cname, func(c *mgo.Collection) {
q := m.query(c)
q.Explain(p)
})
return p
}
func (m MongoModel) query(c *mgo.Collection) *mgo.Query {
q := c.Find(m.F).Skip(m.Start)
if m.Rows > 0 {
q = q.Limit(m.Rows)
}
if len(m.S) > 0 {
q = q.Sort(m.S)
}
if m.Select != nil {
q = q.Select(m.Select)
}
return q
}