jet/postgres/cast.go

166 lines
4.3 KiB
Go
Raw Normal View History

2019-07-31 18:43:54 +02:00
package postgres
import (
"fmt"
2019-08-13 10:16:26 +02:00
"strconv"
2019-12-07 13:52:51 +01:00
2020-06-27 18:48:19 +02:00
"github.com/go-jet/jet/v2/internal/jet"
2019-07-31 18:43:54 +02:00
)
type cast interface {
2019-08-13 10:16:26 +02:00
AS(castType string) Expression
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(precisionAndScale ...int) FloatExpression
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
2019-08-13 10:16:26 +02:00
// Cast expression AS char with optional length
AS_CHAR(length ...int) StringExpression
// Cast expression AS date type
AS_DATE() DateExpression
// Cast expression AS numeric type, using precision and optionally scale
AS_DECIMAL() FloatExpression
// Cast expression AS time type
AS_TIME() TimeExpression
2019-07-31 18:43:54 +02:00
// Cast expression AS text type
AS_TEXT() StringExpression
2019-12-07 13:52:51 +01:00
// Cast expression AS bytea type
AS_BYTEA() StringExpression
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
2019-12-07 13:52:51 +01:00
// Cast expression AS interval type
AS_INTERVAL() IntervalExpression
2019-07-31 18:43:54 +02:00
}
2024-09-03 15:39:36 +02:00
type castArray interface {
AS_STRING() jet.ArrayExpression[StringExpression]
}
2019-07-31 18:43:54 +02:00
type castImpl struct {
2019-08-11 14:29:03 +02:00
jet.Cast
2019-07-31 18:43:54 +02:00
}
2019-08-17 14:49:35 +02:00
// CAST function converts a expr (of any type) into latter specified datatype.
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-11 14:29:03 +02:00
castImpl.Cast = jet.NewCastImpl(expr)
2019-07-31 18:43:54 +02:00
return castImpl
}
2019-08-17 14:49:35 +02:00
// Cast expression as castType
2019-08-13 10:16:26 +02:00
func (b *castImpl) AS(castType string) Expression {
return b.Cast.AS(castType)
}
2019-08-17 14:49:35 +02:00
// Cast expression as bool type
2019-07-31 18:43:54 +02:00
func (b *castImpl) AS_BOOL() BoolExpression {
2019-08-13 10:16:26 +02:00
return BoolExp(b.AS("boolean"))
2019-07-31 18:43:54 +02:00
}
2019-08-17 14:49:35 +02:00
// Cast expression as smallint type
2019-07-31 18:43:54 +02:00
func (b *castImpl) AS_SMALLINT() IntegerExpression {
2019-08-13 10:16:26 +02:00
return 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-13 10:16:26 +02:00
return 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-13 10:16:26 +02:00
return 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(precisionAndScale ...int) FloatExpression {
var castArgs string
var argLen = len(precisionAndScale)
if argLen >= 2 {
castArgs = fmt.Sprintf("(%d, %d)", precisionAndScale[0], precisionAndScale[1])
} else if argLen == 1 {
castArgs = fmt.Sprintf("(%d)", precisionAndScale[0])
2019-07-31 18:43:54 +02:00
}
2019-08-13 10:16:26 +02:00
return FloatExp(b.AS("numeric" + castArgs))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS real type
func (b *castImpl) AS_REAL() FloatExpression {
2019-08-13 10:16:26 +02:00
return 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-13 10:16:26 +02:00
return 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-13 10:16:26 +02:00
return StringExp(b.AS("text"))
}
2019-08-17 10:43:16 +02:00
func (b *castImpl) AS_CHAR(length ...int) StringExpression {
if len(length) > 0 {
return StringExp(b.AS("char(" + strconv.Itoa(length[0]) + ")"))
2019-08-13 10:16:26 +02:00
}
return StringExp(b.AS("char"))
}
// Cast expression AS date type
func (b *castImpl) AS_DATE() DateExpression {
return DateExp(b.AS("date"))
}
// Cast expression AS date type
func (b *castImpl) AS_DECIMAL() FloatExpression {
return FloatExp(b.AS("decimal"))
2019-07-31 18:43:54 +02:00
}
// Cast expression AS text type
func (b *castImpl) AS_BYTEA() StringExpression {
2019-08-13 10:16:26 +02:00
return StringExp(b.AS("bytea"))
}
2019-07-31 18:43:54 +02:00
// Cast expression AS date type
2019-08-13 10:16:26 +02:00
func (b *castImpl) AS_TIME() TimeExpression {
2019-08-01 10:39:57 +02:00
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-13 10:16:26 +02:00
return 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-13 10:16:26 +02:00
return 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-13 10:16:26 +02:00
return TimestampzExp(b.AS("timestamp with time zone"))
2019-07-31 18:43:54 +02:00
}
2019-12-07 13:52:51 +01:00
// Cast expression AS interval type
func (b *castImpl) AS_INTERVAL() IntervalExpression {
return IntervalExp(b.AS("interval"))
}