Documentation ¶
Overview ¶
package where provides composable expressions for WHERE and HAVING clauses in SQL. These can range from the very simplest no-op to complex nested trees of 'and' and 'or' conditions.
Also in this package are query constraints to provide 'ORDER BY', 'LIMIT' and 'OFFSET' clauses. These are similar to 'WHERE' clauses except literal values are used instead of parameter placeholders.
Index ¶
- Constants
- func Build(adverb string, wh Expression, q ...dialect.Quoter) (string, []interface{})
- func BuildQueryConstraint(qc QueryConstraint, quoter ...dialect.Quoter) string
- func Having(wh Expression, q ...dialect.Quoter) (string, []interface{})
- func Limit(n int) *queryConstraint
- func Offset(n int) *queryConstraint
- func OrderBy(column ...string) *queryConstraint
- func Where(wh Expression, q ...dialect.Quoter) (string, []interface{})
- type Clause
- type Condition
- type Expression
- func And(exp ...Expression) Expression
- func Between(column string, a, b interface{}) Expression
- func Eq(column string, value interface{}) Expression
- func Gt(column string, value interface{}) Expression
- func GtEq(column string, value interface{}) Expression
- func In(column string, values ...interface{}) Expression
- func Like(column string, pattern string) Expression
- func Lt(column string, value interface{}) Expression
- func LtEq(column string, value interface{}) Expression
- func NoOp() Expression
- func Not(el Expression) Expression
- func NotEq(column string, value interface{}) Expression
- func NotNull(column string) Expression
- func Null(column string) Expression
- func Or(exp ...Expression) Expression
- type QueryConstraint
Constants ¶
const ( WhereAdverb = "WHERE " HavingVerb = "HAVING " )
Variables ¶
This section is empty.
Functions ¶
func Build ¶ added in v0.17.0
func Build(adverb string, wh Expression, q ...dialect.Quoter) (string, []interface{})
Build constructs the sql clause beginning with some verb/adverb. It will contain '?' style placeholders; these need to be passed through the relevant dialect ReplacePlaceholders processing.
func BuildQueryConstraint ¶
func BuildQueryConstraint(qc QueryConstraint, quoter ...dialect.Quoter) string
func Having ¶ added in v0.17.0
func Having(wh Expression, q ...dialect.Quoter) (string, []interface{})
Having constructs the sql clause beginning "HAVING ...". It will contain '?' style placeholders; these need to be passed through the relevant dialect ReplacePlaceholders processing. A quoter may optionally be supplied, otherwise the Default Quoter is used.
func Limit ¶
func Limit(n int) *queryConstraint
Limit sets the upper limit on the number of records to be returned.
func Offset ¶
func Offset(n int) *queryConstraint
Offset sets the offset into the result set; previous items will be discarded.
func OrderBy ¶
func OrderBy(column ...string) *queryConstraint
OrderBy lists the column(s) by which the database will be asked to sort its results. The columns passed in here will be quoted according to the needs of the current dialect.
func Where ¶ added in v0.17.0
func Where(wh Expression, q ...dialect.Quoter) (string, []interface{})
Where constructs the sql clause beginning "WHERE ...". It will contain '?' style placeholders; these need to be passed through the relevant dialect ReplacePlaceholders processing. A quoter may optionally be supplied, otherwise the Default Quoter is used.
Types ¶
type Clause ¶
type Clause struct {
// contains filtered or unexported fields
}
Clause is a compound expression.
func (Clause) And ¶
func (wh Clause) And(exp Expression) Expression
And combines two clauses into a clause that requires they are both true. SQL implementation note: AND has higher precedence than OR.
func (Clause) Or ¶
func (wh Clause) Or(exp Expression) Expression
Or combines two clauses into a clause that requires either is true. SQL implementation note: AND has higher precedence than OR.
type Condition ¶
type Condition struct {
Column, Predicate string
Args []interface{}
}
Condition is a simple condition such as an equality test. For convenience, use the factory functions 'Eq', 'GtEq' etc.
This can also be constructed directly, which will be useful for non-portable cases, such as Postgresql 'SIMILAR TO'
expr := where.Condition{column, " SIMILAR TO", []interface{}{pattern}}
Also for literal values (taking care to protect against injection attacks)
expr := where.Condition{column, " = 'hello'", nil}
func (Condition) And ¶
func (cl Condition) And(c2 Expression) Expression
And combines two conditions into a clause that requires they are both true.
func (Condition) Or ¶
func (cl Condition) Or(c2 Expression) Expression
Or combines two conditions into a clause that requires either is true.
type Expression ¶
type Expression interface { fmt.Stringer And(Expression) Expression Or(Expression) Expression Build(q dialect.Quoter) (string, []interface{}) }
Expression is an element in a WHERE clause. Expressions consist of simple conditions or more complex clauses of multiple conditions.
func And ¶
func And(exp ...Expression) Expression
And combines some expressions into a clause that requires they are all true.
func Between ¶
func Between(column string, a, b interface{}) Expression
Between returns a between condition on a column.
func Eq ¶
func Eq(column string, value interface{}) Expression
Eq returns an equality condition on a column.
func Gt ¶
func Gt(column string, value interface{}) Expression
Gt returns a greater than condition on a column.
func GtEq ¶
func GtEq(column string, value interface{}) Expression
GtEq returns a greater than or equal condition on a column.
func In ¶
func In(column string, values ...interface{}) Expression
In returns an in condition on a column.
func Like ¶
func Like(column string, pattern string) Expression
Like returns a pattern-matching condition on a column. Be careful: this can hurt performance.
func Lt ¶
func Lt(column string, value interface{}) Expression
Lt returns a less than condition on a column.
func LtEq ¶
func LtEq(column string, value interface{}) Expression
LtEq returns a less than or equal than condition on a column.
func NoOp ¶
func NoOp() Expression
NoOp creates an empty expression. This is useful for conditionally chaining expression-based contextual decisions. It can also be passed to any method that need an expression but for which none is required in that case.
func NotEq ¶
func NotEq(column string, value interface{}) Expression
NotEq returns a not equal condition on a column.
func NotNull ¶
func NotNull(column string) Expression
NotNull returns an 'IS NOT NULL' condition on a column. It's also possible to use Not(Null(...)).
func Or ¶
func Or(exp ...Expression) Expression
Or combines some expressions into a clause that requires that any is true.
type QueryConstraint ¶
QueryConstraint is a value that is appended to a SELECT statement.
func Literal ¶
func Literal(sqlPart string) QueryConstraint
Literal returns the literal string supplied, converting it to a QueryConstraint. The string may contain identifiers, however no quoting rules will be applied. Therefore care must be taken if portability is needed.