jet/postgres/cast.go

133 lines
3.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
)
2024-11-01 12:30:37 +01:00
type cast struct {
2019-08-11 14:29:03 +02:00
jet.Cast
2019-07-31 18:43:54 +02:00
}
2024-11-01 12:30:37 +01:00
// CAST function converts an expr (of any type) into later specified datatype.
func CAST(expr Expression) *cast {
ret := &cast{}
ret.Cast = jet.NewCastImpl(expr)
2019-07-31 18:43:54 +02:00
2024-11-01 12:30:37 +01:00
return ret
2019-07-31 18:43:54 +02:00
}
2024-11-01 12:30:37 +01:00
// AS casts expression as castType
func (b *cast) AS(castType string) Expression {
2019-08-13 10:16:26 +02:00
return b.Cast.AS(castType)
}
2024-11-01 12:30:37 +01:00
// AS_BOOL casts expression as bool type
func (b *cast) AS_BOOL() BoolExpression {
2019-08-13 10:16:26 +02:00
return BoolExp(b.AS("boolean"))
2019-07-31 18:43:54 +02:00
}
2024-11-01 12:30:37 +01:00
// AS_SMALLINT casts expression as smallint type
func (b *cast) AS_SMALLINT() IntegerExpression {
2019-08-13 10:16:26 +02:00
return IntExp(b.AS("smallint"))
2019-07-31 18:43:54 +02:00
}
2024-11-01 12:30:37 +01:00
// AS_INTEGER casts expression AS integer type
func (b *cast) AS_INTEGER() IntegerExpression {
2019-08-13 10:16:26 +02:00
return IntExp(b.AS("integer"))
2019-07-31 18:43:54 +02:00
}
2024-11-01 12:30:37 +01:00
// AS_BIGINT casts expression AS bigint type
func (b *cast) AS_BIGINT() IntegerExpression {
2019-08-13 10:16:26 +02:00
return IntExp(b.AS("bigint"))
2019-07-31 18:43:54 +02:00
}
2024-11-01 12:30:37 +01:00
// AS_NUMERIC casts expression as numeric type, using precision and optionally scale
func (b *cast) 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
}
2024-11-01 12:30:37 +01:00
// AS_REAL casts expression AS real type
func (b *cast) AS_REAL() FloatExpression {
2019-08-13 10:16:26 +02:00
return FloatExp(b.AS("real"))
2019-07-31 18:43:54 +02:00
}
2024-11-01 12:30:37 +01:00
// AS_DOUBLE casts expression AS double precision type
func (b *cast) 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
}
2024-11-01 12:30:37 +01:00
// AS_TEXT casts expression AS text type
func (b *cast) AS_TEXT() StringExpression {
2019-08-13 10:16:26 +02:00
return StringExp(b.AS("text"))
}
2024-11-01 12:30:37 +01:00
// AS_CHAR casts expression AS a character type
func (b *cast) AS_CHAR(length ...int) StringExpression {
2019-08-17 10:43:16 +02:00
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"))
}
2024-11-01 12:30:37 +01:00
// AS_VARCHAR casts expression AS a character varying type
func (b *cast) AS_VARCHAR(length ...int) StringExpression {
if len(length) > 0 {
return StringExp(b.AS("varchar(" + strconv.Itoa(length[0]) + ")"))
}
return StringExp(b.AS("varchar"))
}
// AS_DATE casts expression AS date type
func (b *cast) AS_DATE() DateExpression {
2019-08-13 10:16:26 +02:00
return DateExp(b.AS("date"))
}
2024-11-01 12:30:37 +01:00
// AS_DECIMAL casts expression AS date type
func (b *cast) AS_DECIMAL() FloatExpression {
2019-08-13 10:16:26 +02:00
return FloatExp(b.AS("decimal"))
2019-07-31 18:43:54 +02:00
}
2025-02-28 18:23:15 +01:00
// AS_BYTEA casts expression AS bytea type
func (b *cast) AS_BYTEA() ByteaExpression {
return ByteaExp(b.AS("bytea"))
}
2024-11-01 12:30:37 +01:00
// AS_TIME casts expression AS date type
func (b *cast) 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
}
2024-11-01 12:30:37 +01:00
// AS_TIMEZ casts expression AS time with time timezone type
func (b *cast) 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
}
2024-11-01 12:30:37 +01:00
// AS_TIMESTAMP casts expression AS timestamp type
func (b *cast) 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
}
2024-11-01 12:30:37 +01:00
// AS_TIMESTAMPZ casts expression AS timestamp with timezone type
func (b *cast) 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
2024-11-01 12:30:37 +01:00
// AS_INTERVAL casts expression AS interval type
func (b *cast) AS_INTERVAL() IntervalExpression {
2019-12-07 13:52:51 +01:00
return IntervalExp(b.AS("interval"))
}