Help us shape the future of Go! Take the 2026 Go Developer Survey

update

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 16 Imported by: 4

README

Transaction-Local Statement Reuse

Ordinary Service.Exec is unchanged. Callers that already own a transaction can opt into bounded UPDATE statement reuse:

err := update.WithStatementRunner(tx, func(run *update.StatementRunner) error {
    for _, row := range rows {
        if _, err := run.Exec(ctx, updater, row); err != nil {
            return err
        }
    }
    return nil
})
// Commit only on success; the transaction remains caller-owned.

The runner retains only the last exact SQL shape, and only for the same service, mapping generation, row type, standard builder and presence-marker mapping. SQL construction, value binding, predicates, hooks, cancellation and affected-row checks still run for every call. Custom builders and nontransactional dialects do not retain statements. Services and their configuration must not be mutated concurrently, just as with ordinary execution.

Call run.Reset() before and after non-UPDATE SQL, DDL, or authored queue barriers. Do not put the runner in application-visible providers or contexts. Concurrent or reentrant calls return ErrRunnerBusy; after the callback returns, escaped calls return ErrRunnerClosed. The runner has no public Close method.

Finalization seals admission, joins accepted work and closes the statement before returning. Callback, execution, and cleanup errors remain discoverable through errors.Is/errors.As, including errors ignored by the callback. Panics propagate after cleanup; when cleanup also fails, RunnerPanicError retains both the original panic value and error causes. The runner never commits or rolls back.

Driver panics are rethrown after runner cleanup, but cannot be made transaction- safe by this API: database/sql may retain an internal transaction lease when a driver panics. Drivers must return errors rather than panic. Hook/callback panics do not have this standard-library limitation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRunnerBusy        = errors.New("update statement runner is busy")
	ErrRunnerClosed      = errors.New("update statement runner is closed")
	ErrRunnerTransaction = errors.New("update statement runner requires its caller-owned transaction")
)

Functions

func ShowSQL added in v0.5.4

func ShowSQL(b bool)

func WithStatementRunner added in v0.26.1

func WithStatementRunner(tx *sql.Tx, callback func(*StatementRunner) error) (err error)

WithStatementRunner seals admission, joins accepted execution and closes the retained statement before returning. Callback/execution/cleanup errors are retained even if the callback ignores an execution error. Panics propagate after cleanup. The caller alone must commit or roll back tx.

Types

type Builder

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

Builder represent update DML builder

func NewBuilder

func NewBuilder(table string, columns []string, identityIndex int, dialect *info.Dialect) (*Builder, error)

NewBuilder return insert builder

func (*Builder) Build

func (b *Builder) Build(record interface{}, options ...option.Option) string

Build builds update statement

type RunnerPanicError added in v0.26.1

type RunnerPanicError struct {
	Value any
	Cause error
}

RunnerPanicError preserves a panic together with failures encountered while joining execution or cleaning up. The original panic remains in Value.

func (*RunnerPanicError) Error added in v0.26.1

func (e *RunnerPanicError) Error() string

func (*RunnerPanicError) Unwrap added in v0.26.1

func (e *RunnerPanicError) Unwrap() []error

type Service

type Service struct {
	*config.Config
	// contains filtered or unexported fields
}

Service represents updater

func New

func New(ctx context.Context, db *sql.DB, tableName string, options ...option.Option) (*Service, error)

New creates an updater

func (*Service) Exec

func (s *Service) Exec(ctx context.Context, any interface{}, options ...option.Option) (int64, error)

type StatementRunner added in v0.26.1

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

StatementRunner reuses at most one exact UPDATE shape within one callback. It must not be copied. It never owns transaction completion.

func (*StatementRunner) Exec added in v0.26.1

func (r *StatementRunner) Exec(ctx context.Context, service *Service, value any, options ...option.Option) (count int64, err error)

Exec preserves ordinary per-row binding, hooks, cancellation and guards. Concurrent/reentrant execution is rejected. Calls after the callback return ErrRunnerClosed. Nontransactional dialects and custom builders use ordinary, unretained statements.

func (*StatementRunner) Reset added in v0.26.1

func (r *StatementRunner) Reset() (err error)

Reset invalidates reuse at a SQL/DDL or queue barrier. It does not complete the transaction and is subject to the same serial admission as Exec.

type Updatable added in v0.5.4

type Updatable interface {
	OnUpdate(ctx context.Context) error
}

Updatable interface to be called on updating data

Jump to

Keyboard shortcuts

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