2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
import "errors"
|
|
|
|
|
|
2019-05-29 14:03:38 +02:00
|
|
|
//----------- Logical operators ---------------//
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "not expr"
|
2019-05-31 12:59:57 +02:00
|
|
|
func NOT(expr BoolExpression) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newPrefixBoolExpression(expr, "NOT")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//----------- Comparison operators ---------------//
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a=b"
|
2019-06-04 12:10:23 +02:00
|
|
|
func EQ(lhs, rhs Expression) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "=")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a!=b"
|
2019-06-04 12:10:23 +02:00
|
|
|
func NOT_EQ(lhs, rhs Expression) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "!=")
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func IS_DISTINCT_FROM(lhs, rhs Expression) BoolExpression {
|
2019-05-30 14:49:36 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "IS DISTINCT FROM")
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func IS_NOT_DISTINCT_FROM(lhs, rhs Expression) BoolExpression {
|
2019-05-30 14:49:36 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "IS NOT DISTINCT FROM")
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 14:03:38 +02:00
|
|
|
// Returns a representation of "a<b"
|
2019-06-04 12:10:23 +02:00
|
|
|
func LT(lhs Expression, rhs Expression) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "<")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a<=b"
|
2019-06-04 12:10:23 +02:00
|
|
|
func LT_EQ(lhs, rhs Expression) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "<=")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a>b"
|
2019-06-04 12:10:23 +02:00
|
|
|
func GT(lhs, rhs Expression) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, ">")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a>=b"
|
2019-06-04 12:10:23 +02:00
|
|
|
func GT_EQ(lhs, rhs Expression) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, ">=")
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func EXISTS(subQuery SelectStatement) BoolExpression {
|
2019-05-29 14:03:38 +02:00
|
|
|
return newPrefixBoolExpression(subQuery, "EXISTS")
|
|
|
|
|
}
|
2019-05-31 12:59:57 +02:00
|
|
|
|
|
|
|
|
// --------------- CASE operator -------------------//
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
type CaseOperatorExpression interface {
|
2019-06-04 12:10:23 +02:00
|
|
|
Expression
|
2019-05-31 12:59:57 +02:00
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
WHEN(condition Expression) CaseOperatorExpression
|
|
|
|
|
THEN(then Expression) CaseOperatorExpression
|
|
|
|
|
ELSE(els Expression) CaseOperatorExpression
|
2019-05-31 12:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type caseOperatorImpl struct {
|
|
|
|
|
expressionInterfaceImpl
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
expression Expression
|
|
|
|
|
when []Expression
|
|
|
|
|
then []Expression
|
|
|
|
|
els Expression
|
2019-05-31 12:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
func CASE(expression ...Expression) CaseOperatorExpression {
|
2019-05-31 12:59:57 +02:00
|
|
|
caseExp := &caseOperatorImpl{}
|
|
|
|
|
|
2019-06-03 17:38:47 +02:00
|
|
|
if len(expression) > 0 {
|
2019-05-31 12:59:57 +02:00
|
|
|
caseExp.expression = expression[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
caseExp.expressionInterfaceImpl.parent = caseExp
|
|
|
|
|
|
|
|
|
|
return caseExp
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
func (c *caseOperatorImpl) WHEN(when Expression) CaseOperatorExpression {
|
2019-05-31 12:59:57 +02:00
|
|
|
c.when = append(c.when, when)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
func (c *caseOperatorImpl) THEN(then Expression) CaseOperatorExpression {
|
2019-05-31 12:59:57 +02:00
|
|
|
c.then = append(c.then, then)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
func (c *caseOperatorImpl) ELSE(els Expression) CaseOperatorExpression {
|
2019-05-31 12:59:57 +02:00
|
|
|
c.els = els
|
|
|
|
|
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 14:37:51 +02:00
|
|
|
func (c *caseOperatorImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
2019-05-31 12:59:57 +02:00
|
|
|
if c == nil {
|
2019-06-04 12:10:23 +02:00
|
|
|
return errors.New("Case Expression is nil. ")
|
2019-05-31 12:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.writeString("(CASE")
|
|
|
|
|
|
|
|
|
|
if c.expression != nil {
|
|
|
|
|
err := c.expression.serialize(statement, out)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(c.when) == 0 || len(c.then) == 0 {
|
2019-06-04 12:10:23 +02:00
|
|
|
return errors.New("Invalid case Statement. There should be at least one when/then Expression pair. ")
|
2019-05-31 12:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(c.when) != len(c.then) {
|
2019-06-04 12:10:23 +02:00
|
|
|
return errors.New("When and then Expression count mismatch. ")
|
2019-05-31 12:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, when := range c.when {
|
|
|
|
|
out.writeString("WHEN")
|
2019-05-31 14:37:51 +02:00
|
|
|
err := when.serialize(statement, out, NO_WRAP)
|
2019-05-31 12:59:57 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.writeString("THEN")
|
2019-05-31 14:37:51 +02:00
|
|
|
err = c.then[i].serialize(statement, out, NO_WRAP)
|
2019-05-31 12:59:57 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.els != nil {
|
|
|
|
|
out.writeString("ELSE")
|
2019-05-31 14:37:51 +02:00
|
|
|
err := c.els.serialize(statement, out, NO_WRAP)
|
2019-05-31 12:59:57 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.writeString("END)")
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|