Documentation
¶
Overview ¶
Package queries implements convenience helpers for working with SQL queries.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Collect ¶ added in v0.3.0
Collect is a slices.Collect variant that collects values from an iter.Seq2[T, error]. If an error occurs during the collection, Collect stops the iteration and returns the error.
func Query ¶ added in v0.2.0
Query executes a query that returns rows, scans each row into a T, and returns an iterator over the Ts. If an error occurs, the iterator yields it as the second value, and the caller should then stop the iteration. Queryer can be either sql.DB or sql.Tx, the rest of the arguments are passed directly to [Queryer.QueryContext]. Query fully manages the lifecycle of the sql.Rows returned by [Queryer.QueryContext], so the caller does not have to.
The following Ts are supported:
- int (any kind)
- uint (any kind)
- float (any kind)
- bool
- string
- time.Time
- sql.Scanner (implemented by sql.Null types)
- any struct
See the sql.Rows.Scan documentation for the scanning rules. If the query has multiple columns, T must be a struct, other types can only be used for single-column queries. The fields of a struct T must have the `sql:"COLUMN"` tag, where COLUMN is the name of the corresponding column in the query. Unexported and untagged fields are ignored.
Query panics if:
- The query has no columns.
- A non-struct T is specified with a multi-column query.
- The specified struct T has no field for one of the query columns.
- An unsupported T is specified.
- One of the fields in a struct T has an empty `sql` tag.
If the caller prefers the result to be a slice rather than an iterator, Query can be combined with Collect.
func QueryRow ¶ added in v0.2.0
QueryRow is a Query variant for queries that are expected to return at most one row, so instead of an iterator, it returns a single T. Like sql.DB.QueryRow, QueryRow returns sql.ErrNoRows if the query selects no rows, otherwise it scans the first row and discards the rest. See the Query documentation for details on supported Ts.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is a raw SQL query builder. The zero value is ready to use. Do not copy a non-zero Builder.
func (*Builder) Appendf ¶
Appendf formats according to the given format and appends the result to the query. It works like fmt.Appendf, i.e. all rules from the fmt package are applied. In addition, Appendf supports %?, %$, and %@ verbs, which are automatically expanded to the query placeholders ?, $N, and @pN, where N is the auto-incrementing counter. The corresponding arguments can then be accessed with the Builder.Args method.
IMPORTANT: to avoid SQL injections, make sure to pass arguments from user input with placeholder verbs.
Placeholder verbs map to the following database placeholders:
- MySQL, SQLite: %? -> ?
- PostgreSQL: %$ -> $N
- MSSQL: %@ -> @pN
TODO: document slice arguments usage.
type Interceptor ¶ added in v0.2.0
type Interceptor struct { // Driver is a database driver. // It must implement [driver.Pinger], [driver.ExecerContext], [driver.QueryerContext], // [driver.ConnPrepareContext], and [driver.ConnBeginTx] (most drivers do). // Required. Driver driver.Driver // ExecContext is a callback for both [sql.DB.ExecContext] and [sql.Tx.ExecContext]. // The implementation must call execer.ExecerContext(ctx, query, args) and return the result. // Optional. ExecContext func(ctx context.Context, query string, args []driver.NamedValue, execer driver.ExecerContext) (driver.Result, error) // QueryContext is a callback for both [sql.DB.QueryContext] and [sql.Tx.QueryContext]. // The implementation must call queryer.QueryContext(ctx, query, args) and return the result. // Optional. QueryContext func(ctx context.Context, query string, args []driver.NamedValue, queryer driver.QueryerContext) (driver.Rows, error) // PrepareContext is a callback for [sql.DB.PrepareContext]. // The implementation must call preparer.ConnPrepareContext(ctx, query) and return the result. // Optional. PrepareContext func(ctx context.Context, query string, preparer driver.ConnPrepareContext) (driver.Stmt, error) }
Interceptor is a driver.Driver wrapper that allows to register callbacks for database queries. It must first be registered with sql.Register with the same name that is then passed to sql.Open:
interceptor := queries.Interceptor{...} sql.Register("interceptor", interceptor) db, err := sql.Open("interceptor", "dsn")
func (Interceptor) Open ¶ added in v0.2.0
func (i Interceptor) Open(name string) (driver.Conn, error)
Open implements driver.Driver.
func (Interceptor) OpenConnector ¶ added in v0.2.0
func (i Interceptor) OpenConnector(name string) (driver.Connector, error)
OpenConnector implements driver.DriverContext.
Directories
¶
Path | Synopsis |
---|---|
internal
|
|
assert
Package assert implements assertions for the standard testing package.
|
Package assert implements assertions for the standard testing package. |
assert/EF
Package EF provides type aliases for the parent assert package.
|
Package EF provides type aliases for the parent assert package. |
Package queriestest implements utilities for testing SQL queries.
|
Package queriestest implements utilities for testing SQL queries. |