Documentation
¶
Overview ¶
Package gormexec is the sqlkit.Executor over GORM.
Its job is transaction participation, not object mapping: it runs raw statements through the host's *gorm.DB, so that writes made through sqlkit and writes made through GORM can commit together. It declares no models and never calls AutoMigrate.
Two decisions differ from GORM's defaults. Statements carry GORM's own "?" bind marker on every database, because GORM rewrites it for the dialector; Executor.Dialect reports that, so a store cannot build a statement with the wrong marker. And nested scopes flatten into one transaction rather than taking the SAVEPOINT db.Transaction would, so an inner failure aborts the whole scope exactly as it does on every other executor.
Index ¶
- func ContextWithTx(ctx context.Context, tx *gorm.DB) context.Context
- func TxFromContext(ctx context.Context) (*gorm.DB, bool)
- type Executor
- func (e *Executor) DB() *gorm.DB
- func (e *Executor) Dialect() sqlkit.Dialect
- func (e *Executor) Do(ctx context.Context, fn func(ctx context.Context) error) error
- func (e *Executor) Exec(ctx context.Context, statement sqlkit.Statement) (int64, error)
- func (e *Executor) ExecStatement(ctx context.Context, sql string, args ...any) error
- func (e *Executor) InTransaction(ctx context.Context) bool
- func (e *Executor) Query(ctx context.Context, statement sqlkit.Statement, ...) error
- func (e *Executor) QueryStatement(ctx context.Context, sql string, args ...any) (sqlkit.Rows, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithTx ¶
ContextWithTx returns a context carrying an open GORM transaction, so that a caller that began one itself can have the executor join it. The executor uses this transaction and neither commits nor rolls it back.
The key is this package's own, so a transaction a task store carries is not visible here. A host that wants one transaction across both reads it from the store's context and passes it here.
Types ¶
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor runs raw statements through a *gorm.DB, or through the GORM transaction a context carries.
func New ¶
New returns an executor over a GORM handle and the dialect of the database behind it. A nil handle or dialect is a configuration error.
func (*Executor) Dialect ¶
Dialect implements sqlkit.Executor. It is the dialect given to New with GORM's "?" bind marker, so a statement built with it binds correctly on every database.
func (*Executor) Do ¶
Do implements sqlkit.Executor.
This is the executor's one real decision, and it is a decision to refuse GORM's. db.Transaction nests with a SAVEPOINT, so an inner scope that fails rolls back to the savepoint and leaves the outer transaction alive — a caller that catches the inner error and carries on then commits half a change. Every other executor flattens, and so does this one: it begins a transaction itself and joins every nested call into it. db.Transaction is never called.
func (*Executor) Exec ¶
Exec implements sqlkit.Executor.
func (*Executor) ExecStatement ¶
ExecStatement implements sqlkit.Execer.
func (*Executor) InTransaction ¶
InTransaction implements sqlkit.Executor.