db

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPageSize = 20

DefaultPageSize is the default page size when none is provided.

View Source
const MaxPageSize = 100

MaxPageSize is the default maximum page size.

Variables

View Source
var (
	// ErrMissingTable indicates a missing table name.
	ErrMissingTable = errors.New("table required")
	// ErrMissingColumns indicates a missing column list.
	ErrMissingColumns = errors.New("columns required")
	// ErrMissingValues indicates missing values for insert or update.
	ErrMissingValues = errors.New("values required")
	// ErrMismatchedValues indicates values count mismatch.
	ErrMismatchedValues = errors.New("values count does not match columns")
)

Functions

func Exec

func Exec(ctx context.Context, timeout time.Duration, db Execer, query string, args ...any) (sql.Result, error)

Exec runs an exec statement with timeout.

func Open

func Open(driver, dsn string, options Options) (*sql.DB, error)

Open opens a database and applies options.

func Query

func Query(ctx context.Context, timeout time.Duration, db Queryer, query string, args ...any) (*sql.Rows, error)

Query runs a query with timeout.

func QueryRow

func QueryRow(ctx context.Context, timeout time.Duration, db QueryRower, query string, args ...any) (*sql.Row, context.CancelFunc)

QueryRow runs a row query with timeout.

func WithTimeout

func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

WithTimeout returns a context with timeout when provided.

Types

type Dialect

type Dialect int

Dialect configures SQL placeholder style.

const (
	// DialectQuestion uses ? placeholders.
	DialectQuestion Dialect = iota
	// DialectDollar uses $1 style placeholders.
	DialectDollar
)

type Execer

type Execer interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

Execer runs exec statements with context.

type Helper

type Helper struct {
	Timeout time.Duration
}

Helper wraps query helpers with a default timeout.

func (Helper) Exec

func (h Helper) Exec(ctx context.Context, db Execer, query string, args ...any) (sql.Result, error)

Exec runs an exec statement with timeout.

func (Helper) Query

func (h Helper) Query(ctx context.Context, db Queryer, query string, args ...any) (*sql.Rows, error)

Query runs a query with timeout.

func (Helper) QueryRow

func (h Helper) QueryRow(ctx context.Context, db QueryRower, query string, args ...any) (*sql.Row, context.CancelFunc)

QueryRow runs a row query with timeout.

type InsertBuilder

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

InsertBuilder builds INSERT queries.

func Insert

func Insert(table string) InsertBuilder

Insert starts an INSERT query.

func (InsertBuilder) Build

func (b InsertBuilder) Build() (string, []any, error)

Build returns the SQL string and args.

func (InsertBuilder) Columns

func (b InsertBuilder) Columns(columns ...string) InsertBuilder

Columns sets the column list.

func (InsertBuilder) Dialect

func (b InsertBuilder) Dialect(dialect Dialect) InsertBuilder

Dialect sets the SQL dialect.

func (InsertBuilder) Values

func (b InsertBuilder) Values(values ...any) InsertBuilder

Values sets the insert values.

type LoggedDB

type LoggedDB struct {
	DB   QueryDB
	Hook QueryHook
}

LoggedDB wraps a database with query hooks.

func WithQueryHook

func WithQueryHook(db QueryDB, hook QueryHook) LoggedDB

WithQueryHook wraps a database with a query hook.

func (LoggedDB) ExecContext

func (l LoggedDB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext executes a statement and emits hook timing.

func (LoggedDB) QueryContext

func (l LoggedDB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext executes a query and emits hook timing.

func (LoggedDB) QueryRowContext

func (l LoggedDB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a row query and emits hook timing.

type Options

type Options struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
	PingTimeout     time.Duration
}

Options configures database connection pooling.

type Pagination

type Pagination struct {
	Page    int
	Size    int
	MaxSize int
}

Pagination describes paging configuration.

func (Pagination) LimitOffset

func (p Pagination) LimitOffset() (int, int)

LimitOffset returns SQL limit/offset values.

func (Pagination) Normalize

func (p Pagination) Normalize() Pagination

Normalize applies defaults and bounds.

type QueryDB

type QueryDB interface {
	Execer
	Queryer
	QueryRower
}

QueryDB groups query interfaces for repositories.

type QueryHook

type QueryHook func(ctx context.Context, query string, args []any, duration time.Duration, err error)

QueryHook receives query timing information.

type QueryRower

type QueryRower interface {
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

QueryRower runs row queries with context.

type Queryer

type Queryer interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}

Queryer runs queries with context.

type Repository

type Repository struct {
	DB     QueryDB
	Helper Helper
}

Repository wraps common query helpers and timeouts.

func NewRepository

func NewRepository(db QueryDB, timeout time.Duration) Repository

NewRepository creates a repository with a timeout.

func (Repository) Exec

func (r Repository) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a statement with the repository timeout.

func (Repository) Paginate

func (r Repository) Paginate(p Pagination) (int, int)

Paginate returns limit/offset for a pagination config.

func (Repository) Query

func (r Repository) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query with the repository timeout.

func (Repository) QueryRow

func (r Repository) QueryRow(ctx context.Context, query string, args ...any) (*sql.Row, context.CancelFunc)

QueryRow executes a query row with the repository timeout.

func (Repository) WithTimeout

func (r Repository) WithTimeout(timeout time.Duration) Repository

WithTimeout returns a copy with an updated timeout.

type SelectBuilder

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

SelectBuilder builds SELECT queries.

func Select

func Select(columns ...string) SelectBuilder

Select starts a SELECT query.

func (SelectBuilder) Build

func (b SelectBuilder) Build() (string, []any, error)

Build returns the SQL string and args.

func (SelectBuilder) Dialect

func (b SelectBuilder) Dialect(dialect Dialect) SelectBuilder

Dialect sets the SQL dialect.

func (SelectBuilder) From

func (b SelectBuilder) From(table string) SelectBuilder

From sets the source table.

func (SelectBuilder) Limit

func (b SelectBuilder) Limit(limit int) SelectBuilder

Limit sets the LIMIT clause.

func (SelectBuilder) Offset

func (b SelectBuilder) Offset(offset int) SelectBuilder

Offset sets the OFFSET clause.

func (SelectBuilder) OrderBy

func (b SelectBuilder) OrderBy(order string) SelectBuilder

OrderBy sets the ORDER BY clause.

func (SelectBuilder) Where

func (b SelectBuilder) Where(condition string, args ...any) SelectBuilder

Where adds a WHERE clause.

type UpdateBuilder

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

UpdateBuilder builds UPDATE queries.

func Update

func Update(table string) UpdateBuilder

Update starts an UPDATE query.

func (UpdateBuilder) Build

func (b UpdateBuilder) Build() (string, []any, error)

Build returns the SQL string and args.

func (UpdateBuilder) Dialect

func (b UpdateBuilder) Dialect(dialect Dialect) UpdateBuilder

Dialect sets the SQL dialect.

func (UpdateBuilder) Set

func (b UpdateBuilder) Set(column string, value any) UpdateBuilder

Set adds a column assignment.

func (UpdateBuilder) Where

func (b UpdateBuilder) Where(condition string, args ...any) UpdateBuilder

Where adds a WHERE clause.

Jump to

Keyboard shortcuts

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