/
mongo.go
285 lines (225 loc) · 5.93 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
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
/*
* Copyright (c) 2018.
* andy-zhangtao <ztao8607@gmail.com>
*/
package bwidow
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"time"
"github.com/andy-zhangtao/gogather/zReflect"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/pelletier/go-toml"
)
//Write by zhangtao<ztao8607@gmail.com> . In 2018/5/23.
type BWMongoConf struct {
Endpoint string
DB string
User string
Password string
}
type BWMongo struct {
session *mgo.Session
client *mgo.Session
db *mgo.Database
tableMap map[string]string
}
var conf *BWMongoConf
func (this *BWMongo) Map(u interface{}, name string) {
if this.tableMap == nil {
this.tableMap = make(map[string]string)
}
this.tableMap[reflect.TypeOf(u).Name()] = name
}
func (this *BWMongo) setDB() {
this.db = this.session.Clone().DB(conf.DB)
}
func (this *BWMongo) first(u interface{}) (err error) {
this.setDB()
defer this.db.Session.Close()
return this.db.C(this.tableMap[getTypeName(u)]).Find(nil).One(u)
}
func (this *BWMongo) findOne(u interface{}) (err error) {
this.setDB()
defer this.db.Session.Close()
m := zReflect.ReflectStructInfo(u)
return this.db.C(this.tableMap[getTypeName(u)]).Find(bson.M(m)).One(u)
}
func (this *BWMongo) findAll(u interface{}, a interface{}) (err error) {
this.setDB()
defer this.db.Session.Close()
m := zReflect.ReflectStructInfo(u)
return this.db.C(this.tableMap[getTypeName(u)]).Find(bson.M(m)).All(a)
}
func (this *BWMongo) findAllWithSort(u interface{}, a interface{}, sortField []string) (err error) {
this.setDB()
defer this.db.Session.Close()
m := zReflect.ReflectStructInfo(u)
return this.db.C(this.tableMap[getTypeName(u)]).Find(bson.M(m)).Sort(sortField...).All(a)
}
func (this *BWMongo) save(u interface{}) (err error) {
this.setDB()
defer this.db.Session.Close()
return this.db.C(this.tableMap[getTypeName(u)]).Insert(u)
}
func (this *BWMongo) saveAll(u []interface{}) (err error) {
typeName := reflect.TypeOf(u[0]).Name()
this.setDB()
defer this.db.Session.Close()
bulk := this.db.C(this.tableMap[typeName]).Bulk()
bulk.Insert(u...)
_, err = bulk.Run()
return
}
func (this *BWMongo) update(uPtr interface{}, field []string) (num int, err error) {
this.setDB()
defer this.db.Session.Close()
m := zReflect.ReflectStructInfo(uPtr)
nm := make(map[string]interface{})
for _, f := range field {
nm[f] = m[f]
}
info, err := this.db.C(this.tableMap[getTypeName(uPtr)]).UpdateAll(bson.M(nm), bson.M{"$set": bson.M(m)})
return info.Updated, err
}
func (this *BWMongo) delete(uPtr interface{}, field []string) (num int, err error) {
this.setDB()
defer this.db.Session.Close()
m := zReflect.ReflectStructInfo(uPtr)
nm := make(map[string]interface{})
for _, f := range field {
nm[f] = m[f]
}
info, err := this.db.C(this.tableMap[getTypeName(uPtr)]).RemoveAll(bson.M(nm))
return info.Removed, err
}
func (this *BWMongo) deleteAll(uPtr interface{}) (int, error) {
this.setDB()
defer this.db.Session.Close()
info, err := this.db.C(this.tableMap[getTypeName(uPtr)]).RemoveAll(nil)
return info.Removed, err
}
func (this *BWMongo) DriverInit() (err error) {
if err = this.Check(); err != nil {
return
}
conf = new(BWMongoConf)
conf.User = os.Getenv(BW_MONGO_USER)
conf.Password = os.Getenv(BW_MONGO_PASSWD)
conf.Endpoint = os.Getenv(BW_MONGO_ENDPOINT)
conf.DB = os.Getenv(BW_MONGO_DB)
if conf.Endpoint == "" {
useToml := true
// 读取toml
data, err := ioutil.ReadFile("bwidow_mongo.toml")
if err != nil || len(data) == 0 {
useToml = false
//尝试json
data, err = ioutil.ReadFile("bwidow_mongo.json")
if err != nil || len(data) == 0 {
return errors.New(fmt.Sprintf("Parse Configure Error %s", err.Error()))
}
}
if useToml {
err = toml.Unmarshal(data, conf)
if err != nil {
return err
}
} else {
err = json.Unmarshal(data, conf)
if err != nil {
return err
}
}
}
var session *mgo.Session
if conf.User == "" {
session, err = mgo.Dial(conf.Endpoint)
if err != nil {
return
}
} else {
dialInfo := &mgo.DialInfo{
Addrs: []string{conf.Endpoint},
Database: conf.DB,
Username: conf.User,
Password: conf.Password,
Timeout: 10 * time.Second,
}
session, err = mgo.DialWithInfo(dialInfo)
if err != nil {
return
}
}
v, err := session.BuildInfo()
if err != nil {
return
}
fmt.Println(fmt.Sprintf("Mongo Version [%s]", v.Version))
this.session = session
return
}
func (this *BWMongo) Check() (err error) {
if os.Getenv(BW_MONGO_ENDPOINT) == "" {
// 环境变量不存在, 检查bwidow_mongo.toml文件
if _, err := os.Stat("bwidow_mongo.toml"); os.IsNotExist(err) {
// toml 配置文件不存在,检查bwidow_mongo.json文件
if _, err := os.Stat("bwidow_mongo.json"); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("Can not find Mongo configure. Env[%s]/Toml/Json are all lost!", BW_MONGO_ENDPOINT))
}
}
return nil
}
if os.Getenv(BW_MONGO_DB) == "" {
return errors.New(fmt.Sprintf("[%s] Empty!", BW_MONGO_DB))
}
return nil
}
func (this *BWMongo) checkIndex(uPtr interface{}) (err error) {
this.setDB()
defer this.db.Session.Close()
m := zReflect.ReflectStructInfoWithTag(uPtr, true, "bw")
var key []string
for k, _ := range m {
key = append(key, k)
}
index, _ := this.db.C(this.tableMap[getTypeName(uPtr)]).Indexes()
isExist := false
for _, idx := range index {
if len(m) == len(idx.Key) {
i := 0
for k, _ := range m {
for _, n := range idx.Key {
if k == n {
i++
}
}
}
if len(m) == i {
isExist = true
break
}
}
}
if !isExist {
index := mgo.Index{
Key: key,
Unique: true,
DropDups: true,
Background: false,
Sparse: true,
}
err = this.db.C(this.tableMap[getTypeName(uPtr)]).EnsureIndex(index)
}
return
}
func (this *BWMongo) count(uPtr interface{}) (int, error) {
this.setDB()
defer this.db.Session.Close()
return this.db.C(this.tableMap[getTypeName(uPtr)]).Count()
}