jet/sqlbuilder/bool_expresion.go

200 lines
5.3 KiB
Go
Raw Normal View History

package sqlbuilder
2019-05-07 19:06:21 +02:00
type boolExpression interface {
expression
2019-03-31 09:17:28 +02:00
2019-05-07 19:06:21 +02:00
Eq(expression boolExpression) boolExpression
NotEq(expression boolExpression) boolExpression
GtEq(rhs expression) boolExpression
LtEq(rhs expression) boolExpression
2019-03-31 14:07:58 +02:00
2019-05-07 19:06:21 +02:00
AND(expression boolExpression) boolExpression
OR(expression boolExpression) boolExpression
IS_TRUE() boolExpression
IS_FALSE() boolExpression
2019-03-31 09:17:28 +02:00
}
type boolInterfaceImpl struct {
2019-05-07 19:06:21 +02:00
parent boolExpression
2019-03-31 09:17:28 +02:00
}
2019-05-07 19:06:21 +02:00
func (b *boolInterfaceImpl) Eq(expression boolExpression) boolExpression {
2019-03-31 14:07:58 +02:00
return Eq(b.parent, expression)
}
2019-05-07 19:06:21 +02:00
func (b *boolInterfaceImpl) NotEq(expression boolExpression) boolExpression {
return NotEq(b.parent, expression)
2019-03-31 14:07:58 +02:00
}
2019-05-07 19:06:21 +02:00
func (b *boolInterfaceImpl) GtEq(rhs expression) boolExpression {
return GtEq(b.parent, rhs)
2019-03-31 14:07:58 +02:00
}
2019-05-07 19:06:21 +02:00
func (b *boolInterfaceImpl) LtEq(rhs expression) boolExpression {
return LtEq(b.parent, rhs)
2019-03-31 14:07:58 +02:00
}
2019-05-07 19:06:21 +02:00
func (b *boolInterfaceImpl) AND(expression boolExpression) boolExpression {
2019-03-31 09:17:28 +02:00
return And(b.parent, expression)
}
2019-05-07 19:06:21 +02:00
func (b *boolInterfaceImpl) OR(expression boolExpression) boolExpression {
2019-03-31 09:17:28 +02:00
return Or(b.parent, expression)
}
2019-05-07 19:06:21 +02:00
func (b *boolInterfaceImpl) IS_TRUE() boolExpression {
2019-03-31 09:17:28 +02:00
return IsTrue(b.parent)
}
2019-05-07 19:06:21 +02:00
func (b *boolInterfaceImpl) IS_FALSE() boolExpression {
2019-03-31 09:17:28 +02:00
return nil
}
//---------------------------------------------------//
type boolLiteralExpression struct {
boolInterfaceImpl
literalExpression
}
2019-05-07 19:06:21 +02:00
func newBoolLiteralExpression(value bool) boolExpression {
2019-03-31 09:17:28 +02:00
boolLiteralExpression := boolLiteralExpression{}
boolLiteralExpression.literalExpression = *Literal(value)
2019-03-31 09:17:28 +02:00
boolLiteralExpression.boolInterfaceImpl.parent = &boolLiteralExpression
return &boolLiteralExpression
}
//---------------------------------------------------//
type binaryBoolExpression struct {
2019-03-31 14:07:58 +02:00
expressionInterfaceImpl
2019-03-31 09:17:28 +02:00
boolInterfaceImpl
binaryExpression
}
2019-05-07 19:06:21 +02:00
func newBinaryBoolExpression(lhs, rhs expression, operator string) boolExpression {
2019-03-31 09:17:28 +02:00
boolExpression := binaryBoolExpression{}
2019-03-31 14:07:58 +02:00
boolExpression.binaryExpression = newBinaryExpression(lhs, rhs, operator)
boolExpression.expressionInterfaceImpl.parent = &boolExpression
2019-03-31 09:17:28 +02:00
boolExpression.boolInterfaceImpl.parent = &boolExpression
return &boolExpression
}
//---------------------------------------------------//
type prefixBoolExpression struct {
2019-03-31 14:07:58 +02:00
expressionInterfaceImpl
2019-03-31 09:17:28 +02:00
boolInterfaceImpl
prefixExpression
}
2019-05-07 19:06:21 +02:00
func newPrefixBoolExpression(expression expression, operator string) boolExpression {
2019-03-31 09:17:28 +02:00
boolExpression := prefixBoolExpression{}
2019-03-31 14:07:58 +02:00
boolExpression.prefixExpression = newPrefixExpression(expression, operator)
2019-03-31 09:17:28 +02:00
2019-03-31 14:07:58 +02:00
boolExpression.expressionInterfaceImpl.parent = &boolExpression
2019-03-31 09:17:28 +02:00
boolExpression.boolInterfaceImpl.parent = &boolExpression
return &boolExpression
}
2019-05-07 19:06:21 +02:00
func EXISTS(subQuery selectStatement) boolExpression {
return newPrefixBoolExpression(subQuery, "EXISTS")
2019-03-31 09:17:28 +02:00
}
// Returns a representation of "a=b"
2019-05-07 19:06:21 +02:00
func Eq(lhs, rhs expression) boolExpression {
2019-05-06 12:42:15 +02:00
return newBinaryBoolExpression(lhs, rhs, "=")
}
// Returns a representation of "a=b", where b is a literal
2019-05-07 19:06:21 +02:00
func EqL(lhs expression, val interface{}) boolExpression {
return Eq(lhs, Literal(val))
}
// Returns a representation of "a!=b"
2019-05-07 19:06:21 +02:00
func NotEq(lhs, rhs expression) boolExpression {
return newBinaryBoolExpression(lhs, rhs, "!=")
}
// Returns a representation of "a!=b", where b is a literal
2019-05-07 19:06:21 +02:00
func NeqL(lhs expression, val interface{}) boolExpression {
return NotEq(lhs, Literal(val))
}
// Returns a representation of "a<b"
2019-05-07 19:06:21 +02:00
func Lt(lhs expression, rhs expression) boolExpression {
return newBinaryBoolExpression(lhs, rhs, "<")
}
// Returns a representation of "a<b", where b is a literal
2019-05-07 19:06:21 +02:00
func LtL(lhs expression, val interface{}) boolExpression {
return Lt(lhs, Literal(val))
}
// Returns a representation of "a<=b"
2019-05-07 19:06:21 +02:00
func LtEq(lhs, rhs expression) boolExpression {
return newBinaryBoolExpression(lhs, rhs, "<=")
}
// Returns a representation of "a<=b", where b is a literal
2019-05-07 19:06:21 +02:00
func LteL(lhs expression, val interface{}) boolExpression {
return LtEq(lhs, Literal(val))
}
// Returns a representation of "a>b"
2019-05-07 19:06:21 +02:00
func Gt(lhs, rhs expression) boolExpression {
return newBinaryBoolExpression(lhs, rhs, ">")
}
// Returns a representation of "a>b", where b is a literal
2019-05-07 19:06:21 +02:00
func GtL(lhs expression, val interface{}) boolExpression {
return Gt(lhs, Literal(val))
}
// Returns a representation of "a>=b"
2019-05-07 19:06:21 +02:00
func GtEq(lhs, rhs expression) boolExpression {
return newBinaryBoolExpression(lhs, rhs, ">=")
}
// Returns a representation of "a>=b", where b is a literal
2019-05-07 19:06:21 +02:00
func GteL(lhs expression, val interface{}) boolExpression {
return GtEq(lhs, Literal(val))
}
// Returns a representation of "not expr"
2019-05-07 19:06:21 +02:00
func Not(expr boolExpression) boolExpression {
2019-05-06 12:42:15 +02:00
return newPrefixBoolExpression(expr, "NOT")
2019-03-31 09:17:28 +02:00
}
2019-05-07 19:06:21 +02:00
func IsTrue(expr boolExpression) boolExpression {
2019-05-06 12:42:15 +02:00
return newPrefixBoolExpression(expr, "IS TRUE")
}
2019-05-07 19:06:21 +02:00
func And(lhs, rhs expression) boolExpression {
2019-05-06 12:42:15 +02:00
return newBinaryBoolExpression(lhs, rhs, "AND")
}
// Returns a representation of "c[0] OR ... OR c[n-1]" for c in clauses
2019-05-07 19:06:21 +02:00
func Or(lhs, rhs expression) boolExpression {
2019-05-06 12:42:15 +02:00
return newBinaryBoolExpression(lhs, rhs, "OR")
}
2019-05-07 19:06:21 +02:00
func Like(lhs, rhs expression) boolExpression {
2019-05-06 12:42:15 +02:00
return newBinaryBoolExpression(lhs, rhs, "LIKE")
}
2019-05-07 19:06:21 +02:00
func LikeL(lhs expression, val string) boolExpression {
return Like(lhs, Literal(val))
}
2019-05-07 19:06:21 +02:00
func Regexp(lhs, rhs expression) boolExpression {
2019-05-06 12:42:15 +02:00
return newBinaryBoolExpression(lhs, rhs, "REGEXP")
}
2019-05-07 19:06:21 +02:00
func RegexpL(lhs expression, val string) boolExpression {
return Regexp(lhs, Literal(val))
}