Documentation
¶
Overview ¶
Package sqlh provides lightweight sql helpers.
Index ¶
- func Binary(v interface{ ... }) interface{ ... }
- func Iter(rows Rows, fn func() error) (err error)
- func Json(v any) interface{ ... }
- func Pluck[V any](rows Rows, queryErr error) (out []V, err error)
- func Scan[V any](rows Rows, scan func(*V, Row) error) (out []*V, err error)
- func ScanV[V any](rows Rows, scan func(*V, Row) error) (out []V, err error)
- func Text(v interface{ ... }) interface{ ... }
- type Expr
- func (e Expr) Exec(db interface{ ... }) (sql.Result, error)
- func (e Expr) ExecContext(ctx context.Context, db interface{ ... }) (sql.Result, error)
- func (e Expr) Query(db interface{ ... }) (*sql.Rows, error)
- func (e Expr) QueryContext(ctx context.Context, db interface{ ... }) (*sql.Rows, error)
- func (e Expr) QueryRow(db interface{ ... }) *sql.Row
- func (e Expr) QueryRowContext(ctx context.Context, db interface{ ... }) *sql.Row
- func (e Expr) String() string
- type Row
- type Rows
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Binary ¶
func Binary(v interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}) interface {
sql.Scanner
driver.Valuer
}
Binary converts to or from a binary value.
Example ¶
var location url.URL
_ = db.QueryRow("SELECT location FROM binary_example").Scan(sqlh.Binary(&location))
fmt.Println(location.String())
Output: http://example.com
func Json ¶
Json converts to or from a json value.
Example ¶
var document any
_ = db.QueryRow("SELECT document FROM json_example").Scan(sqlh.Json(&document))
fmt.Println(document)
Output: [1 2 3]
func Pluck ¶
Pluck will scan the results of a query that produces a single column.
Example ¶
userIDs := []int{1, 2, 3}
names, _ := sqlh.Pluck[string](sqlh.SQL(`SELECT name FROM users WHERE id IN (?)`, sqlh.In(userIDs)).Query(db))
for _, name := range names {
fmt.Println(name)
}
Output: user a user b
func Text ¶
func Text(v interface {
encoding.TextMarshaler
encoding.TextUnmarshaler
}) interface {
sql.Scanner
driver.Valuer
}
Text converts to or from a binary value.
Types ¶
type Expr ¶
func DebugSQL ¶
DebugSQL annotates the query with the caller and indents it if it contains a newline.
Example ¶
package main
import (
"fmt"
"github.com/simon-engledew/sqlh"
)
func main() {
subquery := sqlh.DebugSQL(`SELECT id FROM users WHERE suspended_at IS NULL AND parent_id = ?`, 10)
query := sqlh.DebugSQL(`SELECT event FROM events WHERE user_id IN (?)`, subquery)
fmt.Println(query.Statement)
}
Output: /* debug_test.go:12 */ SELECT event FROM events WHERE user_id IN ( /* debug_test.go:11 */ SELECT id FROM users WHERE suspended_at IS NULL AND parent_id = ? )
func In ¶
In takes parameters and returns an Expr that can be used in an SQL IN clause.
Example ¶
package main
import (
"fmt"
"github.com/simon-engledew/sqlh"
)
func main() {
query := sqlh.SQL(`SELECT name FROM in_example WHERE id IN (?)`, sqlh.In([]int{1, 2, 3}))
fmt.Println(query.Statement, query.Args)
}
Output: SELECT name FROM in_example WHERE id IN (?, ?, ?) [1 2 3]
func SQL ¶
SQL takes an SQL fragment and returns an Expr that flattens any nested queries and their arguments.
Example ¶
clause := sqlh.SQL("found = ?", true)
expr := sqlh.SQL(`SELECT name FROM builder_example WHERE id = ? AND ?`, 1, clause)
var name string
_ = db.QueryRow(expr.Statement, expr.Args...).Scan(&name)
fmt.Println(name)
Output: example
func (Expr) Exec ¶
func (e Expr) Exec(db interface { Exec(query string, args ...any) (sql.Result, error) }) (sql.Result, error)
Exec calls db.Exec, passing in the SQL statement and its arguments. See https://pkg.go.dev/database/sql#DB.Exec
func (Expr) ExecContext ¶
func (e Expr) ExecContext(ctx context.Context, db interface { ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) }) (sql.Result, error)
ExecContext calls db.ExecContext, passing in the SQL statement and its arguments. See https://pkg.go.dev/database/sql#DB.ExecContext
func (Expr) Query ¶
func (e Expr) Query(db interface { Query(query string, args ...any) (*sql.Rows, error) }) (*sql.Rows, error)
Query calls db.Query, passing in the SQL statement and its arguments. See https://pkg.go.dev/database/sql#DB.Query
func (Expr) QueryContext ¶
func (e Expr) QueryContext(ctx context.Context, db interface { QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) }) (*sql.Rows, error)
QueryContext calls db.QueryContext, passing in the SQL statement and its arguments. See https://pkg.go.dev/database/sql#DB.QueryContext
func (Expr) QueryRow ¶
QueryRow calls db.QueryRow, passing in the SQL statement and its arguments. See https://pkg.go.dev/database/sql#DB.QueryRow
func (Expr) QueryRowContext ¶
func (e Expr) QueryRowContext(ctx context.Context, db interface { QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row }) *sql.Row
QueryRowContext calls db.QueryRowContext, passing in the SQL statement and its arguments. See https://pkg.go.dev/database/sql#DB.QueryRowContext