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 {
Expression
numericExpression
EQ(rhs FloatExpression) BoolExpression
NOT_EQ(rhs FloatExpression) BoolExpression
@ -22,6 +23,7 @@ type FloatExpression interface {
}
type floatInterfaceImpl struct {
numericExpressionImpl
parent FloatExpression
}

View file

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

View file

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

View file

@ -2,6 +2,7 @@ package sqlbuilder
type IntegerExpression interface {
Expression
numericExpression
EQ(rhs IntegerExpression) BoolExpression
NOT_EQ(rhs IntegerExpression) BoolExpression
@ -29,6 +30,7 @@ type IntegerExpression interface {
}
type integerInterfaceImpl struct {
numericExpressionImpl
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
HAVING(boolExpression BoolExpression) SelectStatement
ORDER_BY(orderByClauses ...OrderByClause) SelectStatement
LIMIT(limit int64) SelectStatement
OFFSET(offset int64) SelectStatement
FOR(lock SelectLock) SelectStatement
AsTable(alias string) ExpressionTable

View file

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

View file

@ -854,7 +854,7 @@ ORDER BY SUM(payment.amount) ASC;
SELECT(
Payment.CustomerID.AS("customer_payment_sum.customer_id"),
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"),
MINf(Payment.Amount).AS("customer_payment_sum.amount_min"),
COUNT(Payment.Amount).AS("customer_payment_sum.amount_count"),