jet/mysql/cast.go

102 lines
2.6 KiB
Go
Raw Normal View History

2019-07-31 18:43:54 +02:00
package mysql
import (
2020-06-27 18:48:19 +02:00
"github.com/go-jet/jet/v2/internal/jet"
2019-08-13 10:16:26 +02:00
"strconv"
2019-07-31 18:43:54 +02:00
)
type cast interface {
2024-10-17 14:12:21 +02:00
// AS casts expressions as castType type
2019-08-13 10:16:26 +02:00
AS(castType string) Expression
2024-10-17 14:12:21 +02:00
// AS_CHAR casts expression as char with optional length
2019-08-17 10:43:16 +02:00
AS_CHAR(length ...int) StringExpression
2024-10-17 14:12:21 +02:00
// AS_DATE casts expression AS date type
2019-08-13 10:16:26 +02:00
AS_DATE() DateExpression
2024-10-17 14:12:21 +02:00
// AS_FLOAT casts expressions as float type
AS_FLOAT() FloatExpression
// AS_DOUBLE casts expressions as double type
AS_DOUBLE() FloatExpression
// AS_DECIMAL casts expression AS numeric type
2019-08-13 10:16:26 +02:00
AS_DECIMAL() FloatExpression
2024-10-17 14:12:21 +02:00
// AS_TIME casts expression AS time type
2019-08-13 10:16:26 +02:00
AS_TIME() TimeExpression
2024-10-17 14:12:21 +02:00
// AS_DATETIME casts expression as datetime type
2019-07-31 18:43:54 +02:00
AS_DATETIME() DateTimeExpression
2024-10-17 14:12:21 +02:00
// AS_SIGNED casts expressions as signed integer type
2019-07-31 18:43:54 +02:00
AS_SIGNED() IntegerExpression
2024-10-17 14:12:21 +02:00
// AS_UNSIGNED casts expression as unsigned integer type
2019-07-31 18:43:54 +02:00
AS_UNSIGNED() IntegerExpression
2024-10-17 14:12:21 +02:00
// AS_BINARY casts 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 {
return DateTimeExp(c.AS("DATETIME"))
}
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 {
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 {
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
}
2022-08-23 12:38:16 +02:00
// AS_DATE casts expression AS DATE type
2019-08-17 10:43:16 +02:00
func (c *castImpl) AS_DATE() DateExpression {
return DateExp(c.AS("DATE"))
2019-08-13 10:16:26 +02:00
}
2024-10-17 14:12:21 +02:00
func (c *castImpl) AS_FLOAT() FloatExpression {
return FloatExp(c.AS("FLOAT"))
}
func (c *castImpl) AS_DOUBLE() FloatExpression {
return FloatExp(c.AS("DOUBLE"))
}
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 {
return StringExp(c.AS("BINARY"))
2019-07-31 18:43:54 +02:00
}