MySQL cast expressions. Simplified.

This commit is contained in:
go-jet 2019-08-01 10:39:57 +02:00
parent 53dbcd9bfc
commit c342f296ca
10 changed files with 103 additions and 81 deletions

View file

@ -9,40 +9,40 @@ var dateVar = Date(2000, 12, 30)
func TestDateExpressionEQ(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, table1ColDate.EQ(table2ColDate), "(table1.col_date = table2.col_date)")
jet.AssertPostgreClauseSerialize(t, table1ColDate.EQ(dateVar), "(table1.col_date = $1::date)", "2000-12-30")
jet.AssertPostgreClauseSerialize(t, table1ColDate.EQ(dateVar), "(table1.col_date = $1::DATE)", "2000-12-30")
}
func TestDateExpressionNOT_EQ(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, table1ColDate.NOT_EQ(table2ColDate), "(table1.col_date != table2.col_date)")
jet.AssertPostgreClauseSerialize(t, table1ColDate.NOT_EQ(dateVar), "(table1.col_date != $1::date)", "2000-12-30")
jet.AssertPostgreClauseSerialize(t, table1ColDate.NOT_EQ(dateVar), "(table1.col_date != $1::DATE)", "2000-12-30")
}
func TestDateExpressionIS_DISTINCT_FROM(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_DISTINCT_FROM(table2ColDate), "(table1.col_date IS DISTINCT FROM table2.col_date)")
jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_DISTINCT_FROM(dateVar), "(table1.col_date IS DISTINCT FROM $1::date)", "2000-12-30")
jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_DISTINCT_FROM(dateVar), "(table1.col_date IS DISTINCT FROM $1::DATE)", "2000-12-30")
}
func TestDateExpressionIS_NOT_DISTINCT_FROM(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_NOT_DISTINCT_FROM(table2ColDate), "(table1.col_date IS NOT DISTINCT FROM table2.col_date)")
jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_NOT_DISTINCT_FROM(dateVar), "(table1.col_date IS NOT DISTINCT FROM $1::date)", "2000-12-30")
jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_NOT_DISTINCT_FROM(dateVar), "(table1.col_date IS NOT DISTINCT FROM $1::DATE)", "2000-12-30")
}
func TestDateExpressionGT(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, table1ColDate.GT(table2ColDate), "(table1.col_date > table2.col_date)")
jet.AssertPostgreClauseSerialize(t, table1ColDate.GT(dateVar), "(table1.col_date > $1::date)", "2000-12-30")
jet.AssertPostgreClauseSerialize(t, table1ColDate.GT(dateVar), "(table1.col_date > $1::DATE)", "2000-12-30")
}
func TestDateExpressionGT_EQ(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, table1ColDate.GT_EQ(table2ColDate), "(table1.col_date >= table2.col_date)")
jet.AssertPostgreClauseSerialize(t, table1ColDate.GT_EQ(dateVar), "(table1.col_date >= $1::date)", "2000-12-30")
jet.AssertPostgreClauseSerialize(t, table1ColDate.GT_EQ(dateVar), "(table1.col_date >= $1::DATE)", "2000-12-30")
}
func TestDateExpressionLT(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, table1ColDate.LT(table2ColDate), "(table1.col_date < table2.col_date)")
jet.AssertPostgreClauseSerialize(t, table1ColDate.LT(dateVar), "(table1.col_date < $1::date)", "2000-12-30")
jet.AssertPostgreClauseSerialize(t, table1ColDate.LT(dateVar), "(table1.col_date < $1::DATE)", "2000-12-30")
}
func TestDateExpressionLT_EQ(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, table1ColDate.LT_EQ(table2ColDate), "(table1.col_date <= table2.col_date)")
jet.AssertPostgreClauseSerialize(t, table1ColDate.LT_EQ(dateVar), "(table1.col_date <= $1::date)", "2000-12-30")
jet.AssertPostgreClauseSerialize(t, table1ColDate.LT_EQ(dateVar), "(table1.col_date <= $1::DATE)", "2000-12-30")
}

View file

@ -6,6 +6,7 @@ import (
)
type cast interface {
jet.Cast
// Cast expression AS bool type
AS_BOOL() BoolExpression
// Cast expression AS smallint type
@ -16,18 +17,14 @@ type cast interface {
AS_BIGINT() IntegerExpression
// Cast expression AS numeric type, using precision and optionally scale
AS_NUMERIC(precision int, scale ...int) FloatExpression
// Cast expression AS numeric type, using precision and optionally scale
AS_DECIMAL() FloatExpression
// Cast expression AS real type
AS_REAL() FloatExpression
// Cast expression AS double precision type
AS_DOUBLE() FloatExpression
// Cast expression AS text type
AS_TEXT() StringExpression
// Cast expression AS date type
AS_DATE() DateExpression
// Cast expression AS time type
AS_TIME() TimeExpression
// Cast expression AS time with time timezone type
AS_TIMEZ() TimezExpression
// Cast expression AS timestamp type
@ -37,33 +34,33 @@ type cast interface {
}
type castImpl struct {
jet.Cast
jet.CastImpl
}
func CAST(expr jet.Expression) cast {
castImpl := &castImpl{}
castImpl.Cast = jet.NewCastImpl(expr)
castImpl.CastImpl = jet.NewCastImpl(expr)
return castImpl
}
func (b *castImpl) AS_BOOL() BoolExpression {
return jet.BoolExp(b.As("boolean"))
return jet.BoolExp(b.AS("boolean"))
}
func (b *castImpl) AS_SMALLINT() IntegerExpression {
return jet.IntExp(b.As("smallint"))
return jet.IntExp(b.AS("smallint"))
}
// Cast expression AS integer type
func (b *castImpl) AS_INTEGER() IntegerExpression {
return jet.IntExp(b.As("integer"))
return jet.IntExp(b.AS("integer"))
}
// Cast expression AS bigint type
func (b *castImpl) AS_BIGINT() IntegerExpression {
return jet.IntExp(b.As("bigint"))
return jet.IntExp(b.AS("bigint"))
}
// Cast expression AS numeric type, using precision and optionally scale
@ -76,49 +73,40 @@ func (b *castImpl) AS_NUMERIC(precision int, scale ...int) FloatExpression {
castType = fmt.Sprintf("numeric(%d)", precision)
}
return jet.FloatExp(b.As(jet.CastType(castType)))
}
func (b *castImpl) AS_DECIMAL() FloatExpression {
return jet.FloatExp(b.As("decimal"))
return jet.FloatExp(b.AS(castType))
}
// Cast expression AS real type
func (b *castImpl) AS_REAL() FloatExpression {
return jet.FloatExp(b.As("real"))
return jet.FloatExp(b.AS("real"))
}
// Cast expression AS double precision type
func (b *castImpl) AS_DOUBLE() FloatExpression {
return jet.FloatExp(b.As("double precision"))
return jet.FloatExp(b.AS("double precision"))
}
// Cast expression AS text type
func (b *castImpl) AS_TEXT() StringExpression {
return jet.StringExp(b.As("text"))
return jet.StringExp(b.AS("text"))
}
// Cast expression AS date type
func (b *castImpl) AS_DATE() DateExpression {
return jet.DateExp(b.As("date"))
}
// Cast expression AS time type
func (b *castImpl) AS_TIME() TimeExpression {
return jet.TimeExp(b.As("time without time zone"))
func (b *castImpl) AS_TIME() jet.TimeExpression {
return TimeExp(b.AS("time without time zone"))
}
// Cast expression AS time with time timezone type
func (b *castImpl) AS_TIMEZ() TimezExpression {
return jet.TimezExp(b.As("time with time zone"))
return jet.TimezExp(b.AS("time with time zone"))
}
// Cast expression AS timestamp type
func (b *castImpl) AS_TIMESTAMP() TimestampExpression {
return jet.TimestampExp(b.As("timestamp without time zone"))
return jet.TimestampExp(b.AS("timestamp without time zone"))
}
// Cast expression AS timestamp with timezone type
func (b *castImpl) AS_TIMESTAMPZ() TimestampzExpression {
return jet.TimestampzExp(b.As("timestamp with time zone"))
return jet.TimestampzExp(b.AS("timestamp with time zone"))
}

View file

@ -5,6 +5,10 @@ import (
"testing"
)
func TestExpressionCAST_AS(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, CAST(String("test")).AS("text"), `$1::text`, "test")
}
func TestExpressionCAST_AS_BOOL(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, CAST(Int(1)).AS_BOOL(), "$1::boolean", int64(1))
jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_BOOL(), "table2.col3::boolean")
@ -41,7 +45,7 @@ func TestExpressionCAST_AS_TEXT(t *testing.T) {
}
func TestExpressionCAST_AS_DATE(t *testing.T) {
jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_DATE(), "table2.col3::date")
jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_DATE(), "table2.col3::DATE")
}
func TestExpressionCAST_AS_TIME(t *testing.T) {