driver

package
v1.2210.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2022 License: MPL-2.0 Imports: 18 Imported by: 4

Documentation

Index

Constants

View Source
const (
	SQLTypeDML = "dml"
	SQLTypeDDL = "ddl"
)
View Source
const (
	DriverTypeMySQL      = "MySQL"
	DriverTypePostgreSQL = "PostgreSQL"
	DriverTypeTiDB       = "TiDB"
	DriverTypeSQLServer  = "SQL Server"
	DriverTypeOracle     = "Oracle"
	DriverTypeDB2        = "DB2"
	DriverTypeOceanBase  = "OceanBase For MySQL"
)
View Source
const (
	ErrorTypeNotQuery = "not query"
	ErrorTypeNotError = "not error"
)
View Source
const (
	DefaultPluginVersion = 1
)
View Source
const (
	PluginNameAnalysisDriver = "analysis-driver"
)
View Source
const (
	PluginNameAuditDriver = "audit-driver"
)
View Source
const (
	PluginNameQueryDriver = "query-driver"
)

Variables

View Source
var ErrNodesCountExceedOne = errors.New("after parse, nodes count exceed one")
View Source
var ErrSQLIsNotSupported = errors.New("SQL is not supported")
View Source
var SQLEGRPCDialOptions = []grpc.DialOption{}
View Source
var SQLEGrpcServer = goPlugin.DefaultGRPCServer

Functions

func AllAdditionalParams added in v1.2203.0

func AllAdditionalParams() map[string]params.Params

func AllDrivers

func AllDrivers() []string

func AllRules

func AllRules() map[string][]*Rule

func GetQueryDriverNames added in v1.2206.0

func GetQueryDriverNames() []string

QueryDriverName = InstanceType

func InitPlugins

func InitPlugins(pluginDir string) error

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

func NewAnalysisDriverPlugin added in v1.2207.0

func NewAnalysisDriverPlugin(newDriver func(cfg *DSN) AnalysisDriver) *analysisDriverPlugin

func NewAuditDriverPlugin added in v1.2206.0

func NewAuditDriverPlugin(r Registerer, newDriver func(cfg *Config) Driver) *auditDriverPlugin

func NewQueryDriverPlugin added in v1.2206.0

func NewQueryDriverPlugin(newDriver func(cfg *DSN) SQLQueryDriver) *queryDriverPlugin

func RegisterAnalysisDriver added in v1.2206.0

func RegisterAnalysisDriver(name string)

RegisterAnalysisDriver like sql.RegisterAuditDriver.

RegisterAnalysisDriver makes a database driver available by the provided driver name. RegisterAnalysisDriver's initialize handler registered by RegisterAnalysisDriver.

func RegisterAuditDriver added in v1.2206.0

func RegisterAuditDriver(name string, rs []*Rule, ap params.Params)

RegisterAuditDriver like sql.RegisterAuditDriver.

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

func RegisterDriverFromClient added in v1.2206.0

func RegisterDriverFromClient(client *PluginClient) error

func RegisterDriverManger added in v1.2207.0

func RegisterDriverManger(client *PluginClient, pluginName string, handler newDriverManagerHandler)

func RegisterSQLQueryDriver added in v1.2206.0

func RegisterSQLQueryDriver(name string)

RegisterSQLQueryDriver like sql.RegisterAuditDriver.

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

func RuleLevelLessOrEqual added in v1.2204.0

func RuleLevelLessOrEqual(a, b string) bool

RuleLevelLessOrEqual return level a <= level b

func ServePlugin

func ServePlugin(r Registerer, newDriver func(cfg *Config) Driver)

ServePlugin start plugin process service. It should be called on plugin process. Deprecated: Use PluginServer.AddDriverPlugin and PluginServer.Serve instead.

Types

type AnalysisDriver added in v1.2206.0

type AnalysisDriver interface {
	ListTablesInSchema(ctx context.Context, conf *ListTablesInSchemaConf) (*ListTablesInSchemaResult, error)
	GetTableMetaByTableName(ctx context.Context, conf *GetTableMetaByTableNameConf) (*GetTableMetaByTableNameResult, error)
	GetTableMetaBySQL(ctx context.Context, conf *GetTableMetaBySQLConf) (*GetTableMetaBySQLResult, error)
	Explain(ctx context.Context, conf *ExplainConf) (*ExplainResult, error)
}

AnalysisDriver is a driver for SQL analysis and getting table metadata

type AnalysisInfoHead added in v1.2206.0

type AnalysisInfoHead struct {
	Name string
	Desc string
}

type AnalysisInfoInTableFormat added in v1.2206.0

type AnalysisInfoInTableFormat struct {
	Columns []AnalysisInfoHead
	Rows    [][]string
}

AnalysisInfoInTableFormat the field Columns represents the column name of a table the field Rows represents the data of the table their relationship is as follows

| Columns[0]  | Columns[1]  | Columns[2]  |
| Rows[0][0] | Rows[0][1] | Rows[0][2] |
| Rows[1][0] | Rows[1][1] | Rows[1][2] |

type AuditResult

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

func NewInspectResults

func NewInspectResults() *AuditResult

func (*AuditResult) Add

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

func (*AuditResult) HasResult added in v1.2201.0

func (rs *AuditResult) HasResult() bool

func (*AuditResult) Level

func (rs *AuditResult) Level() RuleLevel

Level find highest Level in result

func (*AuditResult) Message

func (rs *AuditResult) Message() string

func (*AuditResult) SortByLevel added in v1.2203.0

func (rs *AuditResult) SortByLevel()

type ColumnsInfo added in v1.2206.0

type ColumnsInfo struct {
	AnalysisInfoInTableFormat
}

type Config

type Config struct {
	DSN   *DSN
	Rules []*Rule
}

Config define the configuration for driver.

func NewConfig

func NewConfig(dsn *DSN, rules []*Rule) (*Config, error)

NewConfig return a config for driver.

1. dsn is nil, rules is not nil. Use drive to do Offline Audit. 2. dsn is not nil, rule is nil. Use drive to communicate with database only. 3. dsn is not nil, rule is not nil. Most common usecase.

type DSN added in v1.2111.0

type DSN struct {
	Host             string
	Port             string
	User             string
	Password         string
	AdditionalParams params.Params

	// DatabaseName is the default database to connect.
	DatabaseName string
}

DSN provide necessary information to connect to database.

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. provides handle to communicate with database 2. audit SQL with rules

type DriverManager added in v1.2207.0

type DriverManager interface {
	GetAuditDriver() (Driver, error)
	GetSQLQueryDriver() (SQLQueryDriver, error)
	GetAnalysisDriver() (AnalysisDriver, error)
	// Close invoke grpc.Close of audit plugin to inform plugin process to recycle their resource
	// resource of all drivers should be recycle in this function
	Close(ctx context.Context)
}

func NewDriverManger added in v1.2207.0

func NewDriverManger(log *logrus.Entry, dbType string, config *Config) (DriverManager, error)

type DriverNotSupportedError added in v1.2112.0

type DriverNotSupportedError struct {
	DriverTyp string
}

func (*DriverNotSupportedError) Error added in v1.2112.0

func (e *DriverNotSupportedError) Error() string

type ErrorType added in v1.2206.0

type ErrorType string

type ExplainClassicResult added in v1.2206.0

type ExplainClassicResult struct {
	AnalysisInfoInTableFormat
}

type ExplainConf added in v1.2206.0

type ExplainConf struct {
	// this SQL should be a single SQL
	Sql string
}

type ExplainResult added in v1.2206.0

type ExplainResult struct {
	ClassicResult ExplainClassicResult
}

type GetTableMetaBySQLConf added in v1.2206.0

type GetTableMetaBySQLConf struct {
	// this SQL should be a single SQL
	Sql string
}

type GetTableMetaBySQLResult added in v1.2206.0

type GetTableMetaBySQLResult struct {
	TableMetas []TableMetaItemBySQL
}

type GetTableMetaByTableNameConf added in v1.2206.0

type GetTableMetaByTableNameConf struct {
	Schema string
	Table  string
}

type GetTableMetaByTableNameResult added in v1.2206.0

type GetTableMetaByTableNameResult struct {
	TableMeta TableMetaItem
}

type IndexesInfo added in v1.2206.0

type IndexesInfo struct {
	AnalysisInfoInTableFormat
}

type ListTablesInSchemaConf added in v1.2206.0

type ListTablesInSchemaConf struct {
	Schema string
}

type ListTablesInSchemaResult added in v1.2206.0

type ListTablesInSchemaResult struct {
	Tables []Table
}

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.

type PluginClient added in v1.2206.0

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

func (*PluginClient) Client added in v1.2206.0

func (p *PluginClient) Client() (goPlugin.ClientProtocol, error)

func (*PluginClient) Kill added in v1.2206.0

func (p *PluginClient) Kill()

func (*PluginClient) RegisterDrivers added in v1.2207.0

func (p *PluginClient) RegisterDrivers(c *PluginClient) (pluginName string, err error)

type PluginDriverManager added in v1.2207.0

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

func (*PluginDriverManager) Close added in v1.2207.0

func (d *PluginDriverManager) Close(ctx context.Context)

func (*PluginDriverManager) GetAnalysisDriver added in v1.2207.0

func (d *PluginDriverManager) GetAnalysisDriver() (AnalysisDriver, error)

func (*PluginDriverManager) GetAuditDriver added in v1.2207.0

func (d *PluginDriverManager) GetAuditDriver() (Driver, error)

func (*PluginDriverManager) GetSQLQueryDriver added in v1.2207.0

func (d *PluginDriverManager) GetSQLQueryDriver() (SQLQueryDriver, error)

type PluginServer added in v1.2206.0

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

func NewPlugin added in v1.2206.0

func NewPlugin() *PluginServer

func (*PluginServer) AddAnalysisDriverPlugin added in v1.2207.0

func (p *PluginServer) AddAnalysisDriverPlugin(plugin goPlugin.Plugin)

func (*PluginServer) AddDriverPlugin added in v1.2206.0

func (p *PluginServer) AddDriverPlugin(plugin goPlugin.Plugin)

func (*PluginServer) AddPlugin added in v1.2206.0

func (p *PluginServer) AddPlugin(pluginName string, pluginVersion int, plugin goPlugin.Plugin)

func (*PluginServer) AddQueryDriverPlugin added in v1.2206.0

func (p *PluginServer) AddQueryDriverPlugin(plugin goPlugin.Plugin)

func (*PluginServer) Serve added in v1.2206.0

func (p *PluginServer) Serve()

type QueryConf added in v1.2206.0

type QueryConf struct {
	TimeOutSecond uint32
}

type QueryPrepareConf added in v1.2206.0

type QueryPrepareConf struct {
	Limit  uint32
	Offset uint32
}

type QueryPrepareResult added in v1.2206.0

type QueryPrepareResult struct {
	NewSQL    string
	ErrorType ErrorType
	Error     string
}

type QueryResult added in v1.2206.0

type QueryResult struct {
	Column params.Params
	Rows   []*QueryResultRow
}

The data location in Values should be consistent with that in Column

type QueryResultRow added in v1.2206.0

type QueryResultRow struct {
	Values []*QueryResultValue
}

type QueryResultValue added in v1.2206.0

type QueryResultValue struct {
	Value string
}

type Registerer added in v1.2111.0

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

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

	// AdditionalParams returns all additional params that plugin supported.
	AdditionalParams() params.Params
}

Registerer is the interface that all SQLe plugins must support.

type Rule added in v1.2111.0

type Rule struct {
	Name string
	Desc string

	// Category is the category of the rule. Such as "Naming Conventions"...
	// Rules will be displayed on the SQLE rule list page by category.
	Category string
	Level    RuleLevel
	Params   params.Params
}

type RuleLevel added in v1.2111.0

type RuleLevel string
const (
	RuleLevelNull   RuleLevel = "" // used to indicate no rank
	RuleLevelNormal RuleLevel = "normal"
	RuleLevelNotice RuleLevel = "notice"
	RuleLevelWarn   RuleLevel = "warn"
	RuleLevelError  RuleLevel = "error"
)

func (RuleLevel) LessOrEqual added in v1.2201.0

func (r RuleLevel) LessOrEqual(l RuleLevel) bool

func (RuleLevel) More added in v1.2201.0

func (r RuleLevel) More(l RuleLevel) bool

func (RuleLevel) MoreOrEqual added in v1.2203.0

func (r RuleLevel) MoreOrEqual(l RuleLevel) bool

type SQLQueryDriver added in v1.2206.0

type SQLQueryDriver interface {
	QueryPrepare(ctx context.Context, sql string, conf *QueryPrepareConf) (*QueryPrepareResult, error)
	Query(ctx context.Context, sql string, conf *QueryConf) (*QueryResult, error)
}

SQLQueryDriver is a SQL rewrite and execute driver

type Table added in v1.2206.0

type Table struct {
	Name string
}

type TableMetaItem added in v1.2206.0

type TableMetaItem struct {
	Name           string
	Schema         string
	ColumnsInfo    ColumnsInfo
	IndexesInfo    IndexesInfo
	CreateTableSQL string
}

type TableMetaItemBySQL added in v1.2206.0

type TableMetaItemBySQL struct {
	Name           string
	Schema         string
	ColumnsInfo    ColumnsInfo
	IndexesInfo    IndexesInfo
	CreateTableSQL string
	Message        string
}

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