Documentation ¶
Index ¶
- Variables
- func All[T any](ctx context.Context, exec Executor, q Query, m scan.Mapper[T]) ([]T, error)
- func Allx[T any, Ts ~[]T](ctx context.Context, exec Executor, q Query, m scan.Mapper[T]) (Ts, error)
- func Build(ctx context.Context, q Query) (string, []any, error)
- func BuildN(ctx context.Context, q Query, start int) (string, []any, error)
- func Cursor[T any](ctx context.Context, exec Executor, q Query, m scan.Mapper[T]) (scan.ICursor[T], error)
- func Exec(ctx context.Context, exec Executor, q Query) (sql.Result, error)
- func Express(ctx context.Context, w io.Writer, d Dialect, start int, e any) ([]any, error)
- func ExpressIf(ctx context.Context, w io.Writer, d Dialect, start int, e any, cond bool, ...) ([]any, error)
- func ExpressSlice[T any](ctx context.Context, w io.Writer, d Dialect, start int, expressions []T, ...) ([]any, error)
- func MustBuild(ctx context.Context, q Query) (string, []any)
- func MustBuildN(ctx context.Context, q Query, start int) (string, []any)
- func New[T StdInterface](wrapped T) common[T]
- func NewQueryer[T stdscan.Queryer](wrapped T) scan.Queryer
- func One[T any](ctx context.Context, exec Executor, q Query, m scan.Mapper[T]) (T, error)
- func SkipHooks(ctx context.Context) context.Context
- func SkipModelHooks(ctx context.Context) context.Context
- func SkipQueryHooks(ctx context.Context) context.Context
- type BaseQuery
- func (b BaseQuery[E]) Apply(mods ...Mod[E])
- func (q BaseQuery[E]) Build(ctx context.Context) (string, []any, error)
- func (q BaseQuery[E]) BuildN(ctx context.Context, start int) (string, []any, error)
- func (q BaseQuery[E]) Cache(ctx context.Context, exec Executor) (BaseQuery[*cached], error)
- func (q BaseQuery[E]) CacheN(ctx context.Context, exec Executor, start int) (BaseQuery[*cached], error)
- func (b BaseQuery[E]) Clone() BaseQuery[E]
- func (b BaseQuery[E]) Exec(ctx context.Context, exec Executor) (sql.Result, error)
- func (b BaseQuery[E]) GetLoaders() []Loader
- func (b BaseQuery[E]) GetMapperMods() []scan.MapperMod
- func (q BaseQuery[E]) MustBuild(ctx context.Context) (string, []any)
- func (q BaseQuery[E]) MustBuildN(ctx context.Context, start int) (string, []any)
- func (b BaseQuery[E]) RunHooks(ctx context.Context, exec Executor) (context.Context, error)
- func (b BaseQuery[E]) Type() QueryType
- func (b BaseQuery[E]) WriteQuery(ctx context.Context, w io.Writer, start int) ([]any, error)
- func (b BaseQuery[E]) WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error)
- type BoundQuery
- func (b BoundQuery[E]) Exec(ctx context.Context, exec Executor) (sql.Result, error)
- func (b BoundQuery[E]) GetLoaders() []Loader
- func (b BoundQuery[E]) GetMapperMods() []scan.MapperMod
- func (b BoundQuery[E]) RunHooks(ctx context.Context, exec Executor) (context.Context, error)
- func (b BoundQuery[Arg]) WriteQuery(ctx context.Context, w io.Writer, start int) ([]any, error)
- func (b BoundQuery[E]) WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error)
- type Conn
- func (c Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
- func (c Conn) Close() error
- func (q Conn) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (c Conn) PingContext(ctx context.Context) error
- func (c Conn) PrepareContext(ctx context.Context, query string) (StdPrepared, error)
- type ContextualMod
- type ContextualModFunc
- type ContextualModdable
- type DB
- func (d DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
- func (d DB) Close() error
- func (q DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (d DB) PingContext(ctx context.Context) error
- func (c DB) PrepareContext(ctx context.Context, query string) (StdPrepared, error)
- type DebugPrinter
- type Dialect
- type DialectWithNamed
- type EmbeddedHook
- type Executor
- type Expression
- type ExpressionFunc
- type Hook
- type HookableQuery
- type HookableType
- type Hooks
- type Load
- type Loadable
- type Loader
- type LoaderFunc
- type MapperModder
- type MissingArgError
- type Mod
- type ModFunc
- type Mods
- type PreparedExecutor
- type Preparer
- type Query
- type QueryStmt
- func PrepareQuery[Arg any, P PreparedExecutor, T any](ctx context.Context, exec Preparer[P], q Query, m scan.Mapper[T]) (QueryStmt[Arg, T, []T], error)
- func PrepareQueryx[Arg any, P PreparedExecutor, T any, Ts ~[]T](ctx context.Context, exec Preparer[P], q Query, m scan.Mapper[T]) (QueryStmt[Arg, T, Ts], error)
- type QueryType
- type RawNamedArgError
- type SkipContextualModsKey
- type SkipModelHooksKey
- type SkipQueryHooksKey
- type StdInterface
- type StdPrepared
- type Stmt
- type Tx
- func (t Tx) Commit() error
- func (q Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (c Tx) PrepareContext(ctx context.Context, query string) (StdPrepared, error)
- func (t Tx) Rollback() error
- func (tx *Tx) StmtContext(ctx context.Context, stmt StdPrepared) StdPrepared
- type WrongStartError
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadArgType = errors.New("bind type of multiple named args must be a struct, pointer to struct or map with ~string keys") ErrTooManyNamedArgs = errors.New("too many named args for single arg binder") )
var ErrNoNamedArgs = errors.New("Dialect does not support named arguments")
Functions ¶
func Allx ¶
func Allx[T any, Ts ~[]T](ctx context.Context, exec Executor, q Query, m scan.Mapper[T]) (Ts, error)
Allx takes 2 type parameters. The second is a special return type of the returned slice this is especially useful for when the the Query is Loadable and the loader depends on the return value implementing an interface
func Cursor ¶
func Cursor[T any](ctx context.Context, exec Executor, q Query, m scan.Mapper[T]) (scan.ICursor[T], error)
Cursor returns a cursor that works similar to *sql.Rows
func ExpressIf ¶
func ExpressIf(ctx context.Context, w io.Writer, d Dialect, start int, e any, cond bool, prefix, suffix string) ([]any, error)
ExpressIf expands an express if the condition evaluates to true it can also add a prefix and suffix
func ExpressSlice ¶
func ExpressSlice[T any](ctx context.Context, w io.Writer, d Dialect, start int, expressions []T, prefix, sep, suffix string) ([]any, error)
ExpressSlice is used to express a slice of expressions along with a prefix and suffix
func MustBuild ¶
MustBuild builds a query and panics on error useful for initializing queries that need to be reused
func New ¶
func New[T StdInterface](wrapped T) common[T]
New wraps an stdInterface to make it comply with Queryer It also includes a number of other methods that are often used with *sql.DB, *sql.Tx and *sql.Conn
func NewQueryer ¶
NewQueryer wraps an stdscan.Queryer and makes it a scan.Queryer
func SkipHooks ¶ added in v0.29.0
SkipHooks modifies a context to prevent hooks from running for any query it encounters.
func SkipModelHooks ¶ added in v0.29.0
SkipModelHooks modifies a context to prevent hooks from running on models.
Types ¶
type BaseQuery ¶
type BaseQuery[E Expression] struct { Expression E Dialect Dialect QueryType QueryType }
BaseQuery wraps common functionality such as cloning, applying new mods and the actual query interface implementation
func (BaseQuery[E]) CacheN ¶ added in v0.23.0
func (q BaseQuery[E]) CacheN(ctx context.Context, exec Executor, start int) (BaseQuery[*cached], error)
Convinient function to cache a query from a point
func (BaseQuery[E]) GetLoaders ¶
func (BaseQuery[E]) GetMapperMods ¶
func (BaseQuery[E]) MustBuild ¶
MustBuild builds the query and panics on error useful for initializing queries that need to be reused
func (BaseQuery[E]) MustBuildN ¶
MustBuildN builds the query and panics on error start numbers the arguments from a different point
func (BaseQuery[E]) WriteQuery ¶
type BoundQuery ¶ added in v0.29.0
func BindNamed ¶ added in v0.29.0
func BindNamed[Arg any](ctx context.Context, q Query, args Arg) BoundQuery[Arg]
func (BoundQuery[E]) GetLoaders ¶ added in v0.29.0
func (b BoundQuery[E]) GetLoaders() []Loader
func (BoundQuery[E]) GetMapperMods ¶ added in v0.29.0
func (b BoundQuery[E]) GetMapperMods() []scan.MapperMod
func (BoundQuery[Arg]) WriteQuery ¶ added in v0.29.0
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is similar to *sql.Conn but implements [Queryer]
func NewConn ¶
NewConn wraps an *sql.Conn and returns a type that implements [Queryer] This is useful when an existing *sql.Conn is used in other places in the codebase
func (Conn) BeginTx ¶ added in v0.13.0
BeginTx is similar to *sql.Conn.BeginTx, but return a transaction that implements [Queryer]
func (Conn) Close ¶ added in v0.13.0
Close works the same as *sql.Conn.Close
func (Conn) ExecContext ¶
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (Conn) PingContext ¶ added in v0.13.0
PingContext verifies a connection to the database is still alive, establishing a connection if necessary.
func (Conn) PrepareContext ¶
func (c Conn) PrepareContext(ctx context.Context, query string) (StdPrepared, error)
PrepareContext creates a prepared statement for later queries or executions
type ContextualMod ¶ added in v0.29.0
ContextualMods are special types of mods that require a context. they are only applied at the point of building the query where possible, prefer using regular mods since they are applied once while contextual mods are applied every time a query is built
type ContextualModFunc ¶ added in v0.29.0
type ContextualModdable ¶ added in v0.29.0
type ContextualModdable[T any] struct { Mods []ContextualMod[T] }
func (*ContextualModdable[T]) AppendContextualMod ¶ added in v0.29.0
func (h *ContextualModdable[T]) AppendContextualMod(mods ...ContextualMod[T])
AppendContextualMod a hook to the set
func (*ContextualModdable[T]) AppendContextualModFunc ¶ added in v0.29.0
func (h *ContextualModdable[T]) AppendContextualModFunc(f func(context.Context, T) (context.Context, error))
AppendContextualMod a hook to the set
func (*ContextualModdable[T]) RunContextualMods ¶ added in v0.29.0
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is similar to *sql.DB but implement [Queryer]
func NewDB ¶
NewDB wraps an *sql.DB and returns a type that implements [Queryer] but still retains the expected methods used by *sql.DB This is useful when an existing *sql.DB is used in other places in the codebase
func OpenDB ¶
OpenDB works just like sql.OpenDB, but converts the returned *sql.DB to DB
func (DB) BeginTx ¶
BeginTx is similar to *sql.DB.BeginTx, but return a transaction that implements [Queryer]
func (DB) ExecContext ¶
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (DB) PingContext ¶ added in v0.13.0
PingContext verifies a connection to the database is still alive, establishing a connection if necessary.
func (DB) PrepareContext ¶
func (c DB) PrepareContext(ctx context.Context, query string) (StdPrepared, error)
PrepareContext creates a prepared statement for later queries or executions
type DebugPrinter ¶ added in v0.20.0
DebugPrinter is used to print queries and arguments
type Dialect ¶
type Dialect interface { // WriteArg should write an argument placeholder to the writer with the given index WriteArg(w io.Writer, position int) // WriteQuoted writes the given string to the writer surrounded by the appropriate // quotes for the dialect WriteQuoted(w io.Writer, s string) }
Dialect provides expressions with methods to write parts of the query
type DialectWithNamed ¶
type DialectWithNamed interface { Dialect // WriteNamedArg should write an argument placeholder to the writer with the given name WriteNamedArg(w io.Writer, name string) }
DialectWithNamed is a Dialect with the additional ability to WriteNamedArgs
type EmbeddedHook ¶ added in v0.29.0
func (*EmbeddedHook) AppendHooks ¶ added in v0.29.0
type Executor ¶ added in v0.3.1
type Executor interface { scan.Queryer ExecContext(context.Context, string, ...any) (sql.Result, error) }
func DebugToPrinter ¶ added in v0.20.0
func DebugToPrinter(exec Executor, w DebugPrinter) Executor
DebugToPrinter wraps an existing Executor and writes all queries and args to the given DebugPrinter if w is nil, it fallsback to writing to os.Stdout
type Expression ¶
type Expression interface { // Writes the textual representation of the expression to the writer // using the given dialect. // start is the beginning index of the args if it needs to write any WriteSQL(ctx context.Context, w io.Writer, d Dialect, start int) (args []any, err error) }
Expression represents a section of a query
func Named ¶ added in v0.29.0
func Named(names ...string) Expression
Named args should ONLY be used to prepare statements
func NamedGroup ¶ added in v0.29.0
func NamedGroup(names ...string) Expression
NamedGroup is like Named, but wraps in parentheses
type ExpressionFunc ¶
type Hook ¶ added in v0.29.0
Hook is a function that can be called during lifecycle of an object the context can be modified and returned The caller is expected to use the returned context for subsequent processing
type HookableQuery ¶ added in v0.29.0
type HookableType ¶ added in v0.29.0
If a type implements this interface, it will be called after the query has been executed and it is scanned
type Hooks ¶ added in v0.29.0
Hooks is a set of hooks that can be called all at once
func (*Hooks[T, K]) AppendHooks ¶ added in v0.29.0
AppendHooks a hook to the set
type Load ¶ added in v0.13.0
type Load struct {
// contains filtered or unexported fields
}
Load is an embeddable struct that enables Preloading and AfterLoading
func (*Load) AppendLoader ¶ added in v0.13.0
AppendLoader add to the query's loaders
func (*Load) AppendMapperMod ¶ added in v0.13.0
AppendMapperMod adds to the query's mapper mods
func (*Load) GetLoaders ¶ added in v0.13.0
GetLoaders implements the Loadable interface
func (*Load) GetMapperMods ¶ added in v0.13.0
GetMapperMods implements the MapperModder interface
func (*Load) SetLoaders ¶ added in v0.27.0
SetLoaders sets the query's loaders
func (*Load) SetMapperMods ¶ added in v0.27.0
type Loadable ¶
type Loadable interface {
GetLoaders() []Loader
}
Loadable is an object that has loaders if a query implements this interface, the loaders are called after executing the query
type Loader ¶ added in v0.13.0
Loader is an object that is called after the main query is performed when called from Exec, retrieved is nil when called from One, retrieved is the retrieved object when called from All, retrieved is a slice retrieved objects this is used for loading relationships
type LoaderFunc ¶ added in v0.29.0
Loader builds a query mod that makes an extra query after the object is retrieved it can be used to prevent N+1 queries by loading relationships in batches
type MapperModder ¶
type MissingArgError ¶ added in v0.29.0
type MissingArgError struct{ Name string }
func (MissingArgError) Error ¶ added in v0.29.0
func (e MissingArgError) Error() string
type Mod ¶
type Mod[T any] interface { Apply(T) }
Mod is a generic interface for modifying a query It is the building block for creating queries
type Mods ¶ added in v0.29.0
type PreparedExecutor ¶ added in v0.29.0
type Query ¶
type Query interface { // It should satisfy the Expression interface so that it can be used // in places such as a sub-select // However, it is allowed for a query to use its own dialect and not // the dialect given to it Expression // start is the index of the args, usually 1. // it is present to allow re-indexing in cases of a subquery // The method returns the value of any args placed WriteQuery(ctx context.Context, w io.Writer, start int) (args []any, err error) // Type returns the query type Type() QueryType }
type QueryStmt ¶ added in v0.13.0
func PrepareQuery ¶ added in v0.13.0
func PrepareQueryx ¶ added in v0.13.0
type RawNamedArgError ¶ added in v0.29.0
type RawNamedArgError struct {
Name string
}
func (RawNamedArgError) Error ¶ added in v0.29.0
func (e RawNamedArgError) Error() string
type SkipContextualModsKey ¶ added in v0.29.0
type SkipContextualModsKey struct{}
If set to true, contextual mods are skipped
type SkipModelHooksKey ¶ added in v0.29.0
type SkipModelHooksKey struct{}
If set to true, model hooks are skipped
type SkipQueryHooksKey ¶ added in v0.29.0
type SkipQueryHooksKey struct{}
If set to true, query hooks are skipped
type StdInterface ¶
type StdInterface interface { stdscan.Queryer ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) }
StdInterface is an interface that *sql.DB, *sql.Tx and *sql.Conn satisfy
type StdPrepared ¶ added in v0.29.0
func (StdPrepared) QueryContext ¶ added in v0.29.0
type Stmt ¶ added in v0.13.0
type Stmt[Arg any] struct { // contains filtered or unexported fields }
Stmt is similar to *sql.Stmt but implements [Queryer] instead of taking a list of args, it takes a struct to bind to the query
func InTx ¶ added in v0.29.0
InTx returns a new MappedStmt that will be executed in the given transaction
func Prepare ¶ added in v0.13.0
func Prepare[Arg any, P PreparedExecutor](ctx context.Context, exec Preparer[P], q Query) (Stmt[Arg], error)
Prepare prepares a query using the Preparer and returns a [NamedStmt]
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is similar to *sql.Tx but implements [Queryer]
func NewTx ¶
NewTx wraps an *sql.Tx and returns a type that implements [Queryer] but still retains the expected methods used by *sql.Tx This is useful when an existing *sql.Tx is used in other places in the codebase
func (Tx) ExecContext ¶
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (Tx) PrepareContext ¶
func (c Tx) PrepareContext(ctx context.Context, query string) (StdPrepared, error)
PrepareContext creates a prepared statement for later queries or executions
func (*Tx) StmtContext ¶ added in v0.29.0
func (tx *Tx) StmtContext(ctx context.Context, stmt StdPrepared) StdPrepared
type WrongStartError ¶ added in v0.23.0
func (WrongStartError) Error ¶ added in v0.23.0
func (e WrongStartError) Error() string
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
dialect
|
|
drivers
Package drivers talks to various database backends and retrieves table, column, type, and foreign key information
|
Package drivers talks to various database backends and retrieves table, column, type, and foreign key information |
importers
Package importers helps with dynamic imports for templating
|
Package importers helps with dynamic imports for templating |
test
|
|