jet/postgres/postgres_cast.go

113 lines
2.8 KiB
Go
Raw Normal View History

2019-07-31 18:43:54 +02:00
package postgres
import (
"fmt"
2019-08-03 14:10:47 +02:00
"github.com/go-jet/jet/internal/jet"
2019-07-31 18:43:54 +02:00
)
type cast interface {
2019-08-01 10:39:57 +02:00
jet.Cast
2019-07-31 18:43:54 +02:00
// Cast expression AS bool type
AS_BOOL() BoolExpression
// Cast expression AS smallint type
AS_SMALLINT() IntegerExpression
// Cast expression AS integer type
AS_INTEGER() IntegerExpression
// Cast expression AS bigint type
AS_BIGINT() IntegerExpression
// Cast expression AS numeric type, using precision and optionally scale
AS_NUMERIC(precision int, scale ...int) FloatExpression
2019-08-01 10:39:57 +02:00
2019-07-31 18:43:54 +02:00
// 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
2019-08-01 10:39:57 +02:00
2019-07-31 18:43:54 +02:00
// Cast expression AS time with time timezone type
AS_TIMEZ() TimezExpression
// Cast expression AS timestamp type
AS_TIMESTAMP() TimestampExpression
// Cast expression AS timestamp with timezone type
AS_TIMESTAMPZ() TimestampzExpression
}
type castImpl struct {
2019-08-01 10:39:57 +02:00
jet.CastImpl
2019-07-31 18:43:54 +02:00
}
2019-08-03 14:10:47 +02:00
func CAST(expr Expression) cast {
2019-07-31 18:43:54 +02:00
castImpl := &castImpl{}
2019-08-01 10:39:57 +02:00
castImpl.CastImpl = jet.NewCastImpl(expr)
2019-07-31 18:43:54 +02:00
return castImpl
}
func (b *castImpl) AS_BOOL() BoolExpression {
2019-08-01 10:39:57 +02:00
return jet.BoolExp(b.AS("boolean"))
2019-07-31 18:43:54 +02:00
}
func (b *castImpl) AS_SMALLINT() IntegerExpression {
2019-08-01 10:39:57 +02:00
return jet.IntExp(b.AS("smallint"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS integer type
func (b *castImpl) AS_INTEGER() IntegerExpression {
2019-08-01 10:39:57 +02:00
return jet.IntExp(b.AS("integer"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS bigint type
func (b *castImpl) AS_BIGINT() IntegerExpression {
2019-08-01 10:39:57 +02:00
return jet.IntExp(b.AS("bigint"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS numeric type, using precision and optionally scale
func (b *castImpl) AS_NUMERIC(precision int, scale ...int) FloatExpression {
var castType string
if len(scale) > 0 {
castType = fmt.Sprintf("numeric(%d, %d)", precision, scale[0])
} else {
castType = fmt.Sprintf("numeric(%d)", precision)
}
2019-08-01 10:39:57 +02:00
return jet.FloatExp(b.AS(castType))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS real type
func (b *castImpl) AS_REAL() FloatExpression {
2019-08-01 10:39:57 +02:00
return jet.FloatExp(b.AS("real"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS double precision type
func (b *castImpl) AS_DOUBLE() FloatExpression {
2019-08-01 10:39:57 +02:00
return jet.FloatExp(b.AS("double precision"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS text type
func (b *castImpl) AS_TEXT() StringExpression {
2019-08-01 10:39:57 +02:00
return jet.StringExp(b.AS("text"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS date type
2019-08-01 10:39:57 +02:00
func (b *castImpl) AS_TIME() jet.TimeExpression {
return TimeExp(b.AS("time without time zone"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS time with time timezone type
func (b *castImpl) AS_TIMEZ() TimezExpression {
2019-08-01 10:39:57 +02:00
return jet.TimezExp(b.AS("time with time zone"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS timestamp type
func (b *castImpl) AS_TIMESTAMP() TimestampExpression {
2019-08-01 10:39:57 +02:00
return jet.TimestampExp(b.AS("timestamp without time zone"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS timestamp with timezone type
func (b *castImpl) AS_TIMESTAMPZ() TimestampzExpression {
2019-08-01 10:39:57 +02:00
return jet.TimestampzExp(b.AS("timestamp with time zone"))
2019-07-31 18:43:54 +02:00
}