2019-03-31 14:07:58 +02:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
2019-04-03 11:03:07 +02:00
|
|
|
"bytes"
|
2019-03-31 14:07:58 +02:00
|
|
|
"github.com/dropbox/godropbox/database/sqltypes"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type NumericExpression interface {
|
|
|
|
|
Expression
|
|
|
|
|
|
|
|
|
|
Eq(expression NumericExpression) BoolExpression
|
2019-04-03 11:03:07 +02:00
|
|
|
EqL(literal interface{}) BoolExpression
|
2019-03-31 14:07:58 +02:00
|
|
|
NotEq(expression NumericExpression) BoolExpression
|
2019-04-03 11:03:07 +02:00
|
|
|
NotEqL(literal interface{}) BoolExpression
|
2019-03-31 14:07:58 +02:00
|
|
|
GtEq(rhs NumericExpression) BoolExpression
|
2019-04-03 11:03:07 +02:00
|
|
|
GtEqL(literal interface{}) BoolExpression
|
2019-03-31 14:07:58 +02:00
|
|
|
LtEq(rhs NumericExpression) BoolExpression
|
2019-04-03 11:03:07 +02:00
|
|
|
LtEqL(literal interface{}) BoolExpression
|
2019-03-31 14:07:58 +02:00
|
|
|
|
|
|
|
|
Add(expression NumericExpression) NumericExpression
|
|
|
|
|
Sub(expression NumericExpression) NumericExpression
|
|
|
|
|
Mul(expression NumericExpression) NumericExpression
|
|
|
|
|
Div(expression NumericExpression) NumericExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type numericInterfaceImpl struct {
|
|
|
|
|
parent NumericExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) Eq(expression NumericExpression) BoolExpression {
|
|
|
|
|
return Eq(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 11:03:07 +02:00
|
|
|
func (n *numericInterfaceImpl) EqL(literal interface{}) BoolExpression {
|
|
|
|
|
return Eq(n.parent, Literal(literal))
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 14:07:58 +02:00
|
|
|
func (n *numericInterfaceImpl) NotEq(expression NumericExpression) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return NotEq(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) NotEqL(literal interface{}) BoolExpression {
|
|
|
|
|
return NotEq(n.parent, Literal(literal))
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) GtEq(expression NumericExpression) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return GtEq(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) GtEqL(literal interface{}) BoolExpression {
|
|
|
|
|
return GtEq(n.parent, Literal(literal))
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) LtEq(expression NumericExpression) BoolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return LtEq(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) LtEqL(literal interface{}) BoolExpression {
|
|
|
|
|
return LtEq(n.parent, Literal(literal))
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) Add(expression NumericExpression) NumericExpression {
|
|
|
|
|
return newBinaryNumericExpression(n.parent, expression, []byte(" + "))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) Sub(expression NumericExpression) NumericExpression {
|
|
|
|
|
return newBinaryNumericExpression(n.parent, expression, []byte(" - "))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) Mul(expression NumericExpression) NumericExpression {
|
|
|
|
|
return newBinaryNumericExpression(n.parent, expression, []byte(" * "))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *numericInterfaceImpl) Div(expression NumericExpression) NumericExpression {
|
|
|
|
|
return newBinaryNumericExpression(n.parent, expression, []byte(" / "))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type numericLiteral struct {
|
|
|
|
|
numericInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewNumericLiteral(value interface{}) NumericExpression {
|
|
|
|
|
numericLiteral := numericLiteral{}
|
|
|
|
|
|
|
|
|
|
sqlValue, err := sqltypes.BuildValue(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(errors.Wrap(err, "Invalid literal value"))
|
|
|
|
|
}
|
|
|
|
|
numericLiteral.literalExpression = *NewLiteralExpression(sqlValue)
|
|
|
|
|
numericLiteral.numericInterfaceImpl.parent = &numericLiteral
|
|
|
|
|
|
|
|
|
|
return &numericLiteral
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type binaryNumericExpression struct {
|
|
|
|
|
expressionInterfaceImpl
|
|
|
|
|
numericInterfaceImpl
|
|
|
|
|
|
|
|
|
|
binaryExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newBinaryNumericExpression(lhs, rhs Expression, operator []byte) NumericExpression {
|
|
|
|
|
numericExpression := binaryNumericExpression{}
|
|
|
|
|
|
|
|
|
|
numericExpression.binaryExpression = newBinaryExpression(lhs, rhs, operator)
|
|
|
|
|
|
|
|
|
|
numericExpression.expressionInterfaceImpl.parent = &numericExpression
|
|
|
|
|
numericExpression.numericInterfaceImpl.parent = &numericExpression
|
|
|
|
|
|
|
|
|
|
return &numericExpression
|
|
|
|
|
}
|
2019-04-03 11:03:07 +02:00
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type numericExpressionWrapper struct {
|
|
|
|
|
expressionInterfaceImpl
|
|
|
|
|
numericInterfaceImpl
|
|
|
|
|
|
|
|
|
|
expression Expression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newNumericExpressionWrap(expression Expression) NumericExpression {
|
|
|
|
|
numericExpressionWrap := numericExpressionWrapper{}
|
|
|
|
|
|
|
|
|
|
numericExpressionWrap.expression = expression
|
|
|
|
|
|
|
|
|
|
numericExpressionWrap.expressionInterfaceImpl.parent = &numericExpressionWrap
|
|
|
|
|
numericExpressionWrap.numericInterfaceImpl.parent = &numericExpressionWrap
|
|
|
|
|
|
|
|
|
|
return &numericExpressionWrap
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *numericExpressionWrapper) SerializeSql(out *bytes.Buffer, options ...serializeOption) (err error) {
|
|
|
|
|
out.WriteString("(")
|
|
|
|
|
err = c.expression.SerializeSql(out, options...)
|
|
|
|
|
out.WriteString(")")
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|