Documentation
¶
Overview ¶
Package quarry provides a small, explicit Go SQL composition toolkit.
Quarry keeps SQL visible, binds values explicitly, and stays close to database/sql instead of trying to become an ORM or schema modeler.
Index ¶
- Variables
- type Column
- func (c Column) Any(values any) Predicate
- func (c Column) As(alias string) Column
- func (c Column) Between(low any, high any) Predicate
- func (c Column) Eq(val any) Predicate
- func (c Column) Gt(val any) Predicate
- func (c Column) Gte(val any) Predicate
- func (c Column) ILike(val any) Predicate
- func (c Column) In(values ...any) Predicate
- func (c Column) IsNotNull() Predicate
- func (c Column) IsNull() Predicate
- func (c Column) Like(val any) Predicate
- func (c Column) Lt(val any) Predicate
- func (c Column) Lte(val any) Predicate
- func (c Column) Neq(val any) Predicate
- func (c Column) NotIn(values ...any) Predicate
- type DeleteBuilder
- func (b *DeleteBuilder) Prefix(sql string, args ...any) *DeleteBuilder
- func (b *DeleteBuilder) Returning(cols ...any) *DeleteBuilder
- func (b *DeleteBuilder) Suffix(sql string, args ...any) *DeleteBuilder
- func (b *DeleteBuilder) ToSQL() (string, []any, error)
- func (b *DeleteBuilder) Where(preds ...Predicate) *DeleteBuilder
- func (b *DeleteBuilder) WhereIf(cond bool, pred Predicate) *DeleteBuilder
- type Dialect
- type Expr
- type Feature
- type Filters
- type InsertBuilder
- func (b *InsertBuilder) Columns(cols ...any) *InsertBuilder
- func (b *InsertBuilder) Prefix(sql string, args ...any) *InsertBuilder
- func (b *InsertBuilder) Returning(cols ...any) *InsertBuilder
- func (b *InsertBuilder) Rows(rows ...[]any) *InsertBuilder
- func (b *InsertBuilder) SetMap(values map[string]any) *InsertBuilder
- func (b *InsertBuilder) Suffix(sql string, args ...any) *InsertBuilder
- func (b *InsertBuilder) ToSQL() (string, []any, error)
- func (b *InsertBuilder) Values(vals ...any) *InsertBuilder
- type Predicate
- func And(preds ...Predicate) Predicate
- func Any(col any, values any) Predicate
- func Between(col any, low any, high any) Predicate
- func Eq(col string, val any) Predicate
- func Exists(query Query) Predicate
- func Gt(col string, val any) Predicate
- func Gte(col string, val any) Predicate
- func ILike(col string, val any) Predicate
- func In(col any, values ...any) Predicate
- func IsNotNull(col string) Predicate
- func IsNull(col string) Predicate
- func Like(col string, val any) Predicate
- func Lt(col string, val any) Predicate
- func Lte(col string, val any) Predicate
- func Neq(col string, val any) Predicate
- func Not(pred Predicate) Predicate
- func NotExists(query Query) Predicate
- func NotIn(col any, values ...any) Predicate
- func OptionalEq(col any, val any) Predicate
- func OptionalGt(col any, val any) Predicate
- func OptionalGte(col any, val any) Predicate
- func OptionalILike(col any, val any) Predicate
- func OptionalIn(col any, values ...any) Predicate
- func OptionalLike(col any, val any) Predicate
- func OptionalLt(col any, val any) Predicate
- func OptionalLte(col any, val any) Predicate
- func OptionalNeq(col any, val any) Predicate
- func Or(preds ...Predicate) Predicate
- func Raw(sql string, args ...any) Predicate
- func TupleIn(columns []any, tuples [][]any) Predicate
- type Quarry
- type Query
- type SQLer
- type SelectBuilder
- func (b *SelectBuilder) CrossJoin(expr any) *SelectBuilder
- func (b *SelectBuilder) Distinct() *SelectBuilder
- func (b *SelectBuilder) From(table any) *SelectBuilder
- func (b *SelectBuilder) FullJoin(expr any) *SelectBuilder
- func (b *SelectBuilder) GroupBy(parts ...any) *SelectBuilder
- func (b *SelectBuilder) Having(preds ...Predicate) *SelectBuilder
- func (b *SelectBuilder) Join(expr any) *SelectBuilder
- func (b *SelectBuilder) LeftJoin(expr any) *SelectBuilder
- func (b *SelectBuilder) Limit(n uint64) *SelectBuilder
- func (b *SelectBuilder) LimitDefault(n, fallback int) *SelectBuilder
- func (b *SelectBuilder) Offset(n uint64) *SelectBuilder
- func (b *SelectBuilder) OffsetDefault(n, fallback int) *SelectBuilder
- func (b *SelectBuilder) OrderBy(parts ...any) *SelectBuilder
- func (b *SelectBuilder) OrderBySafe(input string, allowed SortMap) *SelectBuilder
- func (b *SelectBuilder) OrderBySafeDefault(input string, allowed SortMap, fallback string) *SelectBuilder
- func (b *SelectBuilder) Page(page, perPage int) *SelectBuilder
- func (b *SelectBuilder) Prefix(sql string, args ...any) *SelectBuilder
- func (b *SelectBuilder) RightJoin(expr any) *SelectBuilder
- func (b *SelectBuilder) Suffix(sql string, args ...any) *SelectBuilder
- func (b *SelectBuilder) ToSQL() (string, []any, error)
- func (b *SelectBuilder) Where(preds ...Predicate) *SelectBuilder
- func (b *SelectBuilder) WhereIf(cond bool, pred Predicate) *SelectBuilder
- type SortMap
- type Table
- type UpdateBuilder
- func (b *UpdateBuilder) Prefix(sql string, args ...any) *UpdateBuilder
- func (b *UpdateBuilder) Returning(cols ...any) *UpdateBuilder
- func (b *UpdateBuilder) Set(col any, val any) *UpdateBuilder
- func (b *UpdateBuilder) SetIf(cond bool, col any, val any) *UpdateBuilder
- func (b *UpdateBuilder) SetMap(values map[string]any) *UpdateBuilder
- func (b *UpdateBuilder) SetOptional(col any, val any) *UpdateBuilder
- func (b *UpdateBuilder) Suffix(sql string, args ...any) *UpdateBuilder
- func (b *UpdateBuilder) ToSQL() (string, []any, error)
- func (b *UpdateBuilder) Where(preds ...Predicate) *UpdateBuilder
- func (b *UpdateBuilder) WhereIf(cond bool, pred Predicate) *UpdateBuilder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidIdentifier reports a rejected identifier value. ErrInvalidIdentifier = errors.New("invalid identifier") // ErrUnsupportedFeature reports that the active dialect cannot render a feature. ErrUnsupportedFeature = errors.New("unsupported dialect feature") // ErrInvalidBuilderState reports a builder that cannot be rendered as configured. ErrInvalidBuilderState = errors.New("invalid builder state") // ErrPlaceholderMismatch reports a placeholder / argument count mismatch. ErrPlaceholderMismatch = errors.New("placeholder mismatch") )
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct {
// contains filtered or unexported fields
}
Column identifies a SQL column, optionally qualified by a table.
type DeleteBuilder ¶
type DeleteBuilder struct {
// contains filtered or unexported fields
}
DeleteBuilder renders DELETE statements for the active Quarry dialect.
func (*DeleteBuilder) Prefix ¶
func (b *DeleteBuilder) Prefix(sql string, args ...any) *DeleteBuilder
Prefix appends a raw fragment before the DELETE statement.
func (*DeleteBuilder) Returning ¶
func (b *DeleteBuilder) Returning(cols ...any) *DeleteBuilder
Returning appends RETURNING expressions.
Example ¶
package main
import (
"fmt"
quarry "github.com/sphireinc/quarry"
)
func main() {
qq := quarry.New(quarry.Postgres)
sqlText, args, err := qq.DeleteFrom("users").
Where(quarry.Eq("id", 7)).
Returning("id").
ToSQL()
if err != nil {
panic(err)
}
fmt.Println(sqlText)
fmt.Println(args)
}
Output: DELETE FROM "users" WHERE "id" = $1 RETURNING "id" [7]
func (*DeleteBuilder) Suffix ¶
func (b *DeleteBuilder) Suffix(sql string, args ...any) *DeleteBuilder
Suffix appends a raw fragment after the DELETE statement.
func (*DeleteBuilder) Where ¶
func (b *DeleteBuilder) Where(preds ...Predicate) *DeleteBuilder
Where appends DELETE predicates.
func (*DeleteBuilder) WhereIf ¶
func (b *DeleteBuilder) WhereIf(cond bool, pred Predicate) *DeleteBuilder
WhereIf appends pred only when cond is true and the predicate is non-empty.
type Dialect ¶
type Dialect string
Dialect identifies the SQL dialect Quarry should render for.
func (Dialect) Placeholder ¶
Placeholder renders the dialect's positional placeholder token.
func (Dialect) QuoteIdent ¶
QuoteIdent returns the dialect-specific quoted identifier.
type Expr ¶
type Expr interface {
// contains filtered or unexported methods
}
Expr is the minimal contract for SQL fragments Quarry can render.
It stays unexported in practice because only Quarry's own expression types can satisfy the interface.
type Filters ¶
type Filters []Predicate
Filters is a convenience alias for a batch of optional predicates.
type InsertBuilder ¶
type InsertBuilder struct {
// contains filtered or unexported fields
}
InsertBuilder renders INSERT statements for the active Quarry dialect.
func (*InsertBuilder) Columns ¶
func (b *InsertBuilder) Columns(cols ...any) *InsertBuilder
Columns appends INSERT column expressions.
func (*InsertBuilder) Prefix ¶
func (b *InsertBuilder) Prefix(sql string, args ...any) *InsertBuilder
Prefix appends a raw fragment before the INSERT statement.
func (*InsertBuilder) Returning ¶
func (b *InsertBuilder) Returning(cols ...any) *InsertBuilder
Returning appends RETURNING expressions.
Example ¶
package main
import (
"fmt"
quarry "github.com/sphireinc/quarry"
)
func main() {
qq := quarry.New(quarry.Postgres)
sqlText, args, err := qq.InsertInto("users").
Columns("email", "status").
Values("a@example.com", "active").
Returning("id").
ToSQL()
if err != nil {
panic(err)
}
fmt.Println(sqlText)
fmt.Println(args)
}
Output: INSERT INTO "users" ("email", "status") VALUES ($1, $2) RETURNING "id" [a@example.com active]
func (*InsertBuilder) Rows ¶
func (b *InsertBuilder) Rows(rows ...[]any) *InsertBuilder
Rows appends one or more explicit INSERT rows.
func (*InsertBuilder) SetMap ¶
func (b *InsertBuilder) SetMap(values map[string]any) *InsertBuilder
SetMap converts a map into a deterministic INSERT column/value row.
Map keys are treated as trusted identifiers, validated locally, and quoted with the active dialect before rendering.
func (*InsertBuilder) Suffix ¶
func (b *InsertBuilder) Suffix(sql string, args ...any) *InsertBuilder
Suffix appends a raw fragment after the INSERT statement.
func (*InsertBuilder) Values ¶
func (b *InsertBuilder) Values(vals ...any) *InsertBuilder
Values appends a single INSERT row.
type Predicate ¶
type Predicate interface {
Expr
// contains filtered or unexported methods
}
Predicate is a boolean SQL expression that may also be empty and therefore omitted.
func OptionalEq ¶
OptionalEq returns Eq when val is present and a no-op predicate otherwise.
func OptionalGt ¶
OptionalGt returns Gt when val is present and a no-op predicate otherwise.
func OptionalGte ¶
OptionalGte returns Gte when val is present and a no-op predicate otherwise.
func OptionalILike ¶
OptionalILike returns ILike when val is present and a no-op predicate otherwise.
func OptionalIn ¶
OptionalIn returns In when vals is present and a no-op predicate otherwise.
func OptionalLike ¶
OptionalLike returns Like when val is present and a no-op predicate otherwise.
func OptionalLt ¶
OptionalLt returns Lt when val is present and a no-op predicate otherwise.
func OptionalLte ¶
OptionalLte returns Lte when val is present and a no-op predicate otherwise.
func OptionalNeq ¶
OptionalNeq returns Neq when val is present and a no-op predicate otherwise.
func Raw ¶
Raw injects a raw SQL fragment while still binding values safely.
Example ¶
package main
import (
"fmt"
quarry "github.com/sphireinc/quarry"
)
func main() {
qq := quarry.New(quarry.Postgres)
sqlText, args, err := qq.Select(quarry.Raw("COUNT(*) FILTER (WHERE status = ?)", "active")).
From("users").
Where(quarry.Raw("created_at >= ?", "2024-01-01")).
ToSQL()
if err != nil {
panic(err)
}
fmt.Println(sqlText)
fmt.Println(args)
}
Output: SELECT COUNT(*) FILTER (WHERE status = $1) FROM "users" WHERE created_at >= $2 [active 2024-01-01]
type Quarry ¶
type Quarry struct {
// contains filtered or unexported fields
}
Quarry carries the selected dialect and manufactures builders from it.
func (*Quarry) DeleteFrom ¶
func (q *Quarry) DeleteFrom(table any) *DeleteBuilder
DeleteFrom starts a DELETE builder that inherits the receiver's dialect.
func (*Quarry) InsertInto ¶
func (q *Quarry) InsertInto(table any) *InsertBuilder
InsertInto starts an INSERT builder that inherits the receiver's dialect.
func (*Quarry) Select ¶
func (q *Quarry) Select(cols ...any) *SelectBuilder
Select starts a SELECT builder that inherits the receiver's dialect.
Example ¶
package main
import (
"fmt"
quarry "github.com/sphireinc/quarry"
)
func main() {
qq := quarry.New(quarry.Postgres)
sqlText, args, err := qq.Select("id", "email").
From("users").
Where(quarry.Eq("status", "active")).
ToSQL()
if err != nil {
panic(err)
}
fmt.Println(sqlText)
fmt.Println(args)
}
Output: SELECT "id", "email" FROM "users" WHERE "status" = $1 [active]
func (*Quarry) Update ¶
func (q *Quarry) Update(table any) *UpdateBuilder
Update starts an UPDATE builder that inherits the receiver's dialect.
type Query ¶
type Query = SQLer
Query keeps the subquery-oriented API readable without adding a second contract.
type SelectBuilder ¶
type SelectBuilder struct {
// contains filtered or unexported fields
}
SelectBuilder renders SELECT statements for the active Quarry dialect.
Example ¶
package main
import (
"fmt"
quarry "github.com/sphireinc/quarry"
)
func main() {
qq := quarry.New(quarry.Postgres)
sqlText, args, err := qq.Select("id", "email", "created_at").
From("users").
Where(
quarry.Eq("tenant_id", 42),
quarry.OptionalILike("email", "%bob%"),
quarry.OptionalEq("status", (*string)(nil)),
).
OrderBySafeDefault("newest", quarry.SortMap{
"newest": "created_at DESC",
"email": "email ASC",
}, "newest").
Page(1, 25).
ToSQL()
if err != nil {
panic(err)
}
fmt.Println(sqlText)
fmt.Println(args)
}
Output: SELECT "id", "email", "created_at" FROM "users" WHERE "tenant_id" = $1 AND "email" ILIKE $2 ORDER BY created_at DESC LIMIT 25 OFFSET 0 [42 %bob%]
func (*SelectBuilder) CrossJoin ¶
func (b *SelectBuilder) CrossJoin(expr any) *SelectBuilder
CrossJoin appends a CROSS JOIN clause.
func (*SelectBuilder) Distinct ¶
func (b *SelectBuilder) Distinct() *SelectBuilder
Distinct toggles SELECT DISTINCT rendering.
func (*SelectBuilder) From ¶
func (b *SelectBuilder) From(table any) *SelectBuilder
From sets the FROM clause.
func (*SelectBuilder) FullJoin ¶
func (b *SelectBuilder) FullJoin(expr any) *SelectBuilder
FullJoin appends a FULL JOIN clause.
func (*SelectBuilder) GroupBy ¶
func (b *SelectBuilder) GroupBy(parts ...any) *SelectBuilder
GroupBy appends GROUP BY expressions.
func (*SelectBuilder) Having ¶
func (b *SelectBuilder) Having(preds ...Predicate) *SelectBuilder
Having appends HAVING predicates.
func (*SelectBuilder) Join ¶
func (b *SelectBuilder) Join(expr any) *SelectBuilder
Join appends a plain JOIN clause.
func (*SelectBuilder) LeftJoin ¶
func (b *SelectBuilder) LeftJoin(expr any) *SelectBuilder
LeftJoin appends a LEFT JOIN clause.
func (*SelectBuilder) Limit ¶
func (b *SelectBuilder) Limit(n uint64) *SelectBuilder
Limit sets an explicit LIMIT value.
func (*SelectBuilder) LimitDefault ¶
func (b *SelectBuilder) LimitDefault(n, fallback int) *SelectBuilder
LimitDefault applies n when it is positive, otherwise a positive fallback.
func (*SelectBuilder) Offset ¶
func (b *SelectBuilder) Offset(n uint64) *SelectBuilder
Offset sets an explicit OFFSET value.
func (*SelectBuilder) OffsetDefault ¶
func (b *SelectBuilder) OffsetDefault(n, fallback int) *SelectBuilder
OffsetDefault applies n when it is non-negative, otherwise a non-negative fallback.
func (*SelectBuilder) OrderBy ¶
func (b *SelectBuilder) OrderBy(parts ...any) *SelectBuilder
OrderBy appends ORDER BY expressions or trusted fragments.
func (*SelectBuilder) OrderBySafe ¶
func (b *SelectBuilder) OrderBySafe(input string, allowed SortMap) *SelectBuilder
OrderBySafe appends a trusted ORDER BY fragment selected from allowed.
func (*SelectBuilder) OrderBySafeDefault ¶
func (b *SelectBuilder) OrderBySafeDefault(input string, allowed SortMap, fallback string) *SelectBuilder
OrderBySafeDefault appends the selected sort key or falls back to a trusted default.
Example ¶
package main
import (
"fmt"
quarry "github.com/sphireinc/quarry"
)
func main() {
qq := quarry.New(quarry.Postgres)
sqlText, args, err := qq.Select("id", "email").
From("users").
OrderBySafeDefault("newest", quarry.SortMap{
"newest": "created_at DESC",
"email": "email ASC",
}, "newest").
ToSQL()
if err != nil {
panic(err)
}
fmt.Println(sqlText)
fmt.Println(args)
}
Output: SELECT "id", "email" FROM "users" ORDER BY created_at DESC []
func (*SelectBuilder) Page ¶
func (b *SelectBuilder) Page(page, perPage int) *SelectBuilder
Page applies one-based page/per-page pagination and derives LIMIT/OFFSET.
func (*SelectBuilder) Prefix ¶
func (b *SelectBuilder) Prefix(sql string, args ...any) *SelectBuilder
Prefix appends a raw fragment before the SELECT statement.
func (*SelectBuilder) RightJoin ¶
func (b *SelectBuilder) RightJoin(expr any) *SelectBuilder
RightJoin appends a RIGHT JOIN clause.
func (*SelectBuilder) Suffix ¶
func (b *SelectBuilder) Suffix(sql string, args ...any) *SelectBuilder
Suffix appends a raw fragment after the SELECT statement.
func (*SelectBuilder) Where ¶
func (b *SelectBuilder) Where(preds ...Predicate) *SelectBuilder
Where appends WHERE predicates.
func (*SelectBuilder) WhereIf ¶
func (b *SelectBuilder) WhereIf(cond bool, pred Predicate) *SelectBuilder
WhereIf appends pred only when cond is true and the predicate is non-empty.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table identifies a SQL table name and can produce qualified columns from it.
type UpdateBuilder ¶
type UpdateBuilder struct {
// contains filtered or unexported fields
}
UpdateBuilder renders UPDATE statements for the active Quarry dialect.
func (*UpdateBuilder) Prefix ¶
func (b *UpdateBuilder) Prefix(sql string, args ...any) *UpdateBuilder
Prefix appends a raw fragment before the UPDATE statement.
func (*UpdateBuilder) Returning ¶
func (b *UpdateBuilder) Returning(cols ...any) *UpdateBuilder
Returning appends RETURNING expressions.
func (*UpdateBuilder) Set ¶
func (b *UpdateBuilder) Set(col any, val any) *UpdateBuilder
Set appends an explicit SET clause.
func (*UpdateBuilder) SetIf ¶
func (b *UpdateBuilder) SetIf(cond bool, col any, val any) *UpdateBuilder
SetIf appends a SET clause only when cond is true.
func (*UpdateBuilder) SetMap ¶
func (b *UpdateBuilder) SetMap(values map[string]any) *UpdateBuilder
SetMap appends deterministic SET clauses from a map.
Map keys are treated as trusted identifiers, validated locally, and quoted with the active dialect before rendering.
func (*UpdateBuilder) SetOptional ¶
func (b *UpdateBuilder) SetOptional(col any, val any) *UpdateBuilder
SetOptional appends a SET clause only when val is a present, non-empty value.
Example ¶
package main
import (
"fmt"
quarry "github.com/sphireinc/quarry"
)
func main() {
qq := quarry.New(quarry.Postgres)
enabled := true
sqlText, args, err := qq.Update("users").
SetOptional("name", "Quarry User").
SetOptional("email", "user@example.com").
SetIf(enabled, "enabled", enabled).
Where(quarry.Eq("id", 7)).
Returning("id").
ToSQL()
if err != nil {
panic(err)
}
fmt.Println(sqlText)
fmt.Println(args)
}
Output: UPDATE "users" SET "name" = $1, "email" = $2, "enabled" = $3 WHERE "id" = $4 RETURNING "id" [Quarry User user@example.com true 7]
func (*UpdateBuilder) Suffix ¶
func (b *UpdateBuilder) Suffix(sql string, args ...any) *UpdateBuilder
Suffix appends a raw fragment after the UPDATE statement.
func (*UpdateBuilder) Where ¶
func (b *UpdateBuilder) Where(preds ...Predicate) *UpdateBuilder
Where appends UPDATE predicates.
func (*UpdateBuilder) WhereIf ¶
func (b *UpdateBuilder) WhereIf(cond bool, pred Predicate) *UpdateBuilder
WhereIf appends pred only when cond is true and the predicate is non-empty.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package codex provides Quarry's optional named-query and recipe registry.
|
Package codex provides Quarry's optional named-query and recipe registry. |
|
examples
|
|
|
basic_select
command
Package main prints a minimal Quarry SELECT query.
|
Package main prints a minimal Quarry SELECT query. |
|
dynamic_filters
command
Package main prints a Quarry query with optional filters and safe paging.
|
Package main prints a Quarry query with optional filters and safe paging. |
|
partial_update
command
Package main prints a Quarry UPDATE statement with optional fields.
|
Package main prints a Quarry UPDATE statement with optional fields. |
|
raw_sql_codex
command
Package main prints Quarry raw SQL and Codex binding examples.
|
Package main prints Quarry raw SQL and Codex binding examples. |
|
scan_many
command
Package main prints many Quarry rows scanned into Go structs.
|
Package main prints many Quarry rows scanned into Go structs. |
|
scan_one
command
Package main prints a single Quarry row scanned into a Go struct.
|
Package main prints a single Quarry row scanned into a Go struct. |
|
scan_with_quarry_query
command
Package main prints a Quarry query scanned into a scalar result.
|
Package main prints a Quarry query scanned into a scalar result. |
|
internal
|
|
|
Package scan executes Quarry queries and scans rows into Go values.
|
Package scan executes Quarry queries and scans rows into Go values. |