sq package - github.com/bokwoon95/go-structured-query/postgres - Go Packages (original) (raw)

This section is empty.

This section is empty.

func SelectDistinctOn(distinctFields ...Field) func(...Field) SelectQuery

SelectDistinctOn creates a new SelectQuery.

type ArrayField struct {

}

ArrayField either represents an ARRAY column, or a literal slice value.

func Array(slice interface{}) ArrayField

Array returns a new ArrayField representing a literal string value.

func NewArrayField(name string, table Table) ArrayField

NewArrayField returns a new ArrayField representing an array column.

AppendSQLExclude marshals the ArrayField into a buffer and an args slice. It will not table qualify itself if its table qualifer appears in the excludedTableQualifiers list.

As returns a new ArrayField with the new field Alias i.e. 'field AS Alias'.

func (f ArrayField) Asc() ArrayField

Asc returns a new ArrayField indicating that it should be ordered in ascending order i.e. 'ORDER BY field ASC'.

func (f ArrayField) Concat(field ArrayField) Field

Concat concatenates the object ArrayField to the subject ArrayField.

func (f ArrayField) ContainedBy(field ArrayField) Predicate

ContainedBy checks whether the subject ArrayField is contained by the object ArrayField.

func (f ArrayField) Contains(field ArrayField) Predicate

Contains checks whether the subject ArrayField contains the object ArrayField.

func (f ArrayField) Desc() ArrayField

Desc returns a new ArrayField indicating that it should be ordered in descending order i.e. 'ORDER BY field DESC'.

func (f ArrayField) Eq(field ArrayField) Predicate

Eq returns an 'X = Y' Predicate. It only accepts ArrayField.

func (f ArrayField) Ge(field ArrayField) Predicate

Ge returns an 'X >= Y' Predicate. It only accepts ArrayField.

GetAlias implements the Field interface. It returns the Alias of the ArrayField.

GetName implements the Field interface. It returns the Name of the ArrayField.

func (f ArrayField) Gt(field ArrayField) Predicate

Gt returns an 'X > Y' Predicate. It only accepts ArrayField.

func (f ArrayField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f ArrayField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f ArrayField) Le(field ArrayField) Predicate

Le returns an 'X <= Y' Predicate. It only accepts ArrayField.

func (f ArrayField) Lt(field ArrayField) Predicate

Lt returns an 'X < Y' Predicate. It only accepts ArrayField.

func (f ArrayField) Ne(field ArrayField) Predicate

Ne returns an 'X <> Y' Predicate. It only accepts ArrayField.

func (f ArrayField) NullsFirst() ArrayField

NullsFirst returns a new ArrayField indicating that it should be ordered with nulls first i.e. 'ORDER BY field NULLS FIRST'.

func (f ArrayField) NullsLast() ArrayField

NullsLast returns a new ArrayField indicating that it should be ordered with nulls last i.e. 'ORDER BY field NULLS LAST'.

func (f ArrayField) Overlaps(field ArrayField) Predicate

Overlaps checks whether the subject ArrayField and the object ArrayField have any values in common.

func (f ArrayField) Set(value interface{}) FieldAssignment

Set returns a FieldAssignment associating the ArrayField to the value i.e. 'field = value'.

String implements the fmt.Stringer interface. It returns the string representation of an ArrayField.

type Assignment interface { SQLExcludeAppender AssertAssignment() }

Assignment is an interface representing an SQL Assignment 'Field = Value'.

type Assignments []Assignment

Assignments is a list of Assignments, when translated to SQL it looks something like "SET field1 = value1, field2 = value2, etc...".

AppendSQLExclude will write the Assignments into the buffer and args as described in the Assignments description.

type BaseQuery struct { DB DB Log Logger LogFlag LogFlag CTEs []CTE }

BaseQuery is a common query builder that can transform into a SelectQuery, InsertQuery, UpdateQuery or DeleteQuery depending on the method that you call on it.

func With(CTEs ...CTE) BaseQuery

With creates a new BaseQuery with the CTEs.

func WithDB(db DB) BaseQuery

WithDB creates a new BaseQuery with the DB.

func WithDefaultLog(flag LogFlag) BaseQuery

WithDefaultLog creates a new BaseQuery with the default logger and the LogFlag

func (q BaseQuery) DeleteFrom(table BaseTable) DeleteQuery

DeleteFrom transforms the BaseQuery into a DeleteQuery.

func (q BaseQuery) From(table Table) SelectQuery

From transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) InsertInto(table BaseTable) InsertQuery

InsertInto transforms the BaseQuery into an InsertQuery.

func (q BaseQuery) Select(fields ...Field) SelectQuery

Select transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) SelectAll() SelectQuery

SelectAll transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) SelectCount() SelectQuery

SelectCount transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) SelectDistinct(fields ...Field) SelectQuery

SelectDistinct transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) SelectDistinctOn(distinctFields ...Field) func(...Field) SelectQuery

SelectDistinctOn transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) SelectOne() SelectQuery

SelectOne transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) SelectRowx(mapper func(*Row)) SelectQuery

SelectRowx transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) Selectx(mapper func(*Row), accumulator func()) SelectQuery

Selectx transforms the BaseQuery into a SelectQuery.

func (q BaseQuery) Union(queries ...Query) VariadicQuery

Union transforms the BaseQuery into a VariadicQuery.

func (q BaseQuery) UnionAll(queries ...Query) VariadicQuery

UnionAll transforms the BaseQuery into a VariadicQuery.

func (q BaseQuery) Update(table BaseTable) UpdateQuery

Update transforms the BaseQuery into an UpdateQuery.

func (q BaseQuery) With(CTEs ...CTE) BaseQuery

With adds the CTEs to the BaseQuery

func (q BaseQuery) WithDB(db DB) BaseQuery

WithDB adds the DB to the BaseQuery.

func (q BaseQuery) WithDefaultLog(flag LogFlag) BaseQuery

WithDefaultLog adds the default logger and the LogFlag to the BaseQuery.

type BaseTable interface { Table AssertBaseTable() }

BaseTable is an interface that specialises the Table interface. It covers only tables/views that exist in the database.

type BinaryField struct {

}

BinaryField either represents a BYTEA column or a literal []byte value.

Bytes returns a new BinaryField representing a literal []byte value.

func NewBinaryField(name string, table Table) BinaryField

NewBinaryField returns a new BinaryField representing a BYTEA column.

AppendSQLExclude marshals the BinaryField into a buffer and an args slice. It will not table qualify itself if its table qualifer appears in the excludedTableQualifiers list.

GetAlias implements the Field interface. It returns the Alias of the BinaryField.

GetName implements the Field interface. It returns the Name of the BinaryField.

func (f BinaryField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f BinaryField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f BinaryField) Set(v interface{}) FieldAssignment

Set returns a FieldAssignment associating the BinaryField to the value i.e. 'field = value'.

SetBytes returns a FieldAssignment associating the BinaryField to the int value i.e. 'field = value'.

type BooleanField struct {

}

BooleanField either represents a boolean column or a literal bool value.

Bool returns a new Boolean Field representing a literal bool value.

func NewBooleanField(name string, table Table) BooleanField

NewBooleanField returns a new BooleanField representing a boolean column.

AppendSQLExclude marshals the BooleanField into a buffer and an args slice. It will not table qualify itself if its table qualifer appears in the excludedTableQualifiers list.

As returns a new BooleanField with the new field Alias i.e. 'field AS Alias'.

func (f BooleanField) Asc() BooleanField

Asc returns a new BooleanField indicating that it should be ordered in ascending order i.e. 'ORDER BY field ASC'.

func (f BooleanField) Desc() BooleanField

Desc returns a new BooleanField indicating that it should be ordered in descending order i.e. 'ORDER BY field DESC'.

func (f BooleanField) Eq(field BooleanField) Predicate

Eq returns an 'X = Y' Predicate. It only accepts BooleanField.

GetAlias implements the Field interface. It returns the Alias of the BooleanField.

GetName implements the Field interface. It returns the Name of the BooleanField.

func (f BooleanField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f BooleanField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f BooleanField) Ne(field BooleanField) Predicate

Ne returns an 'X <> Y' Predicate. It only accepts BooleanField.

func (f BooleanField) Not() Predicate

Not implements the Predicate interface.

func (f BooleanField) NullsFirst() BooleanField

NullsFirst returns a new BooleanField indicating that it should be ordered with nulls first i.e. 'ORDER BY field NULLS FIRST'.

func (f BooleanField) NullsLast() BooleanField

NullsLast returns a new BooleanField indicating that it should be ordered with nulls last i.e. 'ORDER BY field NULLS LAST'.

func (f BooleanField) Set(val interface{}) FieldAssignment

Set returns a FieldAssignment associating the BooleanField to the value i.e. 'field = value'.

SetBool returns a FieldAssignment associating the BooleanField to the bool value i.e. 'field = value'.

String implements the fmt.Stringer interface. It returns the string representation of a BooleanField.

CTE represents an SQL CTE.

RecursiveCTE constructs a new recursive CTE.

AppendSQL marshals the CTE into a buffer and args slice.

As returns a new CTE with the alias i.e. 'CTE AS alias'.

GetAlias returns the alias of the CTE.

func (CTE) GetColumns added in v0.2.0

func (cte CTE) GetColumns() []string

GetColumns returns the CTE's columns.

GetName returns the name of the CTE.

func (cte CTE) GetQuery() Query

GetQuery returns the CTE's underlying Query.

func (cte *CTE) Initial(query Query) IntermediateCTE

Initial specifies recursive CTE's initial query. If the CTE is not recursive, this operation is a no-op.

func (cte CTE) IsRecursive() bool

IsRecursive checks if the CTE is recursive.

type Column added in v0.4.0

Column keeps track of what the values mapped to what Field in an InsertQuery/SelectQuery.

func (*Column) Set added in v0.4.0

func (col *Column) Set(field Field, value interface{})

Set maps the value to the Field.

func (*Column) SetBool added in v0.4.0

func (col *Column) SetBool(field BooleanField, value bool)

SetBool maps the bool value to the BooleanField.

func (*Column) SetFloat64 added in v0.4.0

func (col *Column) SetFloat64(field NumberField, value float64)

SetFloat64 maps the float64 value to the NumberField.

func (*Column) SetInt added in v0.4.0

func (col *Column) SetInt(field NumberField, value int)

SetInt maps the int value to the NumberField.

func (*Column) SetInt64 added in v0.4.0

func (col *Column) SetInt64(field NumberField, value int64)

SetInt64 maps the int64 value to the NumberField.

func (*Column) SetString added in v0.4.0

func (col *Column) SetString(field StringField, value string)

SetString maps the string value to the StringField.

func (*Column) SetTime added in v0.4.0

SetTime maps the time.Time value to the TimeField.

func (*Column) SetUUID added in v1.2.1

func (col *Column) SetUUID(field UUIDField, value [16]byte)

SetUUID maps the uuid.UUID value to the UUIDField.

type CustomAssignment struct { Format string Values []interface{} }

CustomAssignment is an Assignment that can render itself in an arbitrary way by calling expandValues on its Format and Values.

AppendSQLExclude marshals the CustomAssignment into a buffer and an args slice. It propagates the excludedTableQualifiers down to its child elements.

func (set CustomAssignment) AssertAssignment()

AssertAssignment implements the Assignment interface.

type CustomField struct { Alias string Format string Values []interface{} IsDesc *bool IsNullsFirst *bool }

CustomField is a Field that can render itself in an arbitrary way by calling expandValues on its Format and Values.

func Excluded(field Field) CustomField

Excluded wraps a field to simulate the EXCLUDED.field Postgres construct for the ON CONFLICT DO UPDATE SET clause.

func Fieldf(format string, values ...interface{}) CustomField

Fieldf is a CustomField constructor.

func FirstValueOver(field interface{}, window Window) CustomField

FirstValueOver represents the FIRST_VALUE(field) OVER window function.

func LagOver(field interface{}, offset interface{}, fallback interface{}, window Window) CustomField

LagOver represents the LAG(field, offset, fallback) OVER window function.

func LastValueOver(field interface{}, window Window) CustomField

LastValueOver represents the LAST_VALUE(field) OVER window function.

func LeadOver(field interface{}, offset interface{}, fallback interface{}, window Window) CustomField

LeadOver represents the LEAD(field, offset, fallback) OVER window function.

func NthValueOver(field interface{}, n int, window Window) CustomField

NthValueOver represents the NTH_VALUE(field, n) OVER window function.

AppendSQLExclude marshals the CustomField into an SQL query and args as described in the CustomField struct description.

As returns a new CustomField with the new alias i.e. 'field AS Alias'.

func (f CustomField) Asc() CustomField

Asc returns a new CustomField indicating that it should be ordered in ascending order i.e. 'ORDER BY field ASC'.

func (f CustomField) Desc() CustomField

Desc returns a new CustomField indicating that it should be ordered in descending order i.e. 'ORDER BY field DESC'.

func (f CustomField) Eq(v interface{}) Predicate

Eq returns an 'X = Y' Predicate.

func (f CustomField) Ge(v interface{}) Predicate

Ge returns an 'X >= Y' Predicate.

GetAlias implements the Field interface. It returns the alias of thee CustomField.

GetName implements the Field interface. It returns the name of the CustomField.

func (f CustomField) Gt(v interface{}) Predicate

Gt returns an 'X > Y' Predicate.

func (f CustomField) In(v interface{}) Predicate

In returns an 'X IN (Y)' Predicate.

func (f CustomField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f CustomField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f CustomField) Le(v interface{}) Predicate

Le returns an 'X <= Y' Predicate.

func (f CustomField) Lt(v interface{}) Predicate

Lt returns an 'X < Y' Predicate.

func (f CustomField) Ne(v interface{}) Predicate

Ne returns an 'X <> Y' Predicate.

func (f CustomField) NullsFirst() CustomField

NullsFirst returns a new CustomField indicating that it should be ordered with nulls first i.e. 'ORDER BY field NULLS FIRST'.

func (f CustomField) NullsLast() CustomField

NullsLast returns a new CustomField indicating that it should be ordered with nulls last i.e. 'ORDER BY field NULLS LAST'.

String implements the fmt.Stringer interface. It returns the string representation of a CustomField.

type CustomPredicate struct { Alias string Format string Values []interface{} Negative bool }

CustomPredicate is a Query that can render itself in an arbitrary way as defined by its Format string. Values are interpolated into the Format string as described in the (CustomPredicate).CustomSprintf function.

func Exists(query Query) CustomPredicate

Exists represents the EXISTS() predicate.

func Predicatef(format string, values ...interface{}) CustomPredicate

Predicatef creates a new CustomPredicate.

AppendSQLExclude marshals the CustomPredicate into a buffer and args slice.

As aliases the CustomPredicate.

GetAlias implements the Field interface.

GetName implements the Field interface.

func (p CustomPredicate) Not() Predicate

Not implements the Predicate interface.

type DB interface { Query(query string, args ...interface{}) (sql.Rows, error) QueryContext(ctx context.Context, query string, args ...interface{}) (sql.Rows, error) Exec(query string, args ...interface{}) (sql.Result, error) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) }

DB is an interface providing database querying abilities.

type DeleteQuery struct {

CTEs [][CTE](#CTE)

FromTable [BaseTable](#BaseTable)

UsingTable [Table](#Table)
JoinTables [JoinTables](#JoinTables)

WherePredicate [VariadicPredicate](#VariadicPredicate)

ReturningFields [Fields](#Fields)

DB          [DB](#DB)
RowMapper   func(*[Row](#Row))
Accumulator func()

Log     [Logger](#Logger)
LogFlag [LogFlag](#LogFlag)

}

DeleteQuery represents a DELETE query.

func DeleteFrom(table BaseTable) DeleteQuery

DeleteFrom creates a new DeleteQuery.

AppendSQL marshals the DeleteQuery into a buffer and args slice.

CTE converts a DeleteQuery into a CTE.

func (q DeleteQuery) CustomJoin(joinType JoinType, table Table, predicates ...Predicate) DeleteQuery

CustomJoin custom joins a table to the DeleteQuery. The join type can be specified with a string, e.g. "CROSS JOIN".

func (q DeleteQuery) DeleteFrom(table BaseTable) DeleteQuery

DeleteFrom sets the table to be deleted from in the DeleteQuery.

Exec will execute the DeleteQuery with the given DB. It will only compute the rowsAffected if the ErowsAffected Execflag is passed to it.

ExecContext will execute the DeleteQuery with the given DB and context. It will only compute the rowsAffected if the ErowsAffected Execflag is passed to it.

func (q DeleteQuery) Fetch(db DB) (err error)

Fetch will run DeleteQuery with the given DB. It then maps the results based on the mapper function (and optionally runs the accumulator function).

FetchContext will run DeleteQuery with the given DB and context. It then maps the results based on the mapper function (and optionally runs the accumulator function).

func (q DeleteQuery) FullJoin(table Table, predicate Predicate, predicates ...Predicate) DeleteQuery

FullJoin full joins a table to the DeleteQuery based on the predicates.

func (q DeleteQuery) Join(table Table, predicate Predicate, predicates ...Predicate) DeleteQuery

Join joins a new table to the DeleteQuery based on the predicates.

func (q DeleteQuery) LeftJoin(table Table, predicate Predicate, predicates ...Predicate) DeleteQuery

LeftJoin left joins a new table to the DeleteQuery based on the predicates.

func (q DeleteQuery) NestThis() Query

NestThis indicates to the DeleteQuery that it is nested.

func (q DeleteQuery) Returning(fields ...Field) DeleteQuery

Returning appends the fields to the RETURNING clause of the DeleteQuery.

func (q DeleteQuery) ReturningOne() DeleteQuery

ReturningOne sets the RETURNING clause to RETURNING 1 in the DeleteQuery.

func (q DeleteQuery) ReturningRowx(mapper func(*Row)) DeleteQuery

ReturningRowx sets the rowmapper function of the DeleteQuery.

func (q DeleteQuery) Returningx(mapper func(*Row), accumulator func()) DeleteQuery

Returningx sets the rowmapper and accumulator function of the DeleteQuery.

func (q DeleteQuery) RightJoin(table Table, predicate Predicate, predicates ...Predicate) DeleteQuery

RightJoin right joins a new table to the DeleteQuery based on the predicates.

Subquery converts a DeleteQuery into a Subquery.

func (q DeleteQuery) ToSQL() (string, []interface{})

ToSQL marshals the DeleteQuery into a query string and args slice.

func (q DeleteQuery) Using(table Table) DeleteQuery

Using adds a new table to the DeleteQuery.

func (q DeleteQuery) Where(predicates ...Predicate) DeleteQuery

Where appends the predicates to the WHERE clause in the DeleteQuery.

func (q DeleteQuery) With(ctes ...CTE) DeleteQuery

With appends the CTEs into the DeleteQuery.

type EnumField = StringField

EnumField is a type alias for StringField.

func NewEnumField(name string, table Table) EnumField

NewEnumField returns an EnumField representing an enum column.

ExecFlag is a flag that affects the behavior of Exec.

const ( ErowsAffected ExecFlag = 1 << iota )

ExecFlags

ExitCode represents a reason for terminating the rows.Next() loop.

const ( ExitPeacefully ExitCode = iota )

ExitCodes

Error implements the error interface.

Field is an interface that represents either a Table column or an SQL value.

type FieldAssignment struct { Field Field Value interface{} }

FieldAssignment represents a Field and Value set. Its usage appears in both the UPDATE and INSERT queries whenever values are assigned to columns e.g. 'field = value'.

AppendSQLExclude will write the FieldAssignment into the buffer and args as described in the Assignments description.

func (set FieldAssignment) AssertAssignment()

AssertAssignment implements the Assignment interface.

FieldLiteral is a Field where its underlying string is literally plugged into the SQL query.

AppendSQLExclude marshals the FieldLiteral into a buffer.

GetAlias implements the Field interface. It always returns an empty string because FieldLiterals do not have aliases.

GetName implements the Field interface. It returns the FieldLiteral's underlying string as the name.

Fields represents the "field1, field2, etc..." SQL construct.

AppendSQLExclude will write the a slice of Fields into the buffer and args as described in the Fields description. The list of table qualifiers to be excluded is propagated down to the individual Fields.

AppendSQLExcludeWithAlias is exactly like AppendSQLExclude, but appends each field (i.e. field1 AS alias1, field2 AS alias2, ...) with its alias if it has one.

type FunctionInfo struct { Schema string Name string Alias string Arguments []interface{} }

FunctionInfo is struct that implements the Table/Field interface, containing all the information needed to call itself a Table/Field. It is meant to be embedded in arbitrary structs to also transform them into valid Tables/Fields.

func Functionf(name string, args ...interface{}) *FunctionInfo

Functionf creates a new FunctionInfo.

AppendSQL adds the fully qualified function call into the buffer.

AppendSQLExclude adds the fully qualified function call into the buffer.

GetAlias implements the Table interface. It returns the alias of the FunctionInfo.

GetName implements the Table interface. It returns the name of the FunctionInfo.

type InsertConflict struct {

}

InsertConflict holds the intermediate state of an InsertQuery that may experience a conflict.

func (c InsertConflict) DoNothing() InsertQuery

DoNothing indicates that nothing should be done in case of any conflicts.

func (c InsertConflict) DoUpdateSet(assignments ...Assignment) InsertQuery

DoUpdateSet specifies the assignments to be done in case of a conflict.

func (c InsertConflict) Where(predicates ...Predicate) InsertConflict

Where appends the predicates to the WHERE clause of the InsertQuery conflict.

type InsertQuery struct {

CTEs [][CTE](#CTE)

IntoTable     [BaseTable](#BaseTable)
InsertColumns [Fields](#Fields)

RowValues [RowValues](#RowValues)

SelectQuery *[SelectQuery](#SelectQuery)

HandleConflict      [bool](/builtin#bool)
ConflictFields      [Fields](#Fields)
ConflictPredicate   [VariadicPredicate](#VariadicPredicate)
ConflictConstraint  [string](/builtin#string)
Resolution          [Assignments](#Assignments)
ResolutionPredicate [VariadicPredicate](#VariadicPredicate)

ReturningFields [Fields](#Fields)

DB           [DB](#DB)
ColumnMapper func(*[Column](#Column))
RowMapper    func(*[Row](#Row))
Accumulator  func()

Log     [Logger](#Logger)
LogFlag [LogFlag](#LogFlag)

}

InsertQuery represents an INSERT query.

func InsertInto(table BaseTable) InsertQuery

InsertInto creates a new InsertQuery.

AppendSQL marshals the InsertQuery into a buffer and args slice. Do not call this as an end user, use ToSQL instead. AppendSQL may panic if you wrote panic code in your ColumnMapper, it is only exported to satisfy the Query interface.

CTE converts an InsertQuery into a CTE.

func (InsertQuery) Columns

func (q InsertQuery) Columns(fields ...Field) InsertQuery

Columns sets the insert columns for the InsertQuery.

Exec will execute the InsertQuery with the given DB. It will only compute the rowsAffected if the ErowsAffected Execflag is passed to it.

ExecContext will execute the InsertQuery with the given DB and context. It will only compute the rowsAffected if the ErowsAffected Execflag is passed to it.

func (q InsertQuery) Fetch(db DB) (err error)

Fetch will run InsertQuery with the given DB. It then maps the results based on the mapper function (and optionally runs the accumulator function).

FetchContext will run InsertQuery with the given DB and context. It then maps the results based on the mapper function (and optionally runs the accumulator function).

func (q InsertQuery) InsertInto(table BaseTable) InsertQuery

InsertInto sets the insert table for the InsertQuery.

func (q InsertQuery) NestThis() Query

NestThis indicates to the InsertQuery that it is nested.

func (q InsertQuery) OnConflict(fields ...Field) InsertConflict

OnConflict specifies which Fields may potentially experience a conflict.

func (q InsertQuery) OnConflictOnConstraint(name string) InsertConflict

OnConflictOnConstraint specifies which constraint may potentially experience a conflict.

func (q InsertQuery) Returning(fields ...Field) InsertQuery

Returning appends the fields to the RETURNING clause of the InsertQuery.

func (q InsertQuery) ReturningOne() InsertQuery

ReturningOne sets the RETURNING clause to RETURNING 1 in the InsertQuery.

func (q InsertQuery) ReturningRowx(mapper func(*Row)) InsertQuery

ReturningRowx sets the rowmapper function of the InsertQuery.

func (q InsertQuery) Returningx(mapper func(*Row), accumulator func()) InsertQuery

Returningx sets the rowmapper and accumulator function of the InsertQuery.

func (q InsertQuery) Select(selectQuery SelectQuery) InsertQuery

Select adds a SelectQuery to the InsertQuery.

Subquery converts an InsertQuery into a Subquery.

func (q InsertQuery) ToSQL() (query string, args []interface{})

ToSQL marshals the InsertQuery into a query string and args slice.

func (q InsertQuery) Values(values ...interface{}) InsertQuery

Values appends a new RowValue to the InsertQuery.

func (q InsertQuery) Valuesx(mapper func(*Column)) InsertQuery

Valuesx sets the column mapper for the InsertQuery.

func (q InsertQuery) Where(predicates ...Predicate) InsertQuery

Where appends the predicates to the WHERE clause of InsertQuery conflict resolution.

func (q InsertQuery) With(ctes ...CTE) InsertQuery

With appends a list of CTEs into the InsertQuery.

IntermediateCTE is a CTE used to hold the intermediate state of a recursive CTE just after the CTE's initial query is declared. It can only be converted back into a CTE by adding the recursive queries that UNION into the CTE.

func (cte IntermediateCTE) Union(queries ...Query) CTE

Union specifies the queries to be UNIONed into the CTE. If the CTE is not recursive, this operation is a no-op.

func (cte IntermediateCTE) UnionAll(queries ...Query) CTE

UnionAll specifies the queries to be UNION-ALLed into the CTE. If the CTE is not recursive, this operation is a no-op.

type JSONField struct {

}

JSONField either represents a JSON column or a literal value that can be marshalled into a JSON string.

func JSON(val interface{}) (JSONField, error)

JSON returns a new JSONField representing a literal JSONable value. It returns an error indicating if the value can be marshalled into JSON.

JSONValue returns a new JSONField representing a driver.Valuer value.

func MustJSON(val interface{}) JSONField

MustJSON is like JSON but it panics on error.

func NewJSONField(name string, table Table) JSONField

NewJSONField returns a new JSONField representing a JSON column.

AppendSQLExclude marshals the JSONField into an SQL query and args as described in the JSONField internal struct comments.

As returns a new JSONField with the new field Alias i.e. 'field AS Alias'.

func (f JSONField) Asc() JSONField

Asc returns a new JSONField indicating that it should be ordered in ascending order i.e. 'ORDER BY field ASC'.

func (f JSONField) Desc() JSONField

Desc returns a new JSONField indicating that it should be ordered in descending order i.e. 'ORDER BY field DESC'.

GetAlias implements the Field interface. It returns the Alias of the JSONField.

GetName implements the Field interface. It returns the Name of the JSONField.

func (f JSONField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f JSONField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f JSONField) NullsFirst() JSONField

NullsFirst returns a new JSONField indicating that it should be ordered with nulls first i.e. 'ORDER BY field NULLS FIRST'.

func (f JSONField) NullsLast() JSONField

NullsLast returns a new JSONField indicating that it should be ordered with nulls last i.e. 'ORDER BY field NULLS LAST'.

func (f JSONField) Set(value interface{}) FieldAssignment

Set returns a FieldAssignment associating the JSONField to the value i.e. 'field = value'.

func (f JSONField) SetJSON(value interface{}) FieldAssignment

SetJSON returns a FieldAssignment associating the JSONField to the JSONable value i.e. 'field = value'. Internally it uses MustJSON, which means it will panic if the value cannot be marshalled into JSON.

SetValue returns a FieldAssignment associating the JSONField to the driver.Valuer value i.e. 'field = value'.

String implements the fmt.Stringer interface. It returns the string representation of a JSONField.

type JoinTable struct { JoinType JoinType Table Table OnPredicates VariadicPredicate }

JoinTable represents an SQL join.

func CustomJoin(joinType JoinType, table Table, predicates ...Predicate) JoinTable

CustomJoin constructs a new JoinTable. Meant to be used if you want to do a custom join like CROSS JOIN, NATURAL JOIN, LEFT JOIN LATERAL etc.

func FullJoin(table Table, predicates ...Predicate) JoinTable

FullJoin creates a new full join.

func Join(table Table, predicates ...Predicate) JoinTable

Join creates a new inner join.

func LeftJoin(table Table, predicates ...Predicate) JoinTable

LeftJoin creates a new left join.

func RightJoin(table Table, predicates ...Predicate) JoinTable

RightJoin creates a new right join.

AppendSQL marshals the JoinTable into a buffer and an args slice.

type JoinTables []JoinTable

JoinTables is a list of JoinTables.

AppendSQL will write the JOIN clause into the buffer and args. If there are no JoinTables it simply writes nothing into the buffer. It returns a flag indicating whether anything was written into the buffer.

JoinType represents the various types of SQL joins.

const ( JoinTypeInner JoinType = "JOIN" JoinTypeLeft JoinType = "LEFT JOIN" JoinTypeRight JoinType = "RIGHT JOIN" JoinTypeFull JoinType = "FULL JOIN" )

JoinTypes

Literal allows for the underlying string to be literally plugged into the SQL query

LogFlag is a flag that affects the verbosity of the Logger output.

const ( Linterpolate LogFlag = 1 << iota Lstats Lresults

Lverbose = [Lstats](#Lstats) | [Lresults](#Lresults)

)

LogFlags

Logger is an interface that provides logging.

type NumberField struct {

}

NumberField either represents a number column, a number expression or a literal number value.

func Avg(field interface{}) NumberField

Avg represents the AVG() aggregate function.

func AvgOver(field interface{}, window Window) NumberField

AvgOver represents the AVG() OVER window function.

Count represents the COUNT(*) aggregate function.

func CountOver(window Window) NumberField

CountOver represents the COUNT(*) OVER window function.

func CumeDistOver(window Window) NumberField

CumeDistOver represents the CUME_DIST() OVER window function.

func DenseRankOver(window Window) NumberField

DenseRankOver represents the DENSE_RANK() OVER window function.

Float64 returns a new NumberField representing a literal float64 value.

Int returns a new NumberField representing a literal int value.

Int64 returns a new NumberField representing a literal int64 value.

func Max(field interface{}) NumberField

Max represents the MAX() aggregate function.

func MaxOver(field interface{}, window Window) NumberField

MaxOver represents the MAX() OVER window function.

func Min(field interface{}) NumberField

Min represents the MIN() aggregate function.

func MinOver(field interface{}, window Window) NumberField

MinOver represents the MIN() OVER window function.

func NewNumberField(name string, table Table) NumberField

NewNumberField returns a new NumberField representing a number TableInfo column.

func NtileOver(n int, window Window) NumberField

NtileOver represents the NTILE(n) OVER window function.

func NumberFieldf(format string, values ...interface{}) NumberField

NumberFieldf creates a new number expression.

func PercentRankOver(window Window) NumberField

PercentRankOver represents the PERCENT_RANK() OVER window function.

func RankOver(window Window) NumberField

RankOver represents the RANK() OVER window function.

func RowNumberOver(window Window) NumberField

RowNumberOver represents the ROW_NUMBER() OVER window function.

func Sum(field interface{}) NumberField

Sum represents the SUM() aggregate function.

func SumOver(field interface{}, window Window) NumberField

SumOver represents the SUM() OVER window function.

AppendSQLExclude marshals the NumberField into an SQL query and args as described in the NumberField internal struct comments.

As returns a new NumberField with the new field Alias i.e. 'field AS Alias'.

func (f NumberField) Asc() NumberField

Asc returns a new NumberField indicating that it should be ordered in ascending order i.e. 'ORDER BY field ASC'.

func (f NumberField) Desc() NumberField

Desc returns a new NumberField indicating that it should be ordered in descending order i.e. 'ORDER BY field DESC'.

func (f NumberField) Eq(field NumberField) Predicate

Eq returns an 'X = Y' Predicate. It only accepts NumberField.

EqFloat64 returns an 'X = Y' Predicate. It only accepts float64.

func (f NumberField) EqInt(num int) Predicate

EqInt returns an 'X = Y' Predicate. It only accepts int.

func (f NumberField) Ge(field NumberField) Predicate

Ge returns an 'X >= Y' Predicate. It only accepts NumberField.

GeFloat64 returns an 'X >= Y' Predicate. It only accepts float64.

func (f NumberField) GeInt(num int) Predicate

GeInt returns an 'X >= Y' Predicate. It only accepts int.

GetAlias implements the Field interface. It returns the Alias of the NumberField.

GetName implements the Field interface. It returns the Name of the NumberField.

func (f NumberField) Gt(field NumberField) Predicate

Gt returns an 'X > Y' Predicate. It only accepts NumberField.

GtFloat64 returns an 'X > Y' Predicate. It only accepts float64.

func (f NumberField) GtInt(num int) Predicate

GtInt returns an 'X > Y' Predicate. It only accepts int.

func (f NumberField) In(v interface{}) Predicate

In returns an 'X IN (Y)' Predicate.

func (f NumberField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f NumberField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f NumberField) Le(field NumberField) Predicate

Le returns an 'X <= Y' Predicate. It only accepts NumberField.

LeFloat64 returns an 'X <= Y' Predicate. It only accepts float64.

func (f NumberField) LeInt(num int) Predicate

LeInt returns an 'X <= Y' Predicate. It only accepts int.

func (f NumberField) Lt(field NumberField) Predicate

Lt returns an 'X < Y' Predicate. It only accepts NumberField.

LtFloat64 returns an 'X < Y' Predicate. It only accepts float64.

func (f NumberField) LtInt(num int) Predicate

LtInt returns an 'X < Y' Predicate. It only accepts int.

func (f NumberField) Ne(field NumberField) Predicate

Ne returns an 'X <> Y' Predicate. It only accepts NumberField.

NeFloat64 returns an 'X <> Y' Predicate. It only accepts float64.

func (f NumberField) NeInt(num int) Predicate

NeInt returns an 'X <> Y' Predicate. It only accepts int.

func (f NumberField) NullsFirst() NumberField

NullsFirst returns a new NumberField indicating that it should be ordered with nulls first i.e. 'ORDER BY field NULLS FIRST'.

func (f NumberField) NullsLast() NumberField

NullsLast returns a new NumberField indicating that it should be ordered with nulls last i.e. 'ORDER BY field NULLS LAST'.

func (f NumberField) Set(val interface{}) FieldAssignment

Set returns a FieldAssignment associating the NumberField to the value i.e. 'field = value'.

SetFloat64 returns a FieldAssignment associating the NumberField to the float64 value i.e. 'field = value'.

SetInt returns a FieldAssignment associating the NumberField to the int value i.e. 'field = value'.

SetInt64 returns a FieldAssignment associating the NumberField to the int64 value i.e. 'field = value'.

String implements the fmt.Stringer interface. It returns the string representation of a NumberField.

type Predicate interface { Field Not() Predicate }

Predicate is an interface that evaluates to true or false in SQL.

func Eq(f1, f2 interface{}) Predicate

Eq returns an 'X = Y' Predicate.

func Ge(f1, f2 interface{}) Predicate

Ge returns an 'X >= Y' Predicate.

func Gt(f1, f2 interface{}) Predicate

Gt returns an 'X > Y' Predicate.

func In(f1 interface{}, f2 []interface{}) Predicate

In returns an 'X IN (Y)' Predicate.

func Le(f1, f2 interface{}) Predicate

Le returns an 'X <= Y' Predicate.

func Lt(f1, f2 interface{}) Predicate

Lt returns an 'X < Y' Predicate.

func Ne(f1, f2 interface{}) Predicate

Ne returns an 'X <> Y' Predicate.

func Not(predicate Predicate) Predicate

Not inverts the Predicate i.e. 'NOT Predicate'.

type PredicateCase struct { Condition Predicate Result interface{} }

PredicateCase represents a Predicate and the Result if the Predicate is true.

type PredicateCases struct { Alias string Cases []PredicateCase Fallback interface{} }

PredicateCases is the general form of the CASE expression.

func CaseWhen(predicate Predicate, result interface{}) PredicateCases

CaseWhen creates a new PredicateCases i.e. CASE WHEN X THEN Y.

AppendSQLExclude marshals the PredicateCases into a buffer and an args slice. It propagates the excludedTableQualifiers down to its child elements.

As aliases the PredicateCases.

func (f PredicateCases) Else(fallback interface{}) PredicateCases

Else adds the fallback value for the PredicateCases i.e. ELSE X.

GetAlias returns the alias of the PredicateCases.

GetName returns the name of the PredicateCases, which is always an empty string.

func (f PredicateCases) When(predicate Predicate, result interface{}) PredicateCases

When adds a new PredicateCase to the PredicateCases i.e. WHEN X THEN Y.

type Query interface { SQLAppender

NestThis() [Query](#Query)
ToSQL() ([string](/builtin#string), []interface{})

}

Query is an interface that specialises the Table interface. It covers only queries like SELECT/INSERT/UPDATE/DELETE.

Row represents the state of a row after a call to rows.Next().

func (r *Row) Bool(predicate Predicate) bool

Bool returns the bool value of the Predicate. BooleanFields are considered predicates, so you can use them here.

func (r *Row) BoolValid(predicate Predicate) bool

BoolValid returns the bool value indicating if the Predicate is non-NULL. BooleanFields are considered Predicates, so you can use them here.

Float64 returns the float64 value of the NumberField.

func (r *Row) Float64Valid(field NumberField) bool

Float64Valid returns the bool value indicating if the NumberField is non-NULL.

func (r *Row) Int(field NumberField) int

Int returns the int value of the NumberField.

Int64 returns the int64 value of the NumberField.

func (r *Row) Int64Valid(field NumberField) bool

Int64Valid returns the bool value indicating if the NumberField is non-NULL.

func (r *Row) IntValid(field NumberField) bool

IntValid returns the bool value indicating if the NumberField is non-NULL.

NullBool returns the sql.NullBool value of the Predicate.

NullFloat64 returns the sql.NullFloat64 value of the NumberField.

NullInt64 returns the sql.NullInt64 value of the NumberField.

NullString returns the sql.NullString value of the StringField.

NullTime returns the sql.NullTime value of the TimeField.

func (r *Row) ScanArray(slice interface{}, field Field)

ScanArray accepts a pointer to a slice and scans a postgres array into it. Only []bool, []float64, []int64 or []string slices are supported.

func (r *Row) ScanInto(dest interface{}, field Field)

ScanInto scans the field into a dest, where dest is a pointer.

func (r *Row) ScanJSON(dest interface{}, field Field)

ScanJSON accepts a pointer to a type and scans a postgres json into it. If the JSON cannot be unmarshaled into the dest, will panic

String returns the string value of the StringField.

func (r *Row) StringValid(field StringField) bool

StringValid returns the bool value indicating if the StringField is non-NULL.

Time returns the time.Time value of the TimeField.

func (r *Row) TimeValid(field TimeField) bool

TimeValid returns a bool value indicating if the TimeField is non-NULL.

func (r *Row) UUID(field UUIDField) [16]byte

UUID returns the [16]byte value of the UUIDField

type RowValue []interface{}

RowValue represents an SQL Row Value Expression i.e. (a, b, c...)

AppendSQL marshals the RowValue into a buffer and an args slice.

AppendSQLExclude marshals the RowValue into a buffer and an args slice. It propagates the excludedTableQualifiers down to its child elements.

GetAlias implements the Field interface.

GetName implements the Field interface.

func (r RowValue) In(v interface{}) CustomPredicate

In returns an 'X IN (Y)' Predicate.

func (r RowValue) Set(v interface{}) CustomAssignment

Set returns an Assignment assigning v to the RowValue.

type RowValues []RowValue

RowValues represents a list of RowValues (a, b, c...), (d, e, f...), (g, h, i...)

AppendSQL will write the VALUES clause into the buffer and args as described in the RowValues description. If there are no values it will not write anything into the buffer. It returns a flag indicating whether anything was written into the buffer.

type SQLExcludeAppender interface { AppendSQLExclude( buf *strings.Builder, args *[]interface{}, params map[string]int, excludedTableQualifiers []string, ) }

type SelectQuery struct {

CTEs [][CTE](#CTE)

SelectType   [SelectType](#SelectType)
SelectFields [Fields](#Fields)
DistinctOn   [Fields](#Fields)

FromTable  [Table](#Table)
JoinTables [JoinTables](#JoinTables)

WherePredicate [VariadicPredicate](#VariadicPredicate)

GroupByFields [Fields](#Fields)

HavingPredicate [VariadicPredicate](#VariadicPredicate)

Windows [Windows](#Windows)

OrderByFields [Fields](#Fields)

LimitValue *[int64](/builtin#int64)

OffsetValue *[int64](/builtin#int64)

DB          [DB](#DB)
RowMapper   func(*[Row](#Row))
Accumulator func()

Log     [Logger](#Logger)
LogFlag [LogFlag](#LogFlag)

}

SelectQuery represents a SELECT query.

func From(table Table) SelectQuery

From creates a new SelectQuery.

func Select(fields ...Field) SelectQuery

Select creates a new SelectQuery.

func SelectDistinct(fields ...Field) SelectQuery

SelectDistinct creates a new SelectQuery.

func SelectOne() SelectQuery

SelectOne creates a new SelectQuery.

func SelectRowx(mapper func(*Row)) SelectQuery

SelectRowx creates a new SelectQuery.

func Selectx(mapper func(*Row), accumulator func()) SelectQuery

Selectx creates a new SelectQuery.

AppendSQL marshals the SelectQuery into a buffer and args slice.

CTE converts a SelectQuery into a CTE.

func (q SelectQuery) CustomJoin(joinType JoinType, table Table, predicates ...Predicate) SelectQuery

CustomJoin custom joins a table to the SelectQuery. The join type can be specified with a string, e.g. "CROSS JOIN".

Exec will execute the SelectQuery with the given DB. It will only compute the rowsAffected if the ErowsAffected Execflag is passed to it.

ExecContext will execute the SelectQuery with the given DB and context. It will only compute the rowsAffected if the ErowsAffected Execflag is passed to it.

func (q SelectQuery) Fetch(db DB) (err error)

Fetch will run SelectQuery with the given DB. It then maps the results based on the mapper function (and optionally runs the accumulator function).

FetchContext will run SelectQuery with the given DB and context. It then maps the results based on the mapper function (and optionally runs the accumulator function).

func (q SelectQuery) From(table Table) SelectQuery

From sets the table in the SelectQuery.

func (q SelectQuery) FullJoin(table Table, predicate Predicate, predicates ...Predicate) SelectQuery

FullJoin full joins a table to the SelectQuery based on the predicates.

func (q SelectQuery) GroupBy(fields ...Field) SelectQuery

GroupBy appends the fields to the GROUP BY clause in the SelectQuery.

func (q SelectQuery) Having(predicates ...Predicate) SelectQuery

Having appends the predicates to the HAVING clause in the SelectQuery.

func (q SelectQuery) Join(table Table, predicate Predicate, predicates ...Predicate) SelectQuery

Join joins a new table to the SelectQuery based on the predicates.

func (q SelectQuery) LeftJoin(table Table, predicate Predicate, predicates ...Predicate) SelectQuery

LeftJoin left joins a new table to the SelectQuery based on the predicates.

func (q SelectQuery) Limit(limit int) SelectQuery

Limit sets the limit in the SelectQuery.

func (q SelectQuery) NestThis() Query

NestThis indicates to the SelectQuery that it is nested.

func (q SelectQuery) Offset(offset int) SelectQuery

Offset sets the offset in the SelectQuery.

func (q SelectQuery) OrderBy(fields ...Field) SelectQuery

OrderBy appends the fields to the ORDER BY clause in the SelectQuery.

func (q SelectQuery) RightJoin(table Table, predicate Predicate, predicates ...Predicate) SelectQuery

RightJoin right joins a new table to the SelectQuery based on the predicates.

func (q SelectQuery) Select(fields ...Field) SelectQuery

Select adds the fields to the SelectFields in the SelectQuery.

func (q SelectQuery) SelectAll() SelectQuery

SelectAll sets the SELECT clause to SELECT *.

func (q SelectQuery) SelectCount() SelectQuery

SelectCount sets the SELECT clause to SELECT COUNT(*).

func (q SelectQuery) SelectDistinct(fields ...Field) SelectQuery

SelectDistinct adds the fields to the SelectFields in the SelectQuery.

func (q SelectQuery) SelectDistinctOn(distinctFields ...Field) func(...Field) SelectQuery

SelectDistinctOn adds the distinctFields to the DistinctOn fields and the fields to the SelectFields in the SelectQuery.

func (q SelectQuery) SelectOne() SelectQuery

SelectOne sets the SELECT clause to SELECT 1.

func (q SelectQuery) SelectRowx(mapper func(*Row)) SelectQuery

SelectRowx sets the mapper function in the SelectQuery.

func (q SelectQuery) Selectx(mapper func(*Row), accumulator func()) SelectQuery

Selectx sets the mapper function and accumulator function in the SelectQuery.

Subquery converts a SelectQuery into a Subquery.

func (q SelectQuery) ToSQL() (string, []interface{})

ToSQL marshals the SelectQuery into a query string and args slice.

func (q SelectQuery) Where(predicates ...Predicate) SelectQuery

Where appends the predicates to the WHERE clause in the SelectQuery.

func (q SelectQuery) Window(windows ...Window) SelectQuery

Window appends the windows to the WINDOW clause in the SelectQuery.

func (q SelectQuery) With(ctes ...CTE) SelectQuery

With appends a list of CTEs into the SelectQuery.

SelectType represents the various SQL selects.

const ( SelectTypeDefault SelectType = "SELECT" SelectTypeDistinct SelectType = "SELECT DISTINCT" SelectTypeDistinctOn SelectType = "SELECT DISTINCT ON" )

SelectTypes

type SimpleCase struct { Value interface{} Result interface{} }

SimpleCase represents a Value to be compared against and the Result if it matches.

type SimpleCases struct { Alias string Expression interface{} Cases []SimpleCase Fallback interface{} }

SimpleCases is the simple form of the CASE expression.

func Case(field Field) SimpleCases

Case creates a new SimpleCases i.e. CASE X

AppendSQLExclude marshals the SimpleCases into a buffer and an args slice. It propagates the excludedTableQualifiers down to its child elements.

As aliases the SimpleCases.

func (f SimpleCases) Else(field Field) SimpleCases

Else adds the fallback value for the SimpleCases i.e. ELSE X.

GetAlias returns the alias of the SimpleCases.

GetName returns the name of the simple cases, which is always an empty string.

func (f SimpleCases) When(field Field, result Field) SimpleCases

When adds a new SimpleCase to the SimpleCases i.e. WHEN X THEN Y.

type StringField struct {

}

StringField either represents a string column or a literal string value.

func NewStringField(name string, table Table) StringField

NewStringField returns a new StringField representing a boolean column.

String returns a new StringField representing a literal string value.

AppendSQLExclude marshals the StringField into an SQL query and args as described in the StringField internal struct comments.

As returns a new StringField with the new field Alias i.e. 'field AS Alias'.

func (f StringField) Asc() StringField

Asc returns a new StringField indicating that it should be ordered in ascending order i.e. 'ORDER BY field ASC'.

func (f StringField) Desc() StringField

Desc returns a new StringField indicating that it should be ordered in descending order i.e. 'ORDER BY field DESC'.

func (f StringField) Eq(field StringField) Predicate

Eq returns an 'X = Y' Predicate. It only accepts StringField.

EqString returns an 'X = Y' Predicate. It only accepts string.

func (f StringField) Ge(field StringField) Predicate

Ge returns an 'X >= Y' Predicate. It only accepts StringField.

GeString returns an 'X >= Y' Predicate. It only accepts string.

GetAlias implements the Field interface. It returns the Alias of the StringField.

GetName implements the Field interface. It returns the Name of the StringField.

func (f StringField) Gt(field StringField) Predicate

Gt returns an 'X > Y' Predicate. It only accepts StringField.

GtString returns an 'X > Y' Predicate. It only accepts string.

ILikeString returns an 'A ILIKE B' Predicate. It only accepts string.

func (f StringField) In(v interface{}) Predicate

In returns an 'X IN (Y)' Predicate.

func (f StringField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f StringField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f StringField) Le(field StringField) Predicate

Le returns an 'X <= Y' Predicate. It only accepts StringField.

LeString returns an 'X <= Y' Predicate. It only accepts string.

LikeString returns an 'A LIKE B' Predicate. It only accepts string.

func (f StringField) Lt(field StringField) Predicate

Lt returns an 'X < Y' Predicate. It only accepts StringField.

LtString returns an 'X < Y' Predicate. It only accepts string.

func (f StringField) Ne(field StringField) Predicate

Ne returns an 'X <> Y' Predicate. It only accepts StringField.

NeString returns an 'X <> Y' Predicate. It only accepts string.

NotILikeString returns an 'A NOT ILIKE B' Predicate. It only accepts string.

NotLikeString returns an 'A NOT LIKE B' Predicate. It only accepts string.

func (f StringField) NullsFirst() StringField

NullsFirst returns a new StringField indicating that it should be ordered with nulls first i.e. 'ORDER BY field NULLS FIRST'.

func (f StringField) NullsLast() StringField

NullsLast returns a new StringField indicating that it should be ordered with nulls last i.e. 'ORDER BY field NULLS LAST'.

func (f StringField) Set(value interface{}) FieldAssignment

Set returns a FieldAssignment associating the StringField to the value i.e. 'field = value'.

SetString returns a FieldAssignment associating the StringField to the string value i.e. 'field = value'.

String implements the fmt.Stringer interface. It returns the string representation of a StringField.

Subquery represents an SQL subquery.

AppendSQL marshals the Subquery into a buffer and args slice.

GetAlias returns the alias of the Subquery.

GetName returns the name of the Subquery.

func (subq Subquery) GetQuery() Query

GetQuery returns the Subquery's underlying Query.

func (subq Subquery) NestThis() Query

NestThis indicates to the Subquery that it is nested.

func (subq Subquery) ToSQL() (string, []interface{})

ToSQL marshals the Subquery into a query string and args slice.

Table is an interface representing anything that you can SELECT FROM or JOIN.

TableInfo is struct that implements the Table interface, containing all the information needed to call itself a Table. It is meant to be embedded in arbitrary structs to also transform them into valid Tables.

AppendSQL adds the fully qualified table name into the buffer.

func (tbl *TableInfo) AssertBaseTable()

AssertBaseTable implements the BaseTable interface.

GetAlias implements the Table interface. It returns the alias from the TableInfo.

GetName implements the Table interface. It returns the name from the TableInfo.

type TimeField struct {

}

TimeField either represents a time column or a literal time.Time value.

func NewTimeField(name string, table Table) TimeField

NewTimeField returns a new TimeField representing a time column.

Time returns a new TimeField representing a literal time.Time value.

AppendSQLExclude marshals the TimeField into an SQL query and args as described in the TimeField internal struct comments.

As returns a new TimeField with the new field Alias i.e. 'field AS Alias'.

func (f TimeField) Asc() TimeField

Asc returns a new TimeField indicating that it should be ordered in ascending order i.e. 'ORDER BY field ASC'.

func (f TimeField) Between(start, end TimeField) Predicate

Between returns an 'X BETWEEN Y AND Z' Predicate. It only accepts TimeField.

func (f TimeField) BetweenSymmetricTime(start, end time.Time) Predicate

BetweenSymmetricTime returns an 'X BETWEEN SYMMETRIC Y AND Z' Predicate. It only accepts time.Time.

BetweenTime returns an 'X BETWEEN Y AND Z' Predicate. It only accepts time.Time.

func (f TimeField) Desc() TimeField

Desc returns a new TimeField indicating that it should be ordered in descending order i.e. 'ORDER BY field DESC'.

func (f TimeField) Eq(field TimeField) Predicate

Eq returns an 'X = Y' Predicate. It only accepts TimeField.

EqTime returns an 'X = Y' Predicate. It only accepts time.Time.

func (f TimeField) Ge(field TimeField) Predicate

Ge returns an 'X >= Y' Predicate. It only accepts TimeField.

GeTime returns an 'X >= Y' Predicate. It only accepts time.Time.

GetAlias implements the Field interface. It returns the Alias of the TimeField.

GetName implements the Field interface. It returns the Name of the TimeField.

func (f TimeField) Gt(field TimeField) Predicate

Gt returns an 'X > Y' Predicate. It only accepts TimeField.

GtTime returns an 'X > Y' Predicate. It only accepts time.Time.

func (f TimeField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f TimeField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f TimeField) Le(field TimeField) Predicate

Le returns an 'X <= Y' Predicate. It only accepts TimeField.

LeTime returns an 'X <= Y' Predicate. It only accepts time.Time.

func (f TimeField) Lt(field TimeField) Predicate

Lt returns an 'X < Y' Predicate. It only accepts TimeField.

LtTime returns an 'X < Y' Predicate. It only accepts time.Time.

func (f TimeField) Ne(field TimeField) Predicate

Ne returns an 'X <> Y' Predicate. It only accepts TimeField.

NeTime returns an 'X <> Y' Predicate. It only accepts time.Time.

func (f TimeField) NotBetween(start, end TimeField) Predicate

NotBetween returns an 'X NOT BETWEEN Y AND Z' Predicate. It only accepts TimeField.

func (f TimeField) NotBetweenSymmetricTime(start, end time.Time) Predicate

NotBetweenSymmetricTime returns an 'X NOT BETWEEN Y AND Z' Predicate. It only accepts time.Time.

NotBetweenTime returns an 'X NOT BETWEEN Y AND Z' Predicate. It only accepts time.Time.

func (f TimeField) NullsFirst() TimeField

NullsFirst returns a new TimeField indicating that it should be ordered with nulls first i.e. 'ORDER BY field NULLS FIRST'.

func (f TimeField) NullsLast() TimeField

NullsLast returns a new TimeField indicating that it should be ordered with nulls last i.e. 'ORDER BY field NULLS LAST'.

func (f TimeField) Set(value interface{}) FieldAssignment

Set returns a FieldAssignment associating the TimeField to the value i.e. 'field = value'.

SetTime returns a FieldAssignment associating the TimeField to the time.Time value i.e. 'field = value'.

String implements the fmt.Stringer interface. It returns the string representation of a TimeField.

type UUIDField struct {

}

UUIDField represents a UUID column or a literal UUID value.

func NewUUIDField(name string, table Table) UUIDField

NewUUIDField returns a new UUIDField representing a UUID column.

UUID returns a new UUIDField representing a literal UUID value.

AppendSQLExclude marshals the UUIDField into a buffer and an args slice. It will not table qualify itself if its table qualifer appears in the excludedTableQualifiers list.

func (f UUIDField) Asc() UUIDField

Asc returns a new UUIDField indicating that it should be ordered in ascending order i.e. 'ORDER BY field ASC'

func (f UUIDField) Desc() UUIDField

Asc returns a new UUIDField indicating that it should be ordered in descending order i.e. 'ORDER BY field DESC'.

func (f UUIDField) Eq(field UUIDField) Predicate

Eq returns an 'X = Y' Predicate. It only accepts UUIDField.

func (f UUIDField) EqUUID(u [16]byte) Predicate

Eq returns an 'X = Y' Predicate. It only accepts [16]byte

GetAlias retusn the alias of the UUIDField

GetName returns the name of the UUIDField

func (f UUIDField) In(v interface{}) Predicate

In returns an 'X IN (Y) Predicate'.

func (f UUIDField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (f UUIDField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (f UUIDField) Ne(field UUIDField) Predicate

Eq returns an 'X <> Y' Predicate. It only accepts UUIDField.

func (f UUIDField) NeUUID(u [16]byte) Predicate

Eq returns an 'X <> Y' Predicate. It only accepts [16]byte

func (f UUIDField) Set(value interface{}) FieldAssignment

Set returns a FieldAssignment associating the UUIDField to the value i.e. 'field = value'.

SetUUID returns a fieldAssignment associating the UUIDField to the [16]byte value i.e. 'field = value'

String returns the string representation of the UUIDField

type UpdateQuery struct {

CTEs [][CTE](#CTE)

UpdateTable [BaseTable](#BaseTable)

Assignments [Assignments](#Assignments)

FromTable  [Table](#Table)
JoinTables [JoinTables](#JoinTables)

WherePredicate [VariadicPredicate](#VariadicPredicate)

ReturningFields [Fields](#Fields)

DB           [DB](#DB)
ColumnMapper func(*[Column](#Column))
RowMapper    func(*[Row](#Row))
Accumulator  func()

Log     [Logger](#Logger)
LogFlag [LogFlag](#LogFlag)

}

UpdateQuery represents an UPDATE query.

func Update(table BaseTable) UpdateQuery

Update creates a new UpdateQuery.

AppendSQL marshals the UpdateQuery into a buffer and args slice. Do not call this as an end user, use ToSQL instead. AppendSQL may panic if you wrote panic code in your ColumnMapper, it is only exported to satisfy the Query interface.

CTE converts an UpdateQuery into a CTE.

func (q UpdateQuery) CustomJoin(joinType JoinType, table Table, predicates ...Predicate) UpdateQuery

CustomJoin custom joins a table to the UpdateQuery. The join type can be specified with a string, e.g. "CROSS JOIN".

Exec will execute the UpdateQuery with the given DB. It will only compute the rowsAffected if the ErowsAffected Execflag is passed to it.

ExecContext will execute the UpdateQuery with the given DB and context. It will only compute the rowsAffected if the ErowsAffected Execflag is passed to it.

func (q UpdateQuery) Fetch(db DB) (err error)

Fetch will run UpdateQuery with the given DB. It then maps the results based on the mapper function (and optionally runs the accumulator function).

FetchContext will run UpdateQuery with the given DB and context. It then maps the results based on the mapper function (and optionally runs the accumulator function).

func (q UpdateQuery) From(table Table) UpdateQuery

From specifies a table to select from for the purposes of the UpdateQuery.

func (q UpdateQuery) FullJoin(table Table, predicate Predicate, predicates ...Predicate) UpdateQuery

FullJoin full joins a table to the UpdateQuery based on the predicates.

func (q UpdateQuery) Join(table Table, predicate Predicate, predicates ...Predicate) UpdateQuery

Join joins a new table to the UpdateQuery based on the predicates.

func (q UpdateQuery) LeftJoin(table Table, predicate Predicate, predicates ...Predicate) UpdateQuery

LeftJoin left joins a new table to the UpdateQuery based on the predicates.

func (q UpdateQuery) NestThis() Query

NestThis indicates to the UpdateQuery that it is nested.

func (q UpdateQuery) Returning(fields ...Field) UpdateQuery

Returning appends the fields to the RETURNING clause of the InsertQuery.

func (q UpdateQuery) ReturningOne() UpdateQuery

ReturningOne sets the RETURNING clause to RETURNING 1 in the InsertQuery.

func (q UpdateQuery) ReturningRowx(mapper func(*Row)) UpdateQuery

ReturningRowx sets the rowmapper function of the InsertQuery.

func (q UpdateQuery) Returningx(mapper func(*Row), accumulator func()) UpdateQuery

Returningx sets the rowmapper and accumulator function of the InsertQuery.

func (q UpdateQuery) RightJoin(table Table, predicate Predicate, predicates ...Predicate) UpdateQuery

RightJoin right joins a new table to the UpdateQuery based on the predicates.

func (q UpdateQuery) Set(assignments ...Assignment) UpdateQuery

Set appends the assignments to SET clause of the UpdateQuery.

func (q UpdateQuery) Setx(mapper func(*Column)) UpdateQuery

Setx sets the column mapper function UpdateQuery.

Subquery converts an UpdateQuery into a Subquery.

func (q UpdateQuery) ToSQL() (query string, args []interface{})

func (q UpdateQuery) Update(table BaseTable) UpdateQuery

Update sets the update table for the UpdateQuery.

func (q UpdateQuery) Where(predicates ...Predicate) UpdateQuery

Where appends the predicates to the WHERE clause in the UpdateQuery.

func (q UpdateQuery) With(ctes ...CTE) UpdateQuery

With appends a list of CTEs into the UpdateQuery.

type VariadicPredicate struct { Alias string Operator VariadicPredicateOperator Predicates []Predicate Negative bool

}

VariadicPredicate represents the "x AND y AND z..." or "x OR y OR z..." SQL construct.

func And

func And(predicates ...Predicate) VariadicPredicate

And joins the list of predicates together with the AND operator.

func Or(predicates ...Predicate) VariadicPredicate

Or joins the list of predicates together with the OR operator.

AppendSQLExclude marshals the VariadicPredicate into a buffer and an args slice. It propagates the excludedTableQualifiers down to its child elements.

GetAlias implements the Field interface.

GetName implements the Field interface.

func (p VariadicPredicate) Not() Predicate

Not implements the Predicate interface.

type VariadicPredicateOperator string

VariadicPredicateOperator is an operator that can join a variadic number of Predicates together.

const ( PredicateOr VariadicPredicateOperator = "OR" PredicateAnd VariadicPredicateOperator = "AND" )

Possible VariadicOperators

type VariadicQuery struct { Operator VariadicQueryOperator Queries []Query

DB          [DB](#DB)
Mapper      func(*[Row](#Row))
Accumulator func()

Log     [Logger](#Logger)
LogFlag [LogFlag](#LogFlag)

}

VariadicQuery represents a variadic number of queries joined together by an VariadicQueryOperator.

func Except(queries ...Query) VariadicQuery

Except joins the list of queries together with the EXCEPT operator.

func ExceptAll(queries ...Query) VariadicQuery

ExceptAll joins the list of queries together with the EXCEPT ALL operator.

func Intersect(queries ...Query) VariadicQuery

Intersect joins the list of queries together with the INTERSECT operator.

func IntersectAll(queries ...Query) VariadicQuery

IntersectAll joins the list of queries together with the INTERSECT ALL operator.

func Union(queries ...Query) VariadicQuery

Union joins the list of queries together with the UNION operator.

func UnionAll(queries ...Query) VariadicQuery

UnionAll joins the list of queries together with the UNION ALL operator.

AppendSQL marshals the VariadicQuery into a buffer and args slice.

CTE converts a VariadicQuery into a CTE.

func (vq VariadicQuery) NestThis() Query

NestThis indicates to the VariadicQuery that it is nested.

Subquery converts a VariadicQuery into a Subquery.

func (vq VariadicQuery) ToSQL() (string, []interface{})

ToSQL marshals the VariadicQuery into a query string and args slice.

type VariadicQueryOperator string

VariadicQueryOperator is an operator that can join a variadic number of queries together.

const ( QueryUnion VariadicQueryOperator = "UNION" QueryUnionAll VariadicQueryOperator = "UNION ALL" QueryIntersect VariadicQueryOperator = "INTERSECT" QueryIntersectAll VariadicQueryOperator = "INTERSECT ALL" QueryExcept VariadicQueryOperator = "EXCEPT" QueryExceptAll VariadicQueryOperator = "EXCEPT ALL" )

VariadicQueryOperators

type Window struct { WindowName string

PartitionByFields [Fields](#Fields)
OrderByFields     [Fields](#Fields)
FrameDefinition   [string](/builtin#string)

}

Window represents a window usable in a window function.

func OrderBy(fields ...Field) Window

OrderBy creates a new Window.

func PartitionBy(fields ...Field) Window

PartitionBy creates a new Window.

AppendSQL marshals the Window into a buffer and args slice.

As aliases the VariadicQuery i.e. 'query AS alias'.

func (w Window) Frame(frameDefinition string) Window

Frame sets the frame definition of the window e.g. RANGE BETWEEN 5 PRECEDING AND 10 FOLLOWING.

func (w Window) Name() Window

Name returns the name of the Window.

func (w Window) OrderBy(fields ...Field) Window

OrderBy sets the fields of the window's ORDER BY clause.

func (w Window) PartitionBy(fields ...Field) Window

PartitionBy sets the fields of the window's PARTITION BY clause.

Windows is a list of Windows.

AppendSQL marshals the Windows into a buffer and args slice.