Documentation
¶
Overview ¶
Package sqlx provides base types and helper functions to work with SQL databases.
Index ¶
- Constants
- Variables
- func AdaptPostgresQuery(query string) string
- func ConstraintFailed(err error, constraint, column string) bool
- func ConvertPlaceholders(query string) string
- func DataSource(path string, driver string, writable bool, pragma map[string]string) string
- func ExpandIn[T any](query string, param string, args []T) (string, []any)
- func IsPostgres() bool
- func Select[T any](db Tx, query string, args []any, scan func(rows *sql.Rows) (T, error)) ([]T, error)
- func SetPostgres(postgres bool)
- func TypedError(err error) error
- type DB
- type PostgresTx
- type RowScanner
- type Tx
Constants ¶
const ( DriverSQLite = "sqlite" DriverPostgres = "postgres" )
Driver type constants
const ( Asc = "asc" Desc = "desc" )
Sorting direction.
const ( Sum = "sum" Min = "min" Max = "max" )
Aggregation functions.
Variables ¶
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 ¶
AdaptPostgresQuery adapts a SQLite query to work with PostgreSQL
func ConstraintFailed ¶
ConstraintFailed checks if the error is due to a constraint violation on a column.
func ConvertPlaceholders ¶
func DataSource ¶
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 SetPostgres ¶
func SetPostgres(postgres bool)
SetPostgres sets whether the database is PostgreSQL.
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 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]) UpdateContext ¶
UpdateContext executes a function within a writable transaction.
type PostgresTx ¶
type PostgresTx struct {
// contains filtered or unexported fields
}
PostgresTx wraps a *sql.Tx to adapt queries for PostgreSQL
type RowScanner ¶
rowScanner is an interface to scan rows.