Numeric expression type.

This commit is contained in:
go-jet 2019-06-21 12:30:32 +02:00
parent 1ac324e198
commit 829736279b
8 changed files with 41 additions and 52 deletions

View file

@ -2,6 +2,7 @@ package sqlbuilder
type FloatExpression interface { type FloatExpression interface {
Expression Expression
numericExpression
EQ(rhs FloatExpression) BoolExpression EQ(rhs FloatExpression) BoolExpression
NOT_EQ(rhs FloatExpression) BoolExpression NOT_EQ(rhs FloatExpression) BoolExpression
@ -22,6 +23,7 @@ type FloatExpression interface {
} }
type floatInterfaceImpl struct { type floatInterfaceImpl struct {
numericExpressionImpl
parent FloatExpression parent FloatExpression
} }

View file

@ -186,24 +186,16 @@ func ABSf(floatExpression FloatExpression) FloatExpression {
return newFloatFunc("ABS", floatExpression) return newFloatFunc("ABS", floatExpression)
} }
func ABSi(integerExpression IntegerExpression) FloatExpression { func ABSi(integerExpression IntegerExpression) IntegerExpression {
return newFloatFunc("ABS", integerExpression) return newIntegerFunc("ABS", integerExpression)
} }
func SQRTf(floatExpression FloatExpression) FloatExpression { func SQRT(numericExpression NumericExpression) FloatExpression {
return newFloatFunc("SQRT", floatExpression) return newFloatFunc("SQRT", numericExpression)
} }
func SQRTi(integerExpression IntegerExpression) FloatExpression { func CBRT(numericExpression NumericExpression) FloatExpression {
return newFloatFunc("SQRT", integerExpression) return newFloatFunc("CBRT", numericExpression)
}
func CBRTf(floatExpression FloatExpression) FloatExpression {
return newFloatFunc("CBRT", floatExpression)
}
func CBRTi(integerExpression IntegerExpression) FloatExpression {
return newFloatFunc("CBRT", integerExpression)
} }
func CEIL(floatExpression FloatExpression) FloatExpression { func CEIL(floatExpression FloatExpression) FloatExpression {
@ -242,12 +234,8 @@ func LOG(floatExpression FloatExpression) FloatExpression {
// ----------------- Aggregate functions -------------------// // ----------------- Aggregate functions -------------------//
func AVGf(floatExpression FloatExpression) FloatExpression { func AVG(numericExpression NumericExpression) FloatExpression {
return newFloatFunc("AVG", floatExpression) return newFloatFunc("AVG", numericExpression)
}
func AVGi(integerExpression IntegerExpression) FloatExpression {
return newFloatFunc("AVG", integerExpression)
} }
func BIT_AND(integerExpression IntegerExpression) IntegerExpression { func BIT_AND(integerExpression IntegerExpression) IntegerExpression {

View file

@ -6,13 +6,8 @@ import (
) )
func TestFuncAVG(t *testing.T) { func TestFuncAVG(t *testing.T) {
t.Run("float", func(t *testing.T) { assertClauseSerialize(t, AVG(table1ColFloat), "AVG(table1.col_float)")
assertClauseSerialize(t, AVGf(table1ColFloat), "AVG(table1.col_float)") assertClauseSerialize(t, AVG(table1ColInt), "AVG(table1.col_int)")
})
t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, AVGi(table1ColInt), "AVG(table1.col_int)")
})
} }
func TestFuncBIT_AND(t *testing.T) { func TestFuncBIT_AND(t *testing.T) {
@ -88,27 +83,17 @@ func TestFuncABS(t *testing.T) {
} }
func TestFuncSQRT(t *testing.T) { func TestFuncSQRT(t *testing.T) {
t.Run("float", func(t *testing.T) { assertClauseSerialize(t, SQRT(table1ColFloat), "SQRT(table1.col_float)")
assertClauseSerialize(t, SQRTf(table1ColFloat), "SQRT(table1.col_float)") assertClauseSerialize(t, SQRT(Float(11.2222)), "SQRT($1)", float64(11.2222))
assertClauseSerialize(t, SQRTf(Float(11.2222)), "SQRT($1)", float64(11.2222)) assertClauseSerialize(t, SQRT(table1ColInt), "SQRT(table1.col_int)")
}) assertClauseSerialize(t, SQRT(Int(11)), "SQRT($1)", int64(11))
t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, SQRTi(table1ColInt), "SQRT(table1.col_int)")
assertClauseSerialize(t, SQRTi(Int(11)), "SQRT($1)", int64(11))
})
} }
func TestFuncCBRT(t *testing.T) { func TestFuncCBRT(t *testing.T) {
t.Run("float", func(t *testing.T) { assertClauseSerialize(t, CBRT(table1ColFloat), "CBRT(table1.col_float)")
assertClauseSerialize(t, CBRTf(table1ColFloat), "CBRT(table1.col_float)") assertClauseSerialize(t, CBRT(Float(11.2222)), "CBRT($1)", float64(11.2222))
assertClauseSerialize(t, CBRTf(Float(11.2222)), "CBRT($1)", float64(11.2222)) assertClauseSerialize(t, CBRT(table1ColInt), "CBRT(table1.col_int)")
}) assertClauseSerialize(t, CBRT(Int(11)), "CBRT($1)", int64(11))
t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, CBRTi(table1ColInt), "CBRT(table1.col_int)")
assertClauseSerialize(t, CBRTi(Int(11)), "CBRT($1)", int64(11))
})
} }
func TestFuncCEIL(t *testing.T) { func TestFuncCEIL(t *testing.T) {

View file

@ -2,6 +2,7 @@ package sqlbuilder
type IntegerExpression interface { type IntegerExpression interface {
Expression Expression
numericExpression
EQ(rhs IntegerExpression) BoolExpression EQ(rhs IntegerExpression) BoolExpression
NOT_EQ(rhs IntegerExpression) BoolExpression NOT_EQ(rhs IntegerExpression) BoolExpression
@ -29,6 +30,7 @@ type IntegerExpression interface {
} }
type integerInterfaceImpl struct { type integerInterfaceImpl struct {
numericExpressionImpl
parent IntegerExpression parent IntegerExpression
} }

View file

@ -0,0 +1,14 @@
package sqlbuilder
type NumericExpression interface {
Expression
numericExpression
}
type numericExpression interface {
isNumericExpression()
}
type numericExpressionImpl struct{}
func (n *numericExpressionImpl) isNumericExpression() {}

View file

@ -24,10 +24,8 @@ type SelectStatement interface {
GROUP_BY(groupByClauses ...groupByClause) SelectStatement GROUP_BY(groupByClauses ...groupByClause) SelectStatement
HAVING(boolExpression BoolExpression) SelectStatement HAVING(boolExpression BoolExpression) SelectStatement
ORDER_BY(orderByClauses ...OrderByClause) SelectStatement ORDER_BY(orderByClauses ...OrderByClause) SelectStatement
LIMIT(limit int64) SelectStatement LIMIT(limit int64) SelectStatement
OFFSET(offset int64) SelectStatement OFFSET(offset int64) SelectStatement
FOR(lock SelectLock) SelectStatement FOR(lock SelectLock) SelectStatement
AsTable(alias string) ExpressionTable AsTable(alias string) ExpressionTable

View file

@ -230,8 +230,8 @@ func TestFloatOperators(t *testing.T) {
AllTypes.Real.POW(Float(11.22)), AllTypes.Real.POW(Float(11.22)),
ABSf(AllTypes.Real), ABSf(AllTypes.Real),
SQRTf(AllTypes.Real), SQRT(AllTypes.Real),
CBRTf(AllTypes.Real), CBRT(AllTypes.Real),
CEIL(AllTypes.Real), CEIL(AllTypes.Real),
FLOOR(AllTypes.Real), FLOOR(AllTypes.Real),
ROUND(AllTypes.Decimal), ROUND(AllTypes.Decimal),
@ -284,8 +284,8 @@ func TestIntegerOperators(t *testing.T) {
AllTypes.Integer.BIT_SHIFT_RIGHT(Int(11)), AllTypes.Integer.BIT_SHIFT_RIGHT(Int(11)),
ABSi(AllTypes.Integer), ABSi(AllTypes.Integer),
SQRTi(AllTypes.Integer), SQRT(AllTypes.Integer),
CBRTi(AllTypes.Integer), CBRT(AllTypes.Integer),
) )
fmt.Println(query.DebugSql()) fmt.Println(query.DebugSql())

View file

@ -854,7 +854,7 @@ ORDER BY SUM(payment.amount) ASC;
SELECT( SELECT(
Payment.CustomerID.AS("customer_payment_sum.customer_id"), Payment.CustomerID.AS("customer_payment_sum.customer_id"),
SUMf(Payment.Amount).AS("customer_payment_sum.amount_sum"), SUMf(Payment.Amount).AS("customer_payment_sum.amount_sum"),
AVGf(Payment.Amount).AS("customer_payment_sum.amount_avg"), AVG(Payment.Amount).AS("customer_payment_sum.amount_avg"),
MAXf(Payment.Amount).AS("customer_payment_sum.amount_max"), MAXf(Payment.Amount).AS("customer_payment_sum.amount_max"),
MINf(Payment.Amount).AS("customer_payment_sum.amount_min"), MINf(Payment.Amount).AS("customer_payment_sum.amount_min"),
COUNT(Payment.Amount).AS("customer_payment_sum.amount_count"), COUNT(Payment.Amount).AS("customer_payment_sum.amount_count"),