-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
plugin_db_driver.go
307 lines (262 loc) · 7.18 KB
/
plugin_db_driver.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"context"
"database/sql"
"database/sql/driver"
"sync"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
)
// DriverImpl implements the plugin.Driver interface on the server-side.
// Each new request for a connection/statement/transaction etc, generates
// a new entry tracked centrally in a map. Further requests operate on the
// object ID.
type DriverImpl struct {
s *Server
connMut sync.RWMutex
connMap map[string]*sql.Conn
txMut sync.Mutex
txMap map[string]driver.Tx
stMut sync.RWMutex
stMap map[string]driver.Stmt
rowsMut sync.RWMutex
rowsMap map[string]driver.Rows
}
func NewDriverImpl(s *Server) *DriverImpl {
return &DriverImpl{
s: s,
connMap: make(map[string]*sql.Conn),
txMap: make(map[string]driver.Tx),
stMap: make(map[string]driver.Stmt),
rowsMap: make(map[string]driver.Rows),
}
}
func (d *DriverImpl) Conn(isMaster bool) (string, error) {
dbFunc := d.s.sqlStore.GetMaster
if !isMaster {
dbFunc = d.s.sqlStore.GetReplica
}
conn, err := dbFunc().Db.Conn(context.Background())
if err != nil {
return "", err
}
connID := model.NewId()
d.connMut.Lock()
d.connMap[connID] = conn
d.connMut.Unlock()
return connID, nil
}
// According to https://golang.org/pkg/database/sql/#Conn, a client can call
// Close on a connection, concurrently while running a query.
//
// Therefore, we have to handle the case where the connection is no longer
// present in the map because it has been closed. ErrBadConn is a good choice
// here which indicates the sql package to retry on a new connection.
//
// ConnPing, ConnQuery, ConnClose, Tx, and Stmt do this.
func (d *DriverImpl) ConnPing(connID string) error {
d.connMut.RLock()
conn, ok := d.connMap[connID]
d.connMut.RUnlock()
if !ok {
return driver.ErrBadConn
}
return conn.Raw(func(innerConn interface{}) error {
return innerConn.(driver.Pinger).Ping(context.Background())
})
}
func (d *DriverImpl) ConnQuery(connID, q string, args []driver.NamedValue) (_ string, err error) {
var rows driver.Rows
d.connMut.RLock()
conn, ok := d.connMap[connID]
d.connMut.RUnlock()
if !ok {
return "", driver.ErrBadConn
}
err = conn.Raw(func(innerConn interface{}) error {
rows, err = innerConn.(driver.QueryerContext).QueryContext(context.Background(), q, args)
return err
})
if err != nil {
return "", err
}
rowsID := model.NewId()
d.rowsMut.Lock()
d.rowsMap[rowsID] = rows
d.rowsMut.Unlock()
return rowsID, nil
}
func (d *DriverImpl) ConnExec(connID, q string, args []driver.NamedValue) (_ plugin.ResultContainer, err error) {
var res driver.Result
var ret plugin.ResultContainer
d.connMut.RLock()
conn, ok := d.connMap[connID]
d.connMut.RUnlock()
if !ok {
return ret, driver.ErrBadConn
}
err = conn.Raw(func(innerConn interface{}) error {
res, err = innerConn.(driver.ExecerContext).ExecContext(context.Background(), q, args)
return err
})
if err != nil {
return ret, err
}
ret.LastID, ret.LastIDError = res.LastInsertId()
ret.RowsAffected, ret.RowsAffectedError = res.RowsAffected()
return ret, nil
}
func (d *DriverImpl) ConnClose(connID string) error {
d.connMut.Lock()
conn, ok := d.connMap[connID]
if !ok {
d.connMut.Unlock()
return driver.ErrBadConn
}
delete(d.connMap, connID)
d.connMut.Unlock()
return conn.Close()
}
func (d *DriverImpl) Tx(connID string, opts driver.TxOptions) (_ string, err error) {
var tx driver.Tx
d.connMut.RLock()
conn, ok := d.connMap[connID]
d.connMut.RUnlock()
if !ok {
return "", driver.ErrBadConn
}
err = conn.Raw(func(innerConn interface{}) error {
tx, err = innerConn.(driver.ConnBeginTx).BeginTx(context.Background(), opts)
return err
})
if err != nil {
return "", err
}
txID := model.NewId()
d.txMut.Lock()
d.txMap[txID] = tx
d.txMut.Unlock()
return txID, nil
}
func (d *DriverImpl) TxCommit(txID string) error {
d.txMut.Lock()
tx := d.txMap[txID]
delete(d.txMap, txID)
d.txMut.Unlock()
return tx.Commit()
}
func (d *DriverImpl) TxRollback(txID string) error {
d.txMut.Lock()
tx := d.txMap[txID]
delete(d.txMap, txID)
d.txMut.Unlock()
return tx.Rollback()
}
func (d *DriverImpl) Stmt(connID, q string) (_ string, err error) {
var stmt driver.Stmt
d.connMut.RLock()
conn, ok := d.connMap[connID]
d.connMut.RUnlock()
if !ok {
return "", driver.ErrBadConn
}
err = conn.Raw(func(innerConn interface{}) error {
stmt, err = innerConn.(driver.Conn).Prepare(q)
return err
})
if err != nil {
return "", err
}
stID := model.NewId()
d.stMut.Lock()
d.stMap[stID] = stmt
d.stMut.Unlock()
return stID, nil
}
func (d *DriverImpl) StmtClose(stID string) error {
d.stMut.Lock()
err := d.stMap[stID].Close()
delete(d.stMap, stID)
d.stMut.Unlock()
return err
}
func (d *DriverImpl) StmtNumInput(stID string) int {
d.stMut.RLock()
defer d.stMut.RUnlock()
return d.stMap[stID].NumInput()
}
func (d *DriverImpl) StmtQuery(stID string, args []driver.NamedValue) (string, error) {
argVals := make([]driver.Value, len(args))
for i, a := range args {
argVals[i] = a.Value
}
d.stMut.RLock()
st := d.stMap[stID]
d.stMut.RUnlock()
rows, err := st.Query(argVals) //nolint:staticcheck
if err != nil {
return "", err
}
rowsID := model.NewId()
d.rowsMut.Lock()
d.rowsMap[rowsID] = rows
d.rowsMut.Unlock()
return rowsID, nil
}
func (d *DriverImpl) StmtExec(stID string, args []driver.NamedValue) (plugin.ResultContainer, error) {
argVals := make([]driver.Value, len(args))
for i, a := range args {
argVals[i] = a.Value
}
var ret plugin.ResultContainer
d.stMut.RLock()
st := d.stMap[stID]
d.stMut.RUnlock()
res, err := st.Exec(argVals) //nolint:staticcheck
if err != nil {
return ret, err
}
ret.LastID, ret.LastIDError = res.LastInsertId()
ret.RowsAffected, ret.RowsAffectedError = res.RowsAffected()
return ret, nil
}
func (d *DriverImpl) RowsColumns(rowsID string) []string {
d.rowsMut.RLock()
defer d.rowsMut.RUnlock()
return d.rowsMap[rowsID].Columns()
}
func (d *DriverImpl) RowsClose(rowsID string) error {
d.rowsMut.Lock()
defer d.rowsMut.Unlock()
err := d.rowsMap[rowsID].Close()
delete(d.rowsMap, rowsID)
return err
}
func (d *DriverImpl) RowsNext(rowsID string, dest []driver.Value) error {
d.rowsMut.RLock()
rows := d.rowsMap[rowsID]
d.rowsMut.RUnlock()
return rows.Next(dest)
}
func (d *DriverImpl) RowsHasNextResultSet(rowsID string) bool {
d.rowsMut.RLock()
defer d.rowsMut.RUnlock()
return d.rowsMap[rowsID].(driver.RowsNextResultSet).HasNextResultSet()
}
func (d *DriverImpl) RowsNextResultSet(rowsID string) error {
d.rowsMut.RLock()
defer d.rowsMut.RUnlock()
return d.rowsMap[rowsID].(driver.RowsNextResultSet).NextResultSet()
}
func (d *DriverImpl) RowsColumnTypeDatabaseTypeName(rowsID string, index int) string {
d.rowsMut.RLock()
defer d.rowsMut.RUnlock()
return d.rowsMap[rowsID].(driver.RowsColumnTypeDatabaseTypeName).ColumnTypeDatabaseTypeName(index)
}
func (d *DriverImpl) RowsColumnTypePrecisionScale(rowsID string, index int) (int64, int64, bool) {
d.rowsMut.RLock()
defer d.rowsMut.RUnlock()
return d.rowsMap[rowsID].(driver.RowsColumnTypePrecisionScale).ColumnTypePrecisionScale(index)
}