DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer. To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins. A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id. When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data. Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned. Some pending things: - Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it. - RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface. - Master/Replica API support.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
f0bd973a5c
Коммит
4b95d47923
@@ -6,93 +6,109 @@ package driver
|
||||
import (
|
||||
"context"
|
||||
"database/sql/driver"
|
||||
"reflect"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/plugin"
|
||||
)
|
||||
|
||||
type wrapperTx struct {
|
||||
driver.Tx
|
||||
id string
|
||||
api plugin.Driver
|
||||
}
|
||||
|
||||
func (t *wrapperTx) Commit() error {
|
||||
return t.Tx.Commit()
|
||||
return t.api.TxCommit(t.id)
|
||||
}
|
||||
|
||||
func (t *wrapperTx) Rollback() error {
|
||||
return t.Tx.Rollback()
|
||||
return t.api.TxRollback(t.id)
|
||||
}
|
||||
|
||||
type wrapperStmt struct {
|
||||
driver.Stmt
|
||||
id string
|
||||
api plugin.Driver
|
||||
}
|
||||
|
||||
func (s *wrapperStmt) Close() error {
|
||||
return s.Stmt.Close()
|
||||
return s.api.StmtClose(s.id)
|
||||
}
|
||||
|
||||
func (s *wrapperStmt) NumInput() int {
|
||||
return s.Stmt.NumInput()
|
||||
return s.api.StmtNumInput(s.id)
|
||||
}
|
||||
|
||||
func (s *wrapperStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||
return s.Stmt.(driver.StmtExecContext).ExecContext(ctx, args)
|
||||
func (s *wrapperStmt) ExecContext(_ context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||
resultContainer, err := s.api.StmtExec(s.id, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := &wrapperResult{
|
||||
res: resultContainer,
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *wrapperStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||
return s.Stmt.(driver.StmtQueryContext).QueryContext(ctx, args)
|
||||
func (s *wrapperStmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||
rowsID, err := s.api.StmtQuery(s.id, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows := &wrapperRows{
|
||||
id: rowsID,
|
||||
api: s.api,
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// wrapperResult implements the driver.Result interface.
|
||||
// This differs from other objects because it already contains the
|
||||
// information for its methods. This does two things:
|
||||
//
|
||||
// 1. Simplifies server-side code by avoiding to track result ids
|
||||
// in a map.
|
||||
// 2. Avoids round-trip to compute result methods.
|
||||
type wrapperResult struct {
|
||||
driver.Result
|
||||
res plugin.ResultContainer
|
||||
}
|
||||
|
||||
func (r *wrapperResult) LastInsertId() (int64, error) {
|
||||
return r.Result.LastInsertId()
|
||||
return r.res.LastID, r.res.LastIDError
|
||||
}
|
||||
|
||||
func (r *wrapperResult) RowsAffected() (int64, error) {
|
||||
return r.Result.RowsAffected()
|
||||
return r.res.RowsAffected, r.res.RowsAffectedError
|
||||
}
|
||||
|
||||
type wrapperRows struct {
|
||||
driver.Rows
|
||||
id string
|
||||
api plugin.Driver
|
||||
}
|
||||
|
||||
func (r *wrapperRows) Columns() []string {
|
||||
return r.Rows.Columns()
|
||||
return r.api.RowsColumns(r.id)
|
||||
}
|
||||
|
||||
func (r *wrapperRows) Close() error {
|
||||
return r.Rows.Close()
|
||||
return r.api.RowsClose(r.id)
|
||||
}
|
||||
|
||||
func (r *wrapperRows) Next(dest []driver.Value) error {
|
||||
return r.Rows.Next(dest)
|
||||
return r.api.RowsNext(r.id, dest)
|
||||
}
|
||||
|
||||
func (r *wrapperRows) HasNextResultSet() bool {
|
||||
return r.Rows.(driver.RowsNextResultSet).HasNextResultSet()
|
||||
return r.api.RowsHasNextResultSet(r.id)
|
||||
}
|
||||
|
||||
func (r *wrapperRows) NextResultSet() error {
|
||||
return r.Rows.(driver.RowsNextResultSet).NextResultSet()
|
||||
}
|
||||
|
||||
func (r *wrapperRows) ColumnTypeScanType(index int) reflect.Type {
|
||||
return r.Rows.(driver.RowsColumnTypeScanType).ColumnTypeScanType(index)
|
||||
return r.api.RowsNextResultSet(r.id)
|
||||
}
|
||||
|
||||
func (r *wrapperRows) ColumnTypeDatabaseTypeName(index int) string {
|
||||
return r.Rows.(driver.RowsColumnTypeDatabaseTypeName).ColumnTypeDatabaseTypeName(index)
|
||||
}
|
||||
|
||||
func (r *wrapperRows) ColumnTypeLength(index int) (length int64, ok bool) {
|
||||
return r.Rows.(driver.RowsColumnTypeLength).ColumnTypeLength(index)
|
||||
}
|
||||
|
||||
func (r *wrapperRows) ColumnTypeNullable(index int) (nullable, ok bool) {
|
||||
return r.Rows.(driver.RowsColumnTypeNullable).ColumnTypeNullable(index)
|
||||
return r.api.RowsColumnTypeDatabaseTypeName(r.id, index)
|
||||
}
|
||||
|
||||
func (r *wrapperRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
|
||||
return r.Rows.(driver.RowsColumnTypePrecisionScale).ColumnTypePrecisionScale(index)
|
||||
return r.api.RowsColumnTypePrecisionScale(r.id, index)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user