2019-03-15 22:02:59 +01:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
type BoolExpression interface {
|
|
|
|
|
Expression
|
|
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
Eq(expression BoolExpression) BoolExpression
|
|
|
|
|
NotEq(expression BoolExpression) BoolExpression
|
|
|
|
|
GtEq(rhs Expression) BoolExpression
|
|
|
|
|
LtEq(rhs Expression) BoolExpression
|
|
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
And(expression BoolExpression) BoolExpression
|
|
|
|
|
Or(expression BoolExpression) BoolExpression
|
|
|
|
|
IsTrue() BoolExpression
|
|
|
|
|
IsFalse() BoolExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type boolInterfaceImpl struct {
|
|
|
|
|
parent BoolExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
func (b *boolInterfaceImpl) Eq(expression BoolExpression) BoolExpression {
|
|
|
|
|
return Eq(b.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *boolInterfaceImpl) NotEq(expression BoolExpression) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return NotEq(b.parent, expression)
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *boolInterfaceImpl) GtEq(rhs Expression) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return GtEq(b.parent, rhs)
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *boolInterfaceImpl) LtEq(rhs Expression) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return LtEq(b.parent, rhs)
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-31 09:17:28 +02:00
|
|
|
func (b *boolInterfaceImpl) And(expression BoolExpression) BoolExpression {
|
|
|
|
|
return And(b.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *boolInterfaceImpl) Or(expression BoolExpression) BoolExpression {
|
|
|
|
|
return Or(b.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
func (b *boolInterfaceImpl) IsTrue() BoolExpression {
|
|
|
|
|
return IsTrue(b.parent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *boolInterfaceImpl) IsFalse() BoolExpression {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type boolLiteralExpression struct {
|
|
|
|
|
boolInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
func newBoolLiteralExpression(value bool) BoolExpression {
|
2019-03-31 09:17:28 +02:00
|
|
|
boolLiteralExpression := boolLiteralExpression{}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
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-05 18:03:30 +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-05 18:03:30 +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-05 18:03:30 +02:00
|
|
|
func EXISTS(subQuery SelectStatement) BoolExpression {
|
|
|
|
|
return newPrefixBoolExpression(subQuery, "EXISTS")
|
2019-03-31 09:17:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-15 22:02:59 +01:00
|
|
|
// Returns a representation of "a=b"
|
|
|
|
|
func Eq(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, " = ")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a=b", where b is a literal
|
|
|
|
|
func EqL(lhs Expression, val interface{}) BoolExpression {
|
|
|
|
|
return Eq(lhs, Literal(val))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a!=b"
|
2019-04-03 11:03:07 +02:00
|
|
|
func NotEq(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "!=")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a!=b", where b is a literal
|
|
|
|
|
func NeqL(lhs Expression, val interface{}) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return NotEq(lhs, Literal(val))
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a<b"
|
|
|
|
|
func Lt(lhs Expression, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "<")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a<b", where b is a literal
|
|
|
|
|
func LtL(lhs Expression, val interface{}) BoolExpression {
|
|
|
|
|
return Lt(lhs, Literal(val))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a<=b"
|
2019-04-03 11:03:07 +02:00
|
|
|
func LtEq(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, "<=")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a<=b", where b is a literal
|
|
|
|
|
func LteL(lhs Expression, val interface{}) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return LtEq(lhs, Literal(val))
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a>b"
|
|
|
|
|
func Gt(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, ">")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a>b", where b is a literal
|
|
|
|
|
func GtL(lhs Expression, val interface{}) BoolExpression {
|
|
|
|
|
return Gt(lhs, Literal(val))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a>=b"
|
2019-04-03 11:03:07 +02:00
|
|
|
func GtEq(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, ">=")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "a>=b", where b is a literal
|
|
|
|
|
func GteL(lhs Expression, val interface{}) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return GtEq(lhs, Literal(val))
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "not expr"
|
|
|
|
|
func Not(expr BoolExpression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newPrefixBoolExpression(expr, " NOT")
|
2019-03-31 09:17:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func IsTrue(expr BoolExpression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newPrefixBoolExpression(expr, " IS TRUE")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
func And(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, " AND ")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a representation of "c[0] OR ... OR c[n-1]" for c in clauses
|
2019-04-29 14:39:48 +02:00
|
|
|
func Or(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, " OR ")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Like(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, " LIKE ")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LikeL(lhs Expression, val string) BoolExpression {
|
|
|
|
|
return Like(lhs, Literal(val))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Regexp(lhs, rhs Expression) BoolExpression {
|
2019-05-05 18:03:30 +02:00
|
|
|
return newBinaryBoolExpression(lhs, rhs, " REGEXP ")
|
2019-03-15 22:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RegexpL(lhs Expression, val string) BoolExpression {
|
|
|
|
|
return Regexp(lhs, Literal(val))
|
|
|
|
|
}
|