sqlx

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package sqlx provides base types and helper functions to work with SQL databases.

Index

Constants

View Source
const (
	Asc  = "asc"
	Desc = "desc"
)

Sorting direction.

View Source
const (
	Sum = "sum"
	Min = "min"
	Max = "max"
)

Aggregation functions.

Variables

View Source
var ErrDialect = errors.New("unknown SQL dialect")

Functions

func DataSource

func DataSource(path string, readOnly bool, opts *Options) string

DataSource returns a connection string for a read-only or read-write mode.

func ExpandIn

func ExpandIn[T any](query string, param string, args []T) (string, []any)

ExpandIn expands the IN clause in the query for a given parameter.

func Select

func Select[T any](db Tx, query string, args []any,
	scan func(rows *sql.Rows) (T, error)) ([]T, error)

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.

func New

func New(rw *sql.DB, ro *sql.DB, opts *Options) (*DB, error)

New creates a new database handle. Like Open, but does not create the database schema.

func Open

func Open(rw *sql.DB, ro *sql.DB, opts *Options) (*DB, error)

Open creates a new database handle. Creates the database schema if necessary.

type Dialect

type Dialect string

SQL dialect.

const (
	DialectPostgres Dialect = "postgres"
	DialectSqlite   Dialect = "sqlite"
	DialectUnknown  Dialect = "unknown"
)

func InferDialect

func InferDialect(driverName string) Dialect

InferDialect infers the SQL dialect from the driver name.

func (Dialect) ConstraintFailed

func (d Dialect) ConstraintFailed(err error, constraint, table string, column string) bool

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) Enumerate

func (d Dialect) Enumerate(query string) string

Enumerate replaces ? placeholders with $1, $2, ... $n.

func (Dialect) GlobToLike

func (d Dialect) GlobToLike(pattern string) string

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) LimitAll

func (d Dialect) LimitAll() string

LimitAll returns a SQL query fragment to limit the result to all rows.

func (Dialect) TypedError

func (d Dialect) TypedError(err error) error

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

type RowScanner interface {
	Scan(dest ...any) error
}

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.

type Tx

type Tx interface {
	Query(query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
	Exec(query string, args ...any) (sql.Result, error)
}

Tx is a database transaction (or a transaction-like object). This is an abstraction over sql.Tx and sql.DB.

Jump to

Keyboard shortcuts

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