jet/mysql/cast.go
Eli Ribble 34e5dcbb17
All checks were successful
/ test (push) Successful in 3s
Switch to hosting on source.gleipnir.technology
2026-05-14 16:26:47 +00:00

75 lines
1.7 KiB
Go

package mysql
import (
"strconv"
"source.gleipnir.technology/Gleipnir/jet/internal/jet"
)
// CAST function converts an expr (of any type) into later specified datatype.
func CAST(expr Expression) *cast {
return &cast{
expr: expr,
}
}
type cast struct {
expr Expression
}
// AS casts expressions to castType
func (c *cast) AS(castType string) Expression {
return jet.AtomicCustomExpression(Token("CAST("), c.expr, Token("AS "+castType+")"))
}
// AS_DATETIME cast expression to DATETIME type
func (c *cast) AS_DATETIME() DateTimeExpression {
return DateTimeExp(c.AS("DATETIME"))
}
// AS_SIGNED casts expression to SIGNED type
func (c *cast) AS_SIGNED() IntegerExpression {
return IntExp(c.AS("SIGNED"))
}
// AS_UNSIGNED casts expression to UNSIGNED type
func (c *cast) AS_UNSIGNED() IntegerExpression {
return IntExp(c.AS("UNSIGNED"))
}
// AS_CHAR casts expression to CHAR type with optional length
func (c *cast) AS_CHAR(length ...int) StringExpression {
if len(length) > 0 {
return StringExp(c.AS("CHAR(" + strconv.Itoa(length[0]) + ")"))
}
return StringExp(c.AS("CHAR"))
}
// AS_DATE casts expression AS DATE type
func (c *cast) AS_DATE() DateExpression {
return DateExp(c.AS("DATE"))
}
func (c *cast) AS_FLOAT() FloatExpression {
return FloatExp(c.AS("FLOAT"))
}
func (c *cast) AS_DOUBLE() FloatExpression {
return FloatExp(c.AS("DOUBLE"))
}
// AS_DECIMAL casts expression AS DECIMAL type
func (c *cast) AS_DECIMAL() FloatExpression {
return FloatExp(c.AS("DECIMAL"))
}
// AS_TIME casts expression AS TIME type
func (c *cast) AS_TIME() TimeExpression {
return TimeExp(c.AS("TIME"))
}
// AS_BINARY casts expression as BINARY type
func (c *cast) AS_BINARY() BlobExpression {
return BlobExp(c.AS("BINARY"))
}