sqlx

package
v0.0.0-...-85961ca Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	DriverSQLite   = "sqlite"
	DriverPostgres = "postgres"
)

Driver type 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 AdaptPostgresQuery

func AdaptPostgresQuery(query string) string

AdaptPostgresQuery adapts a SQLite query to work with PostgreSQL

func ConstraintFailed

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

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

func ConvertPlaceholders

func ConvertPlaceholders(query string) string

func DataSource

func DataSource(path string, driver string, writable bool, pragma map[string]string) string

DataSource returns a database connection string. For SQLite, it returns a connection string for a read-only or read-write mode. For PostgreSQL, it returns the connection string as-is.

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 IsPostgres

func IsPostgres() bool

IsPostgres returns whether the database is PostgreSQL.

func Select

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

func SetPostgres

func SetPostgres(postgres bool)

SetPostgres sets whether the database is PostgreSQL.

func TypedError

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
	Driver string  // database driver type

	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, driver string) *DB[T]

New creates a new database instance.

func Open

func Open[T any](rw *sql.DB, ro *sql.DB, newT func(Tx) T, driver string, 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 PostgresTx

type PostgresTx struct {
	// contains filtered or unexported fields
}

PostgresTx wraps a *sql.Tx to adapt queries for PostgreSQL

func (*PostgresTx) Exec

func (ptx *PostgresTx) Exec(query string, args ...any) (sql.Result, error)

Exec adapts and executes a query for PostgreSQL

func (*PostgresTx) Query

func (ptx *PostgresTx) Query(query string, args ...any) (*sql.Rows, error)

Query adapts and executes a query for PostgreSQL

func (*PostgresTx) QueryRow

func (ptx *PostgresTx) QueryRow(query string, args ...any) *sql.Row

QueryRow adapts and executes a query for PostgreSQL

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