builder

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFromItemLateralAndOnly = errors.New("cannot specify both LATERAL and ONLY")
View Source
var ErrInvalidIdentifier = errors.New("invalid identifier")
View Source
var ErrNoConditionsGiven = errors.New("no conditions given")

Functions

This section is empty.

Types

type AggExpBuilder

type AggExpBuilder struct {
	ExpBase
	// contains filtered or unexported fields
}

func Agg

func Agg(name string, exps []Exp) AggExpBuilder

func (AggExpBuilder) Distinct

func (b AggExpBuilder) Distinct() AggExpBuilder

func (AggExpBuilder) Filter

func (b AggExpBuilder) Filter(cond Exp) AggExpBuilder

Filter adds a filter to the aggregate function. Multiple calls to Filter are joined with AND.

func (AggExpBuilder) IsExp

func (b AggExpBuilder) IsExp()

func (AggExpBuilder) OrderBy

func (b AggExpBuilder) OrderBy(exp Exp) OrderByAggExpBuilder

OrderBy adds an ORDER BY clause to the aggregate function. If AggExpBuilder.WithinGroup is called, the ORDER BY clause is used after the aggregate function in WITHIN GROUP.

func (AggExpBuilder) WithinGroup

func (b AggExpBuilder) WithinGroup() AggExpBuilder

WithinGroup adds a WITHIN GROUP order by clause after the aggregate function. Sort arguments are added via AggExpBuilder.OrderBy.

func (AggExpBuilder) WriteSQL

func (b AggExpBuilder) WriteSQL(sb *SQLBuilder)

type CaseBuilder

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

func Case

func Case(exp ...Exp) CaseBuilder

func (CaseBuilder) Else

func (b CaseBuilder) Else(result Exp) CaseBuilder

func (CaseBuilder) End

func (b CaseBuilder) End() CaseExp

func (CaseBuilder) When

func (b CaseBuilder) When(condition Exp) CaseWhenBuilder

type CaseExp

type CaseExp struct {
	ExpBase
	// contains filtered or unexported fields
}

func (CaseExp) WriteSQL

func (c CaseExp) WriteSQL(sb *SQLBuilder)

type CaseWhenBuilder

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

func (CaseWhenBuilder) Then

func (c CaseWhenBuilder) Then(result Exp) CaseBuilder

type CombinationBuilder

type CombinationBuilder struct {
	SelectBuilder
}

func (CombinationBuilder) All

type DeleteBuilder

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

func DeleteFrom

func DeleteFrom(tableName string) DeleteBuilder

func (DeleteBuilder) As

func (b DeleteBuilder) As(alias string) DeleteBuilder

func (DeleteBuilder) Where

func (b DeleteBuilder) Where(cond Exp) DeleteBuilder

Where adds a WHERE condition to the delete. Multiple calls to Where are joined with AND.

func (DeleteBuilder) WriteSQL

func (b DeleteBuilder) WriteSQL(sb *SQLBuilder)

type Exp

type Exp interface {
	IsExp()
	SQLWriter
}

func And

func And(exps ...Exp) Exp

func Arg

func Arg(argument any) Exp

Arg creates an expression that represents an argument that will be bound to a placeholder with the given value. Each call to Arg will create a new placeholder and emit the argument when writing the query.

func Array

func Array(elems ...Exp) Exp

Array builds an array literal.

Make sure that all elements are of the same type.

func Bind

func Bind(argName string) Exp

Bind creates an expression that represents an argument that will be bound to a placeholder with the given value. If Bind is called again with the same name, the same placeholder will be used.

func Bool

func Bool(b bool) Exp

func Default

func Default() Exp

Default builds the DEFAULT keyword.

func Float

func Float(f float64) Exp

func Int

func Int(s int) Exp

func Interval

func Interval(spec string) Exp

Interval builds an interval constant.

func Not

func Not(e Exp) Exp

Not builds a NOT expression.

func Null

func Null() Exp

Null builds the NULL literal.

func Or

func Or(exps ...Exp) Exp

func String

func String(s string) Exp

type ExpBase

type ExpBase struct {
	Exp
}

ExpBase is a base type for expressions to allow embedding of various default operators.

func (ExpBase) Cast

func (b ExpBase) Cast(typ string) Exp

func (ExpBase) Concat

func (b ExpBase) Concat(rgt Exp) Exp

func (ExpBase) Eq

func (b ExpBase) Eq(rgt Exp) Exp

func (ExpBase) Gt

func (b ExpBase) Gt(rgt Exp) Exp

func (ExpBase) Gte

func (b ExpBase) Gte(rgt Exp) Exp

func (ExpBase) ILike

func (b ExpBase) ILike(rgt Exp) MatchingBuilder

func (ExpBase) IsExp

func (ExpBase) IsExp()

func (ExpBase) IsNotNull

func (b ExpBase) IsNotNull() Exp

IsNotNull builds an IS NOT NULL expression.

func (ExpBase) IsNull

func (b ExpBase) IsNull() Exp

IsNull builds an IS NULL expression.

func (ExpBase) JsonContainedBy

func (b ExpBase) JsonContainedBy(rgt Exp) Exp

func (ExpBase) JsonContains

func (b ExpBase) JsonContains(rgt Exp) Exp

func (ExpBase) JsonExtract

func (b ExpBase) JsonExtract(rgt Exp) Exp

JsonExtract builds the -> operator for json / jsonb.

json -> text → json
jsonb -> text → jsonb

Extracts JSON object field with the given key.

json -> integer → json
jsonb -> integer → jsonb

Extracts nth element of JSON array (array elements are indexed from zero, but negative integers count from the end).

func (ExpBase) JsonExtractPath

func (b ExpBase) JsonExtractPath(rgt Exp) Exp

JsonExtractPath builds the #> operator for json / jsonb.

json #> text[] → json
jsonb #> text[] → jsonb

Extracts JSON sub-object at the specified path, where path elements can be either field keys or array indexes.

Example:

fn.JsonExtractPath(qrb.String(`{"a": {"b": ["foo","bar"]}}`).Cast("jsonb"), qrb.Array(qrb.String("a"), qrb.String("b")))

func (ExpBase) JsonExtractPathText

func (b ExpBase) JsonExtractPathText(rgt Exp) Exp

JsonExtractPathText builds the #>> operator for json / jsonb.

json #>> text[] → text
jsonb #>> text[] → text

Extracts JSON sub-object at the specified path as text.

Example:

fn.JsonExtractPathText(qrb.String(`{"a": {"b": ["foo","bar"]}}`).Cast("jsonb"), qrb.Array(qrb.String("a"), qrb.String("b"), qrb.String("1)))

func (ExpBase) JsonExtractText

func (b ExpBase) JsonExtractText(rgt Exp) Exp

JsonExtractText builds the ->> operator for json / jsonb.

json ->> text → text
jsonb ->> text → text

Extracts JSON object field with the given key, as text.

json ->> integer → text
jsonb ->> integer → text

Extracts nth element of JSON array, as text.

func (ExpBase) Like

func (b ExpBase) Like(rgt Exp) MatchingBuilder

func (ExpBase) Lt

func (b ExpBase) Lt(rgt Exp) Exp

func (ExpBase) Lte

func (b ExpBase) Lte(rgt Exp) Exp

func (ExpBase) Neq

func (b ExpBase) Neq(rgt Exp) Exp

func (ExpBase) NotILike

func (b ExpBase) NotILike(rgt Exp) MatchingBuilder

func (ExpBase) NotLike

func (b ExpBase) NotLike(rgt Exp) MatchingBuilder

func (ExpBase) NotSimilarTo

func (b ExpBase) NotSimilarTo(rgt Exp) MatchingBuilder

func (ExpBase) Op

func (b ExpBase) Op(op Operator, rgt Exp) Exp

Op allows to use arbitrary operators.

Example:

N("a").Op(OpAdd, Int(5))

func (ExpBase) SimilarTo

func (b ExpBase) SimilarTo(rgt Exp) MatchingBuilder

type FromExp

type FromExp interface {
	SQLWriter // We do not actually use Exp here, since this cannot appear anywhere outside the FROM clause.
	// contains filtered or unexported methods
}

type FromLateralExp

type FromLateralExp interface {
	FromExp
	// contains filtered or unexported methods
}

type FromSelectBuilder

type FromSelectBuilder struct {
	SelectBuilder
}

func (FromSelectBuilder) As

As sets the alias for the last added from item.

func (FromSelectBuilder) ColumnAliases

func (b FromSelectBuilder) ColumnAliases(aliases ...string) FromSelectBuilder

ColumnAliases sets the column aliases for the last added from item.

type FuncBuilder

type FuncBuilder struct {
	ExpBase
	// contains filtered or unexported fields
}

func Func

func Func(name string, args ...Exp) FuncBuilder

func (FuncBuilder) As

func (f FuncBuilder) As(alias string) FuncBuilder

func (FuncBuilder) ColumnDefinition

func (f FuncBuilder) ColumnDefinition(name, typ string) FuncBuilder

ColumnDefinition adds a column definition to the function call. To add multiple column definitions, call this method multiple times.

func (FuncBuilder) IsExp

func (f FuncBuilder) IsExp()

func (FuncBuilder) WithOrdinality

func (f FuncBuilder) WithOrdinality() FuncBuilder

func (FuncBuilder) WriteSQL

func (f FuncBuilder) WriteSQL(sb *SQLBuilder)

type FuncExp

type FuncExp struct {
	ExpBase
	// contains filtered or unexported fields
}

func Coalesce

func Coalesce(exp Exp, rest ...Exp) FuncExp

func Greatest

func Greatest(exp Exp, rest ...Exp) FuncExp

func Least

func Least(exp Exp, rest ...Exp) FuncExp

func NullIf

func NullIf(value1, value2 Exp) FuncExp

func (FuncExp) IsExp

func (c FuncExp) IsExp()

func (FuncExp) WriteSQL

func (c FuncExp) WriteSQL(sb *SQLBuilder)

type GroupyBySelectBuilder

type GroupyBySelectBuilder struct {
	SelectBuilder
}

func (GroupyBySelectBuilder) Cube

Cube adds a CUBE grouping element for the given expression sets to the GROUP BY clause.

func (GroupyBySelectBuilder) Distinct

Distinct adds the DISTINCT keyword to the GROUP BY clause.

func (GroupyBySelectBuilder) Empty

Empty adds an empty grouping element to the GROUP BY clause.

func (GroupyBySelectBuilder) GroupingSets

func (b GroupyBySelectBuilder) GroupingSets(sets ...[]Exp) GroupyBySelectBuilder

GroupingSets adds a GROUPING SETS grouping element for the given expression sets to the GROUP BY clause.

func (GroupyBySelectBuilder) Rollup

func (b GroupyBySelectBuilder) Rollup(sets ...[]Exp) GroupyBySelectBuilder

Rollup adds a ROLLUP grouping element for the given expression sets to the GROUP BY clause.

type IdentExp

type IdentExp struct {
	ExpBase
	// contains filtered or unexported fields
}

func N

func N(s string) IdentExp

N writes the given name / identifier.

It will validate the identifier when writing the query, but it will not detect all invalid identifiers that are invalid in PostgreSQL (especially considering reserved keywords).

func (IdentExp) IsExp

func (i IdentExp) IsExp()

func (IdentExp) WriteSQL

func (i IdentExp) WriteSQL(sb *SQLBuilder)

type InsertBuilder

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

func InsertInto

func InsertInto(tableName string) InsertBuilder

func (InsertBuilder) As

func (b InsertBuilder) As(alias string) InsertBuilder

func (InsertBuilder) ColumnNames

func (b InsertBuilder) ColumnNames(columnName string, rest ...string) InsertBuilder

func (InsertBuilder) SetMap

func (b InsertBuilder) SetMap(m map[string]any) InsertBuilder

SetMap sets the column names and values to insert from the given map. It overwrites any previous column names and values.

func (InsertBuilder) Values

func (b InsertBuilder) Values(values ...Exp) InsertBuilder

Values appends the given values to insert. It can be called multiple times to insert multiple rows.

func (InsertBuilder) WriteSQL

func (b InsertBuilder) WriteSQL(sb *SQLBuilder)

type JoinSelectBuilder

type JoinSelectBuilder struct {
	SelectBuilder
}

func (JoinSelectBuilder) As

func (JoinSelectBuilder) On

func (b JoinSelectBuilder) On(cond Exp, rest ...Exp) SelectBuilder

func (JoinSelectBuilder) Using

func (b JoinSelectBuilder) Using(columns ...string) SelectBuilder

type JsonBuildObjectBuilder

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

func JsonBuildObject

func JsonBuildObject() JsonBuildObjectBuilder

func (JsonBuildObjectBuilder) IsExp

func (b JsonBuildObjectBuilder) IsExp()

func (JsonBuildObjectBuilder) Prop

func (JsonBuildObjectBuilder) PropIf

func (b JsonBuildObjectBuilder) PropIf(condition bool, key string, value Exp) JsonBuildObjectBuilder

func (JsonBuildObjectBuilder) Start

func (JsonBuildObjectBuilder) Unset

func (JsonBuildObjectBuilder) WriteSQL

func (b JsonBuildObjectBuilder) WriteSQL(sb *SQLBuilder)

type JsonBuildObjectBuilderBuilder

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

func (*JsonBuildObjectBuilderBuilder) End

func (*JsonBuildObjectBuilderBuilder) Prop

func (*JsonBuildObjectBuilderBuilder) PropIf

func (bb *JsonBuildObjectBuilderBuilder) PropIf(condition bool, key string, value Exp) *JsonBuildObjectBuilderBuilder

type MatchingBuilder

type MatchingBuilder interface {
	Exp
	Escape(escapeCharacter rune) MatchingBuilder
}

type Operator

type Operator string
const (
	OpAdd Operator = "+"
	OpSub Operator = "-"
	OpMul Operator = "*"
	OpDiv Operator = "/"

	// OpRegexpMatch matches a string with a POSIX regular expression pattern, case-sensitive.
	OpRegexpMatch Operator = "~"
	// OpRegexpIMatch matches a string with a POSIX regular expression pattern, case-insensitive.
	OpRegexpIMatch Operator = "~*"
	// OpRegexpNotMatch does not match a string with a POSIX regular expression pattern, case-sensitive.
	OpRegexpNotMatch Operator = "!~"
	// OpRegexpINotMatch does not match a string with a POSIX regular expression pattern, case-insensitive.
	OpRegexpINotMatch Operator = "!~*"
)

type OrderByAggExpBuilder

type OrderByAggExpBuilder struct {
	AggExpBuilder
}

func (OrderByAggExpBuilder) Asc

func (OrderByAggExpBuilder) Desc

func (OrderByAggExpBuilder) NullsFirst

func (OrderByAggExpBuilder) NullsLast

type OrderBySelectBuilder

type OrderBySelectBuilder struct {
	SelectBuilder
}

func (OrderBySelectBuilder) Asc

func (OrderBySelectBuilder) Desc

func (OrderBySelectBuilder) NullsFirst

func (OrderBySelectBuilder) NullsLast

type QueryBuilder

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

func Build

func Build(builder SQLWriter) *QueryBuilder

Build starts a new query builder based on the given SQLWriter. For executing the query, use qrbpgx.Build or qrbsql.Build which can set an executor specific to a driver.

func (*QueryBuilder) ToSQL

func (b *QueryBuilder) ToSQL() (sql string, args []any, err error)

func (*QueryBuilder) WithNamedArgs

func (b *QueryBuilder) WithNamedArgs(args map[string]any) *QueryBuilder

func (*QueryBuilder) WithoutValidation

func (b *QueryBuilder) WithoutValidation() *QueryBuilder

WithoutValidation disables validation of the query while building.

Errors might still occur when building the query, but no additional validation (like validating identifiers) will be performed.

type RowsFromBuilder

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

func NewRowsFromBuilder

func NewRowsFromBuilder(fns ...FuncBuilder) RowsFromBuilder

func (RowsFromBuilder) WithOrdinality

func (r RowsFromBuilder) WithOrdinality() RowsFromBuilder

func (RowsFromBuilder) WriteSQL

func (r RowsFromBuilder) WriteSQL(sb *SQLBuilder)

type SQLBuilder

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

func (*SQLBuilder) AddError

func (b *SQLBuilder) AddError(err error)

func (*SQLBuilder) BindPlaceholder

func (b *SQLBuilder) BindPlaceholder(name string) string

func (*SQLBuilder) CreatePlaceholder

func (b *SQLBuilder) CreatePlaceholder(argument any) string

func (*SQLBuilder) Validating

func (b *SQLBuilder) Validating() bool

func (*SQLBuilder) WriteRune

func (b *SQLBuilder) WriteRune(r rune)

func (*SQLBuilder) WriteString

func (b *SQLBuilder) WriteString(s string)

type SQLWriter

type SQLWriter interface {
	WriteSQL(sb *SQLBuilder)
}

type SelectBuilder

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

func (SelectBuilder) ApplyIf

func (b SelectBuilder) ApplyIf(cond bool, f func(q SelectBuilder) SelectBuilder) SelectBuilder

ApplyIf applies the given function to the builder if the condition is true. It returns the builder itself if the condition is false, otherwise it returns the result of the function. It' especially helpful for building a query conditionally.

func (SelectBuilder) Except

func (b SelectBuilder) Except() CombinationBuilder

func (SelectBuilder) From

func (SelectBuilder) FromLateral

func (b SelectBuilder) FromLateral(from FromLateralExp) FromSelectBuilder

func (SelectBuilder) FromOnly

func (b SelectBuilder) FromOnly(from FromExp) FromSelectBuilder

func (SelectBuilder) FullJoin

func (b SelectBuilder) FullJoin(from FromExp) JoinSelectBuilder

func (SelectBuilder) GroupBy

func (b SelectBuilder) GroupBy(exps ...Exp) GroupyBySelectBuilder

GroupBy adds a grouping element for the given expressions to the GROUP BY clause. If no expressions are given, special grouping elements can be added via GroupyBySelectBuilder. Use GroupyBySelectBuilder.Empty to add an empty grouping element.

func (SelectBuilder) Having

func (b SelectBuilder) Having(cond Exp) SelectBuilder

Having adds a HAVING condition to the query. Multiple calls to Having are joined with AND.

func (SelectBuilder) Intersect

func (b SelectBuilder) Intersect() CombinationBuilder

func (SelectBuilder) IsExp

func (b SelectBuilder) IsExp()

func (SelectBuilder) Join

func (SelectBuilder) JoinLateral

func (b SelectBuilder) JoinLateral(from FromExp) JoinSelectBuilder

func (SelectBuilder) LeftJoin

func (b SelectBuilder) LeftJoin(from FromExp) JoinSelectBuilder

func (SelectBuilder) LeftJoinLateral

func (b SelectBuilder) LeftJoinLateral(from FromExp) JoinSelectBuilder

func (SelectBuilder) Limit

func (b SelectBuilder) Limit(exp Exp) SelectBuilder

func (SelectBuilder) Offset

func (b SelectBuilder) Offset(exp Exp) SelectBuilder

func (SelectBuilder) OrderBy

func (b SelectBuilder) OrderBy(exp Exp) OrderBySelectBuilder

func (SelectBuilder) RightJoin

func (b SelectBuilder) RightJoin(from FromExp) JoinSelectBuilder

func (SelectBuilder) Select

func (b SelectBuilder) Select(exps ...Exp) SelectSelectBuilder

Select adds the given expressions to the select list.

func (SelectBuilder) SelectJson

SelectJson sets the JSON selection for this builder.

Any additional selections are added after the JSON selection. The JSON selection can be modified by SelectBuilder.SelectJson.

func (SelectBuilder) Union

func (SelectBuilder) Where

func (b SelectBuilder) Where(cond Exp) SelectBuilder

Where adds a WHERE condition to the query. Multiple calls to Where are joined with AND.

func (SelectBuilder) With

func (b SelectBuilder) With(queryName string) WithBuilder

With adds a WITH query to the select builder. The actual query must be supplied via WithBuilder.As.

func (SelectBuilder) WithRecursive

func (b SelectBuilder) WithRecursive(queryName string) WithBuilder

WithRecursive adds a WITH RECURSIVE query to the select builder. The actual query must be supplied via WithBuilder.As.

func (SelectBuilder) WriteSQL

func (b SelectBuilder) WriteSQL(sb *SQLBuilder)

WriteSQL writes the select as an expression.

type SelectDistinctBuilder

type SelectDistinctBuilder struct {
	SelectBuilder
}

func (SelectDistinctBuilder) On

func (b SelectDistinctBuilder) On(exp Exp, exps ...Exp) SelectBuilder

type SelectJsonSelectBuilder

type SelectJsonSelectBuilder struct {
	SelectBuilder
}

func (SelectJsonSelectBuilder) As

type SelectSelectBuilder

type SelectSelectBuilder struct {
	SelectBuilder
}

func (SelectSelectBuilder) As

func (SelectSelectBuilder) Distinct

type UpdateBuilder

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

func Update

func Update(tableName string) UpdateBuilder

func (UpdateBuilder) As

func (b UpdateBuilder) As(alias string) UpdateBuilder

func (UpdateBuilder) Set

func (b UpdateBuilder) Set(columnName string, value Exp) UpdateBuilder

func (UpdateBuilder) SetMap

func (b UpdateBuilder) SetMap(m map[string]any) UpdateBuilder

SetMap sets the items in the set clause to the given map. It overwrites any previous set clause items.

func (UpdateBuilder) Where

func (b UpdateBuilder) Where(cond Exp) UpdateBuilder

Where adds a WHERE condition to the update. Multiple calls to Where are joined with AND.

func (UpdateBuilder) WriteSQL

func (b UpdateBuilder) WriteSQL(sb *SQLBuilder)

type WithBuilder

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

WithBuilder starts building a WITH query.

func (WithBuilder) As

func (b WithBuilder) As(builder WithQuery) WithSelectBuilder

func (WithBuilder) AsMaterialized

func (b WithBuilder) AsMaterialized(builder WithQuery) WithSelectBuilder

func (WithBuilder) AsNotMaterialized

func (b WithBuilder) AsNotMaterialized(builder WithQuery) WithSelectBuilder

func (WithBuilder) ColumnNames

func (b WithBuilder) ColumnNames(names ...string) WithBuilder

ColumnNames sets the column names for the current WITH query.

type WithQuery

type WithQuery interface {
	SQLWriter
	// contains filtered or unexported methods
}

type WithSearchBuilder

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

func (WithSearchBuilder) By

func (b WithSearchBuilder) By(columnName Exp, columnNames ...Exp) WithSearchByBuilder

type WithSearchByBuilder

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

func (WithSearchByBuilder) Set

func (b WithSearchByBuilder) Set(searchColumnName string) WithSelectBuilder

type WithSelectBuilder

type WithSelectBuilder struct {
	SelectBuilder
}

WithSelectBuilder is a SelectBuilder and can refer to the latest WITH query.

func (WithSelectBuilder) SearchBreadthFirst

func (b WithSelectBuilder) SearchBreadthFirst() WithSearchBuilder

func (WithSelectBuilder) SearchDepthFirst

func (b WithSelectBuilder) SearchDepthFirst() WithSearchBuilder

Jump to

Keyboard shortcuts

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