jet/sqlbuilder/operators.go

196 lines
4.5 KiB
Go
Raw Normal View History

2019-05-29 14:03:38 +02:00
package sqlbuilder
import "errors"
2019-05-29 14:03:38 +02:00
//----------- Logical operators ---------------//
// Returns a representation of "not expr"
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 {
return newBinaryBoolExpression(lhs, rhs, "IS DISTINCT FROM")
}
2019-06-04 12:10:23 +02:00
func IS_NOT_DISTINCT_FROM(lhs, rhs Expression) BoolExpression {
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, ">=")
}
func IS_TRUE(expr BoolExpression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newPostifxBoolExpression(expr, "IS TRUE")
}
func IS_NOT_TRUE(expr BoolExpression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newPostifxBoolExpression(expr, "IS NOT TRUE")
}
func IS_FALSE(expr BoolExpression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newPostifxBoolExpression(expr, "IS FALSE")
}
func IS_NOT_FALSE(expr BoolExpression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newPostifxBoolExpression(expr, "IS NOT FALSE")
}
func IS_UNKNOWN(expr BoolExpression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newPostifxBoolExpression(expr, "IS UNKNOWN")
}
func IS_NOT_UNKNOWN(expr BoolExpression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newPostifxBoolExpression(expr, "IS NOT UNKNOWN")
}
2019-06-04 12:10:23 +02:00
func And(lhs, rhs Expression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newBinaryBoolExpression(lhs, rhs, "AND")
}
// Returns a representation of "c[0] OR ... OR c[n-1]" for c in clauses
2019-06-04 12:10:23 +02:00
func Or(lhs, rhs Expression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newBinaryBoolExpression(lhs, rhs, "OR")
}
2019-06-04 12:10:23 +02:00
func Regexp(lhs, rhs Expression) BoolExpression {
2019-05-29 14:03:38 +02:00
return newBinaryBoolExpression(lhs, rhs, "REGEXP")
}
2019-06-04 12:10:23 +02:00
func RegexpL(lhs Expression, val string) BoolExpression {
return Regexp(lhs, literal(val))
2019-05-29 14:03:38 +02:00
}
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")
}
// --------------- CASE operator -------------------//
type caseOperatorExpression interface {
2019-06-04 12:10:23 +02:00
Expression
2019-06-04 12:10:23 +02:00
WHEN(condition Expression) caseOperatorExpression
THEN(then Expression) caseOperatorExpression
ELSE(els Expression) caseOperatorExpression
}
type caseOperatorImpl struct {
expressionInterfaceImpl
2019-06-04 12:10:23 +02:00
expression Expression
when []Expression
then []Expression
els Expression
}
2019-06-04 12:10:23 +02:00
func CASE(expression ...Expression) caseOperatorExpression {
caseExp := &caseOperatorImpl{}
2019-06-03 17:38:47 +02:00
if len(expression) > 0 {
caseExp.expression = expression[0]
}
caseExp.expressionInterfaceImpl.parent = caseExp
return caseExp
}
2019-06-04 12:10:23 +02:00
func (c *caseOperatorImpl) WHEN(when Expression) caseOperatorExpression {
c.when = append(c.when, when)
return c
}
2019-06-04 12:10:23 +02:00
func (c *caseOperatorImpl) THEN(then Expression) caseOperatorExpression {
c.then = append(c.then, then)
return c
}
2019-06-04 12:10:23 +02:00
func (c *caseOperatorImpl) ELSE(els Expression) caseOperatorExpression {
c.els = els
return c
}
func (c *caseOperatorImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
if c == nil {
2019-06-04 12:10:23 +02:00
return errors.New("Case Expression is nil. ")
}
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. ")
}
if len(c.when) != len(c.then) {
2019-06-04 12:10:23 +02:00
return errors.New("When and then Expression count mismatch. ")
}
for i, when := range c.when {
out.writeString("WHEN")
err := when.serialize(statement, out, NO_WRAP)
if err != nil {
return err
}
out.writeString("THEN")
err = c.then[i].serialize(statement, out, NO_WRAP)
if err != nil {
return err
}
}
if c.els != nil {
out.writeString("ELSE")
err := c.els.serialize(statement, out, NO_WRAP)
if err != nil {
return err
}
}
out.writeString("END)")
return nil
}