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

@ -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"))
}