query

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expr

type Expr interface {
	Args() []any

	Build() string
}

Expr represents an SQL expression.

Args returns the list of arguments the SQL expression has. This is used to correctly handle the parameter binding of dynamic arguments into a query. It is valid for this to return nil.

Build returns the SQL code that represents the expression.

func And

func And(conds ...Expr) Expr

And takes the given expressions and joins them together using the AND clause when built.

func Arg

func Arg(val any) Expr

Arg turns the given value into an argument expression. This would be used for passing user provided values into a query that should be bound to parameters.

func As

func As(in Expr, out string) Expr

As specifies an AS expression on the given expression. For example,

query.As(query.Count("id"), "id_count")

func ColumnAs

func ColumnAs(in, out string) Expr

ColumnAs specifies an AS expression on the two columns.

func Columns

func Columns(cols ...string) Expr

Columns turns the given strings into a list expression of column names. This would be used to control which columns should be retrieved as part of a SELECT query.

Calling this function is no different than calling query.List and passing multiple query.Ident expressions, for example,

query.List(query.Ident("id"), query.Ident("email"))

The main difference is that the above will return an expression wrapped in a pair of parentheses. This is still valid SQL code, this function simply exists for ease of use.

func Count

func Count(cols ...string) Expr

Count returns the COUNT aggregate call expression on the given columns.

func Eq

func Eq(a, b Expr) Expr

Eq a = b

func Exprs

func Exprs(e ...Expr) Expr

Exprs turns the list of expressions into a single expression. When built the expressions will be separated by the string ", ".

func Geq

func Geq(a, b Expr) Expr

Get a >= b

func Gt

func Gt(a, b Expr) Expr

Gt a > b

func Ident

func Ident(s string) Expr

Ident turns the given string into an identifier expression. This would be used for referring directly to a column name in SQL.

For example,

query.Join("users", query.Eq(query.Ident("posts.user_id"), query.Ident("users.id")))

becomes,

JOIN users ON posts.user_id = users.id

func In

func In(a, b Expr) Expr

In a IN b

func Is

func Is(a, b Expr) Expr

Is a IS b

func IsNot

func IsNot(a, b Expr) Expr

IsNot a IS NOT b

func Leq

func Leq(a, b Expr) Expr

Leq a <= b

func Like

func Like(a, b Expr) Expr

Like a LIKE b

func List

func List(vals ...any) Expr

List turns the given values into a list expression. If the given values are lists then they will be wrapped appropriately. If the given values are literal expressions, then they will end up in the built SQL code verbatim and not as placeholders.

func Lit

func Lit(val any) Expr

Lit turns the given value in a literal expression. This is passed directly through into the query, and does not use a parameter binding for it.

func Lower added in v1.2.0

func Lower(expr Expr) Expr

func Lt

func Lt(a, b Expr) Expr

Lt a < b

func Neq

func Neq(a, b Expr) Expr

Neq a != b

func NotIn

func NotIn(a, b Expr) Expr

NotIn a NOT IN b

func Or

func Or(conds ...Expr) Expr

Or takes the given expressions and joins them together using the OR clause when built.

func Sum

func Sum(expr Expr) Expr

Sum returns the SUM aggregate call expression on the given column.

type Option

type Option func(*Query) *Query

func From

func From(table string) Option

func FromAs

func FromAs(table, alias string) Option

func Join

func Join(table string, expr Expr) Option

func Limit

func Limit(n int64) Option

func Offset

func Offset(n int64) Option

func Options

func Options(opts ...Option) Option

func OrWhere

func OrWhere(expr Expr) Option

func OrWhereEq

func OrWhereEq(col string, expr Expr) Option

func OrWhereGeq

func OrWhereGeq(col string, expr Expr) Option

func OrWhereGt

func OrWhereGt(col string, expr Expr) Option

func OrWhereIn

func OrWhereIn(col string, expr Expr) Option

func OrWhereIs

func OrWhereIs(col string, expr Expr) Option

func OrWhereIsNil

func OrWhereIsNil(col string) Option

func OrWhereIsNot

func OrWhereIsNot(col string, expr Expr) Option

func OrWhereIsNotNil

func OrWhereIsNotNil(col string) Option

func OrWhereLeq

func OrWhereLeq(col string, expr Expr) Option

func OrWhereLt

func OrWhereLt(col string, expr Expr) Option

func OrWhereNotEq

func OrWhereNotEq(col string, expr Expr) Option

func OrWhereNotIn

func OrWhereNotIn(col string, expr Expr) Option

func OrderAsc

func OrderAsc(cols ...string) Option

func OrderDesc

func OrderDesc(cols ...string) Option

func Returning

func Returning(cols ...string) Option

func Set

func Set(col string, expr Expr) Option

func Values

func Values(vals ...any) Option

func Where

func Where(expr Expr) Option

func WhereEq

func WhereEq(col string, expr Expr) Option

func WhereGeq

func WhereGeq(col string, expr Expr) Option

func WhereGt

func WhereGt(col string, expr Expr) Option

func WhereIn

func WhereIn(col string, expr Expr) Option

func WhereIs

func WhereIs(col string, expr Expr) Option

func WhereIsNil

func WhereIsNil(col string) Option

func WhereIsNot

func WhereIsNot(col string, expr Expr) Option

func WhereIsNotNil

func WhereIsNotNil(col string) Option

func WhereLeq

func WhereLeq(col string, expr Expr) Option

func WhereLike

func WhereLike(col string, expr Expr) Option

func WhereLt

func WhereLt(col string, expr Expr) Option

func WhereNotEq

func WhereNotEq(col string, expr Expr) Option

func WhereNotIn

func WhereNotIn(col string, expr Expr) Option

type Query

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

func Delete

func Delete(table string, opts ...Option) *Query

func Insert

func Insert(table string, expr Expr, opts ...Option) *Query

func Select

func Select(expr Expr, opts ...Option) *Query

func SelectDistinct

func SelectDistinct(expr Expr, opts ...Option) *Query

func SelectDistinctOn

func SelectDistinctOn(expr1, expr2 Expr, opts ...Option) *Query

func Union

func Union(queries ...*Query) *Query

func Update

func Update(table string, opts ...Option) *Query

func (*Query) Args

func (q *Query) Args() []any

func (*Query) Build

func (q *Query) Build() string

Jump to

Keyboard shortcuts

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