* Pass-through DB Driver implementation This is the first step in implementing a DB layer via RPC. The plan is to migrate the mattermost-plugin-api to use this DB connector so that all queries start to get routed through this library. And then we will add the DB query capability to the plugin RPC API and route all queries via RPC. At that point, this will be completely transparent to all plugins because they will already be using the DB connector and everything will be behind the scenes for them. https://focalboard-community.octo.mattermost.com/workspace/zyoahc9uapdn3xdptac6jb69ic?id=285b80a3-257d-41f6-8cf4-ed80ca9d92e5&v=495cdb4d-c13a-4992-8eb9-80cfee2819a4&c=c7386db7-65fd-469b-8bcf-8dc8f8e61e4f ```release-note NONE ``` * remove deprecated interfaces
58 строки
1.1 KiB
Go
58 строки
1.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package driver
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
)
|
|
|
|
var (
|
|
// Compile-time check to ensure Connector implements the interface.
|
|
_ driver.Connector = &Connector{}
|
|
)
|
|
|
|
// Connector is the DB connector which is used to
|
|
// initialize the underlying DB.
|
|
type Connector struct {
|
|
driverName string
|
|
dsn string
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewConnector(driverName, dsn string) (*Connector, error) {
|
|
db, err := sql.Open(driverName, dsn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Connector{
|
|
driverName: driverName,
|
|
dsn: dsn,
|
|
db: db,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
|
|
conn, err := c.db.Conn(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Conn{conn: conn}, nil
|
|
}
|
|
|
|
func (c *Connector) Driver() driver.Driver {
|
|
return &Driver{c: c}
|
|
}
|
|
|
|
// Driver is a DB driver implementation.
|
|
type Driver struct {
|
|
c *Connector
|
|
}
|
|
|
|
func (d Driver) Open(name string) (driver.Conn, error) {
|
|
return d.c.Connect(context.Background())
|
|
}
|