Literal and cast clean up.

This commit is contained in:
go-jet 2019-08-13 10:16:26 +02:00
parent 9a34dc9fd7
commit 614c7e9754
22 changed files with 438 additions and 177 deletions

View file

@ -2,14 +2,27 @@ package mysql
import (
"github.com/go-jet/jet/internal/jet"
"strconv"
)
type cast interface {
jet.Cast
// Cast expressions as castType type
AS(castType string) Expression
// Cast expression as char with optional length
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
// Cast expression as datetime type
AS_DATETIME() DateTimeExpression
// Cast expressions as signed integer type
AS_SIGNED() IntegerExpression
// Cast expression as unsigned integer type
AS_UNSIGNED() IntegerExpression
// Cast expression as binary type
AS_BINARY() StringExpression
}
@ -25,6 +38,10 @@ func CAST(expr jet.Expression) cast {
return castImpl
}
func (c *castImpl) AS(castType string) Expression {
return c.Cast.AS(castType)
}
func (c *castImpl) AS_DATETIME() DateTimeExpression {
return DateTimeExp(c.AS("DATETIME"))
}
@ -37,6 +54,29 @@ func (c *castImpl) AS_UNSIGNED() IntegerExpression {
return IntExp(c.AS("UNSIGNED"))
}
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"))
}
func (c *castImpl) AS_BINARY() StringExpression {
return StringExp(c.AS("BINARY"))
}

View file

@ -4,6 +4,15 @@ import (
"testing"
)
func TestCAST_AS_DATE(t *testing.T) {
assertClauseSerialize(t, CAST(Int(22)).AS_DATE(), `CAST(? AS DATE)`, int64(22))
func TestCAST(t *testing.T) {
assertClauseSerialize(t, CAST(Float(11.22)).AS("bigint"), `CAST(? AS bigint)`)
assertClauseSerialize(t, CAST(Int(22)).AS_CHAR(), `CAST(? AS CHAR)`)
assertClauseSerialize(t, CAST(Int(22)).AS_CHAR(10), `CAST(? AS CHAR(10))`)
assertClauseSerialize(t, CAST(Int(22)).AS_DATE(), `CAST(? AS DATE)`)
assertClauseSerialize(t, CAST(Int(22)).AS_DECIMAL(), `CAST(? AS DECIMAL)`)
assertClauseSerialize(t, CAST(Int(22)).AS_TIME(), `CAST(? AS TIME)`)
assertClauseSerialize(t, CAST(Int(22)).AS_DATETIME(), `CAST(? AS DATETIME)`)
assertClauseSerialize(t, CAST(Int(22)).AS_SIGNED(), `CAST(? AS SIGNED)`)
assertClauseSerialize(t, CAST(Int(22)).AS_UNSIGNED(), `CAST(? AS UNSIGNED)`)
assertClauseSerialize(t, CAST(Int(22)).AS_BINARY(), `CAST(? AS BINARY)`)
}

View file

@ -17,26 +17,23 @@ var String = jet.String
var Date = func(year int, month time.Month, day int) DateExpression {
return CAST(jet.Date(year, month, day)).AS_DATE()
}
var DateT = func(t time.Time) DateExpression {
return CAST(jet.DateT(t)).AS_DATE()
}
var Time = func(hour, minute, second int, milliseconds ...int) TimeExpression {
return CAST(jet.Time(hour, minute, second, milliseconds...)).AS_TIME()
var Time = func(hour, minute, second int, nanoseconds ...time.Duration) TimeExpression {
return CAST(jet.Time(hour, minute, second, nanoseconds...)).AS_TIME()
}
var TimeT = func(t time.Time) TimeExpression {
return CAST(jet.TimeT(t)).AS_TIME()
}
var DateTime = func(year int, month time.Month, day, hour, minute, second int, milliseconds ...int) DateTimeExpression {
return CAST(jet.Timestamp(year, month, day, hour, minute, second, milliseconds...)).AS_DATETIME()
var DateTime = func(year int, month time.Month, day, hour, minute, second int, nanoseconds ...time.Duration) DateTimeExpression {
return CAST(jet.Timestamp(year, month, day, hour, minute, second, nanoseconds...)).AS_DATETIME()
}
var DateTimeT = func(t time.Time) DateTimeExpression {
return CAST(jet.TimestampT(t)).AS_DATETIME()
}
var Timestamp = func(year int, month time.Month, day, hour, minute, second int, milliseconds ...int) TimestampExpression {
return TIMESTAMP(StringExp(jet.Timestamp(year, month, day, hour, minute, second, milliseconds...)))
var Timestamp = func(year int, month time.Month, day, hour, minute, second int, nanoseconds ...time.Duration) TimestampExpression {
return TIMESTAMP(StringExp(jet.Timestamp(year, month, day, hour, minute, second, nanoseconds...)))
}
var TimestampT = func(t time.Time) TimestampExpression {
return TIMESTAMP(StringExp(jet.TimestampT(t)))

42
mysql/literal_test.go Normal file
View file

@ -0,0 +1,42 @@
package mysql
import (
"testing"
"time"
)
func TestBool(t *testing.T) {
assertClauseSerialize(t, Bool(false), `?`, false)
}
func TestInt(t *testing.T) {
assertClauseSerialize(t, Int(11), `?`, int64(11))
}
func TestFloat(t *testing.T) {
assertClauseSerialize(t, Float(12.34), `?`, float64(12.34))
}
func TestString(t *testing.T) {
assertClauseSerialize(t, String("Some text"), `?`, "Some text")
}
func TestDate(t *testing.T) {
assertClauseSerialize(t, Date(2014, time.January, 2), `CAST(? AS DATE)`, "2014-01-02")
assertClauseSerialize(t, DateT(time.Now()), `CAST(? AS DATE)`)
}
func TestTime(t *testing.T) {
assertClauseSerialize(t, Time(10, 15, 30), `CAST(? AS TIME)`, "10:15:30")
assertClauseSerialize(t, TimeT(time.Now()), `CAST(? AS TIME)`)
}
func TestDateTime(t *testing.T) {
assertClauseSerialize(t, DateTime(2010, time.March, 30, 10, 15, 30), `CAST(? AS DATETIME)`, "2010-03-30 10:15:30")
assertClauseSerialize(t, DateTimeT(time.Now()), `CAST(? AS DATETIME)`)
}
func TestTimestamp(t *testing.T) {
assertClauseSerialize(t, Timestamp(2010, time.March, 30, 10, 15, 30), `TIMESTAMP(?)`, "2010-03-30 10:15:30")
assertClauseSerialize(t, TimestampT(time.Now()), `TIMESTAMP(?)`)
}