sqlx

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: BSD-3-Clause Imports: 8 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 DefaultPragma = map[string]string{
	"journal_mode": "wal",
	"synchronous":  "normal",
	"temp_store":   "memory",
	"mmap_size":    "268435456",
	"foreign_keys": "on",
}

DefaultPragma is a set of default database settings.

Functions

func ConstraintFailed added in v0.4.0

func ConstraintFailed(err error, constraint, column string) bool

ConstraintFailed checks if the error is due to a constraint violation on a column.

func DataSource added in v0.4.0

func DataSource(path string, writable bool) string

DataSource returns an SQLite 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)

func TypedError added in v0.2.0

func TypedError(err error) error

Returns typed errors for some specific cases.

Types

type DB

type DB[T any] struct {
	RW *sql.DB // read-write handle
	RO *sql.DB // read-only handle

	sync.Mutex
	// contains filtered or unexported fields
}

DB is a generic database-backed repository with a domain-specific transaction of type T. Has separate database handles for read-write and read-only operations.

func New

func New[T any](rw *sql.DB, ro *sql.DB, newT func(Tx) T) *DB[T]

newSqlDB creates a new database-backed repository. Like openSQL, but does not create the database schema.

func Open

func Open[T any](rw *sql.DB, ro *sql.DB, newT func(Tx) T, pragma map[string]string) (*DB[T], error)

Open creates a new database-backed repository. Creates the database schema if necessary.

func (*DB[T]) Update

func (d *DB[T]) Update(f func(tx T) error) error

Update executes a function within a writable transaction.

func (*DB[T]) UpdateContext

func (d *DB[T]) UpdateContext(ctx context.Context, f func(tx T) error) error

UpdateContext executes a function within a writable transaction.

func (*DB[T]) View

func (d *DB[T]) View(f func(tx T) error) error

View executes a function within a read-only transaction.

func (*DB[T]) ViewContext

func (d *DB[T]) ViewContext(ctx context.Context, f func(tx T) error) error

ViewContext executes a function within a read-only transaction.

type RowScanner

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

rowScanner is an interface to scan rows.

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

Jump to

Keyboard shortcuts

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