Documentation
¶
Index ¶
- Constants
- Variables
- func Exec(ctx context.Context, timeout time.Duration, db Execer, query string, ...) (sql.Result, error)
- func Open(driver, dsn string, options Options) (*sql.DB, error)
- func Query(ctx context.Context, timeout time.Duration, db Queryer, query string, ...) (*sql.Rows, error)
- func QueryRow(ctx context.Context, timeout time.Duration, db QueryRower, query string, ...) (*sql.Row, context.CancelFunc)
- func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
- type Dialect
- type Execer
- type Helper
- func (h Helper) Exec(ctx context.Context, db Execer, query string, args ...any) (sql.Result, error)
- func (h Helper) Query(ctx context.Context, db Queryer, query string, args ...any) (*sql.Rows, error)
- func (h Helper) QueryRow(ctx context.Context, db QueryRower, query string, args ...any) (*sql.Row, context.CancelFunc)
- type InsertBuilder
- type LoggedDB
- type Options
- type Pagination
- type QueryDB
- type QueryHook
- type QueryRower
- type Queryer
- type Repository
- func (r Repository) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (r Repository) Paginate(p Pagination) (int, int)
- func (r Repository) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (r Repository) QueryRow(ctx context.Context, query string, args ...any) (*sql.Row, context.CancelFunc)
- func (r Repository) WithTimeout(timeout time.Duration) Repository
- type SelectBuilder
- func (b SelectBuilder) Build() (string, []any, error)
- func (b SelectBuilder) Dialect(dialect Dialect) SelectBuilder
- func (b SelectBuilder) From(table string) SelectBuilder
- func (b SelectBuilder) Limit(limit int) SelectBuilder
- func (b SelectBuilder) Offset(offset int) SelectBuilder
- func (b SelectBuilder) OrderBy(order string) SelectBuilder
- func (b SelectBuilder) Where(condition string, args ...any) SelectBuilder
- type UpdateBuilder
Constants ¶
const DefaultPageSize = 20
DefaultPageSize is the default page size when none is provided.
const MaxPageSize = 100
MaxPageSize is the default maximum page size.
Variables ¶
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 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 ¶
WithTimeout returns a context with timeout when provided.
Types ¶
type Execer ¶
type Execer interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
Execer runs exec statements with context.
type Helper ¶
Helper wraps query helpers with a default timeout.
type InsertBuilder ¶
type InsertBuilder struct {
// contains filtered or unexported fields
}
InsertBuilder builds INSERT queries.
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 ¶
LoggedDB wraps a database with query hooks.
func WithQueryHook ¶
WithQueryHook wraps a database with a query hook.
func (LoggedDB) ExecContext ¶
ExecContext executes a statement and emits hook timing.
func (LoggedDB) QueryContext ¶
QueryContext executes a 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 ¶
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 ¶
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) Paginate ¶
func (r Repository) Paginate(p Pagination) (int, int)
Paginate returns limit/offset for a pagination config.
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 (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 (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.