This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
/
mongo.go
178 lines (163 loc) · 4.45 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
package db
import (
"context"
"errors"
"log"
"reflect"
"time"
"github.com/peizhong/letsgo/pkg/config"
"github.com/peizhong/letsgo/pkg/data"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
type MongoHandler struct {
connStr string
}
func structTobsonM(i interface{}) bson.M {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
_, m := data.GetMapAsJson(v.Interface())
bm := mapTobsonM(m)
return bm
}
func mapTobsonM(m map[string]interface{}) bson.M {
// b := (*bson.M)(unsafe.Pointer(&m))
b := bson.M{}
for k, v := range m {
b[k] = v
}
return b
}
// query, ordered params
func queryTobsonD(m []Query) bson.D {
d := bson.D{}
for _, v := range m {
if v.Op == "=" {
d = append(d, bson.E{
Key: v.Key,
Value: v.Value,
})
}
}
return d
}
func (m *MongoHandler) SetContext(string, interface{}) {
}
func (m *MongoHandler) do(f func(client *mongo.Client) (interface{}, error)) (interface{}, error) {
_, m.connStr = getDBConnString("mongo")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(m.connStr))
if err != nil {
return nil, err
}
return f(client)
}
func (m *MongoHandler) colletion(i interface{}, f func(*mongo.Collection) (int, error)) (int, error) {
_, m.connStr = getDBConnString("mongo")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
// todo: 连接池
client, err := mongo.Connect(ctx, options.Client().ApplyURI(m.connStr))
if err != nil {
return 0, err
}
table := data.GetTypeName(i)
// use db
// databases hold collections of documents
collection := client.Database(config.DBName).Collection(table)
return f(collection)
}
func (m *MongoHandler) Ping() error {
_, err := m.do(func(client *mongo.Client) (interface{}, error) {
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
err := client.Ping(ctx, readpref.Primary())
return nil, err
})
return err
}
func (m *MongoHandler) Create(i interface{}) error {
_, err := m.colletion(i, func(collection *mongo.Collection) (int, error) {
_, m := data.GetMapAsJson(i)
// M is an unordered representation of a BSON document
// Elements will be serialized in an undefined, random order
b := mapTobsonM(m)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
res, err := collection.InsertOne(ctx, b)
log.Println("insert", res.InsertedID)
return 1, err
})
return err
}
func (m *MongoHandler) Get(i interface{}, q ...Query) error {
_, err := m.colletion(i, func(collection *mongo.Collection) (int, error) {
// D is an ordered representation of a BSON document
filter := queryTobsonD(q)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
err := collection.FindOne(ctx, filter).Decode(i)
return 0, err
})
return err
}
// todo: set value
func (m *MongoHandler) Gets(i interface{}, q ...Query) (int, error) {
cnt, err := m.colletion(i, func(collection *mongo.Collection) (int, error) {
t := reflect.TypeOf(i)
if t.Kind() != reflect.Ptr {
log.Println(t.Kind())
return 0, errors.New("not ptr")
}
t = t.Elem()
if t.Kind() != reflect.Slice {
log.Println(t.Kind())
return 0, errors.New("ptr not to slice")
}
t = t.Elem()
if t.Kind() != reflect.Ptr {
log.Println(t.Kind())
return 0, errors.New("slice item not ptr")
}
t = t.Elem()
if t.Kind() != reflect.Struct {
log.Println(t.Kind())
return 0, errors.New("prt not to struct")
}
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
filter := queryTobsonD(q)
// return a cursor
cur, err := collection.Find(ctx, filter)
if err != nil {
return 0, err
}
defer cur.Close(ctx)
var cnt int
for cur.Next(ctx) {
n := reflect.New(t)
// prt to stuct
err = cur.Decode(n.Interface())
if err != nil {
return cnt, err
}
// log.Println(n.Interface())
cnt++
}
return cnt, nil
})
return cnt, err
}
func (m *MongoHandler) Update(i interface{}) error {
_, err := m.colletion(i, func(collection *mongo.Collection) (int, error) {
if keymap := data.GetPrimaryKey(i); keymap != nil {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
filter := mapTobsonM(keymap)
// https://docs.mongodb.com/manual/reference/operator/update/
// use $set
update := structTobsonM(i)
collection.ReplaceOne(ctx, filter, update)
}
return 0, nil
})
return err
}