database

package
v6.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package database provides interface abstractions for interacting with relational data stores

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDatabaseNotReady indicates the given database is not ready.
	ErrDatabaseNotReady = platformerrors.New("database is not ready yet")
)
View Source
var ErrUserAlreadyExists = platformerrors.New("user already exists")

ErrUserAlreadyExists indicates that a user with that username has already been created.

Functions

func BoolFromNullBool

func BoolFromNullBool(b sql.NullBool) bool

func Float32FromNullString

func Float32FromNullString(s sql.NullString) float32

func Float32FromString

func Float32FromString(s string) float32

func Float32PointerFromNullString

func Float32PointerFromNullString(f sql.NullString) *float32

func Float64PointerFromNullString

func Float64PointerFromNullString(f sql.NullString) *float64

func Int32PointerFromNullInt32

func Int32PointerFromNullInt32(i sql.NullInt32) *int32

func NullBoolFromBool

func NullBoolFromBool(b bool) sql.NullBool

func NullBoolFromBoolPointer

func NullBoolFromBoolPointer(b *bool) sql.NullBool

func NullInt32FromInt32Pointer

func NullInt32FromInt32Pointer(i *int32) sql.NullInt32

func NullInt32FromUint8Pointer

func NullInt32FromUint8Pointer(i *uint8) sql.NullInt32

func NullInt32FromUint16

func NullInt32FromUint16(i uint16) sql.NullInt32

func NullInt32FromUint16Pointer

func NullInt32FromUint16Pointer(i *uint16) sql.NullInt32

func NullInt32FromUint32Pointer

func NullInt32FromUint32Pointer(i *uint32) sql.NullInt32

func NullInt64FromUint32Pointer

func NullInt64FromUint32Pointer(f *uint32) sql.NullInt64

func NullStringFromFloat32

func NullStringFromFloat32(f float32) sql.NullString

func NullStringFromFloat32Pointer

func NullStringFromFloat32Pointer(f *float32) sql.NullString

func NullStringFromFloat64Pointer

func NullStringFromFloat64Pointer(f *float64) sql.NullString

func NullStringFromString

func NullStringFromString(s string) sql.NullString

func NullStringFromStringPointer

func NullStringFromStringPointer(s *string) sql.NullString

func NullTimeFromTime

func NullTimeFromTime(t time.Time) sql.NullTime

func NullTimeFromTimePointer

func NullTimeFromTimePointer(t *time.Time) sql.NullTime

func RunInTransaction

func RunInTransaction(
	ctx context.Context,
	writeDB *sql.DB,
	rollback func(ctx context.Context, tx SQLQueryExecutorAndTransactionManager),
	fn func(tx SQLQueryExecutorAndTransactionManager) error,
) error

RunInTransaction begins a transaction on writeDB, invokes fn with that transaction as the sole query executor, and commits when fn returns nil. It is the shared engine behind each Client's WithTransaction method — application code should prefer Client.WithTransaction, which wraps this with the implementation's observability.

The transaction handle is the only executor fn receives, so statements cannot accidentally target the read replica or another connection. Lifecycle is managed for the caller:

  • rollback is invoked (with the transaction) on any non-nil error from fn, and the error is returned unwrapped.
  • a panic inside fn triggers rollback and is then re-raised, so no connection leaks and the caller still observes the failure.
  • a nil return from fn commits; commit errors are wrapped and returned.

A failed commit has already released the connection back to the pool, so no second rollback is attempted (it would only surface a spurious ErrTxDone).

func StringFromFloat32

func StringFromFloat32(f float32) string

func StringFromFloat64

func StringFromFloat64(f float64) string

func StringFromNullString

func StringFromNullString(nt sql.NullString) string

func StringPointerFromNullString

func StringPointerFromNullString(nt sql.NullString) *string

func TimeFromNullTime

func TimeFromNullTime(nt sql.NullTime) time.Time

func TimePointerFromNullTime

func TimePointerFromNullTime(nt sql.NullTime) *time.Time

func Uint16PointerFromNullInt32

func Uint16PointerFromNullInt32(f sql.NullInt32) *uint16

func Uint32PointerFromNullInt32

func Uint32PointerFromNullInt32(f sql.NullInt32) *uint32

func Uint32PointerFromNullInt64

func Uint32PointerFromNullInt64(f sql.NullInt64) *uint32

Types

type Client

type Client interface {
	// Reader returns an executor for the read database. It exposes no transaction
	// control by design; use WithTransaction for anything transactional.
	Reader() SQLQueryExecutor
	// Writer returns an executor for the write database, for single, non-transactional
	// statements. Multi-statement work belongs in WithTransaction.
	Writer() SQLQueryExecutor
	// WithTransaction begins a transaction on the write database, invokes fn with it as
	// the sole executor, commits on a nil return, and rolls back on error or panic.
	WithTransaction(ctx context.Context, fn func(tx SQLQueryExecutorAndTransactionManager) error) error
	Close() error
	CurrentTime() time.Time
}

Client is the safe surface for database access. It deliberately does not expose a raw *sql.DB: reads and single-statement writes go through the narrow executors returned by Reader and Writer (which cannot begin a transaction), and all transactional work goes through WithTransaction. A transaction is therefore unreachable except via WithTransaction, so statements cannot accidentally run outside a transaction or against the read replica.

Callers that genuinely need the concrete pool (migrations, session-pinned advisory locks, driver features off this seam) can obtain it via the RawAccess capability.

type ClientConfig

type ClientConfig interface {
	GetReadConnectionString() string
	GetWriteConnectionString() string
	GetMaxPingAttempts() uint64
	GetPingWaitPeriod() time.Duration
	GetMaxIdleConns() int
	GetMaxOpenConns() int
	GetConnMaxLifetime() time.Duration
}

ClientConfig provides the configuration needed by database clients. This interface allows the config package to provide configuration without creating an import cycle.

type Manager

type Manager interface {
	CreateUser(ctx context.Context, username, password string) error
	DeleteUser(ctx context.Context, username string) error

	CreateDatabase(ctx context.Context, dbName, owner string) error
	DeleteDatabase(ctx context.Context, dbName string) error

	UserExists(ctx context.Context, username string) (bool, error)
	DatabaseExists(ctx context.Context, dbName string) (bool, error)

	GrantUserAccessToTable(ctx context.Context, username, schema, table, privilege string) error
	UserCanAccessDatabase(ctx context.Context, username, dbName string) (bool, error)
}

type Migrator

type Migrator interface {
	Migrate(ctx context.Context, db *sql.DB) error
}

Migrator is an interface for running database migrations. Implementations handle the specifics of migration execution (e.g., darwin, goose, etc.)

type RawAccess

type RawAccess interface {
	ReadDB() *sql.DB
	WriteDB() *sql.DB
}

RawAccess is an optional capability exposing the concrete *sql.DB pools for callers that genuinely need them — schema migrations, session-pinned advisory locks, or driver features outside the executor seam. A caller obtains it by asserting on a Client:

raw, ok := client.(database.RawAccess)

Reaching for RawAccess is a deliberate step outside the safe Client surface; prefer Reader, Writer, and WithTransaction wherever they suffice.

type ResultIterator

type ResultIterator interface {
	Next() bool
	Err() error
	Scanner
	io.Closer
}

ResultIterator represents any iterable database response (i.e. sql.Rows).

type SQLQueryExecutor

type SQLQueryExecutor interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

SQLQueryExecutor is a subset interface for sql.{DB|Tx} objects.

type SQLQueryExecutorAndTransactionManager

type SQLQueryExecutorAndTransactionManager interface {
	SQLQueryExecutor
	SQLTransactionManager
}

SQLQueryExecutorAndTransactionManager is a subset interface for sql.{DB|Tx} objects.

type SQLTransactionManager

type SQLTransactionManager interface {
	Rollback() error
}

SQLTransactionManager is a subset interface for sql.{DB|Tx} objects.

type Scanner

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

Scanner represents any database response (i.e. sql.Row[s]).

Directories

Path Synopsis
Package mockdatabase provides moq-generated mocks for the database package.
Package mockdatabase provides moq-generated mocks for the database package.
Package mysql provides an interface for writing to a MySQL instance.
Package mysql provides an interface for writing to a MySQL instance.
Package postgres provides an interface for writing to a Postgres instance.
Package postgres provides an interface for writing to a Postgres instance.
Package sqlite provides an interface for writing to a SQLite database.
Package sqlite provides an interface for writing to a SQLite database.

Jump to

Keyboard shortcuts

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