query

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alias added in v0.1.3

func Alias(table *schema.TableDef, alias string) *aliasedTable

Alias returns an aliased view of table. Use its Col() values anywhere a column is accepted and pass the result of Alias itself to From/joins:

u := query.Alias(schema.Users, "u")
query.Select[T](u.Col("id")).From(u).Where(query.Eq(u.Col("status"), "active"))

Types

type ColRef added in v0.1.2

type ColRef struct {
	// contains filtered or unexported fields
}

func Ref added in v0.1.2

func Ref[T, F any](structPtr *T, fieldPtr *F) ColRef

Ref resolves the column name from the struct field's `db` tag by matching the memory offset between structPtr and fieldPtr.

func (ColRef) Str added in v0.1.2

func (c ColRef) Str() string

func (ColRef) String added in v0.1.2

func (c ColRef) String() string

type Condition

type Condition interface {
	SQL(argOffset int) (string, []any)
}

func And

func And(conds ...Condition) Condition

func Between added in v0.1.3

func Between(col colRef, low, high any) Condition

Between renders `col BETWEEN $n AND $n+1`.

func ContainedBy added in v0.1.3

func ContainedBy(col colRef, val any) Condition

ContainedBy renders `col <@ $n` (the JSON value is contained by the operand).

func Contains added in v0.1.3

func Contains(col colRef, val any) Condition

Contains renders `col @> $n` (the JSON value contains the operand).

func Eq

func Eq(col colRef, val any) Condition

func EqAny added in v0.1.3

func EqAny(col colRef, sub Subquery) Condition

EqAny renders `col = ANY (SELECT ...)`.

func Exists added in v0.1.3

func Exists(sub Subquery) Condition

Exists renders `EXISTS (SELECT ...)`.

func Gt

func Gt(col colRef, val any) Condition

func Gte

func Gte(col colRef, val any) Condition

func ILike

func ILike(col colRef, pattern string) Condition

func In added in v0.1.2

func In(col colRef, vals ...any) Condition

func IsNotNull

func IsNotNull(col colRef) Condition

func IsNull

func IsNull(col colRef) Condition

func KeyExists added in v0.1.3

func KeyExists(col colRef, key any) Condition

KeyExists renders `col ? $n` (the JSON value has the key).

func Like

func Like(col colRef, pattern string) Condition

func Lt

func Lt(col colRef, val any) Condition

func Lte

func Lte(col colRef, val any) Condition

func Neq

func Neq(col colRef, val any) Condition

func NeqAll added in v0.1.3

func NeqAll(col colRef, sub Subquery) Condition

NeqAll renders `col <> ALL (SELECT ...)`.

func Not

func Not(cond Condition) Condition

func NotBetween added in v0.1.3

func NotBetween(col colRef, low, high any) Condition

NotBetween renders `col NOT BETWEEN $n AND $n+1`.

func NotIn added in v0.1.2

func NotIn(col colRef, vals ...any) Condition

func Or

func Or(conds ...Condition) Condition

func Raw added in v0.1.2

func Raw(expr string, args ...any) Condition

type DeleteBuilder

type DeleteBuilder struct {
	// contains filtered or unexported fields
}

func Delete

func Delete(table namedTable) *DeleteBuilder

func (*DeleteBuilder) Limit added in v0.1.3

func (d *DeleteBuilder) Limit(n int) *DeleteBuilder

Limit caps how many rows the delete removes (PostgreSQL DELETE ... LIMIT n).

func (*DeleteBuilder) OrderBy added in v0.1.3

func (d *DeleteBuilder) OrderBy(cols []string, dir OrderDir) *DeleteBuilder

func (*DeleteBuilder) OrderByNulls added in v0.1.3

func (d *DeleteBuilder) OrderByNulls(cols []string, dir OrderDir, nulls NullsOrder) *DeleteBuilder

func (*DeleteBuilder) Returning

func (d *DeleteBuilder) Returning(cols ...string) *DeleteBuilder

func (*DeleteBuilder) Run

func (d *DeleteBuilder) Run(ctx context.Context, exec db.Executor) (int64, error)

func (*DeleteBuilder) SQL

func (d *DeleteBuilder) SQL() (string, []any)

func (*DeleteBuilder) Scan added in v0.1.2

func (d *DeleteBuilder) Scan(ctx context.Context, exec db.Executor, dest ...any) error

func (*DeleteBuilder) Where

func (d *DeleteBuilder) Where(c Condition) *DeleteBuilder

type InsertBuilder

type InsertBuilder struct {
	// contains filtered or unexported fields
}

func Insert

func Insert(table namedTable) *InsertBuilder

func (*InsertBuilder) OnConflictDoNothing added in v0.1.2

func (i *InsertBuilder) OnConflictDoNothing(targets ...string) *InsertBuilder

func (*InsertBuilder) OnConflictDoUpdate added in v0.1.2

func (i *InsertBuilder) OnConflictDoUpdate(targets []string, updateVals map[string]any) *InsertBuilder

func (*InsertBuilder) OnConflictExcluded added in v0.1.2

func (i *InsertBuilder) OnConflictExcluded(targets []string, cols ...string) *InsertBuilder

func (*InsertBuilder) Returning

func (i *InsertBuilder) Returning(cols ...string) *InsertBuilder

func (*InsertBuilder) Run

func (i *InsertBuilder) Run(ctx context.Context, exec db.Executor) error

func (*InsertBuilder) SQL

func (i *InsertBuilder) SQL() (string, []any)

func (*InsertBuilder) Scan

func (i *InsertBuilder) Scan(ctx context.Context, exec db.Executor, dest ...any) error

func (*InsertBuilder) ScanInto added in v0.1.4

func (i *InsertBuilder) ScanInto[T any](ctx context.Context, exec db.Executor) (T, error)

func (*InsertBuilder) Values

func (i *InsertBuilder) Values(rows ...map[string]any) *InsertBuilder

type JoinKind

type JoinKind string
const (
	INNER JoinKind = "INNER"
	LEFT  JoinKind = "LEFT"
	RIGHT JoinKind = "RIGHT"
	FULL  JoinKind = "FULL"
	CROSS JoinKind = "CROSS"
)

type NullsOrder added in v0.1.3

type NullsOrder string
const (
	NullsFirst NullsOrder = "NULLS FIRST"
	NullsLast  NullsOrder = "NULLS LAST"
)

type OrderBy

type OrderBy struct {
	// contains filtered or unexported fields
}

type OrderDir

type OrderDir string
const (
	Asc  OrderDir = "ASC"
	Desc OrderDir = "DESC"
)

type SelectBuilder

type SelectBuilder[T any] struct {
	// contains filtered or unexported fields
}

func Select

func Select[T any](cols ...colRef) *SelectBuilder[T]

func (*SelectBuilder[T]) All

func (q *SelectBuilder[T]) All(ctx context.Context, exec db.Executor) ([]T, error)

func (*SelectBuilder[T]) Count

func (q *SelectBuilder[T]) Count(ctx context.Context, exec db.Executor) (int64, error)

func (*SelectBuilder[T]) CountDistinct added in v0.1.3

func (q *SelectBuilder[T]) CountDistinct(ctx context.Context, exec db.Executor, col colRef) (int64, error)

CountDistinct counts the distinct values of col across the query result: `SELECT COUNT(DISTINCT col) FROM (<query>)`.

func (*SelectBuilder[T]) CrossJoin

func (q *SelectBuilder[T]) CrossJoin(table namedTable) *SelectBuilder[T]

func (*SelectBuilder[T]) Distinct

func (q *SelectBuilder[T]) Distinct() *SelectBuilder[T]

func (*SelectBuilder[T]) Except added in v0.1.3

func (q *SelectBuilder[T]) Except(sub Subquery) *SelectBuilder[T]

func (*SelectBuilder[T]) First

func (q *SelectBuilder[T]) First(ctx context.Context, exec db.Executor) (*T, error)

func (*SelectBuilder[T]) ForShare added in v0.1.3

func (q *SelectBuilder[T]) ForShare() *SelectBuilder[T]

ForShare adds `FOR SHARE` (row-level shared lock).

func (*SelectBuilder[T]) ForUpdate added in v0.1.3

func (q *SelectBuilder[T]) ForUpdate() *SelectBuilder[T]

ForUpdate adds `FOR UPDATE` (row-level lock).

func (*SelectBuilder[T]) From

func (q *SelectBuilder[T]) From(table namedTable) *SelectBuilder[T]

func (*SelectBuilder[T]) FullJoin

func (q *SelectBuilder[T]) FullJoin(table namedTable, on Condition) *SelectBuilder[T]

func (*SelectBuilder[T]) GroupBy

func (q *SelectBuilder[T]) GroupBy(cols ...string) *SelectBuilder[T]

func (*SelectBuilder[T]) Having

func (q *SelectBuilder[T]) Having(c Condition) *SelectBuilder[T]

func (*SelectBuilder[T]) InnerJoin

func (q *SelectBuilder[T]) InnerJoin(table namedTable, on Condition) *SelectBuilder[T]

func (*SelectBuilder[T]) Intersect added in v0.1.3

func (q *SelectBuilder[T]) Intersect(sub Subquery) *SelectBuilder[T]

func (*SelectBuilder[T]) LeftJoin

func (q *SelectBuilder[T]) LeftJoin(table namedTable, on Condition) *SelectBuilder[T]

func (*SelectBuilder[T]) Limit

func (q *SelectBuilder[T]) Limit(n int) *SelectBuilder[T]

func (*SelectBuilder[T]) NoWait added in v0.1.3

func (q *SelectBuilder[T]) NoWait() *SelectBuilder[T]

NoWait makes a locking clause fail immediately if rows are locked.

func (*SelectBuilder[T]) Offset

func (q *SelectBuilder[T]) Offset(n int) *SelectBuilder[T]

func (*SelectBuilder[T]) OrderBy

func (q *SelectBuilder[T]) OrderBy(cols []string, dir OrderDir) *SelectBuilder[T]

func (*SelectBuilder[T]) OrderByNulls added in v0.1.3

func (q *SelectBuilder[T]) OrderByNulls(cols []string, dir OrderDir, nulls NullsOrder) *SelectBuilder[T]

OrderByNulls is OrderBy with an explicit NULLS FIRST / NULLS LAST placement.

func (*SelectBuilder[T]) RightJoin

func (q *SelectBuilder[T]) RightJoin(table namedTable, on Condition) *SelectBuilder[T]

func (*SelectBuilder[T]) SQL

func (q *SelectBuilder[T]) SQL() (string, []any)

func (*SelectBuilder[T]) SkipLocked added in v0.1.3

func (q *SelectBuilder[T]) SkipLocked() *SelectBuilder[T]

SkipLocked skips rows locked by other transactions (SELECT ... FOR UPDATE SKIP LOCKED).

func (*SelectBuilder[T]) Union added in v0.1.3

func (q *SelectBuilder[T]) Union(sub Subquery) *SelectBuilder[T]

func (*SelectBuilder[T]) UnionAll added in v0.1.3

func (q *SelectBuilder[T]) UnionAll(sub Subquery) *SelectBuilder[T]

func (*SelectBuilder[T]) Where

func (q *SelectBuilder[T]) Where(c Condition) *SelectBuilder[T]

func (*SelectBuilder[T]) With added in v0.1.3

func (q *SelectBuilder[T]) With(name string, sub Subquery) *SelectBuilder[T]

With prepends a common table expression: `WITH name AS (SELECT ...)`.

type Subquery added in v0.1.3

type Subquery struct {
	// contains filtered or unexported fields
}

func Sub added in v0.1.3

func Sub[T any](b *SelectBuilder[T]) Subquery

Sub captures a SelectBuilder as a reusable Subquery. The inner generic row type is never scanned into

func (Subquery) As added in v0.1.3

func (s Subquery) As(alias string) ColRef

As renders the subquery as a column reference with the given alias, e.g. `(SELECT ...) AS total`. Only usable for subqueries without bound arguments: placeholder values can't be threaded through the outer select list yet.

type UpdateBuilder

type UpdateBuilder struct {
	// contains filtered or unexported fields
}

func Update

func Update(table namedTable) *UpdateBuilder

func (*UpdateBuilder) Limit added in v0.1.3

func (u *UpdateBuilder) Limit(n int) *UpdateBuilder

Limit caps how many rows the update touches (PostgreSQL UPDATE ... LIMIT n).

func (*UpdateBuilder) OrderBy added in v0.1.3

func (u *UpdateBuilder) OrderBy(cols []string, dir OrderDir) *UpdateBuilder

func (*UpdateBuilder) OrderByNulls added in v0.1.3

func (u *UpdateBuilder) OrderByNulls(cols []string, dir OrderDir, nulls NullsOrder) *UpdateBuilder

func (*UpdateBuilder) Returning

func (u *UpdateBuilder) Returning(cols ...string) *UpdateBuilder

func (*UpdateBuilder) Run

func (u *UpdateBuilder) Run(ctx context.Context, exec db.Executor) (int64, error)

func (*UpdateBuilder) SQL

func (u *UpdateBuilder) SQL() (string, []any)

func (*UpdateBuilder) Scan added in v0.1.2

func (u *UpdateBuilder) Scan(ctx context.Context, exec db.Executor, dest ...any) error

func (*UpdateBuilder) ScanInto added in v0.1.4

func (u *UpdateBuilder) ScanInto[T any](ctx context.Context, exec db.Executor) (T, error)

func (*UpdateBuilder) Set

func (u *UpdateBuilder) Set(vals map[string]any) *UpdateBuilder

Set assigns columns. Later calls overwrite earlier values for the same column instead of appending a duplicate SET clause. Values may be:

  • literals (bound as placeholders): Set(map[string]any{"name": "foo"})
  • column references (rendered as-is): Set(map[string]any{"count": seq})
  • subqueries (rendered as `col = (SELECT ...)`): Set(map[string]any{"total": sub})

func (*UpdateBuilder) Where

func (u *UpdateBuilder) Where(c Condition) *UpdateBuilder

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL