jet/sqlbuilder/func_expression.go

102 lines
2.3 KiB
Go
Raw Normal View History

2019-03-30 10:05:30 +01:00
package sqlbuilder
2019-05-06 12:42:15 +02:00
import "errors"
type funcExpressionImpl struct {
expressionInterfaceImpl
2019-03-30 10:05:30 +01:00
name string
2019-05-07 19:06:21 +02:00
expression []expression
}
2019-05-07 19:06:21 +02:00
func ROW(expressions ...expression) expression {
return newFunc("ROW", expressions, nil)
2019-03-30 10:05:30 +01:00
}
2019-05-07 19:06:21 +02:00
func newFunc(name string, expressions []expression, parent expression) *funcExpressionImpl {
funcExp := &funcExpressionImpl{
name: name,
expression: expressions,
}
2019-03-30 10:05:30 +01:00
if parent != nil {
funcExp.expressionInterfaceImpl.parent = parent
} else {
funcExp.expressionInterfaceImpl.parent = funcExp
}
2019-03-30 10:05:30 +01:00
return funcExp
2019-03-30 10:05:30 +01:00
}
func (f *funcExpressionImpl) serialize(statement statementType, out *queryData) error {
2019-05-13 12:33:11 +02:00
if f == nil {
return errors.New("Function expression is nil. ")
}
2019-05-12 18:15:23 +02:00
out.writeString(f.name + "(")
err := serializeExpressionList(statement, f.expression, ", ", out)
2019-03-30 10:05:30 +01:00
if err != nil {
return err
}
out.writeString(")")
2019-03-30 10:05:30 +01:00
return nil
}
// ------------------- FLOAT FUNCTIONS --------------------------//
type floatFunc struct {
funcExpressionImpl
floatInterfaceImpl
}
func newFloatFunc(name string, expressions ...expression) FloatExpression {
floatFunc := &floatFunc{}
floatFunc.funcExpressionImpl = *newFunc(name, expressions, floatFunc)
floatFunc.floatInterfaceImpl.parent = floatFunc
return floatFunc
2019-05-20 17:37:55 +02:00
}
func COUNTf(floatExpression FloatExpression) FloatExpression {
return newFloatFunc("COUNT", floatExpression)
2019-03-30 10:05:30 +01:00
}
func MAXf(floatExpression FloatExpression) FloatExpression {
return newFloatFunc("MAX", floatExpression)
2019-03-30 10:05:30 +01:00
}
2019-05-06 12:42:15 +02:00
func SUMf(floatExpression FloatExpression) FloatExpression {
return newFloatFunc("SUM", floatExpression)
2019-05-06 12:42:15 +02:00
}
// ------------------- FLOAT FUNCTIONS --------------------------//
2019-05-06 12:42:15 +02:00
type integerFunc struct {
funcExpressionImpl
integerInterfaceImpl
2019-05-06 12:42:15 +02:00
}
func newIntegerFunc(name string, expressions ...expression) IntegerExpression {
floatFunc := &integerFunc{}
2019-05-06 12:42:15 +02:00
floatFunc.funcExpressionImpl = *newFunc(name, expressions, floatFunc)
floatFunc.integerInterfaceImpl.parent = floatFunc
2019-05-06 12:42:15 +02:00
return floatFunc
2019-05-06 12:42:15 +02:00
}
func COUNTi(integerExpression IntegerExpression) IntegerExpression {
return newIntegerFunc("COUNT", integerExpression)
2019-05-06 12:42:15 +02:00
}
func MAXi(integerExpression IntegerExpression) IntegerExpression {
return newIntegerFunc("MAX", integerExpression)
2019-05-06 12:42:15 +02:00
}
func SUMi(integerExpression IntegerExpression) IntegerExpression {
return newIntegerFunc("SUM", integerExpression)
2019-05-06 12:42:15 +02:00
}