chain

package
v0.1.17 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2019 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// NullValue represents the NULL value in SQL
	NullValue = "NULL"
	// CurrentTimestampPGFn is the name of the function of postgres that returns current
	// timestamp with tz.
	CurrentTimestampPGFn = "CURRENT_TIMESTAMP"
)
View Source
const (
	// SQLNothing is the default value for an SQLBool
	SQLNothing sqlBool = ""
	// SQLAnd represents AND in SQL
	SQLAnd sqlBool = "AND"
	// SQLOr represents OR in SQL
	SQLOr sqlBool = "OR"
	// SQLNot represents NOT in SQL
	SQLNot sqlBool = "NOT"
	// SQLAndNot Negates the expresion after AND
	SQLAndNot sqlBool = "AND NOT"
	// SQLOrNot Neates the expresion after OR
	SQLOrNot sqlBool = "OR NOT"
)
View Source
const (
	// SQLAll is a modifier that can be append to UNION, INTERSECT and EXCEPT
	SQLAll sqlModifier = "ALL"
)

Variables

This section is empty.

Functions

func AVG added in v0.1.16

func AVG(column string) string

AVG Renders SQL AVG of the expresion in column

func AndConditions added in v0.1.16

func AndConditions(conditions ...string) string

AndConditions returns a list of conditions separated by AND

func As

func As(field, alias string) string

As is a convenience function to define column alias in go in order to be a bit less error prone and more go semantic.

func COUNT added in v0.1.16

func COUNT(column string) string

COUNT Renders SQL COUNT of the expresion in column

func ColumnGroup added in v0.1.16

func ColumnGroup(columns ...string) string

ColumnGroup returns a list of columns, comma separated and between parenthesis.

func CompareExpresions added in v0.1.16

func CompareExpresions(operator CompOperator, columnLeft, columnRight string) string

CompareExpresions returns a comparision between two SQL expresions using operator

func Constraint

func Constraint(constraint string) string

Constraint wraps the passed constraint name with the required SQL to use it.

func Distinct added in v0.1.16

func Distinct(field string) string

Distinct allows selection of distinct results only.

func Equals

func Equals(field string) string

Equals is a convenience function to enable use of go for where definitions

func GreaterOrEqualThan

func GreaterOrEqualThan(field string) string

GreaterOrEqualThan is a convenience function to enable use of go for where definitions

func GreaterThan

func GreaterThan(field string) string

GreaterThan is a convenience function to enable use of go for where definitions

func In

func In(field string, value ...interface{}) (string, []interface{})

In is a convenience function to enable use of go for where definitions

func InSlice added in v0.1.16

func InSlice(field string, value interface{}) (string, interface{})

InSlice is a convenience function to enable use of go for where definitions and assumes the passed value is already a slice.

func IsNoRows added in v0.1.16

func IsNoRows(err error) bool

IsNoRows returns true if the passed error is one of the many possibilities of no rows returned by the different libraries.

func LesserOrEqualThan

func LesserOrEqualThan(field string) string

LesserOrEqualThan is a convenience function to enable use of go for where definitions

func LesserThan

func LesserThan(field string) string

LesserThan is a convenience function to enable use of go for where definitions

func Like added in v0.1.16

func Like(field string) string

Like is a convenience function to enable use of go for where definitions

func MAX added in v0.1.16

func MAX(column string) string

MAX Renders SQL MAX of the expresion in column

func MIN added in v0.1.16

func MIN(column string) string

MIN Renders SQL MIN of the expresion in column

func MarksToPlaceholders added in v0.1.16

func MarksToPlaceholders(q string, args []interface{}) (string, []interface{}, error)

MarksToPlaceholders replaces `?` in the query with `$1` style placeholders, this must be done with a finished query and requires the args as they depend on the position of the already rendered query, it does some consistency control and finally expands `(?)`.

func NillableInt64 added in v0.1.16

func NillableInt64(i *int64) int64

NillableInt64 returns a safely dereferenced int64 from it's pointer.

func NillableString added in v0.1.16

func NillableString(s *string) string

NillableString returns a safely dereferenced string from it's pointer.

func NotEquals

func NotEquals(field string) string

NotEquals is a convenience function to enable use of go for where definitions

func NotLike added in v0.1.16

func NotLike(field string) string

NotLike is a convenience function to enable use of go for where definitions

func NotNull added in v0.1.2

func NotNull(field string) string

NotNull is a convenience function to enable use of go for where definitions

func Null added in v0.1.2

func Null(field string) string

Null is a convenience function to enable use of go for where definitions

func SUM added in v0.1.16

func SUM(column string) string

SUM Renders SQL SUM of the expresion in column

func SetToCurrentTimestamp added in v0.1.7

func SetToCurrentTimestamp(field string) string

SetToCurrentTimestamp crafts a postgres SQL assignement of the field to the current timestamp with timezone.

func SimpleFunction added in v0.1.16

func SimpleFunction(fName, params string) string

SimpleFunction returns the rendered fName invocation passing params as argument

func TablePrefix added in v0.1.16

func TablePrefix(n string) func(string) string

TablePrefix returns a function that prefixes column names with the passed table name.

Types

type CompOperator added in v0.1.16

type CompOperator string

CompOperator represents a possible operator to compare two SQL expresions

var (
	// Eq is the = operand
	Eq CompOperator = "="
	// Neq is the != operand
	Neq CompOperator = "!="
	// Gt is the > operand
	Gt CompOperator = ">"
	// GtE is the >= operand
	GtE CompOperator = ">="
	// Lt is the < operand
	Lt CompOperator = "<"
	// LtE is the <= operand
	LtE CompOperator = "<="
	// Lk is the LIKE operand
	Lk CompOperator = "LIKE"
	// NLk is the NOT LIKE operand
	NLk CompOperator = "NOT LIKE"
)

type ExpresionChain

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

ExpresionChain holds all the atoms for the SQL expresions that make a query and allows to chain more assuming the chaining is valid.

func NewExpresionChain

func NewExpresionChain(db connection.DB) *ExpresionChain

NewExpresionChain returns a new instance of ExpresionChain hooked to the passed DB

func Not

Not replaces the chaining operation in the last segment atom by 'AND NOT' or 'OR NOT' depending on what the previous one was (either 'AND' or 'OR') as long as the last operation is a 'WHERE' segment atom.

func Or

Or replaces the chaining operation in the last segment atom by 'OR' or 'OR NOT' depending on what the previous one was (either 'AND' or 'AND NOT') as long as the last operation is a 'WHERE' segment atom.

func (*ExpresionChain) AddUnionFromChain added in v0.1.17

func (ec *ExpresionChain) AddUnionFromChain(union *ExpresionChain, all bool) (*ExpresionChain, error)

AddUnionFromChain renders the passed chain and adds it to the current one as a Union returned ExpresionChain pointer is of current chain modified.

func (*ExpresionChain) AndHaving added in v0.1.17

func (ec *ExpresionChain) AndHaving(expr string, args ...interface{}) *ExpresionChain

AndHaving adds a 'HAVING' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) AndWhere

func (ec *ExpresionChain) AndWhere(expr string, args ...interface{}) *ExpresionChain

AndWhere adds a 'AND WHERE' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) AndWhereGroup

func (ec *ExpresionChain) AndWhereGroup(c *ExpresionChain) *ExpresionChain

AndWhereGroup adds an AND ( a = b AND/OR c = d...) basically a group of conditions preceded by AND unless it's the first condition then just the group. It takes an expression chain as a parameter which does not need an DB or any other expresion other than WHEREs `(&ExpressionChain{}).AndWhere(...).OrWhere(...)` THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) Clone

func (ec *ExpresionChain) Clone() *ExpresionChain

Clone returns a copy of the ExpresionChain

func (*ExpresionChain) DB added in v0.1.16

func (ec *ExpresionChain) DB() connection.DB

DB returns the chain DB

func (*ExpresionChain) Delete

func (ec *ExpresionChain) Delete() *ExpresionChain

Delete determines a deletion will be made with the results of the query.

func (*ExpresionChain) Exec

func (ec *ExpresionChain) Exec() (execError error)

Exec executes the chain, works for Insert and Update

func (*ExpresionChain) ExecResult added in v0.1.17

func (ec *ExpresionChain) ExecResult() (rowsAffected int64, execError error)

ExecResult executes the chain and returns rows affected info, works for Insert and Update

func (*ExpresionChain) From added in v0.1.16

func (ec *ExpresionChain) From(table string) *ExpresionChain

From sets the table to be used in the `FROM` expresion. Functionally this is identical to `Table()`, but it makes code more readable in some circumstances. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) FullJoin added in v0.1.16

func (ec *ExpresionChain) FullJoin(expr, on string, args ...interface{}) *ExpresionChain

FullJoin adds a 'FULL JOIN' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) GroupBy

func (ec *ExpresionChain) GroupBy(expr string, args ...interface{}) *ExpresionChain

GroupBy adds a 'GROUP BY' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) GroupByReplace added in v0.1.17

func (ec *ExpresionChain) GroupByReplace(expr string, args ...interface{}) *ExpresionChain

GroupByReplace adds a 'GROUP BY' to the 'ExpresionChain' and returns the same chain to facilitate further chaining, this version of group by removes all other group by entries. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) InnerJoin added in v0.1.10

func (ec *ExpresionChain) InnerJoin(expr, on string, args ...interface{}) *ExpresionChain

InnerJoin adds a 'INNER JOIN' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) Insert

func (ec *ExpresionChain) Insert(insertPairs map[string]interface{}) *ExpresionChain

Insert set fields/values for insertion.

func (*ExpresionChain) InsertMulti

func (ec *ExpresionChain) InsertMulti(insertPairs map[string][]interface{}) (*ExpresionChain, error)

InsertMulti set fields/values for insertion.

func (*ExpresionChain) Join

func (ec *ExpresionChain) Join(expr, on string, args ...interface{}) *ExpresionChain

Join adds a 'JOIN' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) LeftJoin added in v0.1.10

func (ec *ExpresionChain) LeftJoin(expr, on string, args ...interface{}) *ExpresionChain

LeftJoin adds a 'LEFT JOIN' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) Limit

func (ec *ExpresionChain) Limit(limit int64) *ExpresionChain

Limit adds a 'LIMIT' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) NewDB

func (ec *ExpresionChain) NewDB(db connection.DB) *ExpresionChain

NewDB sets the passed db as this chain's db.

func (*ExpresionChain) Offset

func (ec *ExpresionChain) Offset(offset int64) *ExpresionChain

Offset adds a 'OFFSET' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) OnConflict added in v0.1.16

func (ec *ExpresionChain) OnConflict(clause func(*OnConflict)) *ExpresionChain

OnConflict will add a "ON CONFLICT" clause at the end of the query if the main operation is an INSERT.

func (*ExpresionChain) OrHaving added in v0.1.17

func (ec *ExpresionChain) OrHaving(expr string, args ...interface{}) *ExpresionChain

OrHaving adds a 'HAVING' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) OrWhere

func (ec *ExpresionChain) OrWhere(expr string, args ...interface{}) *ExpresionChain

OrWhere adds a 'OR WHERE' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) OrWhereGroup

func (ec *ExpresionChain) OrWhereGroup(c *ExpresionChain) *ExpresionChain

OrWhereGroup adds an OR ( a = b AND/OR c = d...) basically a group of conditions preceded by OR unless it's the first condition and there are no ANDs present. It takes an expression chain as a parameter which does not need an DB or any other expresion other than WHEREs `(&ExpressionChain{}).AndWhere(...).OrWhere(...)` THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) OrderBy

func (ec *ExpresionChain) OrderBy(order *OrderByOperator) *ExpresionChain

OrderBy adds a 'ORDER BY' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) Query

func (ec *ExpresionChain) Query() (connection.ResultFetch, error)

Query is a convenience function to run the current chain through the db query with iterator.

func (*ExpresionChain) QueryIter

func (ec *ExpresionChain) QueryIter() (connection.ResultFetchIter, error)

QueryIter is a convenience function to run the current chain through the db query with iterator.

func (*ExpresionChain) QueryPrimitive added in v0.1.9

func (ec *ExpresionChain) QueryPrimitive() (connection.ResultFetch, error)

QueryPrimitive is a convenience function to run the current chain through the db query.

func (*ExpresionChain) Raw

func (ec *ExpresionChain) Raw(fields ...interface{}) error

Raw executes the query and tries to scan the result into fields without much safeguard nor intelligence so you will have to put some of your own

func (*ExpresionChain) Render

func (ec *ExpresionChain) Render() (string, []interface{}, error)

Render returns the SQL expresion string and the arguments of said expresion, there is no checkig of validity or consistency for the time being.

func (*ExpresionChain) RenderRaw

func (ec *ExpresionChain) RenderRaw() (string, []interface{}, error)

RenderRaw returns the SQL expresion string and the arguments of said expresion, No positional argument replacement is done.

func (*ExpresionChain) Returning added in v0.1.16

func (ec *ExpresionChain) Returning(args ...string) *ExpresionChain

Returning will add an "RETURNING" clause at the end of the query if the main operation is an INSERT.

Please note that `Returning` likely doesn't do what you expect. There are systemic issues with dependencies and `go-lang` standard library that prevent it from operating correctly in many scenarios.

func (*ExpresionChain) RightJoin added in v0.1.10

func (ec *ExpresionChain) RightJoin(expr, on string, args ...interface{}) *ExpresionChain

RightJoin adds a 'RIGHT JOIN' to the 'ExpresionChain' and returns the same chan to facilitate further chaining. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) Select

func (ec *ExpresionChain) Select(fields ...string) *ExpresionChain

Select set fields to be returned by the final query.

func (*ExpresionChain) SelectWithArgs added in v0.1.16

func (ec *ExpresionChain) SelectWithArgs(fields ...SelectArgument) *ExpresionChain

SelectWithArgs set fields to be returned by the final query.

func (*ExpresionChain) Set

func (ec *ExpresionChain) Set(set string) *ExpresionChain

Set will produce your chain to be run inside a Transaction and used for `SET LOCAL` For the moment this is only used with Exec.

func (*ExpresionChain) String added in v0.1.17

func (ec *ExpresionChain) String() string

String implements the stringer interface. It is intended to be used for logging/debugging purposes only.

func (*ExpresionChain) Table

func (ec *ExpresionChain) Table(table string) *ExpresionChain

Table sets the table to be used in the 'FROM' expresion. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

func (*ExpresionChain) Union added in v0.1.17

func (ec *ExpresionChain) Union(unionExpr string, all bool, args ...interface{}) *ExpresionChain

Union adds the passed SQL expresion and args as a union to be made on this expresion, the change is in place, there are no checks about correctness of the query.

func (*ExpresionChain) Update

func (ec *ExpresionChain) Update(expr string, args ...interface{}) *ExpresionChain

Update set fields/values for updates. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

NOTE: values of `nil` will be treated as `NULL`

func (*ExpresionChain) UpdateMap added in v0.1.7

func (ec *ExpresionChain) UpdateMap(exprMap map[string]interface{}) *ExpresionChain

UpdateMap set fields/values for updates but does so from a map of key/value. THIS DOES NOT CREATE A COPY OF THE CHAIN, IT MUTATES IN PLACE.

NOTE: values of `nil` will be treated as `NULL`

func (*ExpresionChain) With added in v0.1.16

func (ec *ExpresionChain) With(name string, cte *ExpresionChain) *ExpresionChain

With adds a CTE to your query (https://www.postgresql.org/docs/11/queries-with.html)

type Function added in v0.1.16

type Function interface {
	// Static adds an argument to the function
	Static(string) Function
	// Parametric adds a placeholder and an argument to the function
	Parametric(interface{}) Function
	// Fn returns the rendered statemtn and list of arguments.
	Fn() (string, []interface{})
	// FnSelect returns a SelectArgument from this function
	FnSelect() SelectArgument
}

Function represents a SQL function.

func ComplexFunction added in v0.1.16

func ComplexFunction(name string) Function

ComplexFunction constructs a complexFunction

type Group

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

Group allows to group a set of expressions and run them together in a transaction.

func (*Group) Add

func (cg *Group) Add(ec *ExpresionChain)

Add appends a chain to the group.

func (*Group) Run

func (cg *Group) Run() (execError error)

Run runs all the chains in a group in a transaction, for this the db of the first query will be used.

func (*Group) Set

func (cg *Group) Set(set string)

Set will cause `SET LOCAL` to be run with this value before executing items of the group in Run.

type OnConflict added in v0.1.16

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

OnConflict is chained to build `OnConflict` statements

func (*OnConflict) DoNothing added in v0.1.16

func (o *OnConflict) DoNothing()

DoNothing terminates the `ON CONFLICT` chain the conflict target is optional for this action.

func (*OnConflict) OnColumn added in v0.1.16

func (o *OnConflict) OnColumn(args ...string) *OnConflictAction

OnColumn is used to construct `ON CONFLICT ( arg0, arg1, arg2 )`. This allows for build things like `ON COLUMN ( myindex, COLLATE my_other_index )`

func (*OnConflict) OnConstraint added in v0.1.16

func (o *OnConflict) OnConstraint(arg string) *OnConflictAction

OnConstraint is used to create an `On CONFLICT ON CONSTRAINT $arg` statement

type OnConflictAction added in v0.1.16

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

OnConflictAction is used to limit developer actions

func (*OnConflictAction) DoNothing added in v0.1.16

func (o *OnConflictAction) DoNothing()

DoNothing terminates the `ON CONFLICT` chain

func (*OnConflictAction) DoUpdate added in v0.1.16

func (o *OnConflictAction) DoUpdate() *OnUpdate

DoUpdate continues the `ON CONFLICT` chain

type OnUpdate added in v0.1.16

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

OnUpdate is used to limit developer actions

func (*OnUpdate) Set added in v0.1.16

func (o *OnUpdate) Set(args ...interface{}) *OnUpdate

Set Sets a field to a value

func (*OnUpdate) SetDefault added in v0.1.16

func (o *OnUpdate) SetDefault(column string) *OnUpdate

SetDefault sets a field to a default value. This is useful to build `ON CONFLICT ON CONSTRAINT my_constraint DO UPDATE SET field = DEFAULT`.

func (*OnUpdate) SetNow added in v0.1.16

func (o *OnUpdate) SetNow(column string) *OnUpdate

SetNow is incrediably useful to set `now()` values. For example: `ON CONFLICT ON CONSTRAINT my_constraint DO UPDATE SET time_value = now()`.

func (*OnUpdate) SetSQL added in v0.1.16

func (o *OnUpdate) SetSQL(args ...string) *OnUpdate

SetSQL Sets a field to a value that needs no escaping, it is assumed to be SQL valid (an expression or column)

func (*OnUpdate) Where added in v0.1.16

func (o *OnUpdate) Where(ec *ExpresionChain)

Where Adds Where condition to an update on conflict, does not return the OnUpdate because it is intended to be the last part of the expresion.

type OrderByOperator added in v0.1.14

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

OrderByOperator unifies the `Asc` and `Desc` functions

func Asc added in v0.1.14

func Asc(columns ...string) *OrderByOperator

Asc declares OrderBy ascending, so least to greatest

func Desc added in v0.1.14

func Desc(columns ...string) *OrderByOperator

Desc declares OrderBy descending, or greatest to least

func (*OrderByOperator) Asc added in v0.1.14

func (o *OrderByOperator) Asc(columns ...string) *OrderByOperator

Asc allows for complex chained OrderBy clauses

func (*OrderByOperator) Desc added in v0.1.14

func (o *OrderByOperator) Desc(columns ...string) *OrderByOperator

Desc allows for complex chained OrderBy clauses

func (*OrderByOperator) String added in v0.1.14

func (o *OrderByOperator) String() string

String converts the operator to a string

type SelectArgument added in v0.1.16

type SelectArgument struct {
	Field string

	Args []interface{}
	// contains filtered or unexported fields
}

SelectArgument contains the components of a select column

func (SelectArgument) As added in v0.1.16

func (s SelectArgument) As(alias string) SelectArgument

As aliases the argument

Jump to

Keyboard shortcuts

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