engine

package
v2.0.0-rc04 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTableName

func ParseTableName(sql string) string

ParseTableName extracts the table name from a single table SQL query.

func SetExecutorContext

func SetExecutorContext(ctx context.Context, ec *ExecutorContext) context.Context

SetExecutorContext 添加执行上下文

Types

type AfterHandler

type AfterHandler func(ctx context.Context, ec *ExecutorContext, er *ExecutorResult)

AfterHandler sql 执行后回调

type Chain

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

Chain 中间件执行链

func NewHookChain

func NewHookChain(hooks ...Hook) *Chain

NewHookChain 创建 hooks Chain

func (*Chain) After

func (c *Chain) After(ctx context.Context, ec *ExecutorContext, er *ExecutorResult)

After 执行后

func (*Chain) Before

func (c *Chain) Before(ctx context.Context, ec *ExecutorContext) error

Before 执行前

type Execer

type Execer interface {
	// NamedExecContext 使用命名参数执行sql
	NamedExecContext(ctx context.Context, execSQL string, arg any) (sql.Result, error)

	// ExecContext 使用数组参数执行sql
	ExecContext(ctx context.Context, execSQL string, args ...any) (sql.Result, error)
}

Execer 更新语句执行器

type Executor

type Executor interface {
	Execer
	Queryer
}

Executor sql 语句执行器。Execer 和 Queryer 的组合

type ExecutorContext

type ExecutorContext struct {
	Type      SQLType
	TableName string
	SQL       string
	Args      []any
	NameArgs  any
	Start     time.Time
}

ExecutorContext SQL 执行器上下文

func GetExecutorContext

func GetExecutorContext(ctx context.Context) *ExecutorContext

GetExecutorContext 获取执行上下文

type ExecutorResult

type ExecutorResult struct {
	Err       error         // 执行异常异常
	Affected  int64         // 新增、删除、修改返回的影响行数
	QueryRows int64         // 查询记录行数
	Duration  time.Duration // 耗时
}

ExecutorResult 执行结果

type Hook

type Hook interface {
	// Before sql 执行前
	Before(ctx context.Context, ec *ExecutorContext) error
	// After sql 执行后
	After(ctx context.Context, ec *ExecutorContext, er *ExecutorResult)
}

Hook sql 执行中间件

type Queryer

type Queryer interface {

	// SelectContext 查询多条数据
	SelectContext(ctx context.Context, dest any, query string, args ...any) error

	// GetContext 查询单条数据
	GetContext(ctx context.Context, dest any, query string, args ...any) error

	// QueryContext 查询多条数据,返回 sql.Rows
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}

Queryer 查询语句执行器

type SQLType

type SQLType string

SQLType sql 类型

const (
	// SELECT 查询语句
	SELECT SQLType = "SELECT"
	// DELETE 删除语句
	DELETE SQLType = "DELETE"
	// UPDATE 更新语句
	UPDATE SQLType = "UPDATE"
	// INSERT 插入语句
	INSERT SQLType = "INSERT"
	// UNKNOWN 未知语句
	UNKNOWN SQLType = "UNKNOWN"
)

func ParseSQLType

func ParseSQLType(query string) SQLType

ParseSQLType 解析 sql 类型

Jump to

Keyboard shortcuts

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