Documentation ¶
Overview ¶
Package libdal provides common types and functions for domain-specific DALs.
In particular, common error types and error handling function for all domain-specific DALs, e.g. controller DAL and configuration DAL, which all connect to the same underlying DB and maintain the same interface guarantees
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrConflict is returned by select methods in the DAL when a resource already exists. // // Its use will be documented in the corresponding methods. ErrConflict = errors.New("conflict") // ErrNotFound is returned by select methods in the DAL when no results are found. ErrNotFound = errors.New("not found") // ErrConstraint is returned by select methods in the DAL when a constraint is violated. ErrConstraint = errors.New("constraint violation") )
Functions ¶
func IsNotFound ¶
func TranslatePGError ¶
Types ¶
type Connection ¶
type Connection interface { ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row }
Connection is a common interface for *sql.DB and *sql.Tx.
type Handle ¶
type Handle[T any] struct { Connection Connection Make MakeWithHandle[T] // contains filtered or unexported fields }
Handle is a wrapper around a database connection that can be embedded within a struct to provide access to a database connection and methods for managing transactions.
func New ¶
func New[T any](sql Connection, fn MakeWithHandle[T]) *Handle[T]
New creates a new Handle
func (*Handle[T]) Adopt ¶ added in v0.370.0
func (h *Handle[T]) Adopt(tx Connection) *T
Adopt creates a new Handle with the given transaction.
TODO: This needs to be removed - DALs should not be shared.
func (*Handle[T]) Begin ¶
Begin creates a new transaction or increments the transaction counter if the handle is already in a transaction.
In all cases a new handle is returned.
func (*Handle[T]) CommitOrRollback ¶
CommitOrRollback commits the transaction if err is nil, otherwise rolls it back.
Use it in a defer like so, particularly taking note of named return value `(err error)`. Without this it will not work.
func (d *DAL) SomeMethod() (err error) { tx, err := d.Begin() if err != nil { return err } defer tx.CommitOrRollback(&err) // ... return nil }
type MakeWithHandle ¶
MakeWithHandle is a function that can be used to create a new T with a SQLHandle.