-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
166 lines (138 loc) · 5.33 KB
/
db.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
package release
import (
"database/sql"
"github.com/obgnail/plugin-platform/common/common_type"
"github.com/obgnail/plugin-platform/common/config"
"github.com/obgnail/plugin-platform/common/protocol"
"github.com/obgnail/plugin-platform/common/utils/message"
"github.com/obgnail/plugin-platform/host/resource/common"
)
var _ common_type.LocalDB = (*LocalDB)(nil)
var _ common_type.SysDB = (*SysDB)(nil)
type CommonDB struct {
db *sql.DB
plugin common_type.IPlugin
sender common.Sender
}
func NewCommonDB(plugin common_type.IPlugin, sender common.Sender) *CommonDB {
return &CommonDB{plugin: plugin, sender: sender}
}
func (d *CommonDB) buildMessage(databaseRequestMessage *protocol.DatabaseMessage_DatabaseRequestMessage) *protocol.PlatformMessage {
msg := message.GetInitMessage(nil, nil)
msg.Resource = &protocol.ResourceMessage{
Database: &protocol.DatabaseMessage{DBRequest: databaseRequestMessage},
}
return msg
}
func (d *CommonDB) sendMsgToHost(platformMessage *protocol.PlatformMessage) (*protocol.PlatformMessage, common_type.PluginError) {
return d.sender.Send(d.plugin, platformMessage)
}
func (d *CommonDB) sendToHostAsync(platformMessage *protocol.PlatformMessage, callback common_type.DBCallBack) {
cb := &dbCallbackWrapper{Func: callback}
d.sender.SendAsync(d.plugin, platformMessage, cb.callBack)
}
func (d *CommonDB) CommonSelect(db, sql string) ([]*common_type.RawData, []*common_type.ColumnDesc, common_type.PluginError) {
dbMsg := &protocol.DatabaseMessage_DatabaseRequestMessage{DB: db, Statement: sql}
msg, err := d.sendMsgToHost(d.buildMessage(dbMsg))
if err != nil {
return nil, nil, err
}
if e := d.checkError(msg); e != nil {
return nil, nil, e
}
rawDataset, columns := common.ParseTableData(msg.GetResource().GetDatabase().GetDBResponse().GetData())
return rawDataset, columns, nil
}
func (d *CommonDB) CommonAsyncSelect(db, sql string, callback common_type.DBCallBack) {
dbMsg := &protocol.DatabaseMessage_DatabaseRequestMessage{DB: db, Statement: sql}
d.sendToHostAsync(d.buildMessage(dbMsg), callback)
}
func (d *CommonDB) CommonExec(db, sql string) common_type.PluginError {
dbMsg := &protocol.DatabaseMessage_DatabaseRequestMessage{DB: db, Statement: sql}
msg, err := d.sendMsgToHost(d.buildMessage(dbMsg))
if err != nil {
return err
}
if e := d.checkError(msg); e != nil {
return e
}
return nil
}
func (d *CommonDB) Unmarshal(rawData []*common_type.RawData, columnDesc []*common_type.ColumnDesc, v interface{}) common_type.PluginError {
if len(rawData) == 0 {
return nil
}
if err := common.Unmarshal(rawData, columnDesc, v); err != nil {
return common_type.NewPluginError(common_type.UnmarshalFailure, err.Error())
}
return nil
}
func (d *CommonDB) checkError(msg *protocol.PlatformMessage) common_type.PluginError {
err := msg.GetResource().GetDatabase().GetDBResponse().GetError()
if err != nil {
return common_type.NewPluginError(int(err.GetCode()), err.GetMsg())
}
err = msg.GetResource().GetDatabase().GetDBResponse().GetDBError()
if err != nil {
return common_type.NewPluginError(int(err.GetCode()), err.GetMsg())
}
return nil
}
type dbCallbackWrapper struct {
Func common_type.DBCallBack
}
func (w *dbCallbackWrapper) callBack(input, result *protocol.PlatformMessage, err common_type.PluginError) {
dbResponse := result.GetResource().GetDatabase().GetDBResponse()
data := dbResponse.GetData()
rawDataset, columns := common.ParseTableData(data)
w.Func(rawDataset, columns, err)
}
type LocalDB struct {
db string
common *CommonDB
}
func NewLocalDB(plugin common_type.IPlugin, sender common.Sender) common_type.LocalDB {
db := config.String("platform.mysql_user_plugin_db_name", "plugins")
return &LocalDB{common: NewCommonDB(plugin, sender), db: db}
}
func (d *LocalDB) Select(sql string) ([]*common_type.RawData, []*common_type.ColumnDesc, common_type.PluginError) {
return d.common.CommonSelect(d.db, sql)
}
func (d *LocalDB) AsyncSelect(sql string, callback common_type.DBCallBack) {
d.common.CommonAsyncSelect(d.db, sql, callback)
}
func (d *LocalDB) Exec(sql string) common_type.PluginError {
return d.common.CommonExec(d.db, sql)
}
func (d *LocalDB) ImportSQL(sqlFilePath string) common_type.PluginError {
dbMsg := &protocol.DatabaseMessage_DatabaseRequestMessage{SqlFileName: sqlFilePath}
msg, err := d.common.sendMsgToHost(d.common.buildMessage(dbMsg))
if err != nil {
return err
}
if e := d.common.checkError(msg); e != nil {
return e
}
return nil
}
func (d *LocalDB) Unmarshal(rawData []*common_type.RawData, columnDesc []*common_type.ColumnDesc, v interface{}) common_type.PluginError {
return d.common.Unmarshal(rawData, columnDesc, v)
}
type SysDB struct {
common *CommonDB
}
func NewSysDB(plugin common_type.IPlugin, sender common.Sender) *SysDB {
return &SysDB{common: NewCommonDB(plugin, sender)}
}
func (d *SysDB) Select(db, sql string) ([]*common_type.RawData, []*common_type.ColumnDesc, common_type.PluginError) {
return d.common.CommonSelect(db, sql)
}
func (d *SysDB) AsyncSelect(db, sql string, callback common_type.DBCallBack) {
d.common.CommonAsyncSelect(db, sql, callback)
}
func (d *SysDB) Exec(db, sql string) common_type.PluginError {
return d.common.CommonExec(db, sql)
}
func (d *SysDB) Unmarshal(rawData []*common_type.RawData, columnDesc []*common_type.ColumnDesc, v interface{}) common_type.PluginError {
return d.common.Unmarshal(rawData, columnDesc, v)
}