query

package
v1.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package query is a type-safe builder for package ast. It does not produce SQL text.

Index

Constants

View Source
const (
	OpEq  = ast.OpEq
	OpNe  = ast.OpNe
	OpLt  = ast.OpLt
	OpLte = ast.OpLte
	OpGt  = ast.OpGt
	OpGte = ast.OpGte

	Any = ast.Any
	All = ast.All

	JoinInner     = ast.JoinInner
	JoinLeftOuter = ast.JoinLeftOuter
	JoinCross     = ast.JoinCross

	Ascending  = ast.Ascending
	Descending = ast.Descending

	TypeVarchar   = ast.TypeVarchar
	TypeInteger   = ast.TypeInteger
	TypeBigint    = ast.TypeBigint
	TypeNumeric   = ast.TypeNumeric
	TypeVarbinary = ast.TypeVarbinary

	KindUser    = ast.KindUser
	KindUserset = ast.KindUserset
	KindAll     = ast.KindAll
)

Variables

View Source
var AggDistinct = AggOption{/* contains filtered or unexported fields */}

AggDistinct makes Count(x, AggDistinct) COUNT(DISTINCT x).

View Source
var Star starArg

Star makes Count(Star) COUNT(*).

Functions

This section is empty.

Types

type AggOption

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

AggOption is AggDistinct or AggFilter.

func AggFilter

func AggFilter(cond Predicate) AggOption

AggFilter is FILTER (WHERE cond).

type Aliased

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

Aliased is a named result column.

func As

func As(p Projection, alias string) Aliased

As names a result column.

type AnyExpr

type AnyExpr interface {
	// contains filtered or unexported methods
}

AnyExpr is an Expr of any T.

type AnySet

type AnySet[T Scalar] interface {
	// contains filtered or unexported methods
}

AnySet is a BoundSet or SetExpr. Elem is never called; it only ties the set to T.

type BoundSet

type BoundSet[T Scalar] struct {
	// contains filtered or unexported fields
}

BoundSet is a list of T values sent as one parameter, for use with Quantified.

func BindAll

func BindAll[T Scalar](vs []T) BoundSet[T]

BindAll sends vs as one list parameter.

type CaseSearched

type CaseSearched[T Scalar] struct {
	// contains filtered or unexported fields
}

CaseSearched builds "CASE WHEN cond THEN ... END". Start one with Case:

query.Case[string]().
	When(query.Eq(t.ObjectType(), query.Lit("folder")), t.ObjectID()).
	Else(query.Lit("")).
	End()

func Case

func Case[T Scalar]() *CaseSearched[T]

func (*CaseSearched[T]) Else

func (c *CaseSearched[T]) Else(result Expr[T]) *CaseSearched[T]

Else replaces any earlier Else.

func (*CaseSearched[T]) End

func (c *CaseSearched[T]) End() Expr[T]

End panics if there are no branches.

func (*CaseSearched[T]) When

func (c *CaseSearched[T]) When(cond Predicate, then Expr[T]) *CaseSearched[T]

type CaseSimple

type CaseSimple[T Scalar, B Comparable] struct {
	// contains filtered or unexported fields
}

CaseSimple builds "CASE base WHEN value THEN ... END". Start one with CaseOf:

query.CaseOf[int64](t.ObjectType()).
	When(query.Lit("folder"), query.Lit[int64](1)).
	Else(query.Lit[int64](0)).
	End()

func CaseOf

func CaseOf[T Scalar, B Comparable](base Expr[B]) *CaseSimple[T, B]

func (*CaseSimple[T, B]) Else

func (c *CaseSimple[T, B]) Else(result Expr[T]) *CaseSimple[T, B]

Else replaces any earlier Else.

func (*CaseSimple[T, B]) End

func (c *CaseSimple[T, B]) End() Expr[T]

End panics if there are no branches.

func (*CaseSimple[T, B]) When

func (c *CaseSimple[T, B]) When(value Expr[B], then Expr[T]) *CaseSimple[T, B]

type CastType

type CastType = ast.CastType

type Comparable

type Comparable interface {
	Scalar
	comparable
}

Comparable is Scalar without []byte.

type CountArg

type CountArg interface {
	// contains filtered or unexported methods
}

CountArg is any Expr, or Star.

type Expr

type Expr[T Scalar] struct {
	// contains filtered or unexported fields
}

Expr is a SQL value of Go type T.

func Bind

func Bind[T Scalar](v T) Expr[T]

Bind sends v as a parameter.

func Cast

func Cast[To Scalar](e AnyExpr, t CastType) Expr[To]

func Coalesce

func Coalesce[T Scalar](es ...Expr[T]) Expr[T]

func Count

func Count(e CountArg, opts ...AggOption) Expr[int64]

func JSONArray

func JSONArray(es ...AnyExpr) Expr[[]byte]

func JSONObject

func JSONObject(pairs ...JSONPair) Expr[[]byte]

func Lit

func Lit[T Literal](v T) Expr[T]

Lit inlines v into the SQL text.

func Lower

func Lower(e Expr[string]) Expr[string]

func ScalarExpr

func ScalarExpr[T Scalar](s *Statement) Expr[T]

ScalarExpr uses a single-value subquery as an Expr.

func Upper

func Upper(e Expr[string]) Expr[string]

type JSONPair

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

JSONPair is one key/value pair for JSONObject.

func Pair

func Pair[T Scalar](key Expr[string], value Expr[T]) JSONPair

type JoinType

type JoinType = ast.JoinType

type Literal

type Literal interface {
	// contains filtered or unexported methods
}

Literal excludes time.Time, whose literal syntax differs by database.

type Op

type Op = ast.Op

type OrderTerm

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

OrderTerm is one ORDER BY term.

func Asc

func Asc(e AnyExpr) OrderTerm

func Desc

func Desc(e AnyExpr) OrderTerm

func Order

func Order(e AnyExpr, dir SortDirection) OrderTerm

type Ordered

type Ordered interface {
	ordinal | time.Time
}

type Predicate

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

Predicate is a true/false expression. It is not an Expr[bool] because some databases have no boolean type, so it cannot be a value or result column.

func And

func And(p Predicate, ps ...Predicate) Predicate

And and Or require at least one predicate; a single predicate is returned as-is.

func Between

func Between[T Ordered](a, lo, hi Expr[T]) Predicate

Between is "a BETWEEN lo AND hi".

func Compare

func Compare[T Ordered](op Op, a, b Expr[T]) Predicate

func Eq

func Eq[T Comparable](a, b Expr[T]) Predicate

func Exists

func Exists(s *Statement) Predicate

Exists is "EXISTS (stmt)".

func False

func False() Predicate

func Gt

func Gt[T Ordered](a, b Expr[T]) Predicate

func Gte

func Gte[T Ordered](a, b Expr[T]) Predicate

func In

func In[T Comparable](a Expr[T], vs ...Expr[T]) Predicate

In is "a IN (vs...)". For a list parameter or subquery, use Quantified.

func IsNull

func IsNull[T Scalar](a Expr[T]) Predicate

IsNull is "a IS NULL".

func Like

func Like(a, pattern Expr[string]) Predicate

Like is "a LIKE pattern". For case-insensitive matching, wrap both sides in Lower.

func Lt

func Lt[T Ordered](a, b Expr[T]) Predicate

func Lte

func Lte[T Ordered](a, b Expr[T]) Predicate

func Ne

func Ne[T Comparable](a, b Expr[T]) Predicate

func Not

func Not(p Predicate) Predicate

func Or

func Or(p Predicate, ps ...Predicate) Predicate

func Quantified

func Quantified[T Comparable](a Expr[T], op Op, q Quantifier, set AnySet[T]) Predicate

Quantified is "a op ANY (set)" or "a op ALL (set)".

func True

func True() Predicate

type Projection

type Projection interface {
	// contains filtered or unexported methods
}

Projection is anything that can go in a SELECT list.

type Quantifier

type Quantifier = ast.Quantifier

type Scalar

type Scalar interface {
	Ordered | ~[]byte
}

Scalar follows database/sql/driver.Value, minus bool (use Predicate). Time.Time has no ~ because drivers reject named struct types.

type SetExpr

type SetExpr[T Scalar] struct {
	// contains filtered or unexported fields
}

SetExpr is a multi-row subquery of T values.

func SetSubExpr

func SetSubExpr[T Scalar](s *Statement) SetExpr[T]

SetSubExpr uses a multi-row subquery as a SetExpr.

type SortDirection

type SortDirection = ast.SortDirection

type Statement

type Statement struct{ ast.Select }

Statement builds an ast.Select. Where, Having, Limit, and Offset replace earlier values; other methods append.

func Select

func Select(cols ...Projection) *Statement

func (*Statement) Columns

func (s *Statement) Columns(cols ...Projection) *Statement

func (*Statement) CrossJoin

func (s *Statement) CrossJoin(t Tuple) *Statement

func (*Statement) Distinct

func (s *Statement) Distinct() *Statement

func (*Statement) From

func (s *Statement) From(ts ...Tuple) *Statement

func (*Statement) GroupBy

func (s *Statement) GroupBy(es ...AnyExpr) *Statement

func (*Statement) Having

func (s *Statement) Having(p Predicate) *Statement

func (*Statement) Join

func (s *Statement) Join(t Tuple, on Predicate) *Statement

func (*Statement) JoinOn

func (s *Statement) JoinOn(jt JoinType, t Tuple, on Predicate) *Statement

func (*Statement) Limit

func (s *Statement) Limit(n uint64) *Statement

func (*Statement) Offset

func (s *Statement) Offset(n uint64) *Statement

func (*Statement) OrderBy

func (s *Statement) OrderBy(terms ...OrderTerm) *Statement

func (*Statement) Where

func (s *Statement) Where(p Predicate) *Statement

type SubjectKinds

type SubjectKinds = ast.SubjectKinds

type Tuple

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

Tuple is the tuple table under an alias. Its methods return columns qualified by that alias.

func NewTuple

func NewTuple(alias string) Tuple

func (Tuple) Condition

func (t Tuple) Condition() Expr[string]

func (Tuple) ConditionContext

func (t Tuple) ConditionContext() Expr[[]byte]

ConditionContext is a proto-encoded structpb.Struct.

func (Tuple) ObjectID

func (t Tuple) ObjectID() Expr[string]

func (Tuple) ObjectRelation

func (t Tuple) ObjectRelation() Expr[string]

func (Tuple) ObjectType

func (t Tuple) ObjectType() Expr[string]

func (Tuple) Store

func (t Tuple) Store() Expr[string]

func (Tuple) SubjectID

func (t Tuple) SubjectID() Expr[string]

func (Tuple) SubjectKindIn

func (t Tuple) SubjectKindIn(kinds SubjectKinds) Predicate

SubjectKindIn is true when the subject is one of kinds.

func (Tuple) SubjectRelation

func (t Tuple) SubjectRelation() Expr[string]

SubjectRelation is e.g. `member` in `group:eng#member`. It is also empty for wildcards like `user:*`, so empty does not imply a non-userset subject.

func (Tuple) SubjectType

func (t Tuple) SubjectType() Expr[string]

Jump to

Keyboard shortcuts

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