sqliteactor

package
v0.0.0-...-799727d Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoTrace

func DoTrace(ctx context.Context, a *Actor, scope TraceScope, fn func(*sql.DB) error) error

func DoValue

func DoValue[T any](ctx context.Context, a *Actor, fn func(*sql.DB) (T, error)) (T, error)

func DoValueTrace

func DoValueTrace[T any](ctx context.Context, a *Actor, scope TraceScope, fn func(*sql.DB) (T, error)) (T, error)

func SetTraceSink

func SetTraceSink(sink TraceSink)

func TxTrace

func TxTrace(ctx context.Context, a *Actor, scope TraceScope, fn func(*sql.Tx) error) error

func TxValue

func TxValue[T any](ctx context.Context, a *Actor, fn func(*sql.Tx) (T, error)) (T, error)

func TxValueTrace

func TxValueTrace[T any](ctx context.Context, a *Actor, scope TraceScope, fn func(*sql.Tx) (T, error)) (T, error)

func WithTraceScope

func WithTraceScope(scope TraceScope, fn func())

Types

type Actor

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

func New

func New(db *sql.DB) (*Actor, error)

func (*Actor) Close

func (a *Actor) Close() error

func (*Actor) Do

func (a *Actor) Do(ctx context.Context, fn func(*sql.DB) error) error

func (*Actor) Tx

func (a *Actor) Tx(ctx context.Context, fn func(*sql.Tx) error) error

type Opened

type Opened struct {
	// DB 只保留给迁移中的旧访问点使用。
	// 设计说明:
	// - 新代码必须优先走 Actor;
	// - 这轮改造还没把所有 sqlite 访问点一次性迁完,所以先把 owner actor 立起来,
	//   再逐步清掉旧的裸 DB 调用;
	// - 这里显式保留 DB 字段,是为了让迁移期间能编译通过,而不是鼓励继续扩散裸 DB 写法。
	DB *sql.DB

	// Actor 是正式入口。
	// 规则:
	// - 所有新的运行时代码都应该通过 Actor 串行执行;
	// - 不允许把 Rows / Tx / Stmt 带出闭包,否则会重新引入生命周期失控问题。
	Actor *Actor
}

func Open

func Open(path string, debug bool) (*Opened, error)

Open 打开 sqlite 的单 owner actor 入口。 设计规则: - 一个库文件 = 一个 owner = 一个连接 = 一个串行执行入口; - `*sql.DB` 虽然并发安全,但它默认是连接池,这对 sqlite 往往不是正确模型; - 这里强制 `SetMaxOpenConns(1)` / `SetMaxIdleConns(1)`,把物理连接压成 1 条; - 保留 WAL,但 WAL 只改善读写关系,不代表可以随便并发写; - `busy_timeout` / retry 不作为主方案,主方案是结构上避免竞争。

type TraceEvent

type TraceEvent struct {
	TS             string   `json:"ts"`
	Operation      string   `json:"operation"`
	SQL            string   `json:"sql"`
	SQLFingerprint string   `json:"sql_fingerprint"`
	Args           []any    `json:"args,omitempty"`
	ElapsedMS      int64    `json:"elapsed_ms"`
	RowsAffected   int64    `json:"rows_affected"`
	Err            string   `json:"err,omitempty"`
	RoundID        string   `json:"round_id,omitempty"`
	Trigger        string   `json:"trigger,omitempty"`
	Intent         string   `json:"intent,omitempty"`
	Stage          string   `json:"stage,omitempty"`
	CallerChain    []string `json:"caller_chain,omitempty"`
}

TraceEvent 是单条 SQL 的追踪记录。

type TraceScope

type TraceScope struct {
	RoundID     string
	Trigger     string
	Intent      string
	Stage       string
	CallerChain []string
}

TraceScope 是一次数据库调用的关联标签。 设计说明: - 这是纯运行时观测数据,不参与业务语义; - scope 由上层在进入 DB 能力入口前显式注入; - driver 层只负责把当前 goroutine 的 scope 贴到每条 SQL 上。

type TraceSink

type TraceSink interface {
	Handle(TraceEvent)
}

TraceSink 接收 SQL 追踪事件。

Jump to

Keyboard shortcuts

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