2019-07-31 18:43:54 +02:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import (
|
2019-08-03 14:10:47 +02:00
|
|
|
"github.com/go-jet/jet/internal/jet"
|
2019-08-13 10:16:26 +02:00
|
|
|
"strconv"
|
2019-07-31 18:43:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type cast interface {
|
2019-08-13 10:16:26 +02:00
|
|
|
// Cast expressions as castType type
|
|
|
|
|
AS(castType string) Expression
|
|
|
|
|
// Cast expression as char with optional length
|
2019-08-17 10:43:16 +02:00
|
|
|
AS_CHAR(length ...int) StringExpression
|
2019-08-13 10:16:26 +02:00
|
|
|
// 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
|
|
|
|
|
// Cast expression as datetime type
|
2019-07-31 18:43:54 +02:00
|
|
|
AS_DATETIME() DateTimeExpression
|
2019-08-13 10:16:26 +02:00
|
|
|
// Cast expressions as signed integer type
|
2019-07-31 18:43:54 +02:00
|
|
|
AS_SIGNED() IntegerExpression
|
2019-08-13 10:16:26 +02:00
|
|
|
// Cast expression as unsigned integer type
|
2019-07-31 18:43:54 +02:00
|
|
|
AS_UNSIGNED() IntegerExpression
|
2019-08-13 10:16:26 +02:00
|
|
|
// Cast expression as binary type
|
2019-07-31 18:43:54 +02:00
|
|
|
AS_BINARY() StringExpression
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type castImpl struct {
|
2019-08-11 14:29:03 +02:00
|
|
|
jet.Cast
|
2019-07-31 18:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// CAST function converts a expr (of any type) into latter specified datatype.
|
2019-08-16 11:19:06 +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 10:43:16 +02:00
|
|
|
// AS casts expressions to castType
|
2019-08-13 10:16:26 +02:00
|
|
|
func (c *castImpl) AS(castType string) Expression {
|
|
|
|
|
return c.Cast.AS(castType)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// AS_DATETIME cast expression to DATETIME type
|
2019-07-31 18:43:54 +02:00
|
|
|
func (c *castImpl) AS_DATETIME() DateTimeExpression {
|
2019-08-06 13:58:17 +02:00
|
|
|
return DateTimeExp(c.AS("DATETIME"))
|
2019-08-06 11:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// AS_SIGNED casts expression to SIGNED type
|
2019-07-31 18:43:54 +02:00
|
|
|
func (c *castImpl) AS_SIGNED() IntegerExpression {
|
2019-08-06 13:58:17 +02:00
|
|
|
return IntExp(c.AS("SIGNED"))
|
2019-07-31 18:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// AS_UNSIGNED casts expression to UNSIGNED type
|
2019-07-31 18:43:54 +02:00
|
|
|
func (c *castImpl) AS_UNSIGNED() IntegerExpression {
|
2019-08-06 13:58:17 +02:00
|
|
|
return IntExp(c.AS("UNSIGNED"))
|
2019-07-31 18:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// AS_CHAR casts expression to CHAR type with optional length
|
|
|
|
|
func (c *castImpl) AS_CHAR(length ...int) StringExpression {
|
|
|
|
|
if len(length) > 0 {
|
|
|
|
|
return StringExp(c.AS("CHAR(" + strconv.Itoa(length[0]) + ")"))
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
return StringExp(c.AS("CHAR"))
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// AS_DATE casts expression AS DATE type
|
|
|
|
|
func (c *castImpl) AS_DATE() DateExpression {
|
|
|
|
|
return DateExp(c.AS("DATE"))
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// AS_DECIMAL casts expression AS DECIMAL type
|
|
|
|
|
func (c *castImpl) AS_DECIMAL() FloatExpression {
|
|
|
|
|
return FloatExp(c.AS("DECIMAL"))
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// AS_TIME casts expression AS TIME type
|
|
|
|
|
func (c *castImpl) AS_TIME() TimeExpression {
|
|
|
|
|
return TimeExp(c.AS("TIME"))
|
2019-08-13 10:16:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// AS_BINARY casts expression as BINARY type
|
2019-07-31 18:43:54 +02:00
|
|
|
func (c *castImpl) AS_BINARY() StringExpression {
|
2019-08-06 13:58:17 +02:00
|
|
|
return StringExp(c.AS("BINARY"))
|
2019-07-31 18:43:54 +02:00
|
|
|
}
|