-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathmongodb.go
149 lines (125 loc) · 3.6 KB
/
mongodb.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
package mongodb
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/clidey/whodb/core/src/engine"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MongoDBPlugin struct{}
func (p *MongoDBPlugin) IsAvailable(config *engine.PluginConfig) bool {
client, err := DB(config)
if err != nil {
return false
}
defer client.Disconnect(context.TODO())
return true
}
func (p *MongoDBPlugin) GetDatabases(config *engine.PluginConfig) ([]string, error) {
return nil, errors.ErrUnsupported
}
func (p *MongoDBPlugin) GetSchema(config *engine.PluginConfig) ([]string, error) {
client, err := DB(config)
if err != nil {
return nil, err
}
defer client.Disconnect(context.TODO())
databases, err := client.ListDatabaseNames(context.TODO(), bson.M{})
if err != nil {
return nil, err
}
return databases, nil
}
func (p *MongoDBPlugin) GetStorageUnits(config *engine.PluginConfig, database string) ([]engine.StorageUnit, error) {
client, err := DB(config)
if err != nil {
return nil, err
}
defer client.Disconnect(context.TODO())
db := client.Database(database)
collections, err := db.ListCollectionNames(context.TODO(), bson.M{})
if err != nil {
return nil, err
}
storageUnits := []engine.StorageUnit{}
for _, collectionName := range collections {
stats := bson.M{}
err := db.RunCommand(context.TODO(), bson.D{{Key: "collStats", Value: collectionName}}).Decode(&stats)
if err != nil {
return nil, err
}
storageUnits = append(storageUnits, engine.StorageUnit{
Name: collectionName,
Attributes: []engine.Record{
{Key: "Storage Size", Value: fmt.Sprintf("%v", stats["storageSize"])},
{Key: "Count", Value: fmt.Sprintf("%v", stats["count"])},
},
})
}
return storageUnits, nil
}
func (p *MongoDBPlugin) GetRows(config *engine.PluginConfig, database, collection, filter string, pageSize, pageOffset int) (*engine.GetRowsResult, error) {
client, err := DB(config)
if err != nil {
return nil, err
}
defer client.Disconnect(context.TODO())
db := client.Database(database)
coll := db.Collection(collection)
var bsonFilter bson.M
if len(filter) > 0 {
if err := bson.UnmarshalExtJSON([]byte(filter), true, &bsonFilter); err != nil {
return nil, fmt.Errorf("invalid filter format: %v", err)
}
}
totalCount, err := coll.CountDocuments(context.TODO(), bsonFilter)
if err != nil {
return nil, err
}
findOptions := options.Find()
findOptions.SetLimit(int64(pageSize))
findOptions.SetSkip(int64(pageOffset))
cursor, err := coll.Find(context.TODO(), bsonFilter, findOptions)
if err != nil {
return nil, err
}
defer cursor.Close(context.TODO())
var rowsResult []bson.M
if err = cursor.All(context.TODO(), &rowsResult); err != nil {
return nil, err
}
result := &engine.GetRowsResult{
Columns: []engine.Column{
{
Name: "document",
Type: "Document",
},
},
Rows: [][]string{},
}
for _, doc := range rowsResult {
jsonBytes, err := json.Marshal(doc)
if err != nil {
return nil, err
}
result.Rows = append(result.Rows, []string{
string(jsonBytes),
})
}
result.TotalCount = int(totalCount)
return result, nil
}
func (p *MongoDBPlugin) RawExecute(config *engine.PluginConfig, query string) (*engine.GetRowsResult, error) {
return nil, errors.ErrUnsupported
}
func (p *MongoDBPlugin) Chat(config *engine.PluginConfig, schema string, model string, previousConversation string, query string) ([]*engine.ChatMessage, error) {
return nil, errors.ErrUnsupported
}
func NewMongoDBPlugin() *engine.Plugin {
return &engine.Plugin{
Type: engine.DatabaseType_MongoDB,
PluginFunctions: &MongoDBPlugin{},
}
}