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

session

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package session binds a named database Session into context for Exec / Query / Tx, and looks it up by session name or model.

Generated model methods call MustFor(ctx, TUser) (or similar) to obtain the Session that should run against that table. Callers create a Session with New / NewReadonly, inject it with With / WithModel / WithSchemaModel, then pass the context down.

This package blank-imports github.com/xoctopus/sqlx/pkg/adaptors so registered drivers are available when opening connections.

Session

a, err := adaptor.Open(ctx, dsn)
s := session.New(a, "main")
// or session.NewReadonly(rw, ro, "main")

ctx = session.With(ctx, s)
// and/or session.WithModel(ctx, &User{}, s)

_, err = session.MustFor(ctx, TUser).Exec(ctx, stmt)
// generated code typically uses:
// session.MustFor(ctx, TUser).Adaptor().Exec(...)

Session exposes Name, T (resolve table), Adaptor (optional ReadOnly), and Exec / Query / Tx on the read-write adaptor. InTx reports whether ctx already carries an active *sql.Tx.

Context lookup

Catalog Register

Register records tables from one or more builder.Catalog values and panics if the same table key is registered twice (TableName, or Schema.TableName). This is separate from `@model Register=Catalog`, which only adds generated tables into a Catalog variable at init.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CarryModel added in v0.2.9

func CarryModel(m builder.Model, s Session) contextx.Carrier

CarryModel returns a Carrier that applies WithModel.

func CarrySchemaModel added in v0.2.9

func CarrySchemaModel(m builder.Model, s Session) contextx.Carrier

CarrySchemaModel returns a Carrier that applies WithSchemaModel.

func InTx added in v0.2.9

func InTx(ctx context.Context) bool

InTx reports whether ctx carries an active *sql.Tx executor.

func Register added in v0.2.2

func Register(cats ...builder.Catalog)

Register records tables from cats and panics if a table key is registered twice. The key is TableName, or Schema.TableName when the table has a schema.

func With added in v0.0.4

func With(ctx context.Context, s Session) context.Context

With binds s into ctx under s.Name().

func WithModel added in v0.2.9

func WithModel(ctx context.Context, m builder.Model, s Session) context.Context

WithModel binds s into ctx under m.TableName().

func WithSchemaModel added in v0.2.9

func WithSchemaModel(ctx context.Context, m builder.Model, s Session) context.Context

WithSchemaModel binds s into ctx under the schema-qualified model key.

Types

type Adaptor added in v0.0.3

type Adaptor = adaptor.Adaptor

func Open

func Open(ctx context.Context, endpoint string) (Adaptor, error)

type AdaptorOption added in v0.1.18

type AdaptorOption struct {
	// ReadOnly selects the read-only adaptor when true.
	ReadOnly bool
}

AdaptorOption configures Adaptor selection.

type AdaptorOptionApplier added in v0.2.1

type AdaptorOptionApplier func(*AdaptorOption)

AdaptorOptionApplier mutates AdaptorOption.

func ReadOnly added in v0.0.3

func ReadOnly() AdaptorOptionApplier

ReadOnly selects the read-only adaptor from NewReadonly.

type Session added in v0.0.3

type Session interface {
	// Name returns the logical session name.
	Name() string
	// T resolves a table from a model, Table, or WithTable value.
	T(any) builder.Table
	// Adaptor returns the underlying adaptor; ReadOnly() selects the RO adaptor when set.
	Adaptor(...AdaptorOptionApplier) adaptor.Adaptor

	// Tx runs fn inside a transaction on the RW adaptor.
	Tx(context.Context, func(context.Context) error) error
	// Exec executes f on the RW adaptor.
	Exec(context.Context, frag.Fragment) (sql.Result, error)
	// Query runs f on the RW adaptor.
	Query(context.Context, frag.Fragment) (*sql.Rows, error)
}

Session is a named database access handle.

func For added in v0.0.3

func For(ctx context.Context, m any) (Session, bool)

For retrieves a Session by session name (string) or model (builder.Model).

func From added in v0.0.4

func From(ctx context.Context, name string) (Session, bool)

From retrieves a Session bound under the given name.

func FromByModel added in v0.2.9

func FromByModel(ctx context.Context, m builder.Model) (Session, bool)

FromByModel retrieves a Session by schema-qualified key, falling back to TableName.

func Must added in v0.2.1

func Must(ctx context.Context, name string) Session

Must is like From but panics when the session is missing.

func MustByModel added in v0.2.9

func MustByModel(ctx context.Context, m builder.Model) Session

MustByModel is like FromByModel but panics when the session is missing.

func MustFor added in v0.2.1

func MustFor(ctx context.Context, m any) Session

MustFor is like For but panics when the session is missing.

func New added in v0.0.3

func New(a adaptor.Adaptor, name string) Session

New creates a Session backed by a single adaptor.

func NewReadonly added in v0.2.1

func NewReadonly(rw adaptor.Adaptor, ro adaptor.Adaptor, name string) Session

NewReadonly creates a Session with separate read-write and read-only adaptors.

Jump to

Keyboard shortcuts

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