Documentation
¶
Overview ¶
Package events 数据库层事件包: 查询与模型生命周期事件对象、分发器接口与默认总线实现。
Index ¶
- Constants
- type ConnectionEstablishedEvent
- type Dispatcher
- type Event
- type EventBus
- func (b *EventBus) Dispatch(e Event)
- func (b *EventBus) DispatchUntil(e Event) bool
- func (b *EventBus) Listen(name string, l Listener)
- func (b *EventBus) ListenUntil(name string, l ListenerUntil)
- func (b *EventBus) ListenUntilWithPriority(name string, l ListenerUntil, priority int)
- func (b *EventBus) ListenWithPriority(name string, l Listener, priority int)
- func (b *EventBus) Once(name string, l Listener)
- type Listener
- type ListenerUntil
- type ModelEvent
- type QueryExecuted
- type StatementPreparedEvent
- type TransactionEvent
Constants ¶
const ( StatementPrepared = "statementPrepared" TransactionBeginning = "transactionBeginning" TransactionCommitting = "transactionCommitting" TransactionCommitted = "transactionCommitted" TransactionRolledBack = "transactionRolledBack" ConnectionEstablished = "connectionEstablished" )
生命周期事件名。
const ( Retrieving = "retrieving" Retrieved = "retrieved" Creating = "creating" Created = "created" Updating = "updating" Updated = "updated" Saving = "saving" Saved = "saved" Deleting = "deleting" Deleted = "deleted" Trashed = "trashed" Restoring = "restoring" Restored = "restored" Replicating = "replicating" ForceDeleting = "forceDeleting" ForceDeleted = "forceDeleted" )
模型生命周期事件名(与观察者钩子同名)。
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConnectionEstablishedEvent ¶
type ConnectionEstablishedEvent struct {
ConnectionName string
}
ConnectionEstablishedEvent 首次建连事件(对应 ConnectionEstablished)。
func (ConnectionEstablishedEvent) EventName ¶
func (e ConnectionEstablishedEvent) EventName() string
type EventBus ¶
type EventBus struct {
// contains filtered or unexported fields
}
EventBus 默认分发器实现:按事件名注册监听器。在既有精确名注册之上支持:
- 通配监听:Listen 的 name 形如 "event.*"(尾部 "*"),分发时精确匹配 在前,其后按通配前缀最长优先;
- 优先级:ListenWithPriority 数字小先执行,同优先级按注册序(稳定);
- 可中断传播:ListenerUntil + DispatchUntil;
- 一次性监听:Once。
并发安全(沿用 RWMutex 风格)。
func NewEventBus ¶
func NewEventBus() *EventBus
func (*EventBus) Dispatch ¶
Dispatch 分发事件(监听器同步执行:精确匹配在前按优先级/注册序,其后 命中的通配监听按前缀最长优先)。ListenerUntil 监听器在此忽略返回值。
func (*EventBus) DispatchUntil ¶
DispatchUntil 分发事件直至某监听器中断:ListenerUntil 返回 false 时立即 停止传播并返回 false(until/halt 语义);全部执行完毕 返回 true。普通监听器(无返回值)视为继续传播。
func (*EventBus) ListenUntil ¶
func (b *EventBus) ListenUntil(name string, l ListenerUntil)
ListenUntil 注册可中断监听器(配合 DispatchUntil:返回 false 停止传播)。
func (*EventBus) ListenUntilWithPriority ¶
func (b *EventBus) ListenUntilWithPriority(name string, l ListenerUntil, priority int)
ListenUntilWithPriority 同 ListenWithPriority,但监听器可中断传播 (配合 DispatchUntil)。
func (*EventBus) ListenWithPriority ¶
ListenWithPriority 带优先级注册监听器:数字小先执行,同优先级按注册序 (稳定);通配模式监听同样适用。
type ListenerUntil ¶
ListenerUntil 可中断监听器:返回 false 时停止后续传播(halt 语义,配合 DispatchUntil 使用;返回 true 继续传播)。
type ModelEvent ¶
ModelEvent 模型生命周期事件对象。
type QueryExecuted ¶
type QueryExecuted struct {
Sql string
Bindings []any
TimeNs int64
RawSql string // 绑定插值后的 SQL(对应 toRawSql)
ConnectionName string // 连接标识(对应 $connectionName)
ReadWriteType string // "read" / "write",读写分离时区分(对应 $readWriteType)
}
QueryExecuted 携带一条已执行查询的信息。
type StatementPreparedEvent ¶
StatementPreparedEvent 预处理完成事件(对应 StatementPrepared,携带语句与 连接名;不携带 PDOStatement,Go 侧无对应句柄)。
func (StatementPreparedEvent) EventName ¶
func (e StatementPreparedEvent) EventName() string
type TransactionEvent ¶
TransactionEvent 事务生命周期事件(对应 TransactionBeginning/Committing/ Committed/RolledBack,均携带连接名与事务层数)。
func (TransactionEvent) EventName ¶
func (e TransactionEvent) EventName() string