jet/cast.go

134 lines
3.3 KiB
Go
Raw Normal View History

2019-06-21 13:56:57 +02:00
package jet
2019-07-07 12:19:05 +02:00
import "fmt"
type cast interface {
// 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
// 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
AS_TIMESTAMP() TimestampExpression
// Cast expression AS timestamp with timezone type
AS_TIMESTAMPZ() TimestampzExpression
}
type castImpl struct {
2019-06-04 12:10:23 +02:00
Expression
2019-06-03 17:05:29 +02:00
castType string
}
2019-07-18 17:43:11 +02:00
// CAST wraps expression for casting.
// For instance: CAST(table.column).AS_BOOL()
2019-07-07 12:19:05 +02:00
func CAST(expression Expression) cast {
return &castImpl{
2019-06-04 12:10:23 +02:00
Expression: expression,
}
}
2019-07-08 10:48:03 +02:00
func (b *castImpl) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
2019-06-04 12:10:23 +02:00
err := b.Expression.serialize(statement, out, options...)
out.writeString("::" + b.castType)
return err
}
2019-07-07 12:19:05 +02:00
func (b *castImpl) AS_BOOL() BoolExpression {
b.castType = "boolean"
return BoolExp(b)
}
2019-07-07 12:19:05 +02:00
func (b *castImpl) AS_SMALLINT() IntegerExpression {
b.castType = "smallint"
return IntExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS integer type
func (b *castImpl) AS_INTEGER() IntegerExpression {
b.castType = "integer"
return IntExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS bigint type
func (b *castImpl) AS_BIGINT() IntegerExpression {
b.castType = "bigint"
return IntExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS numeric type, using precision and optionally scale
func (b *castImpl) AS_NUMERIC(precision int, scale ...int) FloatExpression {
2019-07-07 12:19:05 +02:00
if len(scale) > 0 {
b.castType = fmt.Sprintf("numeric(%d, %d)", precision, scale[0])
} else {
b.castType = fmt.Sprintf("numeric(%d)", precision)
}
2019-07-07 12:19:05 +02:00
return FloatExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS real type
func (b *castImpl) AS_REAL() FloatExpression {
b.castType = "real"
return FloatExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS double precision type
func (b *castImpl) AS_DOUBLE() FloatExpression {
b.castType = "double precision"
return FloatExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS text type
func (b *castImpl) AS_TEXT() StringExpression {
b.castType = "text"
return StringExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS date type
func (b *castImpl) AS_DATE() DateExpression {
b.castType = "date"
return DateExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS time type
func (b *castImpl) AS_TIME() TimeExpression {
b.castType = "time without time zone"
return TimeExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS time with time timezone type
func (b *castImpl) AS_TIMEZ() TimezExpression {
b.castType = "time with time zone"
return TimezExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS timestamp type
func (b *castImpl) AS_TIMESTAMP() TimestampExpression {
b.castType = "timestamp without time zone"
return TimestampExp(b)
}
2019-07-07 12:19:05 +02:00
// Cast expression AS timestamp with timezone type
func (b *castImpl) AS_TIMESTAMPZ() TimestampzExpression {
b.castType = "timestamp with time zone"
return TimestampzExp(b)
}