-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
251 lines (173 loc) · 5.18 KB
/
command.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
package gom
import (
"errors"
"reflect"
"strings"
"github.com/eaciit/toolkit"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// Command = command struct
type Command struct {
set *Set
}
// newCommand = create new command
func newCommand(s *Set) *Command {
c := new(Command)
c.set = s
return c
}
// Pipe = Return Pipe Aggregate
func (c *Command) Pipe() []bson.M {
return c.set.buildPipe()
}
// Get = get data. it'll use Filter as default. if pipe not null => Filter will be ignored
func (c *Command) Get() (int64, error) {
tableName := c.set.tableName
result := c.set.result
if result == nil {
return 0, errors.New("result argument must be set")
}
resultVal := reflect.ValueOf(result)
if resultVal.Kind() != reflect.Ptr && resultVal.Kind() != reflect.Slice {
return 0, errors.New("result argument must be a slice")
}
client := c.set.gom.GetClient()
ctx, cancelFunc := c.set.GetContext()
defer cancelFunc()
if tableName == "" {
return 0, errors.New("table name not defined")
}
collection := client.Database(c.set.gom.GetDatabase()).Collection(tableName)
var cur *mongo.Cursor
var err error
cur, err = collection.Aggregate(ctx, c.set.buildPipe())
if err != nil {
return 0, errors.New(toolkit.Sprintf("Error finding all documents: %s", err.Error()))
}
defer cur.Close(ctx)
err = cur.All(ctx, result)
if err != nil {
return 0, errors.New(toolkit.Sprintf("Decode error: %s", err.Error()))
}
countTotal, _ := collection.EstimatedDocumentCount(ctx)
return countTotal, nil
}
// GetOne = get one data. it'll use Filter as default, pipe ignored.
func (c *Command) GetOne() error {
tableName := c.set.tableName
result := c.set.result
if result == nil {
return errors.New("result argument must be set")
}
resultVal := reflect.ValueOf(result)
if resultVal.Kind() != reflect.Ptr {
return errors.New("result argument must be a pointer")
}
if strings.Contains(reflect.TypeOf(result).String(), "[]") {
return errors.New("result argument must be a pointer, not a slice")
}
client := c.set.gom.GetClient()
ctx, cancelFunc := c.set.GetContext()
defer cancelFunc()
collection := client.Database(c.set.gom.GetDatabase()).Collection(tableName)
err := collection.FindOne(ctx, c.set.filter).Decode(c.set.result)
if err != nil {
return errors.New(toolkit.Sprintf("Error finding document: %s", err.Error()))
}
return nil
}
// Insert = insert one data, for multiple data use InsertAll
func (c *Command) Insert(data interface{}) (interface{}, error) {
client := c.set.gom.GetClient()
collection := client.Database(c.set.gom.GetDatabase()).Collection(c.set.tableName)
dataM, err := c.set.buildData(data, true)
if err != nil {
return nil, err
}
ctx, cancelFunc := c.set.GetContext()
defer cancelFunc()
res, err := collection.InsertOne(ctx, dataM)
if err != nil {
return nil, err
}
id := res.InsertedID
return id, nil
}
// InsertAll = insert multiple data
func (c *Command) InsertAll(data interface{}) ([]interface{}, error) {
client := c.set.gom.GetClient()
collection := client.Database(c.set.gom.GetDatabase()).Collection(c.set.tableName)
datas, err := c.set.buildData(data, true)
if err != nil {
return []interface{}{}, err
}
ctx, cancelFunc := c.set.GetContext()
defer cancelFunc()
res, err := collection.InsertMany(ctx, datas.([]interface{}))
if err != nil {
return []interface{}{}, err
}
ids := res.InsertedIDs
return ids, nil
}
// Update = update data with filter or pipe
func (c *Command) Update(data interface{}) (int64, error) {
client := c.set.gom.GetClient()
collection := client.Database(c.set.gom.GetDatabase()).Collection(c.set.tableName)
dataM, err := c.set.buildData(data, false)
if err != nil {
return 0, err
}
if len(c.set.filter.(bson.M)) == 0 {
return 0, errors.New("filter can't be empty")
}
ctx, cancelFunc := c.set.GetContext()
defer cancelFunc()
res, err := collection.UpdateOne(ctx, c.set.filter, bson.M{
"$set": dataM,
})
if err != nil {
return 0, err
}
return res.MatchedCount, nil
}
// DeleteOne = delete one data with filter or pipe
func (c *Command) DeleteOne() (int64, error) {
client := c.set.gom.GetClient()
collection := client.Database(c.set.gom.GetDatabase()).Collection(c.set.tableName)
if len(c.set.filter.(bson.M)) == 0 {
return 0, errors.New("filter can't be empty")
}
ctx, cancelFunc := c.set.GetContext()
defer cancelFunc()
res, err := collection.DeleteOne(ctx, c.set.filter)
if err != nil {
return 0, err
}
return res.DeletedCount, nil
}
// DeleteAll = delete all data with filter or pipe
func (c *Command) DeleteAll() (int64, error) {
client := c.set.gom.GetClient()
collection := client.Database(c.set.gom.GetDatabase()).Collection(c.set.tableName)
ctx, cancelFunc := c.set.GetContext()
defer cancelFunc()
res, err := collection.DeleteMany(ctx, c.set.filter)
if err != nil {
return 0, err
}
return res.DeletedCount, nil
}
// Drop = drop table/collection
func (c *Command) Drop() error {
client := c.set.gom.GetClient()
collection := client.Database(c.set.gom.GetDatabase()).Collection(c.set.tableName)
ctx, cancelFunc := c.set.GetContext()
defer cancelFunc()
err := collection.Drop(ctx)
if err != nil {
return err
}
return nil
}