Documentation ¶
Index ¶
- Variables
- func DBDurationFrom(ctx context.Context) time.Duration
- func Targets[T any](recs []*Recorder[T]) []*T
- func WithClient(ctx context.Context, c Client) context.Context
- func WithDBDuration(ctx context.Context, start time.Time) context.Context
- type Client
- func ClientFrom(ctx context.Context) Client
- func Delete(ctx context.Context, table string) (Client, sq.DeleteBuilder)
- func Insert(ctx context.Context, table string) (Client, sq.InsertBuilder)
- func Select(ctx context.Context, table string, columns ...string) (Client, sq.SelectBuilder)
- func Update(ctx context.Context, table string) (Client, sq.UpdateBuilder)
- type DBClient
- func (c *DBClient) Begin(ctx context.Context, opts *sql.TxOptions) (*TxnClient, error)
- func (c *DBClient) Builder() *sq.StatementBuilderType
- func (c *DBClient) Close() error
- func (c *DBClient) DB() *sql.DB
- func (c *DBClient) Exec(ctx context.Context, q string, args ...any) (sql.Result, error)
- func (c *DBClient) LogQuery(ctx context.Context, q string, args []any)
- func (c *DBClient) Ping(ctx context.Context) error
- func (c *DBClient) Query(ctx context.Context, q string, args ...any) (*sql.Rows, error)
- func (c *DBClient) QueryRow(ctx context.Context, q string, args ...any) *sql.Row
- func (c *DBClient) SetLogger(l QueryLogger)
- type DeleteFunc
- type QueryLogger
- type Recorder
- func (r *Recorder[T]) Delete(ctx context.Context) error
- func (r *Recorder[T]) Exists(ctx context.Context) (bool, error)
- func (r *Recorder[T]) Insert(ctx context.Context) error
- func (r *Recorder[T]) Load(ctx context.Context) error
- func (r *Recorder[T]) LoadWhere(ctx context.Context, pred any, args ...any) error
- func (r *Recorder[T]) Update(ctx context.Context) error
- func (r *Recorder[T]) UpdatedValues() map[string]any
- func (r *Recorder[T]) Values() map[string]any
- func (r *Recorder[T]) WhereIDs() map[string]any
- type Schemer
- func (s *Schemer[T]) Columns(withKeys bool) []string
- func (s *Schemer[T]) DeleteWhere(ctx context.Context, fn DeleteFunc) (sql.Result, error)
- func (s *Schemer[T]) Exists(ctx context.Context, pred any, args ...any) (bool, error)
- func (s *Schemer[T]) First(ctx context.Context, fn WhereFunc) (*Recorder[T], error)
- func (s *Schemer[T]) InsertColumns() []string
- func (s *Schemer[T]) List(ctx context.Context, limit, offset uint64) ([]*Recorder[T], error)
- func (s *Schemer[T]) ListWhere(ctx context.Context, fn WhereFunc) ([]*Recorder[T], error)
- func (s *Schemer[T]) Record(tgt *T) *Recorder[T]
- func (s *Schemer[T]) Table() string
- type TxnClient
- func (c *TxnClient) Builder() *sq.StatementBuilderType
- func (c *TxnClient) Commit() error
- func (c *TxnClient) Exec(ctx context.Context, q string, args ...any) (sql.Result, error)
- func (c *TxnClient) LogQuery(ctx context.Context, q string, args []any)
- func (c *TxnClient) Query(ctx context.Context, q string, args ...any) (*sql.Rows, error)
- func (c *TxnClient) QueryRow(ctx context.Context, q string, args ...any) *sql.Row
- func (c *TxnClient) Rollback() error
- type WhereFunc
Constants ¶
This section is empty.
Variables ¶
var ErrNoClient = errors.New("no client in context")
Functions ¶
func DBDurationFrom ¶
DBDurationFrom extracts db execution duration from the given context.
func WithClient ¶
WithClient returns a modified variant of the given context with an embedded client.
Types ¶
type Client ¶
type Client interface { Exec(ctx context.Context, q string, args ...any) (sql.Result, error) Query(ctx context.Context, q string, args ...any) (*sql.Rows, error) QueryRow(ctx context.Context, q string, args ...any) *sql.Row Builder() *sq.StatementBuilderType LogQuery(ctx context.Context, q string, args []any) }
Client represents a DB or Transaction client that runs queries with a squirrel builder.
func ClientFrom ¶
ClientFrom fetches the embedded client from the given context.
func Delete ¶
Delete returns the Client with a DeleteBuilder from the given context, or a nil Client if there is none.
func Insert ¶
Insert returns the Client with an InsertBuilder from the given context, or a nil Client if there is none.
type DBClient ¶
type DBClient struct {
// contains filtered or unexported fields
}
DBClient is a wrapper around an *sql.DB instance, a QueryLogger, and a squirrel query builder.
func New ¶
New initiates a new database connection with the given connection string options, returning a DBClient. See database/sql#Open.
func (*DBClient) Builder ¶
func (c *DBClient) Builder() *sq.StatementBuilderType
Builder is the squirrel query builder for this db connection.
func (*DBClient) Close ¶
Close closes the database and prevents new queries from starting. Close then waits for all queries that have started processing on the server to finish.
func (*DBClient) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*DBClient) Query ¶
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*DBClient) QueryRow ¶
QueryRow executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
func (*DBClient) SetLogger ¶
func (c *DBClient) SetLogger(l QueryLogger)
SetLogger sets the given logger, or resetting it to a no-op logger if nil.
type DeleteFunc ¶
type DeleteFunc func(query sq.DeleteBuilder) sq.DeleteBuilder
DeleteFunc modifies a basic delete operation to add conditions.
Technically, conditions are not limited to adding where clauses. It will receive a select statement with the 'SELECT ... FROM tablename' portion composed already.
type QueryLogger ¶
QueryLogger is a wrapper for a type that logs SQL queries.
type Recorder ¶
type Recorder[T any] struct { Schemer *Schemer[T] Target *T // contains filtered or unexported fields }
Recorder records changes to Target of type T using its Schemer.
func (*Recorder[T]) Delete ¶
Delete removes this Recorder's Target from its Schemer's table in the database.
func (*Recorder[T]) Exists ¶
Exists checks if the given Recorder Target exists in the db according to its primary keys.
func (*Recorder[T]) Insert ¶
Insert uses the Recorder's Schemer to insert the Target into the database.
func (*Recorder[T]) Load ¶
Load reloads the Recorder Target's columns (except primary keys) from the database.
func (*Recorder[T]) LoadWhere ¶
LoadWhere loads a single Recorder Target using the given predicate args for a Where clause on the squirrel query builder.
func (*Recorder[T]) Update ¶
Insert uses the Recorder's Schemer to update the Target in the database, skipping if no values were updated since this Recorder was instantiated.
func (*Recorder[T]) UpdatedValues ¶
UpdatedValues returns an updated column/value map since this Recorder was instantiated.
type Schemer ¶
type Schemer[T any] struct { // contains filtered or unexported fields }
Schemer maintains the table and column mapping of the generic type T. It parses the column and primary key info from the struct field tags.
func (*Schemer[T]) Columns ¶
Columns returns the column names for the Schemer's type T, optionally with or without primary key columns. Columns are disambiguated with the table name for join queries.
func (*Schemer[T]) DeleteWhere ¶
DeleteWhere deletes rows filtered by the given DeleteFunc. The context must have a client embedded with WithClient().
func (*Schemer[T]) Exists ¶
Exists checks if any Recorder Target exists using the given predicate args for a Where clause on the squirrel query builder.
func (*Schemer[T]) First ¶
First returns a *Recorder[T] of the first row, filtered by the given WhereFunc. The context must have a client embedded with WithClient().
func (*Schemer[T]) InsertColumns ¶
InsertColumns returns column names for inserts for the Schemer's type T. Column names do not include the table prefix.
func (*Schemer[T]) List ¶
List returns rows of type T embedded in Recorders, using the given limit and offset values. The context must have a client embedded with WithClient().
func (*Schemer[T]) ListWhere ¶
ListWhere returns rows of type T embedded in Recorders, filtered by the given WhereFunc. The context must have a client embedded with WithClient().
func (*Schemer[T]) Record ¶
Record returns a Recorder for the given instance, creating a new one if nil is provided. For updating purposes, the returned recorder's UpdatedValues() do not take the given target's fields into account. Be aware how default values (like "" or 0) will affect your database. Use one of the Load or List funcs before setting properties in order to take advantage of partial updates.
type TxnClient ¶
type TxnClient struct {
// contains filtered or unexported fields
}
DBClient is a wrapper around an *sql.Tx instance, a QueryLogger, and a squirrel query builder.
func WithTransaction ¶
WithTransaction begins a new transaction with a *DBClient in the given context, returning a new context with the *TxnClient.
func (*TxnClient) Builder ¶
func (c *TxnClient) Builder() *sq.StatementBuilderType
Builder is the squirrel query builder for this db connection.
func (*TxnClient) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*TxnClient) Query ¶
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*TxnClient) QueryRow ¶
QueryRow executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
type WhereFunc ¶
type WhereFunc func(query sq.SelectBuilder) sq.SelectBuilder
WhereFunc modifies a basic select operation to add conditions.
Technically, conditions are not limited to adding where clauses. It will receive a select statement with the 'SELECT ... FROM tablename' portion composed already.