2019-03-31 14:07:58 +02:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
2019-05-13 12:33:11 +02:00
|
|
|
import "errors"
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
type numericExpression interface {
|
|
|
|
|
expression
|
2019-03-31 14:07:58 +02:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
Eq(expression numericExpression) boolExpression
|
|
|
|
|
EqL(literal interface{}) boolExpression
|
|
|
|
|
NotEq(expression numericExpression) boolExpression
|
|
|
|
|
NotEqL(literal interface{}) boolExpression
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
Gt(rhs numericExpression) boolExpression
|
|
|
|
|
GtEq(rhs numericExpression) boolExpression
|
|
|
|
|
GtEqL(literal interface{}) boolExpression
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
LtEq(rhs numericExpression) boolExpression
|
|
|
|
|
LtEqL(literal interface{}) boolExpression
|
2019-03-31 14:07:58 +02:00
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
Add(expression numericExpression) numericExpression
|
|
|
|
|
Sub(expression numericExpression) numericExpression
|
|
|
|
|
Mul(expression numericExpression) numericExpression
|
|
|
|
|
Div(expression numericExpression) numericExpression
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type numericInterfaceImpl struct {
|
2019-05-07 19:06:21 +02:00
|
|
|
parent numericExpression
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) Eq(expression numericExpression) boolExpression {
|
2019-03-31 14:07:58 +02:00
|
|
|
return Eq(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) EqL(literal interface{}) boolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return Eq(n.parent, Literal(literal))
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) NotEq(expression numericExpression) boolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return NotEq(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) NotEqL(literal interface{}) boolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return NotEq(n.parent, Literal(literal))
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) Gt(expression numericExpression) boolExpression {
|
2019-04-29 14:39:48 +02:00
|
|
|
return Gt(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) GtEq(expression numericExpression) boolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return GtEq(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) GtEqL(literal interface{}) boolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return GtEq(n.parent, Literal(literal))
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) LtEq(expression numericExpression) boolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return LtEq(n.parent, expression)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) LtEqL(literal interface{}) boolExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
return LtEq(n.parent, Literal(literal))
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) Add(expression numericExpression) numericExpression {
|
2019-05-06 12:42:15 +02:00
|
|
|
return newBinaryNumericExpression(n.parent, expression, "+")
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) Sub(expression numericExpression) numericExpression {
|
2019-05-06 12:42:15 +02:00
|
|
|
return newBinaryNumericExpression(n.parent, expression, "-")
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) Mul(expression numericExpression) numericExpression {
|
2019-05-06 12:42:15 +02:00
|
|
|
return newBinaryNumericExpression(n.parent, expression, "*")
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func (n *numericInterfaceImpl) Div(expression numericExpression) numericExpression {
|
2019-05-06 12:42:15 +02:00
|
|
|
return newBinaryNumericExpression(n.parent, expression, "/")
|
2019-03-31 14:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type numericLiteral struct {
|
|
|
|
|
numericInterfaceImpl
|
|
|
|
|
literalExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func NewNumericLiteral(value interface{}) numericExpression {
|
2019-03-31 14:07:58 +02:00
|
|
|
numericLiteral := numericLiteral{}
|
2019-04-29 14:39:48 +02:00
|
|
|
numericLiteral.literalExpression = *Literal(value)
|
2019-03-31 14:07:58 +02:00
|
|
|
|
|
|
|
|
numericLiteral.numericInterfaceImpl.parent = &numericLiteral
|
|
|
|
|
|
|
|
|
|
return &numericLiteral
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------//
|
|
|
|
|
type binaryNumericExpression struct {
|
|
|
|
|
expressionInterfaceImpl
|
|
|
|
|
numericInterfaceImpl
|
|
|
|
|
|
|
|
|
|
binaryExpression
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func newBinaryNumericExpression(lhs, rhs expression, operator string) numericExpression {
|
2019-03-31 14:07:58 +02:00
|
|
|
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
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
expression expression
|
2019-04-03 11:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 19:06:21 +02:00
|
|
|
func newNumericExpressionWrap(expression expression) numericExpression {
|
2019-04-03 11:03:07 +02:00
|
|
|
numericExpressionWrap := numericExpressionWrapper{}
|
|
|
|
|
|
|
|
|
|
numericExpressionWrap.expression = expression
|
|
|
|
|
|
|
|
|
|
numericExpressionWrap.expressionInterfaceImpl.parent = &numericExpressionWrap
|
|
|
|
|
numericExpressionWrap.numericInterfaceImpl.parent = &numericExpressionWrap
|
|
|
|
|
|
|
|
|
|
return &numericExpressionWrap
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 12:33:11 +02:00
|
|
|
func (n *numericExpressionWrapper) serialize(statement statementType, out *queryData) error {
|
|
|
|
|
if n == nil {
|
|
|
|
|
return errors.New("Numeric expression wrapper is nil. ")
|
|
|
|
|
}
|
2019-05-12 18:15:23 +02:00
|
|
|
//out.writeString("(")
|
2019-05-13 12:33:11 +02:00
|
|
|
err := n.expression.serialize(statement, out)
|
2019-05-12 18:15:23 +02:00
|
|
|
//out.writeString(")")
|
2019-04-03 11:03:07 +02:00
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
2019-04-03 11:03:07 +02:00
|
|
|
}
|