Strictly type Integer and Real expressions.
This commit is contained in:
parent
7f5ba98819
commit
2b6288d317
32 changed files with 640 additions and 547 deletions
|
|
@ -1,101 +1,203 @@
|
|||
package sqlbuilder
|
||||
|
||||
import "errors"
|
||||
|
||||
//----------- Logical operators ---------------//
|
||||
|
||||
// Returns a representation of "not expr"
|
||||
func NOT(expr boolExpression) boolExpression {
|
||||
func NOT(expr BoolExpression) BoolExpression {
|
||||
return newPrefixBoolExpression(expr, "NOT")
|
||||
}
|
||||
|
||||
//----------- Comparison operators ---------------//
|
||||
|
||||
// Returns a representation of "a=b"
|
||||
func EQ(lhs, rhs expression) boolExpression {
|
||||
func EQ(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "=")
|
||||
}
|
||||
|
||||
// Returns a representation of "a!=b"
|
||||
func NOT_EQ(lhs, rhs expression) boolExpression {
|
||||
func NOT_EQ(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "!=")
|
||||
}
|
||||
|
||||
func IS_DISTINCT_FROM(lhs, rhs expression) boolExpression {
|
||||
func IS_DISTINCT_FROM(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "IS DISTINCT FROM")
|
||||
}
|
||||
|
||||
func IS_NOT_DISTINCT_FROM(lhs, rhs expression) boolExpression {
|
||||
func IS_NOT_DISTINCT_FROM(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "IS NOT DISTINCT FROM")
|
||||
}
|
||||
|
||||
// Returns a representation of "a<b"
|
||||
func LT(lhs expression, rhs expression) boolExpression {
|
||||
func LT(lhs expression, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "<")
|
||||
}
|
||||
|
||||
// Returns a representation of "a<=b"
|
||||
func LT_EQ(lhs, rhs expression) boolExpression {
|
||||
func LT_EQ(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "<=")
|
||||
}
|
||||
|
||||
// Returns a representation of "a>b"
|
||||
func GT(lhs, rhs expression) boolExpression {
|
||||
func GT(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, ">")
|
||||
}
|
||||
|
||||
// Returns a representation of "a>=b"
|
||||
func GT_EQ(lhs, rhs expression) boolExpression {
|
||||
func GT_EQ(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, ">=")
|
||||
}
|
||||
|
||||
func IS_TRUE(expr boolExpression) boolExpression {
|
||||
func IS_TRUE(expr BoolExpression) BoolExpression {
|
||||
return newPostifxBoolExpression(expr, "IS TRUE")
|
||||
}
|
||||
|
||||
func IS_NOT_TRUE(expr boolExpression) boolExpression {
|
||||
func IS_NOT_TRUE(expr BoolExpression) BoolExpression {
|
||||
return newPostifxBoolExpression(expr, "IS NOT TRUE")
|
||||
}
|
||||
|
||||
func IS_FALSE(expr boolExpression) boolExpression {
|
||||
func IS_FALSE(expr BoolExpression) BoolExpression {
|
||||
return newPostifxBoolExpression(expr, "IS FALSE")
|
||||
}
|
||||
|
||||
func IS_NOT_FALSE(expr boolExpression) boolExpression {
|
||||
func IS_NOT_FALSE(expr BoolExpression) BoolExpression {
|
||||
return newPostifxBoolExpression(expr, "IS NOT FALSE")
|
||||
}
|
||||
|
||||
func IS_UNKNOWN(expr boolExpression) boolExpression {
|
||||
func IS_UNKNOWN(expr BoolExpression) BoolExpression {
|
||||
return newPostifxBoolExpression(expr, "IS UNKNOWN")
|
||||
}
|
||||
|
||||
func IS_NOT_UNKNOWN(expr boolExpression) boolExpression {
|
||||
func IS_NOT_UNKNOWN(expr BoolExpression) BoolExpression {
|
||||
return newPostifxBoolExpression(expr, "IS NOT UNKNOWN")
|
||||
}
|
||||
|
||||
func And(lhs, rhs expression) boolExpression {
|
||||
func And(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "AND")
|
||||
}
|
||||
|
||||
// Returns a representation of "c[0] OR ... OR c[n-1]" for c in clauses
|
||||
func Or(lhs, rhs expression) boolExpression {
|
||||
func Or(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "OR")
|
||||
}
|
||||
|
||||
func Like(lhs, rhs expression) boolExpression {
|
||||
func Like(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "LIKE")
|
||||
}
|
||||
|
||||
func LikeL(lhs expression, val string) boolExpression {
|
||||
func LikeL(lhs expression, val string) BoolExpression {
|
||||
return Like(lhs, Literal(val))
|
||||
}
|
||||
|
||||
func Regexp(lhs, rhs expression) boolExpression {
|
||||
func Regexp(lhs, rhs expression) BoolExpression {
|
||||
return newBinaryBoolExpression(lhs, rhs, "REGEXP")
|
||||
}
|
||||
|
||||
func RegexpL(lhs expression, val string) boolExpression {
|
||||
func RegexpL(lhs expression, val string) BoolExpression {
|
||||
return Regexp(lhs, Literal(val))
|
||||
}
|
||||
|
||||
func EXISTS(subQuery selectStatement) boolExpression {
|
||||
func EXISTS(subQuery selectStatement) BoolExpression {
|
||||
return newPrefixBoolExpression(subQuery, "EXISTS")
|
||||
}
|
||||
|
||||
// --------------- CASE operator -------------------//
|
||||
|
||||
type caseOperatorExpression interface {
|
||||
expression
|
||||
|
||||
WHEN(condition expression) caseOperatorExpression
|
||||
THEN(then expression) caseOperatorExpression
|
||||
ELSE(els expression) caseOperatorExpression
|
||||
}
|
||||
|
||||
type caseOperatorImpl struct {
|
||||
expressionInterfaceImpl
|
||||
|
||||
expression expression
|
||||
when []expression
|
||||
then []expression
|
||||
els expression
|
||||
}
|
||||
|
||||
func CASE(expression ...expression) caseOperatorExpression {
|
||||
caseExp := &caseOperatorImpl{}
|
||||
|
||||
if len(expression) == 1 {
|
||||
caseExp.expression = expression[0]
|
||||
}
|
||||
|
||||
caseExp.expressionInterfaceImpl.parent = caseExp
|
||||
|
||||
return caseExp
|
||||
}
|
||||
|
||||
func (c *caseOperatorImpl) WHEN(when expression) caseOperatorExpression {
|
||||
c.when = append(c.when, when)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *caseOperatorImpl) THEN(then expression) caseOperatorExpression {
|
||||
c.then = append(c.then, then)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *caseOperatorImpl) ELSE(els expression) caseOperatorExpression {
|
||||
c.els = els
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *caseOperatorImpl) serialize(statement statementType, out *queryData) error {
|
||||
if c == nil {
|
||||
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 {
|
||||
return errors.New("Invalid case Statement. There should be at least one when/then expression pair. ")
|
||||
}
|
||||
|
||||
if len(c.when) != len(c.then) {
|
||||
return errors.New("When and then expression count mismatch. ")
|
||||
}
|
||||
|
||||
for i, when := range c.when {
|
||||
out.writeString("WHEN")
|
||||
err := when.serialize(statement, out)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out.writeString("THEN")
|
||||
err = c.then[i].serialize(statement, out)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if c.els != nil {
|
||||
out.writeString("ELSE")
|
||||
err := c.els.serialize(statement, out)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
out.writeString("END)")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue