2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-06-02 18:12:44 +02:00
|
|
|
|
2019-08-01 10:39:57 +02:00
|
|
|
import "strconv"
|
2019-07-07 12:19:05 +02:00
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
type Cast interface {
|
2019-08-01 10:39:57 +02:00
|
|
|
AS(castType string) Expression
|
|
|
|
|
|
|
|
|
|
AS_CHAR(lenght ...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-06-02 18:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
type CastImpl struct {
|
|
|
|
|
expression Expression
|
2019-06-02 18:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-01 10:39:57 +02:00
|
|
|
func NewCastImpl(expression Expression) CastImpl {
|
2019-07-31 18:43:54 +02:00
|
|
|
castImpl := CastImpl{
|
|
|
|
|
expression: expression,
|
|
|
|
|
}
|
2019-07-28 14:57:02 +02:00
|
|
|
|
2019-08-01 10:39:57 +02:00
|
|
|
return castImpl
|
2019-07-28 14:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-01 10:39:57 +02:00
|
|
|
func (b *CastImpl) AS(castType string) Expression {
|
2019-07-31 18:43:54 +02:00
|
|
|
castExp := &castExpression{
|
|
|
|
|
expression: b.expression,
|
|
|
|
|
cast: string(castType),
|
2019-07-28 14:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
castExp.expressionInterfaceImpl.parent = castExp
|
2019-07-28 14:57:02 +02:00
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
return castExp
|
2019-06-02 18:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-01 10:39:57 +02:00
|
|
|
func (b *CastImpl) AS_CHAR(lenght ...int) StringExpression {
|
|
|
|
|
if len(lenght) > 0 {
|
|
|
|
|
return StringExp(b.AS("CHAR(" + strconv.Itoa(lenght[0]) + ")"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cast expression AS date type
|
|
|
|
|
func (b *CastImpl) AS_TIME() TimeExpression {
|
|
|
|
|
return TimeExp(b.AS("TIME"))
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
type castExpression struct {
|
|
|
|
|
expressionInterfaceImpl
|
2019-06-02 18:12:44 +02:00
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
expression Expression
|
|
|
|
|
cast string
|
2019-06-02 18:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
func (b *castExpression) accept(visitor visitor) {
|
|
|
|
|
b.expression.accept(visitor)
|
2019-06-02 18:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
func (b *castExpression) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error {
|
2019-06-02 18:12:44 +02:00
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
expression := b.expression
|
|
|
|
|
castType := b.cast
|
2019-06-02 18:12:44 +02:00
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
if castOverride := out.dialect.CastOverride; castOverride != nil {
|
|
|
|
|
return castOverride(expression, castType)(statement, out, options...)
|
2019-07-07 12:19:05 +02:00
|
|
|
}
|
2019-06-02 18:12:44 +02:00
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
out.writeString("CAST(")
|
|
|
|
|
err := expression.serialize(statement, out, options...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-06-02 18:12:44 +02:00
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
out.writeString("AS")
|
|
|
|
|
out.writeString(castType + ")")
|
2019-06-02 18:12:44 +02:00
|
|
|
|
2019-07-31 18:43:54 +02:00
|
|
|
return err
|
2019-06-02 18:12:44 +02:00
|
|
|
}
|