driver

package
v1.2110.0-pre1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2021 License: MPL-2.0 Imports: 16 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNodesCountExceedOne = errors.New("after parse, nodes count exceed one")

Functions

func AllDrivers

func AllDrivers() []string

func AllRules

func AllRules() []*model.Rule

func InitPlugins

func InitPlugins(pluginDir string) error

InitPlugins init plugins at plugins directory. It should be called on host process.

func Register

func Register(name string, h handler, rs []*model.Rule)

Register like sql.Register.

Register makes a database driver available by the provided driver name. Driver's initialize handler and audit rules register by Register.

func ServePlugin

func ServePlugin(driver Driver, base BaseDriver, initDriver func(cfg *Config))

ServePlugin start plugin process service. It should be called on plugin process. initDriver is a closure which should hold the pointer to driver.

Types

type AuditResult

type AuditResult struct {
	// contains filtered or unexported fields
}

func NewInspectResults

func NewInspectResults() *AuditResult

func (*AuditResult) Add

func (rs *AuditResult) Add(level, message string, args ...interface{})

func (*AuditResult) Level

func (rs *AuditResult) Level() string

Level find highest Level in result

func (*AuditResult) Message

func (rs *AuditResult) Message() string

type BaseDriver

type BaseDriver interface {
	// Name returns plugin name.
	Name() string

	// Rules returns all rules that plugin supported.
	Rules() []*model.Rule
}

BaseDriver is the interface that all SQLe plugins must support.

type Config

type Config struct {
	IsOfflineAudit bool
	Schema         string
	Inst           *model.Instance
	Rules          []*model.Rule
}

func NewConfig

func NewConfig(inst *model.Instance, rules []*model.Rule, schema string, isOfflineAudit bool) *Config

type Driver

type Driver interface {
	Close(ctx context.Context)
	Ping(ctx context.Context) error
	Exec(ctx context.Context, query string) (driver.Result, error)
	Tx(ctx context.Context, queries ...string) ([]driver.Result, error)

	// Schemas export all supported schemas.
	//
	// For example, performance_schema/performance_schema... which in MySQL is not allowed for auditing.
	Schemas(ctx context.Context) ([]string, error)

	// Parse parse sqlText to Node array.
	//
	// sqlText may be single SQL or batch SQLs.
	Parse(ctx context.Context, sqlText string) ([]Node, error)

	// Audit sql with rules. sql is single SQL text.
	//
	// Multi Audit call may be in one context.
	// For example:
	//		driver, _ := NewDriver(..., ..., ...)
	// 		driver.Audit(..., "CREATE TABLE t1(id int)")
	// 		driver.Audit(..., "SELECT * FROM t1 WHERE id = 1")
	//      ...
	// driver should keep SQL context during it's lifecycle.
	Audit(ctx context.Context, sql string) (*AuditResult, error)

	// GenRollbackSQL generate sql's rollback SQL.
	GenRollbackSQL(ctx context.Context, sql string) (string, string, error)
}

Driver is a interface that must be implemented by a database.

It's implementation maybe on the same process or over gRPC(by go-plugin).

Driver is responsible for two primary things: 1. privode handle to communicate with database 2. audit SQL with rules

func NewDriver

func NewDriver(log *logrus.Entry, inst *model.Instance, isOfflineAudit bool, dbType, schema string) (Driver, error)

NewDriver return a new instantiated Driver.

type ErrDriverNotSupported

type ErrDriverNotSupported struct {
	DriverTyp string
}

func (*ErrDriverNotSupported) Error

func (e *ErrDriverNotSupported) Error() string

type Node

type Node struct {
	// Text is the raw SQL text of Node.
	Text string

	// Type is type of SQL, such as DML/DDL/DCL.
	Type string

	// Fingerprint is fingerprint of Node's raw SQL.
	Fingerprint string
}

Node is a interface which unify SQL ast tree. It produce by Driver.Parse.

Directories

Path Synopsis
Package proto is a generated protocol buffer package.
Package proto is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL