Documentation
¶
Overview ¶
Package sqlx provides base types and helper functions to work with SQL databases.
Index ¶
- Constants
- Variables
- func DataSource(path string, readOnly bool, opts *Options) string
- func ExpandIn[T any](query string, param string, args []T) (string, []any)
- func Select[T any](db Tx, query string, args []any, scan func(rows *sql.Rows) (T, error)) ([]T, error)
- type DB
- type Dialect
- type Options
- type RowScanner
- type Transactor
- type Tx
Constants ¶
const ( Asc = "asc" Desc = "desc" )
Sorting direction.
const ( Sum = "sum" Min = "min" Max = "max" )
Aggregation functions.
Variables ¶
var ErrDialect = errors.New("unknown SQL dialect")
Functions ¶
func DataSource ¶
DataSource returns a connection string for a read-only or read-write mode.
Types ¶
type DB ¶
type DB struct {
Dialect Dialect // database dialect
RW *sql.DB // read-write handle
RO *sql.DB // read-only handle
Timeout time.Duration // transaction timeout
}
DB is a database handle. Has separate connection pools for read-write and read-only operations.
type Dialect ¶
type Dialect string
SQL dialect.
func InferDialect ¶
InferDialect infers the SQL dialect from the driver name.
func (Dialect) ConstraintFailed ¶
ConstraintFailed checks if the error is due to a constraint violation on a column. Error examples:
- sqlite3.Error (NOT NULL constraint failed: rkey.type)
- *pq.Error (pq: null value in column "type" of relation "rkey" violates not-null constraint)
func (Dialect) GlobToLike ¶
GlobToLike creates a like-style pattern from a glob-style pattern. Only supports * and ? wildcards, not [abc] and [!abc]. Escapes % and _ special characters with a backslash.
func (Dialect) TypedError ¶
Returns ErrKeyType if the error is due to a not-null constraint violation on rkey.type. Otherwise, returns the original error.
type Options ¶
type Options struct {
// SQL dialect.
Dialect Dialect
// Options to set on the database connection.
// If nil, uses the engine-specific defaults.
// If the map is empty, no options are set.
Pragma map[string]string
// Timeout for database operations.
Timeout time.Duration
// Whether the database is read-only.
ReadOnly bool
}
Options is the configuration for the database.
type RowScanner ¶
rowScanner is an interface to scan rows.
type Transactor ¶
type Transactor[T any] struct { // contains filtered or unexported fields }
Transactor is a domain transaction manager. T is the type of the domain transaction.
func NewTransactor ¶
func NewTransactor[T any](db *DB, newTx func(Dialect, Tx) T) *Transactor[T]
NewTransactor creates a new transaction manager.
func (*Transactor[T]) Update ¶
func (t *Transactor[T]) Update(f func(tx T) error) error
Update executes a function within a writable transaction.
func (*Transactor[T]) UpdateContext ¶
func (t *Transactor[T]) UpdateContext(ctx context.Context, f func(tx T) error) error
UpdateContext executes a function within a writable transaction.
func (*Transactor[T]) View ¶
func (t *Transactor[T]) View(f func(tx T) error) error
View executes a function within a read-only transaction.
func (*Transactor[T]) ViewContext ¶
func (t *Transactor[T]) ViewContext(ctx context.Context, f func(tx T) error) error
ViewContext executes a function within a read-only transaction.