jet/sqlbuilder/expression.go

151 lines
3.2 KiB
Go
Raw Normal View History

package sqlbuilder
import (
"github.com/dropbox/godropbox/errors"
)
2019-03-31 09:17:28 +02:00
// An expression
2019-05-07 19:06:21 +02:00
type expression interface {
clause
projection
groupByClause
2019-05-07 19:06:21 +02:00
IN(subQuery selectStatement) boolExpression
NOT_IN(subQuery selectStatement) boolExpression
2019-05-07 19:06:21 +02:00
AS(alias string) projection
IS_DISTINCT_FROM(expression expression) boolExpression
IS_NULL() boolExpression
ASC() orderByClause
DESC() orderByClause
}
2019-03-31 09:17:28 +02:00
type expressionInterfaceImpl struct {
2019-05-07 19:06:21 +02:00
parent expression
}
2019-05-07 19:06:21 +02:00
func (e *expressionInterfaceImpl) IN(subQuery selectStatement) boolExpression {
2019-05-06 12:42:15 +02:00
return newBinaryBoolExpression(e.parent, subQuery, "IN")
}
2019-05-07 19:06:21 +02:00
func (e *expressionInterfaceImpl) NOT_IN(subQuery selectStatement) boolExpression {
2019-05-06 12:42:15 +02:00
return newBinaryBoolExpression(e.parent, subQuery, "NOT_IN")
}
2019-05-07 19:06:21 +02:00
func (e *expressionInterfaceImpl) AS(alias string) projection {
2019-03-31 09:17:28 +02:00
return NewAlias(e.parent, alias)
}
2019-05-07 19:06:21 +02:00
func (e *expressionInterfaceImpl) IS_DISTINCT_FROM(expression expression) boolExpression {
return newBinaryBoolExpression(e.parent, expression, "IS DISTINCT FROM")
}
2019-05-07 19:06:21 +02:00
func (e *expressionInterfaceImpl) IS_NULL() boolExpression {
return nil
}
2019-05-07 19:06:21 +02:00
func (e *expressionInterfaceImpl) ASC() orderByClause {
return &orderByClauseImpl{expression: e.parent, ascent: true}
}
2019-05-07 19:06:21 +02:00
func (e *expressionInterfaceImpl) DESC() orderByClause {
return &orderByClauseImpl{expression: e.parent, ascent: false}
}
2019-05-07 19:06:21 +02:00
func (e *expressionInterfaceImpl) serializeForGroupBy(out *queryData) error {
return e.parent.serialize(out)
}
func (e *expressionInterfaceImpl) serializeForProjection(out *queryData) error {
return e.parent.serialize(out)
}
2019-03-31 09:17:28 +02:00
// Representation of binary operations (e.g. comparisons, arithmetic)
type binaryExpression struct {
2019-05-07 19:06:21 +02:00
lhs, rhs expression
operator string
}
2019-05-07 19:06:21 +02:00
func newBinaryExpression(lhs, rhs expression, operator string, parent ...expression) binaryExpression {
2019-03-31 09:17:28 +02:00
binaryExpression := binaryExpression{
lhs: lhs,
rhs: rhs,
operator: operator,
}
2019-03-31 14:07:58 +02:00
return binaryExpression
}
2019-05-07 19:06:21 +02:00
func isSimpleOperand(expression expression) bool {
2019-05-01 18:23:19 +02:00
if _, ok := expression.(*literalExpression); ok {
return true
}
2019-05-07 19:06:21 +02:00
if _, ok := expression.(column); ok {
2019-05-01 18:23:19 +02:00
return true
}
2019-05-03 12:51:57 +02:00
if _, ok := expression.(*numericFunc); ok {
2019-05-01 18:23:19 +02:00
return true
}
return false
}
2019-05-07 19:06:21 +02:00
func (c *binaryExpression) serialize(out *queryData) error {
if c.lhs == nil {
return errors.Newf("nil lhs.")
}
2019-05-01 17:25:10 +02:00
if c.rhs == nil {
return errors.Newf("nil rhs.")
}
2019-05-01 18:23:19 +02:00
wrap := !isSimpleOperand(c.lhs) && !isSimpleOperand(c.rhs)
2019-05-01 17:25:10 +02:00
2019-05-01 18:23:19 +02:00
if wrap {
2019-05-01 17:25:10 +02:00
out.WriteString("(")
}
2019-05-07 19:06:21 +02:00
if err := c.lhs.serialize(out); err != nil {
return err
}
2019-05-06 12:42:15 +02:00
out.WriteString(" " + c.operator + " ")
2019-05-07 19:06:21 +02:00
if err := c.rhs.serialize(out); err != nil {
return err
}
2019-05-01 18:23:19 +02:00
if wrap {
2019-05-01 17:25:10 +02:00
out.WriteString(")")
}
return nil
}
2019-03-31 09:17:28 +02:00
// A not expression which negates a expression value
type prefixExpression struct {
2019-05-07 19:06:21 +02:00
expression expression
operator string
}
2019-05-07 19:06:21 +02:00
func newPrefixExpression(expression expression, operator string) prefixExpression {
2019-03-31 09:17:28 +02:00
prefixExpression := prefixExpression{
expression: expression,
operator: operator,
}
2019-03-31 14:07:58 +02:00
return prefixExpression
}
2019-05-07 19:06:21 +02:00
func (p *prefixExpression) serialize(out *queryData) error {
out.WriteString(p.operator + " ")
2019-03-31 09:17:28 +02:00
if p.expression == nil {
return errors.Newf("nil prefix expression.")
}
2019-05-07 19:06:21 +02:00
if err := p.expression.serialize(out); err != nil {
return err
}
2019-03-31 09:17:28 +02:00
return nil
}