jet/mysql/cast.go

76 lines
1.7 KiB
Go
Raw Normal View History

2019-07-31 18:43:54 +02:00
package mysql
import (
2019-08-13 10:16:26 +02:00
"strconv"
2019-07-31 18:43:54 +02:00
2026-02-02 13:21:35 +01:00
"github.com/go-jet/jet/v2/internal/jet"
)
2019-07-31 18:43:54 +02:00
2026-02-02 13:21:35 +01:00
// CAST function converts an expr (of any type) into later specified datatype.
2024-11-01 12:30:37 +01:00
func CAST(expr Expression) *cast {
2026-02-02 13:21:35 +01:00
return &cast{
expr: expr,
}
}
2019-07-31 18:43:54 +02:00
2026-02-02 13:21:35 +01:00
type cast struct {
expr Expression
2019-07-31 18:43:54 +02:00
}
2019-08-17 10:43:16 +02:00
// AS casts expressions to castType
2024-11-01 12:30:37 +01:00
func (c *cast) AS(castType string) Expression {
2026-02-02 13:21:35 +01:00
return jet.AtomicCustomExpression(Token("CAST("), c.expr, Token("AS "+castType+")"))
2019-08-13 10:16:26 +02:00
}
2019-08-17 10:43:16 +02:00
// AS_DATETIME cast expression to DATETIME type
2024-11-01 12:30:37 +01:00
func (c *cast) AS_DATETIME() DateTimeExpression {
return DateTimeExp(c.AS("DATETIME"))
}
2019-08-17 10:43:16 +02:00
// AS_SIGNED casts expression to SIGNED type
2024-11-01 12:30:37 +01:00
func (c *cast) 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
2024-11-01 12:30:37 +01:00
func (c *cast) 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
2024-11-01 12:30:37 +01:00
func (c *cast) AS_CHAR(length ...int) StringExpression {
2019-08-17 10:43:16 +02:00
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
2024-11-01 12:30:37 +01:00
func (c *cast) AS_DATE() DateExpression {
2019-08-17 10:43:16 +02:00
return DateExp(c.AS("DATE"))
2019-08-13 10:16:26 +02:00
}
2024-11-01 12:30:37 +01:00
func (c *cast) AS_FLOAT() FloatExpression {
2024-10-17 14:12:21 +02:00
return FloatExp(c.AS("FLOAT"))
}
2024-11-01 12:30:37 +01:00
func (c *cast) AS_DOUBLE() FloatExpression {
2024-10-17 14:12:21 +02:00
return FloatExp(c.AS("DOUBLE"))
}
2019-08-17 10:43:16 +02:00
// AS_DECIMAL casts expression AS DECIMAL type
2024-11-01 12:30:37 +01:00
func (c *cast) AS_DECIMAL() FloatExpression {
2019-08-17 10:43:16 +02:00
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
2024-11-01 12:30:37 +01:00
func (c *cast) AS_TIME() TimeExpression {
2019-08-17 10:43:16 +02:00
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
2025-02-28 18:23:15 +01:00
func (c *cast) AS_BINARY() BlobExpression {
return BlobExp(c.AS("BINARY"))
2019-07-31 18:43:54 +02:00
}