2019-03-02 12:34:08 +01:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-05 17:15:20 +02:00
|
|
|
"errors"
|
2019-06-07 14:23:14 +02:00
|
|
|
"fmt"
|
2019-03-02 12:34:08 +01:00
|
|
|
)
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
// An Expression
|
|
|
|
|
type Expression interface {
|
2019-05-07 19:06:21 +02:00
|
|
|
clause
|
|
|
|
|
projection
|
|
|
|
|
groupByClause
|
2019-06-04 12:10:23 +02:00
|
|
|
OrderByClause
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
IS_NULL() BoolExpression
|
|
|
|
|
IS_NOT_NULL() BoolExpression
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
IN(expressions ...Expression) BoolExpression
|
|
|
|
|
NOT_IN(expressions ...Expression) BoolExpression
|
2019-05-05 18:03:30 +02:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
AS(alias string) projection
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
ASC() OrderByClause
|
|
|
|
|
DESC() OrderByClause
|
2019-06-02 18:12:44 +02:00
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
TO(dbType string) Expression
|
|
|
|
|
TO_BOOL() BoolExpression
|
|
|
|
|
TO_SMALLINT() IntegerExpression
|
|
|
|
|
TO_INTEGER() IntegerExpression
|
|
|
|
|
TO_BIGINT() IntegerExpression
|
|
|
|
|
TO_NUMERIC(precision int, scale ...int) FloatExpression
|
|
|
|
|
TO_REAL() FloatExpression
|
|
|
|
|
TO_DOUBLE() FloatExpression
|
|
|
|
|
TO_TEXT() StringExpression
|
|
|
|
|
TO_DATE() DateExpression
|
|
|
|
|
TO_TIME() TimeExpression
|
|
|
|
|
TO_TIMEZ() TimezExpression
|
|
|
|
|
TO_TIMESTAMP() TimestampExpression
|
|
|
|
|
TO_TIMESTAMPZ() TimestampzExpression
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
type expressionInterfaceImpl struct {
|
2019-06-04 12:10:23 +02:00
|
|
|
parent Expression
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
func (e *expressionInterfaceImpl) IS_NULL() BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newPostifxBoolExpression(e.parent, "IS NULL")
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
func (e *expressionInterfaceImpl) IS_NOT_NULL() BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newPostifxBoolExpression(e.parent, "IS NOT NULL")
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (e *expressionInterfaceImpl) IN(expressions ...Expression) BoolExpression {
|
2019-06-04 11:52:37 +02:00
|
|
|
return newBinaryBoolExpression(e.parent, WRAP(expressions...), "IN")
|
2019-05-05 18:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (e *expressionInterfaceImpl) NOT_IN(expressions ...Expression) BoolExpression {
|
2019-06-04 11:52:37 +02:00
|
|
|
return newBinaryBoolExpression(e.parent, WRAP(expressions...), "NOT IN")
|
2019-05-05 18:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (e *expressionInterfaceImpl) AS(alias string) projection {
|
2019-03-31 09:17:28 +02:00
|
|
|
return NewAlias(e.parent, alias)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (e *expressionInterfaceImpl) ASC() OrderByClause {
|
2019-05-07 19:06:21 +02:00
|
|
|
return &orderByClauseImpl{expression: e.parent, ascent: true}
|
2019-04-03 11:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (e *expressionInterfaceImpl) DESC() OrderByClause {
|
2019-05-07 19:06:21 +02:00
|
|
|
return &orderByClauseImpl{expression: e.parent, ascent: false}
|
2019-04-03 11:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO(dbType string) Expression {
|
2019-06-03 17:05:29 +02:00
|
|
|
return newCast(e.parent, dbType)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_BOOL() BoolExpression {
|
2019-06-02 18:12:44 +02:00
|
|
|
return newBoolCast(e.parent)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_SMALLINT() IntegerExpression {
|
|
|
|
|
return newIntegerCast(e.parent, "smallint")
|
2019-06-02 18:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_INTEGER() IntegerExpression {
|
|
|
|
|
return newIntegerCast(e.parent, "integer")
|
2019-06-02 18:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_BIGINT() IntegerExpression {
|
|
|
|
|
return newIntegerCast(e.parent, "bigint")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *expressionInterfaceImpl) TO_NUMERIC(precision int, scale ...int) FloatExpression {
|
|
|
|
|
var castType string
|
|
|
|
|
if len(scale) > 0 {
|
|
|
|
|
castType = fmt.Sprintf("numeric(%d, %d)", precision, scale[0])
|
|
|
|
|
} else {
|
|
|
|
|
castType = fmt.Sprintf("numeric(%d)", precision)
|
|
|
|
|
}
|
|
|
|
|
return newFloatCast(e.parent, castType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *expressionInterfaceImpl) TO_REAL() FloatExpression {
|
|
|
|
|
return newFloatCast(e.parent, "real")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *expressionInterfaceImpl) TO_DOUBLE() FloatExpression {
|
|
|
|
|
return newFloatCast(e.parent, "double precision")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *expressionInterfaceImpl) TO_TEXT() StringExpression {
|
2019-06-02 18:12:44 +02:00
|
|
|
return newTextCast(e.parent)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_DATE() DateExpression {
|
2019-06-02 18:12:44 +02:00
|
|
|
return newDateCast(e.parent)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_TIME() TimeExpression {
|
2019-06-02 18:12:44 +02:00
|
|
|
return newTimeCast(e.parent)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_TIMEZ() TimezExpression {
|
2019-06-02 18:12:44 +02:00
|
|
|
return newTimezCast(e.parent)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_TIMESTAMP() TimestampExpression {
|
2019-06-02 18:12:44 +02:00
|
|
|
return newTimestampCast(e.parent)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
func (e *expressionInterfaceImpl) TO_TIMESTAMPZ() TimestampzExpression {
|
2019-06-02 18:12:44 +02:00
|
|
|
return newTimestampzCast(e.parent)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
func (e *expressionInterfaceImpl) serializeForGroupBy(statement statementType, out *queryData) error {
|
2019-06-01 15:00:37 +02:00
|
|
|
return e.parent.serialize(statement, out, NO_WRAP)
|
2019-05-07 19:06:21 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
func (e *expressionInterfaceImpl) serializeForProjection(statement statementType, out *queryData) error {
|
2019-06-01 15:00:37 +02:00
|
|
|
return e.parent.serialize(statement, out, NO_WRAP)
|
2019-04-03 11:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
func (e *expressionInterfaceImpl) serializeForOrderBy(statement statementType, out *queryData) error {
|
2019-06-01 15:00:37 +02:00
|
|
|
return e.parent.serialize(statement, out, NO_WRAP)
|
2019-05-08 12:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
// Representation of binary operations (e.g. comparisons, arithmetic)
|
2019-05-29 14:03:38 +02:00
|
|
|
type binaryOpExpression struct {
|
2019-06-04 12:10:23 +02:00
|
|
|
lhs, rhs Expression
|
2019-05-05 18:03:30 +02:00
|
|
|
operator string
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func newBinaryExpression(lhs, rhs Expression, operator string) binaryOpExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
binaryExpression := binaryOpExpression{
|
2019-03-31 09:17:28 +02:00
|
|
|
lhs: lhs,
|
|
|
|
|
rhs: rhs,
|
|
|
|
|
operator: operator,
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
return binaryExpression
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-31 14:37:51 +02:00
|
|
|
func (c *binaryOpExpression) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
2019-05-13 12:33:11 +02:00
|
|
|
if c == nil {
|
2019-06-04 12:10:23 +02:00
|
|
|
return errors.New("Binary Expression is nil.")
|
2019-05-13 12:33:11 +02:00
|
|
|
}
|
2019-03-02 12:34:08 +01:00
|
|
|
if c.lhs == nil {
|
2019-06-05 17:15:20 +02:00
|
|
|
return errors.New("nil lhs.")
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
2019-05-01 17:25:10 +02:00
|
|
|
if c.rhs == nil {
|
2019-06-05 17:15:20 +02:00
|
|
|
return errors.New("nil rhs.")
|
2019-05-01 17:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-31 14:37:51 +02:00
|
|
|
wrap := !contains(options, NO_WRAP)
|
2019-05-01 17:25:10 +02:00
|
|
|
|
2019-05-01 18:23:19 +02:00
|
|
|
if wrap {
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString("(")
|
2019-05-01 17:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
if err := c.lhs.serialize(statement, out); err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(" " + c.operator + " ")
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
if err := c.rhs.serialize(statement, out); err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 18:23:19 +02:00
|
|
|
if wrap {
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(")")
|
2019-05-01 17:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-02 12:34:08 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
// A prefix operator Expression
|
2019-05-29 14:03:38 +02:00
|
|
|
type prefixOpExpression struct {
|
2019-06-04 12:10:23 +02:00
|
|
|
expression Expression
|
2019-05-05 18:03:30 +02:00
|
|
|
operator string
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func newPrefixExpression(expression Expression, operator string) prefixOpExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
prefixExpression := prefixOpExpression{
|
2019-03-31 09:17:28 +02:00
|
|
|
expression: expression,
|
|
|
|
|
operator: operator,
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
return prefixExpression
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-31 14:37:51 +02:00
|
|
|
func (p *prefixOpExpression) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
2019-05-13 12:33:11 +02:00
|
|
|
if p == nil {
|
2019-06-04 12:10:23 +02:00
|
|
|
return errors.New("Prefix Expression is nil.")
|
2019-05-13 12:33:11 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(p.operator + " ")
|
2019-03-02 12:34:08 +01:00
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
if p.expression == nil {
|
2019-06-05 17:15:20 +02:00
|
|
|
return errors.New("nil prefix Expression.")
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
2019-05-08 13:47:01 +02:00
|
|
|
if err := p.expression.serialize(statement, out); err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
return nil
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
// A postifx operator Expression
|
2019-05-29 14:03:38 +02:00
|
|
|
type postfixOpExpression struct {
|
2019-06-04 12:10:23 +02:00
|
|
|
expression Expression
|
2019-05-29 14:03:38 +02:00
|
|
|
operator string
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func newPostfixOpExpression(expression Expression, operator string) postfixOpExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
postfixOpExpression := postfixOpExpression{
|
|
|
|
|
expression: expression,
|
|
|
|
|
operator: operator,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return postfixOpExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 14:37:51 +02:00
|
|
|
func (p *postfixOpExpression) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
2019-05-29 14:03:38 +02:00
|
|
|
if p == nil {
|
2019-06-04 12:10:23 +02:00
|
|
|
return errors.New("Postifx operator Expression is nil.")
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.expression == nil {
|
2019-06-05 17:15:20 +02:00
|
|
|
return errors.New("nil prefix Expression.")
|
2019-05-29 14:03:38 +02:00
|
|
|
}
|
|
|
|
|
if err := p.expression.serialize(statement, out); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.writeString(p.operator)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|