gormexec

package module
v0.0.0-...-591bdfc Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithTx

func ContextWithTx(ctx context.Context, tx *gorm.DB) context.Context

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.

func TxFromContext

func TxFromContext(ctx context.Context) (*gorm.DB, bool)

TxFromContext returns the transaction active on a context, if any.

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

func New(db *gorm.DB, dialect sqlkit.Dialect) (*Executor, error)

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) DB

func (e *Executor) DB() *gorm.DB

DB returns the underlying handle.

func (*Executor) Dialect

func (e *Executor) Dialect() sqlkit.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

func (e *Executor) Do(ctx context.Context, fn func(ctx context.Context) error) error

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

func (e *Executor) Exec(ctx context.Context, statement sqlkit.Statement) (int64, error)

Exec implements sqlkit.Executor.

func (*Executor) ExecStatement

func (e *Executor) ExecStatement(ctx context.Context, sql string, args ...any) error

ExecStatement implements sqlkit.Execer.

func (*Executor) InTransaction

func (e *Executor) InTransaction(ctx context.Context) bool

InTransaction implements sqlkit.Executor.

func (*Executor) Query

func (e *Executor) Query(ctx context.Context, statement sqlkit.Statement, scan func(rows sqlkit.Rows) error) error

Query implements sqlkit.Executor.

func (*Executor) QueryStatement

func (e *Executor) QueryStatement(ctx context.Context, sql string, args ...any) (sqlkit.Rows, error)

QueryStatement implements sqlkit.Querier.

Jump to

Keyboard shortcuts

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