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

@ -3,13 +3,13 @@ package postgres
import (
"fmt"
"github.com/go-jet/jet/internal/jet"
"strconv"
)
type cast interface {
jet.Cast
AS(castType string) Expression
// Cast expression AS bool type
AS_BOOL() BoolExpression
// Cast expression AS smallint type
AS_SMALLINT() IntegerExpression
// Cast expression AS integer type
@ -18,16 +18,22 @@ type cast interface {
AS_BIGINT() IntegerExpression
// Cast expression AS numeric type, using precision and optionally scale
AS_NUMERIC(precisionAndScale ...int) FloatExpression
// Cast expression AS real type
AS_REAL() FloatExpression
// Cast expression AS double precision type
AS_DOUBLE() FloatExpression
// Cast expression AS char with optional length
AS_CHAR(length ...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 text type
AS_TEXT() StringExpression
AS_BYTEA() StringExpression
// Cast expression AS time with time timezone type
AS_TIMEZ() TimezExpression
// Cast expression AS timestamp type
@ -48,22 +54,26 @@ func CAST(expr Expression) cast {
return castImpl
}
func (b *castImpl) AS(castType string) Expression {
return b.Cast.AS(castType)
}
func (b *castImpl) AS_BOOL() BoolExpression {
return jet.BoolExp(b.AS("boolean"))
return BoolExp(b.AS("boolean"))
}
func (b *castImpl) AS_SMALLINT() IntegerExpression {
return jet.IntExp(b.AS("smallint"))
return IntExp(b.AS("smallint"))
}
// Cast expression AS integer type
func (b *castImpl) AS_INTEGER() IntegerExpression {
return jet.IntExp(b.AS("integer"))
return IntExp(b.AS("integer"))
}
// Cast expression AS bigint type
func (b *castImpl) AS_BIGINT() IntegerExpression {
return jet.IntExp(b.AS("bigint"))
return IntExp(b.AS("bigint"))
}
// Cast expression AS numeric type, using precision and optionally scale
@ -77,45 +87,63 @@ func (b *castImpl) AS_NUMERIC(precisionAndScale ...int) FloatExpression {
castArgs = fmt.Sprintf("(%d)", precisionAndScale[0])
}
return jet.FloatExp(b.AS("numeric" + castArgs))
return FloatExp(b.AS("numeric" + castArgs))
}
// Cast expression AS real type
func (b *castImpl) AS_REAL() FloatExpression {
return jet.FloatExp(b.AS("real"))
return FloatExp(b.AS("real"))
}
// Cast expression AS double precision type
func (b *castImpl) AS_DOUBLE() FloatExpression {
return jet.FloatExp(b.AS("double precision"))
return FloatExp(b.AS("double precision"))
}
// Cast expression AS text type
func (b *castImpl) AS_TEXT() StringExpression {
return jet.StringExp(b.AS("text"))
return StringExp(b.AS("text"))
}
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 text type
func (b *castImpl) AS_BYTEA() StringExpression {
return jet.StringExp(b.AS("bytea"))
return StringExp(b.AS("bytea"))
}
// Cast expression AS date type
func (b *castImpl) AS_TIME() jet.TimeExpression {
func (b *castImpl) AS_TIME() TimeExpression {
return TimeExp(b.AS("time without time zone"))
}
// Cast expression AS time with time timezone type
func (b *castImpl) AS_TIMEZ() TimezExpression {
return jet.TimezExp(b.AS("time with time zone"))
return TimezExp(b.AS("time with time zone"))
}
// Cast expression AS timestamp type
func (b *castImpl) AS_TIMESTAMP() TimestampExpression {
return jet.TimestampExp(b.AS("timestamp without time zone"))
return TimestampExp(b.AS("timestamp without time zone"))
}
// Cast expression AS timestamp with timezone type
func (b *castImpl) AS_TIMESTAMPZ() TimestampzExpression {
return jet.TimestampzExp(b.AS("timestamp with time zone"))
return TimestampzExp(b.AS("timestamp with time zone"))
}

View file

@ -44,7 +44,7 @@ func TestExpressionCAST_AS_TEXT(t *testing.T) {
}
func TestExpressionCAST_AS_DATE(t *testing.T) {
assertClauseSerialize(t, CAST(table2Col3).AS_DATE(), "table2.col3::DATE")
assertClauseSerialize(t, CAST(table2Col3).AS_DATE(), "table2.col3::date")
}
func TestExpressionCAST_AS_TIME(t *testing.T) {

View file

@ -26,6 +26,7 @@ var BoolExp = jet.BoolExp
var IntExp = jet.IntExp
var FloatExp = jet.FloatExp
var TimeExp = jet.TimeExp
var StringExp = jet.StringExp
var TimezExp = jet.TimezExp
var DateExp = jet.DateExp
var TimestampExp = jet.TimestampExp

View file

@ -13,43 +13,33 @@ var String = jet.String
var Bytea = func(value string) StringExpression {
return CAST(jet.String(value)).AS_BYTEA()
}
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 Timez = func(hour, minute, second, milliseconds int, timezone int) TimezExpression {
var Timez = func(hour, minute, second int, milliseconds time.Duration, timezone string) TimezExpression {
return CAST(jet.Timez(hour, minute, second, milliseconds, timezone)).AS_TIMEZ()
}
var TimezT = func(t time.Time) TimezExpression {
return CAST(jet.TimezT(t)).AS_TIMEZ()
}
var Timestamp = func(year int, month time.Month, day, hour, minute, second, milliseconds int) TimestampExpression {
return CAST(jet.Timestamp(year, month, day, hour, minute, second, milliseconds)).AS_TIMESTAMP()
var Timestamp = func(year int, month time.Month, day, hour, minute, second int, milliseconds ...time.Duration) TimestampExpression {
return CAST(jet.Timestamp(year, month, day, hour, minute, second, milliseconds...)).AS_TIMESTAMP()
}
var TimestampT = func(t time.Time) TimestampExpression {
return CAST(jet.TimestampzT(t)).AS_TIMESTAMP()
}
var Timestampz = func(year, month, day, hour, minute, second, milliseconds int, timezone int) TimestampzExpression {
var Timestampz = func(year int, month time.Month, day, hour, minute, second int, milliseconds time.Duration, timezone string) TimestampzExpression {
return CAST(jet.Timestampz(year, month, day, hour, minute, second, milliseconds, timezone)).AS_TIMESTAMPZ()
}
var TimestampzT = func(t time.Time) TimestampzExpression {
return CAST(jet.TimestampzT(t)).AS_TIMESTAMPZ()
}

View file

@ -1,7 +1,50 @@
package postgres
import "testing"
import (
"testing"
"time"
)
func TestDateLiteral(t *testing.T) {
assertClauseSerialize(t, Date(2019, 8, 6), "$1::DATE", "2019-08-06")
func TestBool(t *testing.T) {
assertClauseSerialize(t, Bool(false), `$1`, false)
}
func TestInt(t *testing.T) {
assertClauseSerialize(t, Int(11), `$1`, int64(11))
}
func TestFloat(t *testing.T) {
assertClauseSerialize(t, Float(12.34), `$1`, float64(12.34))
}
func TestString(t *testing.T) {
assertClauseSerialize(t, String("Some text"), `$1`, "Some text")
}
func TestDate(t *testing.T) {
assertClauseSerialize(t, Date(2014, time.January, 2), `$1::date`, "2014-01-02")
assertClauseSerialize(t, DateT(time.Now()), `$1::date`)
}
func TestTime(t *testing.T) {
assertClauseSerialize(t, Time(10, 15, 30), `$1::time without time zone`, "10:15:30")
assertClauseSerialize(t, TimeT(time.Now()), `$1::time without time zone`)
}
func TestTimez(t *testing.T) {
assertClauseSerialize(t, Timez(10, 15, 30, 0, "UTC"),
`$1::time with time zone`, "10:15:30 UTC")
assertClauseSerialize(t, TimezT(time.Now()), `$1::time with time zone`)
}
func TestTimestamp(t *testing.T) {
assertClauseSerialize(t, Timestamp(2010, time.March, 30, 10, 15, 30),
`$1::timestamp without time zone`, "2010-03-30 10:15:30")
assertClauseSerialize(t, TimestampT(time.Now()), `$1::timestamp without time zone`)
}
func TestTimestampz(t *testing.T) {
assertClauseSerialize(t, Timestampz(2010, time.March, 30, 10, 15, 30, 0, "UTC"),
`$1::timestamp with time zone`, "2010-03-30 10:15:30 UTC")
assertClauseSerialize(t, TimestampzT(time.Now()), `$1::timestamp with time zone`)
}