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 ¶
- With: bind under s.Name()
- WithModel: bind under m.TableName()
- WithSchemaModel: bind under schema.TableName
- From / Must: lookup by name
- FromByModel / MustByModel: lookup by model key, fallback TableName
- For / MustFor: name (string) or builder.Model
- CarryModel / CarrySchemaModel: contextx.Carrier helpers
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 ¶
- func CarryModel(m builder.Model, s Session) contextx.Carrier
- func CarrySchemaModel(m builder.Model, s Session) contextx.Carrier
- func InTx(ctx context.Context) bool
- func Register(cats ...builder.Catalog)
- func With(ctx context.Context, s Session) context.Context
- func WithModel(ctx context.Context, m builder.Model, s Session) context.Context
- func WithSchemaModel(ctx context.Context, m builder.Model, s Session) context.Context
- type Adaptor
- type AdaptorOption
- type AdaptorOptionApplier
- type Session
- func For(ctx context.Context, m any) (Session, bool)
- func From(ctx context.Context, name string) (Session, bool)
- func FromByModel(ctx context.Context, m builder.Model) (Session, bool)
- func Must(ctx context.Context, name string) Session
- func MustByModel(ctx context.Context, m builder.Model) Session
- func MustFor(ctx context.Context, m any) Session
- func New(a adaptor.Adaptor, name string) Session
- func NewReadonly(rw adaptor.Adaptor, ro adaptor.Adaptor, name string) Session
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CarryModel ¶ added in v0.2.9
CarryModel returns a Carrier that applies WithModel.
func CarrySchemaModel ¶ added in v0.2.9
CarrySchemaModel returns a Carrier that applies WithSchemaModel.
func Register ¶ added in v0.2.2
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.
Types ¶
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
For retrieves a Session by session name (string) or model (builder.Model).
func FromByModel ¶ added in v0.2.9
FromByModel retrieves a Session by schema-qualified key, falling back to TableName.
func MustByModel ¶ added in v0.2.9
MustByModel is like FromByModel but panics when the session is missing.