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 ¶
SkipHooks modifies a context to prevent hooks from running for any query it encounters.
func SkipModelHooks ¶
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 ¶
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 ¶
func (BoundQuery[E]) GetLoaders ¶
func (b BoundQuery[E]) GetLoaders() []Loader
func (BoundQuery[E]) GetMapperMods ¶
func (b BoundQuery[E]) GetMapperMods() []scan.MapperMod
func (BoundQuery[Arg]) WriteQuery ¶
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 ¶
BeginTx is similar to *sql.Conn.BeginTx, but return a transaction that implements [Queryer]
func (Conn) ExecContext ¶
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (Conn) PingContext ¶
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 ¶
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 ¶
type ContextualModdable ¶
type ContextualModdable[T any] struct { Mods []ContextualMod[T] }
func (*ContextualModdable[T]) AppendContextualMod ¶
func (h *ContextualModdable[T]) AppendContextualMod(mods ...ContextualMod[T])
AppendContextualMod a hook to the set
func (*ContextualModdable[T]) AppendContextualModFunc ¶
func (h *ContextualModdable[T]) AppendContextualModFunc(f func(context.Context, T) (context.Context, error))
AppendContextualMod a hook to the set
func (*ContextualModdable[T]) RunContextualMods ¶
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 ¶
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 ¶
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 Executor ¶
type Executor interface { scan.Queryer ExecContext(context.Context, string, ...any) (sql.Result, error) }
func DebugToPrinter ¶
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 ¶
func Named(names ...string) Expression
Named args should ONLY be used to prepare statements
func NamedGroup ¶
func NamedGroup(names ...string) Expression
NamedGroup is like Named, but wraps in parentheses
type ExpressionFunc ¶
type Hook ¶
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 ¶
type HookableType ¶
If a type implements this interface, it will be called after the query has been executed and it is scanned
type Hooks ¶
Hooks is a set of hooks that can be called all at once
func (*Hooks[T, K]) AppendHooks ¶
AppendHooks a hook to the set
type Load ¶
type Load struct {
// contains filtered or unexported fields
}
Load is an embeddable struct that enables Preloading and AfterLoading
func (*Load) AppendLoader ¶
AppendLoader add to the query's loaders
func (*Load) AppendMapperMod ¶
AppendMapperMod adds to the query's mapper mods
func (*Load) GetLoaders ¶
GetLoaders implements the Loadable interface
func (*Load) GetMapperMods ¶
GetMapperMods implements the MapperModder interface
func (*Load) SetLoaders ¶
SetLoaders sets the query's loaders
func (*Load) SetMapperMods ¶
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 ¶
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 ¶
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 ¶
type MissingArgError struct{ Name string }
func (MissingArgError) Error ¶
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 ¶
type PreparedExecutor ¶
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 ¶
func PrepareQuery ¶
func PrepareQueryx ¶
type RawNamedArgError ¶
type RawNamedArgError struct {
Name string
}
func (RawNamedArgError) Error ¶
func (e RawNamedArgError) Error() string
type SkipContextualModsKey ¶
type SkipContextualModsKey struct{}
If set to true, contextual mods 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 ¶
func (StdPrepared) QueryContext ¶
type Stmt ¶
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 Prepare ¶
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 ¶
func (tx *Tx) StmtContext(ctx context.Context, stmt StdPrepared) StdPrepared
type WrongStartError ¶
func (WrongStartError) Error ¶
func (e WrongStartError) Error() string
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
dialect
|
|
bobgen-mysql
command
|
|
bobgen-psql
command
|
|
bobgen-sql
command
|
|
bobgen-sqlite
command
|
|
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
|
|