MySQL cast expressions. Simplified.
This commit is contained in:
parent
53dbcd9bfc
commit
c342f296ca
10 changed files with 103 additions and 81 deletions
41
cast.go
41
cast.go
|
|
@ -1,24 +1,32 @@
|
|||
package jet
|
||||
|
||||
type CastType string
|
||||
import "strconv"
|
||||
|
||||
type Cast interface {
|
||||
As(castType CastType) Expression
|
||||
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
|
||||
}
|
||||
|
||||
type CastImpl struct {
|
||||
expression Expression
|
||||
}
|
||||
|
||||
func NewCastImpl(expression Expression) Cast {
|
||||
func NewCastImpl(expression Expression) CastImpl {
|
||||
castImpl := CastImpl{
|
||||
expression: expression,
|
||||
}
|
||||
|
||||
return &castImpl
|
||||
return castImpl
|
||||
}
|
||||
|
||||
func (b *CastImpl) As(castType CastType) Expression {
|
||||
func (b *CastImpl) AS(castType string) Expression {
|
||||
castExp := &castExpression{
|
||||
expression: b.expression,
|
||||
cast: string(castType),
|
||||
|
|
@ -29,6 +37,29 @@ func (b *CastImpl) As(castType CastType) Expression {
|
|||
return castExp
|
||||
}
|
||||
|
||||
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"))
|
||||
}
|
||||
|
||||
type castExpression struct {
|
||||
expressionInterfaceImpl
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue