sq

package
Version: v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2021 License: MIT Imports: 16 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SelectDistinctOn

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

SelectDistinctOn creates a new SelectQuery.

Types

type ArrayField

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

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

func Array

func Array(slice interface{}) ArrayField

Array returns a new ArrayField representing a literal string value.

func NewArrayField

func NewArrayField(name string, table Table) ArrayField

NewArrayField returns a new ArrayField representing an array column.

func (ArrayField) AppendSQLExclude

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

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.

func (ArrayField) As

func (f ArrayField) As(alias string) ArrayField

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

func (ArrayField) Asc

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 (ArrayField) Concat

func (f ArrayField) Concat(field ArrayField) Field

Concat concatenates the object ArrayField to the subject ArrayField.

func (ArrayField) ContainedBy

func (f ArrayField) ContainedBy(field ArrayField) Predicate

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

func (ArrayField) Contains

func (f ArrayField) Contains(field ArrayField) Predicate

Contains checks whether the subject ArrayField contains the object ArrayField.

func (ArrayField) Desc

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 (ArrayField) Eq

func (f ArrayField) Eq(field ArrayField) Predicate

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

func (ArrayField) Ge

func (f ArrayField) Ge(field ArrayField) Predicate

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

func (ArrayField) GetAlias

func (f ArrayField) GetAlias() string

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

func (ArrayField) GetName

func (f ArrayField) GetName() string

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

func (ArrayField) Gt

func (f ArrayField) Gt(field ArrayField) Predicate

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

func (ArrayField) IsNotNull

func (f ArrayField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (ArrayField) IsNull

func (f ArrayField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (ArrayField) Le

func (f ArrayField) Le(field ArrayField) Predicate

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

func (ArrayField) Lt

func (f ArrayField) Lt(field ArrayField) Predicate

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

func (ArrayField) Ne

func (f ArrayField) Ne(field ArrayField) Predicate

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

func (ArrayField) NullsFirst

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 (ArrayField) NullsLast

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 (ArrayField) Overlaps

func (f ArrayField) Overlaps(field ArrayField) Predicate

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

func (ArrayField) Set

func (f ArrayField) Set(value ArrayField) FieldAssignment

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

func (ArrayField) String

func (f ArrayField) String() string

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

type Assignment

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

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

type Assignments

type Assignments []Assignment

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

func (Assignments) AppendSQLExclude

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

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

type BaseQuery

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

func With(CTEs ...CTE) BaseQuery

With creates a new BaseQuery with the CTEs.

func WithDB

func WithDB(db DB) BaseQuery

WithDB creates a new BaseQuery with the DB.

func WithDefaultLog

func WithDefaultLog(flag LogFlag) BaseQuery

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

func (BaseQuery) DeleteFrom

func (q BaseQuery) DeleteFrom(table BaseTable) DeleteQuery

DeleteFrom transforms the BaseQuery into a DeleteQuery.

func (BaseQuery) From

func (q BaseQuery) From(table Table) SelectQuery

From transforms the BaseQuery into a SelectQuery.

func (BaseQuery) InsertInto

func (q BaseQuery) InsertInto(table BaseTable) InsertQuery

InsertInto transforms the BaseQuery into an InsertQuery.

func (BaseQuery) Select

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

Select transforms the BaseQuery into a SelectQuery.

func (BaseQuery) SelectAll

func (q BaseQuery) SelectAll() SelectQuery

SelectAll transforms the BaseQuery into a SelectQuery.

func (BaseQuery) SelectCount

func (q BaseQuery) SelectCount() SelectQuery

SelectCount transforms the BaseQuery into a SelectQuery.

func (BaseQuery) SelectDistinct

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

SelectDistinct transforms the BaseQuery into a SelectQuery.

func (BaseQuery) SelectDistinctOn

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

SelectDistinctOn transforms the BaseQuery into a SelectQuery.

func (BaseQuery) SelectOne

func (q BaseQuery) SelectOne() SelectQuery

SelectOne transforms the BaseQuery into a SelectQuery.

func (BaseQuery) SelectRowx

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

SelectRowx transforms the BaseQuery into a SelectQuery.

func (BaseQuery) Selectx

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

Selectx transforms the BaseQuery into a SelectQuery.

func (BaseQuery) Union added in v0.2.0

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

Union transforms the BaseQuery into a VariadicQuery.

func (BaseQuery) UnionAll added in v0.2.0

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

UnionAll transforms the BaseQuery into a VariadicQuery.

func (BaseQuery) Update

func (q BaseQuery) Update(table BaseTable) UpdateQuery

Update transforms the BaseQuery into an UpdateQuery.

func (BaseQuery) With

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

With adds the CTEs to the BaseQuery

func (BaseQuery) WithDB

func (q BaseQuery) WithDB(db DB) BaseQuery

WithDB adds the DB to the BaseQuery.

func (BaseQuery) WithDefaultLog

func (q BaseQuery) WithDefaultLog(flag LogFlag) BaseQuery

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

type BaseTable

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

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

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

func Bytes

func Bytes(b []byte) BinaryField

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

func NewBinaryField

func NewBinaryField(name string, table Table) BinaryField

NewBinaryField returns a new BinaryField representing a BYTEA column.

func (BinaryField) AppendSQLExclude

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

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.

func (BinaryField) GetAlias

func (f BinaryField) GetAlias() string

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

func (BinaryField) GetName

func (f BinaryField) GetName() string

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

func (BinaryField) IsNotNull

func (f BinaryField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (BinaryField) IsNull

func (f BinaryField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (BinaryField) Set

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

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

func (BinaryField) SetBytes

func (f BinaryField) SetBytes(b []byte) FieldAssignment

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

type BooleanField

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

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

func Bool

func Bool(b bool) BooleanField

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

func NewBooleanField

func NewBooleanField(name string, table Table) BooleanField

NewBooleanField returns a new BooleanField representing a boolean column.

func (BooleanField) AppendSQLExclude

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

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.

func (BooleanField) As

func (f BooleanField) As(alias string) BooleanField

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

func (BooleanField) Asc

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 (BooleanField) Desc

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 (BooleanField) Eq

func (f BooleanField) Eq(field BooleanField) Predicate

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

func (BooleanField) GetAlias

func (f BooleanField) GetAlias() string

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

func (BooleanField) GetName

func (f BooleanField) GetName() string

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

func (BooleanField) IsNotNull

func (f BooleanField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (BooleanField) IsNull

func (f BooleanField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (BooleanField) Ne

func (f BooleanField) Ne(field BooleanField) Predicate

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

func (BooleanField) Not

func (f BooleanField) Not() Predicate

Not implements the Predicate interface.

func (BooleanField) NullsFirst

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 (BooleanField) NullsLast

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 (BooleanField) Set

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

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

func (BooleanField) SetBool

func (f BooleanField) SetBool(val bool) FieldAssignment

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

func (BooleanField) String

func (f BooleanField) String() string

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

type CTE

type CTE map[string]CustomField

CTE represents an SQL CTE.

func RecursiveCTE added in v0.2.0

func RecursiveCTE(name string, columns ...string) CTE

RecursiveCTE constructs a new recursive CTE.

func (CTE) AppendSQL

func (cte CTE) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL marshals the CTE into a buffer and args slice.

func (CTE) As

func (cte CTE) As(alias string) CTE

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

func (CTE) GetAlias

func (cte CTE) GetAlias() string

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.

func (CTE) GetName

func (cte CTE) GetName() string

GetName returns the name of the CTE.

func (CTE) GetQuery added in v0.2.0

func (cte CTE) GetQuery() Query

GetQuery returns the CTE's underlying Query.

func (*CTE) Initial added in v0.2.0

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) IsRecursive added in v0.2.0

func (cte CTE) IsRecursive() bool

IsRecursive checks if the CTE is recursive.

type Column added in v0.4.0

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

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

func (col *Column) SetTime(field TimeField, value time.Time)

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

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.

func (CustomAssignment) AppendSQLExclude

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

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

func (CustomAssignment) AssertAssignment

func (set CustomAssignment) AssertAssignment()

AssertAssignment implements the Assignment interface.

type CustomField

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

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

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

Fieldf is a CustomField constructor.

func FirstValueOver added in v0.4.1

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

FirstValueOver represents the FIRST_VALUE(field) OVER window function.

func LagOver added in v0.4.1

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

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

func LastValueOver added in v0.4.1

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

LastValueOver represents the LAST_VALUE(field) OVER window function.

func LeadOver added in v0.4.1

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

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

func NthValueOver added in v0.4.1

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

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

func (CustomField) AppendSQLExclude

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

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

func (CustomField) As

func (f CustomField) As(alias string) CustomField

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

func (CustomField) Asc

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 (CustomField) Desc

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 (CustomField) Eq

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

Eq returns an 'X = Y' Predicate.

func (CustomField) Ge

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

Ge returns an 'X >= Y' Predicate.

func (CustomField) GetAlias

func (f CustomField) GetAlias() string

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

func (CustomField) GetName

func (f CustomField) GetName() string

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

func (CustomField) Gt

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

Gt returns an 'X > Y' Predicate.

func (CustomField) In

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

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

func (CustomField) IsNotNull

func (f CustomField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (CustomField) IsNull

func (f CustomField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (CustomField) Le

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

Le returns an 'X <= Y' Predicate.

func (CustomField) Lt

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

Lt returns an 'X < Y' Predicate.

func (CustomField) Ne

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

Ne returns an 'X <> Y' Predicate.

func (CustomField) NullsFirst

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 (CustomField) NullsLast

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'.

func (CustomField) String

func (f CustomField) String() string

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

type CustomPredicate

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

func Exists(query Query) CustomPredicate

Exists represents the EXISTS() predicate.

func Predicatef

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

Predicatef creates a new CustomPredicate.

func (CustomPredicate) AppendSQLExclude

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

AppendSQLExclude marshals the CustomPredicate into a buffer and args slice.

func (CustomPredicate) As

As aliases the CustomPredicate.

func (CustomPredicate) GetAlias

func (p CustomPredicate) GetAlias() string

GetAlias implements the Field interface.

func (CustomPredicate) GetName

func (p CustomPredicate) GetName() string

GetName implements the Field interface.

func (CustomPredicate) Not

func (p CustomPredicate) Not() Predicate

Not implements the Predicate interface.

type DB

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

type DeleteQuery struct {

	// WITH
	CTEs []CTE
	// DELETE FROM
	FromTable BaseTable
	// USING
	UsingTable Table
	JoinTables JoinTables
	// WHERE
	WherePredicate VariadicPredicate
	// RETURNING
	ReturningFields Fields
	// DB
	DB          DB
	RowMapper   func(*Row)
	Accumulator func()
	// Logging
	Log     Logger
	LogFlag LogFlag
	// contains filtered or unexported fields
}

DeleteQuery represents a DELETE query.

func DeleteFrom

func DeleteFrom(table BaseTable) DeleteQuery

DeleteFrom creates a new DeleteQuery.

func (DeleteQuery) AppendSQL

func (q DeleteQuery) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL marshals the DeleteQuery into a buffer and args slice.

func (DeleteQuery) CTE added in v0.2.0

func (q DeleteQuery) CTE(name string, columns ...string) CTE

CTE converts a DeleteQuery into a CTE.

func (DeleteQuery) CustomJoin

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 (DeleteQuery) DeleteFrom

func (q DeleteQuery) DeleteFrom(table BaseTable) DeleteQuery

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

func (DeleteQuery) Exec

func (q DeleteQuery) Exec(db DB, flag ExecFlag) (rowsAffected int64, err error)

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

func (DeleteQuery) ExecContext

func (q DeleteQuery) ExecContext(ctx context.Context, db DB, flag ExecFlag) (rowsAffected int64, err error)

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 (DeleteQuery) Fetch

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).

func (DeleteQuery) FetchContext

func (q DeleteQuery) FetchContext(ctx context.Context, db DB) (err error)

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 (DeleteQuery) FullJoin

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

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

func (DeleteQuery) Join

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

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

func (DeleteQuery) LeftJoin

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 (DeleteQuery) NestThis

func (q DeleteQuery) NestThis() Query

NestThis indicates to the DeleteQuery that it is nested.

func (DeleteQuery) Returning

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

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

func (DeleteQuery) ReturningOne

func (q DeleteQuery) ReturningOne() DeleteQuery

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

func (DeleteQuery) ReturningRowx

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

ReturningRowx sets the rowmapper function of the DeleteQuery.

func (DeleteQuery) Returningx

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

Returningx sets the rowmapper and accumulator function of the DeleteQuery.

func (DeleteQuery) RightJoin

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

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

func (DeleteQuery) Subquery added in v0.2.0

func (q DeleteQuery) Subquery(alias string) Subquery

Subquery converts a DeleteQuery into a Subquery.

func (DeleteQuery) ToSQL

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

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

func (DeleteQuery) Using

func (q DeleteQuery) Using(table Table) DeleteQuery

Using adds a new table to the DeleteQuery.

func (DeleteQuery) Where

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

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

func (DeleteQuery) With

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

With appends the CTEs into the DeleteQuery.

type EnumField

type EnumField = StringField

EnumField is a type alias for StringField.

func NewEnumField

func NewEnumField(name string, table Table) EnumField

NewEnumField returns an EnumField representing an enum column.

type ExecFlag

type ExecFlag int

ExecFlag is a flag that affects the behavior of Exec.

const (
	ErowsAffected ExecFlag = 1 << iota
)

ExecFlags

type ExitCode

type ExitCode int

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

const (
	ExitPeacefully ExitCode = iota
)

ExitCodes

func (ExitCode) Error

func (e ExitCode) Error() string

Error implements the error interface.

type Field

type Field interface {
	// Fields should respect the excludedTableQualifiers argument in ToSQL().
	// E.g. if the field 'name' belongs to a table called 'users' and the
	// excludedTableQualifiers contains 'users', the field should present itself
	// as 'name' and not 'users.name'. i.e. any table qualifiers in the list
	// must be excluded.
	//
	// This is to play nice with certain clauses in the INSERT and UPDATE
	// queries that expressly forbid table qualified columns.
	AppendSQLExclude(buf *strings.Builder, args *[]interface{}, params map[string]int, excludedTableQualifiers []string)
	GetAlias() string
	GetName() string
}

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

type FieldAssignment

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'.

func (FieldAssignment) AppendSQLExclude

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

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

func (FieldAssignment) AssertAssignment

func (set FieldAssignment) AssertAssignment()

AssertAssignment implements the Assignment interface.

type FieldLiteral

type FieldLiteral string

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

func (FieldLiteral) AppendSQLExclude

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

AppendSQLExclude marshals the FieldLiteral into a buffer.

func (FieldLiteral) GetAlias

func (f FieldLiteral) GetAlias() string

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

func (FieldLiteral) GetName

func (f FieldLiteral) GetName() string

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

type Fields

type Fields []Field

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

func (Fields) AppendSQLExclude

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

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.

func (Fields) AppendSQLExcludeWithAlias

func (fs Fields) AppendSQLExcludeWithAlias(buf *strings.Builder, args *[]interface{}, params map[string]int, excludedTableQualifiers []string)

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

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

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

Functionf creates a new FunctionInfo.

func (*FunctionInfo) AppendSQL

func (f *FunctionInfo) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL adds the fully qualified function call into the buffer.

func (*FunctionInfo) AppendSQLExclude

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

AppendSQLExclude adds the fully qualified function call into the buffer.

func (*FunctionInfo) GetAlias

func (f *FunctionInfo) GetAlias() string

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

func (*FunctionInfo) GetName

func (f *FunctionInfo) GetName() string

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

type InsertConflict added in v0.2.1

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

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

func (InsertConflict) DoNothing added in v0.2.1

func (c InsertConflict) DoNothing() InsertQuery

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

func (InsertConflict) DoUpdateSet added in v0.2.1

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

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

func (InsertConflict) Where added in v0.2.1

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

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

type InsertQuery

type InsertQuery struct {

	// WITH
	CTEs []CTE
	// INSERT INTO
	IntoTable     BaseTable
	InsertColumns Fields
	// VALUES
	RowValues RowValues
	// SELECT
	SelectQuery *SelectQuery
	// ON CONFLICT
	HandleConflict      bool
	ConflictFields      Fields
	ConflictPredicate   VariadicPredicate
	ConflictConstraint  string
	Resolution          Assignments
	ResolutionPredicate VariadicPredicate
	// RETURNING
	ReturningFields Fields
	// DB
	DB           DB
	ColumnMapper func(*Column)
	RowMapper    func(*Row)
	Accumulator  func()
	// Logging
	Log     Logger
	LogFlag LogFlag
	// contains filtered or unexported fields
}

InsertQuery represents an INSERT query.

func InsertInto

func InsertInto(table BaseTable) InsertQuery

InsertInto creates a new InsertQuery.

func (InsertQuery) AppendSQL

func (q InsertQuery) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

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.

func (InsertQuery) CTE added in v0.2.0

func (q InsertQuery) CTE(name string, columns ...string) CTE

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.

func (InsertQuery) Exec

func (q InsertQuery) Exec(db DB, flag ExecFlag) (rowsAffected int64, err error)

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

func (InsertQuery) ExecContext

func (q InsertQuery) ExecContext(ctx context.Context, db DB, flag ExecFlag) (rowsAffected int64, err error)

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 (InsertQuery) Fetch

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).

func (InsertQuery) FetchContext

func (q InsertQuery) FetchContext(ctx context.Context, db DB) (err error)

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 (InsertQuery) InsertInto

func (q InsertQuery) InsertInto(table BaseTable) InsertQuery

InsertInto sets the insert table for the InsertQuery.

func (InsertQuery) NestThis

func (q InsertQuery) NestThis() Query

NestThis indicates to the InsertQuery that it is nested.

func (InsertQuery) OnConflict

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

OnConflict specifies which Fields may potentially experience a conflict.

func (InsertQuery) OnConflictOnConstraint

func (q InsertQuery) OnConflictOnConstraint(name string) InsertConflict

OnConflictOnConstraint specifies which constraint may potentially experience a conflict.

func (InsertQuery) Returning

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

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

func (InsertQuery) ReturningOne

func (q InsertQuery) ReturningOne() InsertQuery

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

func (InsertQuery) ReturningRowx

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

ReturningRowx sets the rowmapper function of the InsertQuery.

func (InsertQuery) Returningx

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

Returningx sets the rowmapper and accumulator function of the InsertQuery.

func (InsertQuery) Select

func (q InsertQuery) Select(selectQuery SelectQuery) InsertQuery

Select adds a SelectQuery to the InsertQuery.

func (InsertQuery) Subquery added in v0.2.0

func (q InsertQuery) Subquery(alias string) Subquery

Subquery converts an InsertQuery into a Subquery.

func (InsertQuery) ToSQL

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

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

func (InsertQuery) Values

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

Values appends a new RowValue to the InsertQuery.

func (InsertQuery) Valuesx added in v0.4.0

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

Valuesx sets the column mapper for the InsertQuery.

func (InsertQuery) Where

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

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

func (InsertQuery) With

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

With appends a list of CTEs into the InsertQuery.

type IntermediateCTE added in v0.2.3

type IntermediateCTE map[string]CustomField

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 (IntermediateCTE) Union added in v0.2.3

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 (IntermediateCTE) UnionAll added in v0.2.3

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

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

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

func JSON

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.

func JSONValue

func JSONValue(val driver.Valuer) JSONField

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

func MustJSON

func MustJSON(val interface{}) JSONField

MustJSON is like JSON but it panics on error.

func NewJSONField

func NewJSONField(name string, table Table) JSONField

NewJSONField returns a new JSONField representing a JSON column.

func (JSONField) AppendSQLExclude

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

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

func (JSONField) As

func (f JSONField) As(alias string) JSONField

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

func (JSONField) Asc

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 (JSONField) Desc

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'.

func (JSONField) GetAlias

func (f JSONField) GetAlias() string

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

func (JSONField) GetName

func (f JSONField) GetName() string

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

func (JSONField) IsNotNull

func (f JSONField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (JSONField) IsNull

func (f JSONField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (JSONField) NullsFirst

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 (JSONField) NullsLast

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 (JSONField) Set

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

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

func (JSONField) SetJSON

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.

func (JSONField) SetValue

func (f JSONField) SetValue(value driver.Valuer) FieldAssignment

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

func (JSONField) String

func (f JSONField) String() string

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

type JoinTable

type JoinTable struct {
	JoinType     JoinType
	Table        Table
	OnPredicates VariadicPredicate
}

JoinTable represents an SQL join.

func CustomJoin

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

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

FullJoin creates a new full join.

func Join

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

Join creates a new inner join.

func LeftJoin

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

LeftJoin creates a new left join.

func RightJoin

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

RightJoin creates a new right join.

func (JoinTable) AppendSQL

func (join JoinTable) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

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

type JoinTables

type JoinTables []JoinTable

JoinTables is a list of JoinTables.

func (JoinTables) AppendSQL

func (joins JoinTables) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

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.

type JoinType

type JoinType string

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

type LogFlag

type LogFlag int

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

const (
	Linterpolate LogFlag = 1 << iota
	Lstats
	Lresults
	// Lparse
	Lverbose = Lstats | Lresults
)

LogFlags

type Logger

type Logger interface {
	Output(calldepth int, s string) error
}

Logger is an interface that provides logging.

type NumberField

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

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

func Avg

func Avg(field interface{}) NumberField

Avg represents the AVG() aggregate function.

func AvgOver

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

AvgOver represents the AVG() OVER window function.

func Count

func Count() NumberField

Count represents the COUNT(*) aggregate function.

func CountOver

func CountOver(window Window) NumberField

CountOver represents the COUNT(*) OVER window function.

func CumeDistOver added in v0.4.1

func CumeDistOver(window Window) NumberField

CumeDistOver represents the CUME_DIST() OVER window function.

func DenseRankOver added in v0.4.1

func DenseRankOver(window Window) NumberField

DenseRankOver represents the DENSE_RANK() OVER window function.

func Float64

func Float64(num float64) NumberField

Float64 returns a new NumberField representing a literal float64 value.

func Int

func Int(num int) NumberField

Int returns a new NumberField representing a literal int value.

func Int64

func Int64(num int64) NumberField

Int64 returns a new NumberField representing a literal int64 value.

func Max

func Max(field interface{}) NumberField

Max represents the MAX() aggregate function.

func MaxOver

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

MaxOver represents the MAX() OVER window function.

func Min

func Min(field interface{}) NumberField

Min represents the MIN() aggregate function.

func MinOver

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

MinOver represents the MIN() OVER window function.

func NewNumberField

func NewNumberField(name string, table Table) NumberField

NewNumberField returns a new NumberField representing a number TableInfo column.

func NtileOver added in v0.4.1

func NtileOver(n int, window Window) NumberField

NtileOver represents the NTILE(n) OVER window function.

func NumberFieldf

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

NumberFieldf creates a new number expression.

func PercentRankOver added in v0.4.1

func PercentRankOver(window Window) NumberField

PercentRankOver represents the PERCENT_RANK() OVER window function.

func RankOver added in v0.4.1

func RankOver(window Window) NumberField

RankOver represents the RANK() OVER window function.

func RowNumberOver added in v0.4.1

func RowNumberOver(window Window) NumberField

RowNumberOver represents the ROW_NUMBER() OVER window function.

func Sum

func Sum(field interface{}) NumberField

Sum represents the SUM() aggregate function.

func SumOver

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

SumOver represents the SUM() OVER window function.

func (NumberField) AppendSQLExclude

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

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

func (NumberField) As

func (f NumberField) As(alias string) NumberField

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

func (NumberField) Asc

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 (NumberField) Desc

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 (NumberField) Eq

func (f NumberField) Eq(field NumberField) Predicate

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

func (NumberField) EqFloat64

func (f NumberField) EqFloat64(num float64) Predicate

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

func (NumberField) EqInt

func (f NumberField) EqInt(num int) Predicate

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

func (NumberField) Ge

func (f NumberField) Ge(field NumberField) Predicate

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

func (NumberField) GeFloat64

func (f NumberField) GeFloat64(num float64) Predicate

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

func (NumberField) GeInt

func (f NumberField) GeInt(num int) Predicate

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

func (NumberField) GetAlias

func (f NumberField) GetAlias() string

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

func (NumberField) GetName

func (f NumberField) GetName() string

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

func (NumberField) Gt

func (f NumberField) Gt(field NumberField) Predicate

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

func (NumberField) GtFloat64

func (f NumberField) GtFloat64(num float64) Predicate

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

func (NumberField) GtInt

func (f NumberField) GtInt(num int) Predicate

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

func (NumberField) In

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

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

func (NumberField) IsNotNull

func (f NumberField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (NumberField) IsNull

func (f NumberField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (NumberField) Le

func (f NumberField) Le(field NumberField) Predicate

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

func (NumberField) LeFloat64

func (f NumberField) LeFloat64(num float64) Predicate

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

func (NumberField) LeInt

func (f NumberField) LeInt(num int) Predicate

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

func (NumberField) Lt

func (f NumberField) Lt(field NumberField) Predicate

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

func (NumberField) LtFloat64

func (f NumberField) LtFloat64(num float64) Predicate

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

func (NumberField) LtInt

func (f NumberField) LtInt(num int) Predicate

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

func (NumberField) Ne

func (f NumberField) Ne(field NumberField) Predicate

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

func (NumberField) NeFloat64

func (f NumberField) NeFloat64(num float64) Predicate

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

func (NumberField) NeInt

func (f NumberField) NeInt(num int) Predicate

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

func (NumberField) NullsFirst

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 (NumberField) NullsLast

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 (NumberField) Set

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

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

func (NumberField) SetFloat64

func (f NumberField) SetFloat64(num float64) FieldAssignment

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

func (NumberField) SetInt

func (f NumberField) SetInt(num int) FieldAssignment

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

func (NumberField) SetInt64

func (f NumberField) SetInt64(num int64) FieldAssignment

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

func (NumberField) String

func (f NumberField) String() string

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

type Predicate

type Predicate interface {
	Field
	Not() Predicate
}

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

func Eq added in v0.2.0

func Eq(f1, f2 interface{}) Predicate

Eq returns an 'X = Y' Predicate.

func Ge added in v0.2.0

func Ge(f1, f2 interface{}) Predicate

Ge returns an 'X >= Y' Predicate.

func Gt added in v0.2.0

func Gt(f1, f2 interface{}) Predicate

Gt returns an 'X > Y' Predicate.

func Le added in v0.2.0

func Le(f1, f2 interface{}) Predicate

Le returns an 'X <= Y' Predicate.

func Lt added in v0.2.0

func Lt(f1, f2 interface{}) Predicate

Lt returns an 'X < Y' Predicate.

func Ne added in v0.2.0

func Ne(f1, f2 interface{}) Predicate

Ne returns an 'X <> Y' Predicate.

func Not

func Not(predicate Predicate) Predicate

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

type PredicateCase

type PredicateCase struct {
	Condition Predicate
	Result    interface{}
}

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

type PredicateCases

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

PredicateCases is the general form of the CASE expression.

func CaseWhen

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

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

func (PredicateCases) AppendSQLExclude

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

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

func (PredicateCases) As

func (f PredicateCases) As(alias string) PredicateCases

As aliases the PredicateCases.

func (PredicateCases) Else

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

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

func (PredicateCases) GetAlias

func (f PredicateCases) GetAlias() string

GetAlias returns the alias of the PredicateCases.

func (PredicateCases) GetName

func (f PredicateCases) GetName() string

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

func (PredicateCases) When

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

type Query interface {
	AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)
	// When NestThis is called on a query, it signals to the query that it is
	// being nested as part of a larger query. The nested query should:
	// - hold off rebinding question mark ?, ? to dollar $1, $2 placeholders because the parent query will do it
	// - hold off logging anything because the parent query will do it
	NestThis() Query
	ToSQL() (string, []interface{})
}

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

type Row

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

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

func (*Row) Bool

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 (*Row) BoolValid

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.

func (*Row) Float64

func (r *Row) Float64(field NumberField) float64

Float64 returns the float64 value of the NumberField.

func (*Row) Float64Valid

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

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

func (*Row) Int

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

Int returns the int value of the NumberField.

func (*Row) Int64

func (r *Row) Int64(field NumberField) int64

Int64 returns the int64 value of the NumberField.

func (*Row) Int64Valid

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

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

func (*Row) IntValid

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

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

func (*Row) NullBool

func (r *Row) NullBool(predicate Predicate) sql.NullBool

NullBool returns the sql.NullBool value of the Predicate.

func (*Row) NullFloat64

func (r *Row) NullFloat64(field NumberField) sql.NullFloat64

NullFloat64 returns the sql.NullFloat64 value of the NumberField.

func (*Row) NullInt64

func (r *Row) NullInt64(field NumberField) sql.NullInt64

NullInt64 returns the sql.NullInt64 value of the NumberField.

func (*Row) NullString

func (r *Row) NullString(field StringField) sql.NullString

NullString returns the sql.NullString value of the StringField.

func (*Row) NullTime

func (r *Row) NullTime(field TimeField) sql.NullTime

NullTime returns the sql.NullTime value of the TimeField.

func (*Row) ScanArray

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 (*Row) ScanInto

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

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

func (*Row) String

func (r *Row) String(field StringField) string

String returns the string value of the StringField.

func (*Row) StringValid

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

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

func (*Row) Time

func (r *Row) Time(field TimeField) time.Time

Time returns the time.Time value of the TimeField.

func (*Row) TimeValid

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

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

func (*Row) UUID added in v1.2.1

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

UUID returns the [16]byte value of the UUIDField

type RowValue

type RowValue []interface{}

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

func (RowValue) AppendSQL

func (r RowValue) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

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

func (RowValue) AppendSQLExclude

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

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

func (RowValue) GetAlias added in v0.4.0

func (r RowValue) GetAlias() string

GetAlias implements the Field interface.

func (RowValue) GetName added in v0.4.0

func (r RowValue) GetName() string

GetName implements the Field interface.

func (RowValue) In

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

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

func (RowValue) Set

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

Set returns an Assignment assigning v to the RowValue.

type RowValues

type RowValues []RowValue

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

func (RowValues) AppendSQL

func (rs RowValues) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

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 SelectQuery

type SelectQuery struct {

	// WITH
	CTEs []CTE
	// SELECT
	SelectType   SelectType
	SelectFields Fields
	DistinctOn   Fields
	// FROM
	FromTable  Table
	JoinTables JoinTables
	// WHERE
	WherePredicate VariadicPredicate
	// GROUP BY
	GroupByFields Fields
	// HAVING
	HavingPredicate VariadicPredicate
	// WINDOW
	Windows Windows
	// ORDER BY
	OrderByFields Fields
	// LIMIT
	LimitValue *int64
	// OFFSET
	OffsetValue *int64
	// DB
	DB          DB
	RowMapper   func(*Row)
	Accumulator func()
	// Logging
	Log     Logger
	LogFlag LogFlag
	// contains filtered or unexported fields
}

SelectQuery represents a SELECT query.

func From

func From(table Table) SelectQuery

From creates a new SelectQuery.

func Select

func Select(fields ...Field) SelectQuery

Select creates a new SelectQuery.

func SelectDistinct

func SelectDistinct(fields ...Field) SelectQuery

SelectDistinct creates a new SelectQuery.

func SelectOne

func SelectOne() SelectQuery

SelectOne creates a new SelectQuery.

func SelectRowx

func SelectRowx(mapper func(*Row)) SelectQuery

SelectRowx creates a new SelectQuery.

func Selectx

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

Selectx creates a new SelectQuery.

func (SelectQuery) AppendSQL

func (q SelectQuery) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL marshals the SelectQuery into a buffer and args slice.

func (SelectQuery) CTE added in v0.2.0

func (q SelectQuery) CTE(name string, columns ...string) CTE

CTE converts a SelectQuery into a CTE.

func (SelectQuery) CustomJoin

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".

func (SelectQuery) Exec

func (q SelectQuery) Exec(db DB, flag ExecFlag) (rowsAffected int64, err error)

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

func (SelectQuery) ExecContext

func (q SelectQuery) ExecContext(ctx context.Context, db DB, flag ExecFlag) (rowsAffected int64, err error)

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 (SelectQuery) Fetch

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).

func (SelectQuery) FetchContext

func (q SelectQuery) FetchContext(ctx context.Context, db DB) (err error)

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 (SelectQuery) From

func (q SelectQuery) From(table Table) SelectQuery

From sets the table in the SelectQuery.

func (SelectQuery) FullJoin

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

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

func (SelectQuery) GroupBy

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

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

func (SelectQuery) Having

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

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

func (SelectQuery) Join

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

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

func (SelectQuery) LeftJoin

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 (SelectQuery) Limit

func (q SelectQuery) Limit(limit int) SelectQuery

Limit sets the limit in the SelectQuery.

func (SelectQuery) NestThis

func (q SelectQuery) NestThis() Query

NestThis indicates to the SelectQuery that it is nested.

func (SelectQuery) Offset

func (q SelectQuery) Offset(offset int) SelectQuery

Offset sets the offset in the SelectQuery.

func (SelectQuery) OrderBy

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

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

func (SelectQuery) RightJoin

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 (SelectQuery) Select

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

Select adds the fields to the SelectFields in the SelectQuery.

func (SelectQuery) SelectAll

func (q SelectQuery) SelectAll() SelectQuery

SelectAll sets the SELECT clause to SELECT *.

func (SelectQuery) SelectCount

func (q SelectQuery) SelectCount() SelectQuery

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

func (SelectQuery) SelectDistinct

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

SelectDistinct adds the fields to the SelectFields in the SelectQuery.

func (SelectQuery) SelectDistinctOn

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 (SelectQuery) SelectOne

func (q SelectQuery) SelectOne() SelectQuery

SelectOne sets the SELECT clause to SELECT 1.

func (SelectQuery) SelectRowx

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

SelectRowx sets the mapper function in the SelectQuery.

func (SelectQuery) Selectx

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

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

func (SelectQuery) Subquery added in v0.2.0

func (q SelectQuery) Subquery(alias string) Subquery

Subquery converts a SelectQuery into a Subquery.

func (SelectQuery) ToSQL

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

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

func (SelectQuery) Where

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

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

func (SelectQuery) Window

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

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

func (SelectQuery) With

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

With appends a list of CTEs into the SelectQuery.

type SelectType

type SelectType string

SelectType represents the various SQL selects.

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

SelectTypes

type SimpleCase

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

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

type SimpleCases

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

SimpleCases is the simple form of the CASE expression.

func Case

func Case(field Field) SimpleCases

Case creates a new SimpleCases i.e. CASE X

func (SimpleCases) AppendSQLExclude

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

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

func (SimpleCases) As

func (f SimpleCases) As(alias string) SimpleCases

As aliases the SimpleCases.

func (SimpleCases) Else

func (f SimpleCases) Else(field Field) SimpleCases

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

func (SimpleCases) GetAlias

func (f SimpleCases) GetAlias() string

GetAlias returns the alias of the SimpleCases.

func (SimpleCases) GetName

func (f SimpleCases) GetName() string

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

func (SimpleCases) When

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

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

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

func NewStringField

func NewStringField(name string, table Table) StringField

NewStringField returns a new StringField representing a boolean column.

func String

func String(s string) StringField

String returns a new StringField representing a literal string value.

func (StringField) AppendSQLExclude

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

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

func (StringField) As

func (f StringField) As(alias string) StringField

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

func (StringField) Asc

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 (StringField) Desc

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 (StringField) Eq

func (f StringField) Eq(field StringField) Predicate

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

func (StringField) EqString

func (f StringField) EqString(s string) Predicate

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

func (StringField) Ge

func (f StringField) Ge(field StringField) Predicate

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

func (StringField) GeString

func (f StringField) GeString(s string) Predicate

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

func (StringField) GetAlias

func (f StringField) GetAlias() string

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

func (StringField) GetName

func (f StringField) GetName() string

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

func (StringField) Gt

func (f StringField) Gt(field StringField) Predicate

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

func (StringField) GtString

func (f StringField) GtString(s string) Predicate

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

func (StringField) ILikeString

func (f StringField) ILikeString(s string) Predicate

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

func (StringField) In

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

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

func (StringField) IsNotNull

func (f StringField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (StringField) IsNull

func (f StringField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (StringField) Le

func (f StringField) Le(field StringField) Predicate

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

func (StringField) LeString

func (f StringField) LeString(s string) Predicate

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

func (StringField) LikeString

func (f StringField) LikeString(s string) Predicate

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

func (StringField) Lt

func (f StringField) Lt(field StringField) Predicate

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

func (StringField) LtString

func (f StringField) LtString(s string) Predicate

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

func (StringField) Ne

func (f StringField) Ne(field StringField) Predicate

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

func (StringField) NeString

func (f StringField) NeString(s string) Predicate

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

func (StringField) NotILikeString

func (f StringField) NotILikeString(s string) Predicate

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

func (StringField) NotLikeString

func (f StringField) NotLikeString(s string) Predicate

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

func (StringField) NullsFirst

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 (StringField) NullsLast

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 (StringField) Set

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

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

func (StringField) SetString

func (f StringField) SetString(s string) FieldAssignment

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

func (StringField) String

func (f StringField) String() string

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

type Subquery added in v0.2.0

type Subquery map[string]CustomField

Subquery represents an SQL subquery.

func (Subquery) AppendSQL added in v0.2.0

func (subq Subquery) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL marshals the Subquery into a buffer and args slice.

func (Subquery) GetAlias added in v0.2.0

func (subq Subquery) GetAlias() string

GetAlias returns the alias of the Subquery.

func (Subquery) GetName added in v0.2.0

func (subq Subquery) GetName() string

GetName returns the name of the Subquery.

func (Subquery) GetQuery added in v0.2.0

func (subq Subquery) GetQuery() Query

GetQuery returns the Subquery's underlying Query.

func (Subquery) NestThis added in v0.2.0

func (subq Subquery) NestThis() Query

NestThis indicates to the Subquery that it is nested.

func (Subquery) ToSQL added in v0.2.0

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

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

type Table

type Table interface {
	AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)
	GetAlias() string
	GetName() string // Table name must exclude the schema (if any)
}

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

type TableInfo

type TableInfo struct {
	Schema string
	Name   string
	Alias  string
}

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.

func (*TableInfo) AppendSQL

func (tbl *TableInfo) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL adds the fully qualified table name into the buffer.

func (*TableInfo) AssertBaseTable

func (tbl *TableInfo) AssertBaseTable()

AssertBaseTable implements the BaseTable interface.

func (*TableInfo) GetAlias

func (tbl *TableInfo) GetAlias() string

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

func (*TableInfo) GetName

func (tbl *TableInfo) GetName() string

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

type TimeField

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

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

func NewTimeField

func NewTimeField(name string, table Table) TimeField

NewTimeField returns a new TimeField representing a time column.

func Time

func Time(t time.Time) TimeField

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

func (TimeField) AppendSQLExclude

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

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

func (TimeField) As

func (f TimeField) As(alias string) TimeField

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

func (TimeField) Asc

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 (TimeField) Between

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

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

func (TimeField) BetweenSymmetricTime

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

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

func (TimeField) BetweenTime

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

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

func (TimeField) Desc

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 (TimeField) Eq

func (f TimeField) Eq(field TimeField) Predicate

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

func (TimeField) EqTime

func (f TimeField) EqTime(t time.Time) Predicate

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

func (TimeField) Ge

func (f TimeField) Ge(field TimeField) Predicate

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

func (TimeField) GeTime

func (f TimeField) GeTime(t time.Time) Predicate

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

func (TimeField) GetAlias

func (f TimeField) GetAlias() string

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

func (TimeField) GetName

func (f TimeField) GetName() string

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

func (TimeField) Gt

func (f TimeField) Gt(field TimeField) Predicate

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

func (TimeField) GtTime

func (f TimeField) GtTime(t time.Time) Predicate

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

func (TimeField) IsNotNull

func (f TimeField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (TimeField) IsNull

func (f TimeField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (TimeField) Le

func (f TimeField) Le(field TimeField) Predicate

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

func (TimeField) LeTime

func (f TimeField) LeTime(t time.Time) Predicate

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

func (TimeField) Lt

func (f TimeField) Lt(field TimeField) Predicate

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

func (TimeField) LtTime

func (f TimeField) LtTime(t time.Time) Predicate

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

func (TimeField) Ne

func (f TimeField) Ne(field TimeField) Predicate

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

func (TimeField) NeTime

func (f TimeField) NeTime(t time.Time) Predicate

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

func (TimeField) NotBetween

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

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

func (TimeField) NotBetweenSymmetricTime

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

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

func (TimeField) NotBetweenTime

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

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

func (TimeField) NullsFirst

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 (TimeField) NullsLast

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 (TimeField) Set

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

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

func (TimeField) SetTime

func (f TimeField) SetTime(value time.Time) FieldAssignment

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

func (TimeField) String

func (f TimeField) String() string

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

type UUIDField added in v1.2.1

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

UUIDField represents a UUID column or a literal UUID value.

func NewUUIDField added in v1.2.1

func NewUUIDField(name string, table Table) UUIDField

NewUUIDField returns a new UUIDField representing a UUID column.

func UUID added in v1.2.1

func UUID(u [16]byte) UUIDField

UUID returns a new UUIDField representing a literal UUID value.

func (UUIDField) AppendSQLExclude added in v1.2.1

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

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 (UUIDField) Asc added in v1.2.1

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 (UUIDField) Desc added in v1.2.1

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 (UUIDField) Eq added in v1.2.1

func (f UUIDField) Eq(field UUIDField) Predicate

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

func (UUIDField) EqUUID added in v1.2.1

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

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

func (UUIDField) GetAlias added in v1.2.1

func (f UUIDField) GetAlias() string

GetAlias retusn the alias of the UUIDField

func (UUIDField) GetName added in v1.2.1

func (f UUIDField) GetName() string

GetName returns the name of the UUIDField

func (UUIDField) In added in v1.2.1

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

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

func (UUIDField) IsNotNull added in v1.2.1

func (f UUIDField) IsNotNull() Predicate

IsNotNull returns an 'X IS NOT NULL' Predicate.

func (UUIDField) IsNull added in v1.2.1

func (f UUIDField) IsNull() Predicate

IsNull returns an 'X IS NULL' Predicate.

func (UUIDField) Ne added in v1.2.1

func (f UUIDField) Ne(field UUIDField) Predicate

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

func (UUIDField) NeUUID added in v1.2.1

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

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

func (UUIDField) Set added in v1.2.1

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

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

func (UUIDField) SetUUID added in v1.2.1

func (f UUIDField) SetUUID(u [16]byte) FieldAssignment

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

func (UUIDField) String added in v1.2.1

func (f UUIDField) String() string

String returns the string representation of the UUIDField

type UpdateQuery

type UpdateQuery struct {

	// WITH
	CTEs []CTE
	// UPDATE
	UpdateTable BaseTable
	// SET
	Assignments Assignments
	// FROM
	FromTable  Table
	JoinTables JoinTables
	// WHERE
	WherePredicate VariadicPredicate
	// RETURNING
	ReturningFields Fields
	// DB
	DB           DB
	ColumnMapper func(*Column)
	RowMapper    func(*Row)
	Accumulator  func()
	// Logging
	Log     Logger
	LogFlag LogFlag
	// contains filtered or unexported fields
}

UpdateQuery represents an UPDATE query.

func Update

func Update(table BaseTable) UpdateQuery

Update creates a new UpdateQuery.

func (UpdateQuery) AppendSQL

func (q UpdateQuery) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

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.

func (UpdateQuery) CTE added in v0.2.0

func (q UpdateQuery) CTE(name string, columns ...string) CTE

CTE converts an UpdateQuery into a CTE.

func (UpdateQuery) CustomJoin

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".

func (UpdateQuery) Exec

func (q UpdateQuery) Exec(db DB, flag ExecFlag) (rowsAffected int64, err error)

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

func (UpdateQuery) ExecContext

func (q UpdateQuery) ExecContext(ctx context.Context, db DB, flag ExecFlag) (rowsAffected int64, err error)

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 (UpdateQuery) Fetch

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).

func (UpdateQuery) FetchContext

func (q UpdateQuery) FetchContext(ctx context.Context, db DB) (err error)

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 (UpdateQuery) From

func (q UpdateQuery) From(table Table) UpdateQuery

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

func (UpdateQuery) FullJoin

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

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

func (UpdateQuery) Join

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

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

func (UpdateQuery) LeftJoin

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 (UpdateQuery) NestThis

func (q UpdateQuery) NestThis() Query

NestThis indicates to the UpdateQuery that it is nested.

func (UpdateQuery) Returning

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

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

func (UpdateQuery) ReturningOne

func (q UpdateQuery) ReturningOne() UpdateQuery

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

func (UpdateQuery) ReturningRowx

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

ReturningRowx sets the rowmapper function of the InsertQuery.

func (UpdateQuery) Returningx

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

Returningx sets the rowmapper and accumulator function of the InsertQuery.

func (UpdateQuery) RightJoin

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 (UpdateQuery) Set

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

Set appends the assignments to SET clause of the UpdateQuery.

func (UpdateQuery) Setx added in v0.4.0

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

Setx sets the column mapper function UpdateQuery.

func (UpdateQuery) Subquery added in v0.2.0

func (q UpdateQuery) Subquery(alias string) Subquery

Subquery converts an UpdateQuery into a Subquery.

func (UpdateQuery) ToSQL

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

func (UpdateQuery) Update

func (q UpdateQuery) Update(table BaseTable) UpdateQuery

Update sets the update table for the UpdateQuery.

func (UpdateQuery) Where

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

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

func (UpdateQuery) With

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

With appends a list of CTEs into the UpdateQuery.

type VariadicPredicate

type VariadicPredicate struct {
	Alias      string
	Operator   VariadicPredicateOperator
	Predicates []Predicate
	Negative   bool
	// contains filtered or unexported fields
}

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

func Or(predicates ...Predicate) VariadicPredicate

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

func (VariadicPredicate) AppendSQLExclude

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

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

func (VariadicPredicate) GetAlias

func (p VariadicPredicate) GetAlias() string

GetAlias implements the Field interface.

func (VariadicPredicate) GetName

func (p VariadicPredicate) GetName() string

GetName implements the Field interface.

func (VariadicPredicate) Not

func (p VariadicPredicate) Not() Predicate

Not implements the Predicate interface.

type VariadicPredicateOperator

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

type VariadicQuery struct {
	Operator VariadicQueryOperator
	Queries  []Query
	// DB
	DB          DB
	Mapper      func(*Row)
	Accumulator func()
	// Logging
	Log     Logger
	LogFlag LogFlag
	// contains filtered or unexported fields
}

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

func Except

func Except(queries ...Query) VariadicQuery

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

func ExceptAll

func ExceptAll(queries ...Query) VariadicQuery

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

func Intersect

func Intersect(queries ...Query) VariadicQuery

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

func IntersectAll

func IntersectAll(queries ...Query) VariadicQuery

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

func Union

func Union(queries ...Query) VariadicQuery

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

func UnionAll

func UnionAll(queries ...Query) VariadicQuery

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

func (VariadicQuery) AppendSQL

func (vq VariadicQuery) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL marshals the VariadicQuery into a buffer and args slice.

func (VariadicQuery) CTE added in v0.2.0

func (vq VariadicQuery) CTE(name string, columns ...string) CTE

CTE converts a VariadicQuery into a CTE.

func (VariadicQuery) NestThis

func (vq VariadicQuery) NestThis() Query

NestThis indicates to the VariadicQuery that it is nested.

func (VariadicQuery) Subquery added in v0.2.0

func (vq VariadicQuery) Subquery(name string) Subquery

Subquery converts a VariadicQuery into a Subquery.

func (VariadicQuery) ToSQL

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

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

type VariadicQueryOperator

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

type Window struct {
	WindowName string

	PartitionByFields Fields
	OrderByFields     Fields
	FrameDefinition   string
	// contains filtered or unexported fields
}

Window represents a window usable in a window function.

func OrderBy

func OrderBy(fields ...Field) Window

OrderBy creates a new Window.

func PartitionBy

func PartitionBy(fields ...Field) Window

PartitionBy creates a new Window.

func (Window) AppendSQL

func (w Window) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL marshals the Window into a buffer and args slice.

func (Window) As

func (w Window) As(name string) Window

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

func (Window) Frame

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 (Window) Name

func (w Window) Name() Window

Name returns the name of the Window.

func (Window) OrderBy

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

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

func (Window) PartitionBy

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

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

type Windows

type Windows []Window

Windows is a list of Windows.

func (Windows) AppendSQL

func (ws Windows) AppendSQL(buf *strings.Builder, args *[]interface{}, params map[string]int)

AppendSQL marshals the Windows into a buffer and args slice.

Jump to

Keyboard shortcuts

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