/
database_mongo.go
367 lines (300 loc) · 9.76 KB
/
database_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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package database
import (
"context"
"database/sql"
"strings"
"time"
"github.com/flarco/g"
"github.com/slingdata-io/sling-cli/core/dbio"
"github.com/slingdata-io/sling-cli/core/dbio/iop"
"github.com/spf13/cast"
"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"
)
// MongoDBConn is a Mongo connection
type MongoDBConn struct {
BaseConn
URL string
Client *mongo.Client
}
// Init initiates the object
func (conn *MongoDBConn) Init() error {
conn.BaseConn.URL = conn.URL
conn.BaseConn.Type = dbio.TypeDbMongoDB
instance := Connection(conn)
conn.BaseConn.instance = &instance
return conn.BaseConn.Init()
}
// Init initiates the object
func (conn *MongoDBConn) getNewClient(timeOut ...int) (client *mongo.Client, err error) {
to := 15
if len(timeOut) > 0 {
to = timeOut[0]
}
client, err = mongo.Connect(
conn.BaseConn.Context().Ctx,
options.Client().ApplyURI(conn.URL),
)
if err != nil {
return nil, g.Error(err, "could not connect to MongoDB server")
}
ctx, cancel := context.WithTimeout(conn.BaseConn.Context().Ctx, time.Duration(to)*time.Second)
defer cancel()
opts := options.Client().SetCompressors([]string{"zstd", "snappy", "zlib"})
return mongo.Connect(ctx, options.Client().ApplyURI(conn.URL), opts)
}
// Connect connects to the database
func (conn *MongoDBConn) Connect(timeOut ...int) error {
var err error
conn.Client, err = conn.getNewClient(timeOut...)
if err != nil {
return g.Error(err, "Failed to connect to client")
}
ctx, cancel := context.WithTimeout(conn.BaseConn.Context().Ctx, 5*time.Second)
defer cancel()
err = conn.Client.Ping(ctx, readpref.Primary())
if err != nil {
return g.Error(err, "Failed to ping mongo server")
}
return nil
}
func (conn *MongoDBConn) Close() error {
ctx, cancel := context.WithTimeout(conn.BaseConn.Context().Ctx, 5*time.Second)
defer cancel()
err := conn.Client.Disconnect(ctx)
if err != nil {
return g.Error(err, "Failed to disconnect")
}
return nil
}
// NewTransaction creates a new transaction
func (conn *MongoDBConn) NewTransaction(ctx context.Context, options ...*sql.TxOptions) (tx Transaction, err error) {
// does not support transaction
return
}
// NewTransaction creates a new transaction
func (conn *MongoDBConn) GetTableColumns(table *Table, fields ...string) (columns iop.Columns, err error) {
tables, err := conn.GetTables(table.Schema)
if err != nil {
return columns, g.Error("could not query to get tables")
}
found := false
for _, tableRow := range tables.Rows {
if strings.EqualFold(cast.ToString(tableRow[0]), table.Name) {
found = true
}
}
if !found {
return nil, g.Error("did not find collection %s", table.FullName())
}
ds, err := conn.StreamRows(table.FullName(), g.M("limit", 10, "silent", true))
if err != nil {
return columns, g.Error("could not query to get columns")
}
data, err := ds.Collect(10)
if err != nil {
return columns, g.Error("could not collect to get columns")
}
for i := range data.Columns {
data.Columns[i].Schema = table.Schema
data.Columns[i].Table = table.Name
data.Columns[i].DbType = "-"
}
return data.Columns, nil
}
func (conn *MongoDBConn) ExecContext(ctx context.Context, sql string, args ...interface{}) (result sql.Result, err error) {
return nil, g.Error("ExecContext not implemented on MongoConn")
}
func (conn *MongoDBConn) BulkExportFlow(tables ...Table) (df *iop.Dataflow, err error) {
if len(tables) == 0 {
return
}
options, _ := g.UnmarshalMap(tables[0].SQL)
ds, err := conn.StreamRowsContext(conn.Context().Ctx, tables[0].FullName(), options)
if err != nil {
return df, g.Error(err, "could start datastream")
}
df, err = iop.MakeDataFlow(ds)
if err != nil {
return df, g.Error(err, "could start dataflow")
}
return
}
func (conn *MongoDBConn) StreamRowsContext(ctx context.Context, collectionName string, Opts ...map[string]interface{}) (ds *iop.Datastream, err error) {
opts := getQueryOptions(Opts)
Limit := int64(0) // infinite
if val := cast.ToInt64(opts["limit"]); val > 0 {
Limit = val
}
findOpts := &options.FindOptions{Limit: &Limit}
fields := cast.ToStringSlice(opts["fields"])
if len(fields) > 0 {
d := bson.D{}
for _, field := range fields {
d = append(d, bson.D{{Key: field, Value: 1}}...)
}
findOpts = options.Find().SetProjection(d)
}
updateKey := cast.ToString(opts["update_key"])
incrementalValue := cast.ToString(opts["value"])
startValue := cast.ToString(opts["start_value"])
endValue := cast.ToString(opts["end_value"])
filter := bson.D{}
if updateKey != "" && incrementalValue != "" {
// incremental mode
incrementalValue = strings.Trim(incrementalValue, "'")
filter = append(filter, bson.D{{Key: updateKey, Value: bson.D{{Key: "$gt", Value: incrementalValue}}}}...)
} else if updateKey != "" && startValue != "" && endValue != "" {
// backfill mode
filter = append(filter, bson.D{{Key: updateKey, Value: bson.D{{Key: "$gte", Value: startValue}}}}...)
filter = append(filter, bson.D{{Key: updateKey, Value: bson.D{{Key: "$lte", Value: endValue}}}}...)
}
if strings.TrimSpace(collectionName) == "" {
g.Warn("Empty collection name")
return ds, nil
}
queryContext := g.NewContext(ctx)
table, _ := ParseTableName(collectionName, conn.Type)
collection := conn.Client.Database(table.Schema).Collection(table.Name)
if !cast.ToBool(opts["silent"]) {
conn.LogSQL(g.Marshal(g.M("database", table.Schema, "collection", table.Name, "filter", filter, "options", g.M("limit", findOpts.Limit, "projection", findOpts.Projection))))
}
cur, err := collection.Find(queryContext.Ctx, filter, findOpts)
if err != nil {
return ds, g.Error(err, "error querying collection")
}
ds = iop.NewDatastreamContext(queryContext.Ctx, nil)
// js := iop.NewJSONStream(ds, cur, true, conn.GetProp("jmespath"))
js := iop.NewJSONStream(ds, cur, true, conn.GetProp("jmespath"))
js.HasMapPayload = true
limit := cast.ToUint64(Limit)
nextFunc := func(it *iop.Iterator) bool {
if Limit > 0 && it.Counter >= limit {
return false
} else if it.Context.Err() != nil {
return false
}
for cur.Next(queryContext.Ctx) {
if js.NextFunc(it) {
// fix Object ID
it.Row[0] = strings.TrimSuffix(
strings.TrimPrefix(
cast.ToString(it.Row[0]), `ObjectID("`,
), `")`,
)
return true
}
}
return false
}
ds.SetIterator(ds.NewIterator(ds.Columns, nextFunc))
ds.NoDebug = strings.Contains(collectionName, noDebugKey)
ds.SetMetadata(conn.GetProp("METADATA"))
ds.SetConfig(conn.Props())
err = ds.Start()
if err != nil {
queryContext.Cancel()
return ds, g.Error(err, "could start datastream")
}
return
}
// GetSchemas returns schemas
func (conn *MongoDBConn) GetSchemas() (data iop.Dataset, err error) {
queryContext := g.NewContext(conn.Context().Ctx)
res, err := conn.Client.ListDatabases(queryContext.Ctx, bson.D{})
if err != nil {
return data, g.Error(err, "could not list mongo databases")
}
data = iop.NewDataset(iop.NewColumnsFromFields("schema_name"))
for _, db := range res.Databases {
data.Append([]interface{}{db.Name})
}
return data, nil
}
// GetSchemas returns schemas
func (conn *MongoDBConn) GetTables(schema string) (data iop.Dataset, err error) {
queryContext := g.NewContext(conn.Context().Ctx)
names, err := conn.Client.Database(schema).ListCollectionNames(queryContext.Ctx, bson.D{})
if err != nil {
return data, g.Error(err, "could not list mongo collections in database %s", schema)
}
data = iop.NewDataset(iop.NewColumnsFromFields("table_name"))
for _, name := range names {
data.Append([]interface{}{name})
}
return data, nil
}
// GetSchemata obtain full schemata info for a schema and/or table in current database
func (conn *MongoDBConn) GetSchemata(schemaName string, tableNames ...string) (Schemata, error) {
currDatabase := dbio.TypeDbMongoDB.String()
schemata := Schemata{
Databases: map[string]Database{},
conn: conn,
}
schemaData, err := conn.GetSchemas()
if err != nil {
return schemata, g.Error(err, "Could not get databases")
}
schemas := map[string]Schema{}
for _, schemaRow := range schemaData.Rows {
schemaName := cast.ToString(schemaRow[0])
tablesData, err := conn.GetTables(schemaName)
if err != nil {
return schemata, g.Error(err, "Could not get tables")
}
for _, tableRow := range tablesData.Rows {
tableName := cast.ToString(tableRow[0])
columnName := "data"
dataType := "json"
schema := Schema{
Name: schemaName,
Tables: map[string]Table{},
}
table := Table{
Name: tableName,
Schema: schemaName,
Database: currDatabase,
IsView: false,
Columns: iop.Columns{},
Dialect: conn.GetType(),
}
if _, ok := schemas[strings.ToLower(schema.Name)]; ok {
schema = schemas[strings.ToLower(schema.Name)]
}
if _, ok := schemas[strings.ToLower(schema.Name)].Tables[strings.ToLower(tableName)]; ok {
table = schemas[strings.ToLower(schema.Name)].Tables[strings.ToLower(tableName)]
}
column := iop.Column{
Name: columnName,
Type: iop.ColumnType(conn.template.NativeTypeMap[dataType]),
Table: tableName,
Schema: schemaName,
Database: currDatabase,
Position: 1,
DbType: dataType,
}
table.Columns = append(table.Columns, column)
if g.In(tableName, tableNames...) {
columns, err := conn.GetSQLColumns(table)
if err != nil {
return schemata, g.Error(err, "could not get columns")
}
for i := range columns {
columns[i].Database = table.Database
}
if len(columns) > 0 {
table.Columns = columns
}
}
schema.Tables[strings.ToLower(tableName)] = table
schemas[strings.ToLower(schema.Name)] = schema
}
schemata.Databases[strings.ToLower(currDatabase)] = Database{
Name: currDatabase,
Schemas: schemas,
}
}
return schemata, nil
}