CAST refactor.
This commit is contained in:
parent
cd1d033ffb
commit
db43f471ec
11 changed files with 187 additions and 308 deletions
201
cast.go
201
cast.go
|
|
@ -1,154 +1,131 @@
|
||||||
package jet
|
package jet
|
||||||
|
|
||||||
type cast struct {
|
import "fmt"
|
||||||
|
|
||||||
|
type cast interface {
|
||||||
|
// Cast expression AS bool type
|
||||||
|
AS_BOOL() BoolExpression
|
||||||
|
// Cast expression AS smallint type
|
||||||
|
AS_SMALLINT() IntegerExpression
|
||||||
|
// Cast expression AS integer type
|
||||||
|
AS_INTEGER() IntegerExpression
|
||||||
|
// Cast expression AS bigint type
|
||||||
|
AS_BIGINT() IntegerExpression
|
||||||
|
// Cast expression AS numeric type, using precision and optionally scale
|
||||||
|
AS_NUMERIC(precision int, scale ...int) FloatExpression
|
||||||
|
// Cast expression AS real type
|
||||||
|
AS_REAL() FloatExpression
|
||||||
|
// Cast expression AS double precision type
|
||||||
|
AS_DOUBLE() FloatExpression
|
||||||
|
// Cast expression AS text type
|
||||||
|
AS_TEXT() StringExpression
|
||||||
|
// Cast expression AS date type
|
||||||
|
AS_DATE() DateExpression
|
||||||
|
// Cast expression AS time type
|
||||||
|
AS_TIME() TimeExpression
|
||||||
|
// Cast expression AS time with time timezone type
|
||||||
|
AS_TIMEZ() TimezExpression
|
||||||
|
// Cast expression AS timestamp type
|
||||||
|
AS_TIMESTAMP() TimestampExpression
|
||||||
|
// Cast expression AS timestamp with timezone type
|
||||||
|
AS_TIMESTAMPZ() TimestampzExpression
|
||||||
|
}
|
||||||
|
|
||||||
|
type castImpl struct {
|
||||||
Expression
|
Expression
|
||||||
castType string
|
castType string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCast(expression Expression, castType string) *cast {
|
func CAST(expression Expression) cast {
|
||||||
return &cast{
|
return &castImpl{
|
||||||
Expression: expression,
|
Expression: expression,
|
||||||
castType: castType,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *cast) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
func (b *castImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
||||||
err := b.Expression.serialize(statement, out, options...)
|
err := b.Expression.serialize(statement, out, options...)
|
||||||
out.writeString("::" + b.castType)
|
out.writeString("::" + b.castType)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type boolCast struct {
|
func (b *castImpl) AS_BOOL() BoolExpression {
|
||||||
expressionInterfaceImpl
|
b.castType = "boolean"
|
||||||
boolInterfaceImpl
|
return BoolExp(b)
|
||||||
cast
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBoolCast(expression Expression) BoolExpression {
|
func (b *castImpl) AS_SMALLINT() IntegerExpression {
|
||||||
boolCast := &boolCast{cast: *newCast(expression, "boolean")}
|
b.castType = "smallint"
|
||||||
|
return IntExp(b)
|
||||||
boolCast.boolInterfaceImpl.parent = boolCast
|
|
||||||
boolCast.expressionInterfaceImpl.parent = boolCast
|
|
||||||
|
|
||||||
return boolCast
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type integerCast struct {
|
// Cast expression AS integer type
|
||||||
expressionInterfaceImpl
|
func (b *castImpl) AS_INTEGER() IntegerExpression {
|
||||||
integerInterfaceImpl
|
b.castType = "integer"
|
||||||
cast
|
return IntExp(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIntegerCast(expression Expression, intType string) IntegerExpression {
|
// Cast expression AS bigint type
|
||||||
integerCast := &integerCast{cast: *newCast(expression, intType)}
|
func (b *castImpl) AS_BIGINT() IntegerExpression {
|
||||||
|
b.castType = "bigint"
|
||||||
integerCast.integerInterfaceImpl.parent = integerCast
|
return IntExp(b)
|
||||||
integerCast.expressionInterfaceImpl.parent = integerCast
|
|
||||||
|
|
||||||
return integerCast
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type floatCast struct {
|
// Cast expression AS numeric type, using precision and optionally scale
|
||||||
expressionInterfaceImpl
|
func (b *castImpl) AS_NUMERIC(precision int, scale ...int) FloatExpression {
|
||||||
floatInterfaceImpl
|
|
||||||
cast
|
if len(scale) > 0 {
|
||||||
|
b.castType = fmt.Sprintf("numeric(%d, %d)", precision, scale[0])
|
||||||
|
} else {
|
||||||
|
b.castType = fmt.Sprintf("numeric(%d)", precision)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFloatCast(expression Expression, floatType string) FloatExpression {
|
return FloatExp(b)
|
||||||
floatCast := &floatCast{cast: *newCast(expression, floatType)}
|
|
||||||
|
|
||||||
floatCast.floatInterfaceImpl.parent = floatCast
|
|
||||||
floatCast.expressionInterfaceImpl.parent = floatCast
|
|
||||||
|
|
||||||
return floatCast
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type textCast struct {
|
// Cast expression AS real type
|
||||||
expressionInterfaceImpl
|
func (b *castImpl) AS_REAL() FloatExpression {
|
||||||
stringInterfaceImpl
|
b.castType = "real"
|
||||||
cast
|
return FloatExp(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTextCast(expression Expression) StringExpression {
|
// Cast expression AS double precision type
|
||||||
textCast := &textCast{cast: *newCast(expression, "text")}
|
func (b *castImpl) AS_DOUBLE() FloatExpression {
|
||||||
|
b.castType = "double precision"
|
||||||
textCast.stringInterfaceImpl.parent = textCast
|
return FloatExp(b)
|
||||||
textCast.expressionInterfaceImpl.parent = textCast
|
|
||||||
|
|
||||||
return textCast
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type dateCast struct {
|
// Cast expression AS text type
|
||||||
expressionInterfaceImpl
|
func (b *castImpl) AS_TEXT() StringExpression {
|
||||||
dateInterfaceImpl
|
b.castType = "text"
|
||||||
cast
|
return StringExp(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDateCast(expression Expression) DateExpression {
|
// Cast expression AS date type
|
||||||
dateCast := &dateCast{cast: *newCast(expression, "date")}
|
func (b *castImpl) AS_DATE() DateExpression {
|
||||||
|
b.castType = "date"
|
||||||
dateCast.dateInterfaceImpl.parent = dateCast
|
return DateExp(b)
|
||||||
dateCast.expressionInterfaceImpl.parent = dateCast
|
|
||||||
|
|
||||||
return dateCast
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type timeCast struct {
|
// Cast expression AS time type
|
||||||
expressionInterfaceImpl
|
func (b *castImpl) AS_TIME() TimeExpression {
|
||||||
timeInterfaceImpl
|
b.castType = "time without time zone"
|
||||||
cast
|
return TimeExp(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTimeCast(expression Expression) TimeExpression {
|
// Cast expression AS time with time timezone type
|
||||||
timeCast := &timeCast{cast: *newCast(expression, "time without time zone")}
|
func (b *castImpl) AS_TIMEZ() TimezExpression {
|
||||||
|
b.castType = "time with time zone"
|
||||||
timeCast.timeInterfaceImpl.parent = timeCast
|
return TimezExp(b)
|
||||||
timeCast.expressionInterfaceImpl.parent = timeCast
|
|
||||||
|
|
||||||
return timeCast
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type timezCast struct {
|
// Cast expression AS timestamp type
|
||||||
expressionInterfaceImpl
|
func (b *castImpl) AS_TIMESTAMP() TimestampExpression {
|
||||||
timezInterfaceImpl
|
b.castType = "timestamp without time zone"
|
||||||
cast
|
return TimestampExp(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTimezCast(expression Expression) TimezExpression {
|
// Cast expression AS timestamp with timezone type
|
||||||
timezCast := &timezCast{cast: *newCast(expression, "time with time zone")}
|
func (b *castImpl) AS_TIMESTAMPZ() TimestampzExpression {
|
||||||
|
b.castType = "timestamp with time zone"
|
||||||
timezCast.timezInterfaceImpl.parent = timezCast
|
return TimestampzExp(b)
|
||||||
timezCast.expressionInterfaceImpl.parent = timezCast
|
|
||||||
|
|
||||||
return timezCast
|
|
||||||
}
|
|
||||||
|
|
||||||
type timestampCast struct {
|
|
||||||
expressionInterfaceImpl
|
|
||||||
timestampInterfaceImpl
|
|
||||||
cast
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTimestampCast(expression Expression) TimestampExpression {
|
|
||||||
timestampCast := ×tampCast{cast: *newCast(expression, "timestamp without time zone")}
|
|
||||||
|
|
||||||
timestampCast.timestampInterfaceImpl.parent = timestampCast
|
|
||||||
timestampCast.expressionInterfaceImpl.parent = timestampCast
|
|
||||||
|
|
||||||
return timestampCast
|
|
||||||
}
|
|
||||||
|
|
||||||
type timestampzCast struct {
|
|
||||||
expressionInterfaceImpl
|
|
||||||
timestampzInterfaceImpl
|
|
||||||
cast
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTimestampzCast(expression Expression) TimestampzExpression {
|
|
||||||
timestampzCast := ×tampzCast{cast: *newCast(expression, "timestamp with time zone")}
|
|
||||||
|
|
||||||
timestampzCast.timestampzInterfaceImpl.parent = timestampzCast
|
|
||||||
timestampzCast.expressionInterfaceImpl.parent = timestampzCast
|
|
||||||
|
|
||||||
return timestampzCast
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
58
cast_test.go
Normal file
58
cast_test.go
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
package jet
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_BOOL(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(Int(1)).AS_BOOL(), "$1::boolean", int64(1))
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_BOOL(), "table2.col3::boolean")
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3.ADD(table2Col3)).AS_BOOL(), "(table2.col3 + table2.col3)::boolean")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_SMALLINT(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_SMALLINT(), "table2.col3::smallint")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_INTEGER(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_INTEGER(), "table2.col3::integer")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_BIGINT(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_BIGINT(), "table2.col3::bigint")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_NUMERIC(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_NUMERIC(11, 11), "table2.col3::numeric(11, 11)")
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_NUMERIC(11), "table2.col3::numeric(11)")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_REAL(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_REAL(), "table2.col3::real")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_DOUBLE(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_DOUBLE(), "table2.col3::double precision")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_TEXT(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_TEXT(), "table2.col3::text")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_DATE(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_DATE(), "table2.col3::date")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_TIME(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_TIME(), "table2.col3::time without time zone")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_TIMEZ(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_TIMEZ(), "table2.col3::time with time zone")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_TIMESTAMP(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_TIMESTAMP(), "table2.col3::timestamp without time zone")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpressionCAST_AS_TIMESTAMPZ(t *testing.T) {
|
||||||
|
assertClauseSerialize(t, CAST(table2Col3).AS_TIMESTAMPZ(), "table2.col3::timestamp with time zone")
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,6 @@ package jet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Common expression interface
|
// Common expression interface
|
||||||
|
|
@ -29,35 +28,6 @@ type Expression interface {
|
||||||
ASC() OrderByClause
|
ASC() OrderByClause
|
||||||
// Expression will be used to sort query result in ascending order
|
// Expression will be used to sort query result in ascending order
|
||||||
DESC() OrderByClause
|
DESC() OrderByClause
|
||||||
|
|
||||||
// Cast expression to dbType
|
|
||||||
TO(dbType string) Expression
|
|
||||||
// Cast expression to bool type
|
|
||||||
TO_BOOL() BoolExpression
|
|
||||||
// Cast expression to smallint type
|
|
||||||
TO_SMALLINT() IntegerExpression
|
|
||||||
// Cast expression to integer type
|
|
||||||
TO_INTEGER() IntegerExpression
|
|
||||||
// Cast expression to bigint type
|
|
||||||
TO_BIGINT() IntegerExpression
|
|
||||||
// Cast expression to numeric type, using precision and optionally scale
|
|
||||||
TO_NUMERIC(precision int, scale ...int) FloatExpression
|
|
||||||
// Cast expression to real type
|
|
||||||
TO_REAL() FloatExpression
|
|
||||||
// Cast expression to double precision type
|
|
||||||
TO_DOUBLE() FloatExpression
|
|
||||||
// Cast expression to text type
|
|
||||||
TO_TEXT() StringExpression
|
|
||||||
// Cast expression to date type
|
|
||||||
TO_DATE() DateExpression
|
|
||||||
// Cast expression to time type
|
|
||||||
TO_TIME() TimeExpression
|
|
||||||
// Cast expression to time with time timezone type
|
|
||||||
TO_TIMEZ() TimezExpression
|
|
||||||
// Cast expression to timestamp type
|
|
||||||
TO_TIMESTAMP() TimestampExpression
|
|
||||||
// Cast expression to timestamp with timezone type
|
|
||||||
TO_TIMESTAMPZ() TimestampzExpression
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type expressionInterfaceImpl struct {
|
type expressionInterfaceImpl struct {
|
||||||
|
|
@ -96,68 +66,6 @@ func (e *expressionInterfaceImpl) DESC() OrderByClause {
|
||||||
return newOrderByClause(e.parent, false)
|
return newOrderByClause(e.parent, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO(dbType string) Expression {
|
|
||||||
return newCast(e.parent, dbType)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_BOOL() BoolExpression {
|
|
||||||
return newBoolCast(e.parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_SMALLINT() IntegerExpression {
|
|
||||||
return newIntegerCast(e.parent, "smallint")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_INTEGER() IntegerExpression {
|
|
||||||
return newIntegerCast(e.parent, "integer")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_BIGINT() IntegerExpression {
|
|
||||||
return newIntegerCast(e.parent, "bigint")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_NUMERIC(precision int, scale ...int) FloatExpression {
|
|
||||||
var castType string
|
|
||||||
if len(scale) > 0 {
|
|
||||||
castType = fmt.Sprintf("numeric(%d, %d)", precision, scale[0])
|
|
||||||
} else {
|
|
||||||
castType = fmt.Sprintf("numeric(%d)", precision)
|
|
||||||
}
|
|
||||||
return newFloatCast(e.parent, castType)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_REAL() FloatExpression {
|
|
||||||
return newFloatCast(e.parent, "real")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_DOUBLE() FloatExpression {
|
|
||||||
return newFloatCast(e.parent, "double precision")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_TEXT() StringExpression {
|
|
||||||
return newTextCast(e.parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_DATE() DateExpression {
|
|
||||||
return newDateCast(e.parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_TIME() TimeExpression {
|
|
||||||
return newTimeCast(e.parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_TIMEZ() TimezExpression {
|
|
||||||
return newTimezCast(e.parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_TIMESTAMP() TimestampExpression {
|
|
||||||
return newTimestampCast(e.parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) TO_TIMESTAMPZ() TimestampzExpression {
|
|
||||||
return newTimestampzCast(e.parent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *expressionInterfaceImpl) serializeForGroupBy(statement statementType, out *queryData) error {
|
func (e *expressionInterfaceImpl) serializeForGroupBy(statement statementType, out *queryData) error {
|
||||||
return e.parent.serialize(statement, out, noWrap)
|
return e.parent.serialize(statement, out, noWrap)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,60 +25,6 @@ func TestExpressionIS_NOT_DISTINCT_FROM(t *testing.T) {
|
||||||
assertClauseSerialize(t, table2Col3.ADD(table2Col3).IS_NOT_DISTINCT_FROM(Int(23)), "((table2.col3 + table2.col3) IS NOT DISTINCT FROM $1)", int64(23))
|
assertClauseSerialize(t, table2Col3.ADD(table2Col3).IS_NOT_DISTINCT_FROM(Int(23)), "((table2.col3 + table2.col3) IS NOT DISTINCT FROM $1)", int64(23))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExpressionCAST_TO_BOOL(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_BOOL(), "table2.col3::boolean")
|
|
||||||
assertClauseSerialize(t, table2Col3.ADD(table2Col3).TO_BOOL(), "(table2.col3 + table2.col3)::boolean")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_SMALLINT(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_SMALLINT(), "table2.col3::smallint")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_INTEGER(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_INTEGER(), "table2.col3::integer")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_BIGINT(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_BIGINT(), "table2.col3::bigint")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_NUMERIC(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_NUMERIC(11, 11), "table2.col3::numeric(11, 11)")
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_NUMERIC(11), "table2.col3::numeric(11)")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_REAL(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_REAL(), "table2.col3::real")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_DOUBLE(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_DOUBLE(), "table2.col3::double precision")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TEXT(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_TEXT(), "table2.col3::text")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_DATE(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_DATE(), "table2.col3::date")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TIME(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_TIME(), "table2.col3::time without time zone")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TIMEZ(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_TIMEZ(), "table2.col3::time with time zone")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TIMESTAMP(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_TIMESTAMP(), "table2.col3::timestamp without time zone")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExpressionCAST_TO_TIMESTAMPZ(t *testing.T) {
|
|
||||||
assertClauseSerialize(t, table2Col3.TO_TIMESTAMPZ(), "table2.col3::timestamp with time zone")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIN(t *testing.T) {
|
func TestIN(t *testing.T) {
|
||||||
|
|
||||||
assertClauseSerialize(t, Float(1.11).IN(table1.SELECT(table1Col1)),
|
assertClauseSerialize(t, Float(1.11).IN(table1.SELECT(table1Col1)),
|
||||||
|
|
|
||||||
|
|
@ -101,13 +101,13 @@ type timeLiteral struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Time(hour, minute, second, milliseconds int) TimeExpression {
|
func Time(hour, minute, second, milliseconds int) TimeExpression {
|
||||||
timeLiteral := timeLiteral{}
|
timeLiteral := &timeLiteral{}
|
||||||
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d", hour, minute, second, milliseconds)
|
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d", hour, minute, second, milliseconds)
|
||||||
timeLiteral.literalExpression = *literal(timeStr)
|
timeLiteral.literalExpression = *literal(timeStr)
|
||||||
|
|
||||||
timeLiteral.timeInterfaceImpl.parent = &timeLiteral
|
timeLiteral.timeInterfaceImpl.parent = timeLiteral
|
||||||
|
|
||||||
return timeLiteral.TO_TIME()
|
return CAST(timeLiteral).AS_TIME()
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
|
|
@ -117,13 +117,13 @@ type timezLiteral struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression {
|
func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression {
|
||||||
timezLiteral := timezLiteral{}
|
timezLiteral := &timezLiteral{}
|
||||||
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d %+03d", hour, minute, second, milliseconds, timezone)
|
timeStr := fmt.Sprintf("%02d:%02d:%02d.%03d %+03d", hour, minute, second, milliseconds, timezone)
|
||||||
timezLiteral.literalExpression = *literal(timeStr)
|
timezLiteral.literalExpression = *literal(timeStr)
|
||||||
|
|
||||||
timezLiteral.timezInterfaceImpl.parent = &timezLiteral
|
timezLiteral.timezInterfaceImpl.parent = timezLiteral
|
||||||
|
|
||||||
return timezLiteral.TO_TIMEZ()
|
return CAST(timezLiteral).AS_TIMEZ()
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
|
|
@ -133,13 +133,13 @@ type timestampLiteral struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Timestamp(year, month, day, hour, minute, second, milliseconds int) TimestampExpression {
|
func Timestamp(year, month, day, hour, minute, second, milliseconds int) TimestampExpression {
|
||||||
timestampLiteral := timestampLiteral{}
|
timestampLiteral := ×tampLiteral{}
|
||||||
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, milliseconds)
|
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, milliseconds)
|
||||||
timestampLiteral.literalExpression = *literal(timeStr)
|
timestampLiteral.literalExpression = *literal(timeStr)
|
||||||
|
|
||||||
timestampLiteral.timestampInterfaceImpl.parent = ×tampLiteral
|
timestampLiteral.timestampInterfaceImpl.parent = timestampLiteral
|
||||||
|
|
||||||
return timestampLiteral.TO_TIMESTAMP()
|
return CAST(timestampLiteral).AS_TIMESTAMP()
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
|
|
@ -149,15 +149,15 @@ type timestampzLiteral struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Timestampz(year, month, day, hour, minute, second, milliseconds, timezone int) TimestampzExpression {
|
func Timestampz(year, month, day, hour, minute, second, milliseconds, timezone int) TimestampzExpression {
|
||||||
timestampzLiteral := timestampzLiteral{}
|
timestampzLiteral := ×tampzLiteral{}
|
||||||
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %+04d",
|
timeStr := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %+04d",
|
||||||
year, month, day, hour, minute, second, milliseconds, timezone)
|
year, month, day, hour, minute, second, milliseconds, timezone)
|
||||||
|
|
||||||
timestampzLiteral.literalExpression = *literal(timeStr)
|
timestampzLiteral.literalExpression = *literal(timeStr)
|
||||||
|
|
||||||
timestampzLiteral.timestampzInterfaceImpl.parent = ×tampzLiteral
|
timestampzLiteral.timestampzInterfaceImpl.parent = timestampzLiteral
|
||||||
|
|
||||||
return timestampzLiteral.TO_TIMESTAMPZ()
|
return CAST(timestampzLiteral).AS_TIMESTAMPZ()
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------//
|
//---------------------------------------------------//
|
||||||
|
|
@ -167,13 +167,13 @@ type dateLiteral struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Date(year, month, day int) DateExpression {
|
func Date(year, month, day int) DateExpression {
|
||||||
dateLiteral := dateLiteral{}
|
dateLiteral := &dateLiteral{}
|
||||||
|
|
||||||
timeStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day)
|
timeStr := fmt.Sprintf("%04d-%02d-%02d", year, month, day)
|
||||||
dateLiteral.literalExpression = *literal(timeStr)
|
dateLiteral.literalExpression = *literal(timeStr)
|
||||||
dateLiteral.dateInterfaceImpl.parent = &dateLiteral
|
dateLiteral.dateInterfaceImpl.parent = dateLiteral
|
||||||
|
|
||||||
return dateLiteral.TO_DATE()
|
return CAST(dateLiteral).AS_DATE()
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------//
|
//--------------------------------------------------//
|
||||||
|
|
|
||||||
|
|
@ -66,20 +66,20 @@ func TestExpressionOperators(t *testing.T) {
|
||||||
AllTypes.SmallintPtr.NOT_IN(Int(11), Int(22), NULL),
|
AllTypes.SmallintPtr.NOT_IN(Int(11), Int(22), NULL),
|
||||||
AllTypes.SmallintPtr.NOT_IN(AllTypes.SELECT(AllTypes.IntegerPtr)),
|
AllTypes.SmallintPtr.NOT_IN(AllTypes.SELECT(AllTypes.IntegerPtr)),
|
||||||
|
|
||||||
String("TRUE").TO_BOOL(),
|
CAST(String("TRUE")).AS_BOOL(),
|
||||||
String("111").TO_SMALLINT(),
|
CAST(String("111")).AS_SMALLINT(),
|
||||||
String("111").TO_INTEGER(),
|
CAST(String("111")).AS_INTEGER(),
|
||||||
String("111").TO_BIGINT(),
|
CAST(String("111")).AS_BIGINT(),
|
||||||
String("11.23").TO_NUMERIC(30, 10),
|
CAST(String("11.23")).AS_NUMERIC(30, 10),
|
||||||
String("11.23").TO_NUMERIC(30),
|
CAST(String("11.23")).AS_NUMERIC(30),
|
||||||
String("11.23").TO_REAL(),
|
CAST(String("11.23")).AS_REAL(),
|
||||||
String("11.23").TO_DOUBLE(),
|
CAST(String("11.23")).AS_DOUBLE(),
|
||||||
Int(234).TO_TEXT(),
|
CAST(Int(234)).AS_TEXT(),
|
||||||
String("1/8/1999").TO_DATE(),
|
CAST(String("1/8/1999")).AS_DATE(),
|
||||||
String("04:05:06.789").TO_TIME(),
|
CAST(String("04:05:06.789")).AS_TIME(),
|
||||||
String("04:05:06 PST").TO_TIMEZ(),
|
CAST(String("04:05:06 PST")).AS_TIMEZ(),
|
||||||
String("1999-01-08 04:05:06").TO_TIMESTAMP(),
|
CAST(String("1999-01-08 04:05:06")).AS_TIMESTAMP(),
|
||||||
String("January 8 04:05:06 1999 PST").TO_TIMESTAMPZ(),
|
CAST(String("January 8 04:05:06 1999 PST")).AS_TIMESTAMPZ(),
|
||||||
|
|
||||||
TO_CHAR(AllTypes.Timestamp, String("HH12:MI:SS")),
|
TO_CHAR(AllTypes.Timestamp, String("HH12:MI:SS")),
|
||||||
TO_CHAR(AllTypes.Integer, String("999")),
|
TO_CHAR(AllTypes.Integer, String("999")),
|
||||||
|
|
|
||||||
|
|
@ -865,7 +865,10 @@ WHERE film.rental_rate = (
|
||||||
ORDER BY film.film_id ASC;
|
ORDER BY film.film_id ASC;
|
||||||
`
|
`
|
||||||
|
|
||||||
maxFilmRentalRate := Film.SELECT(MAXf(Film.RentalRate)).TO_DOUBLE()
|
maxFilmRentalRate := CAST(
|
||||||
|
Film.
|
||||||
|
SELECT(MAXf(Film.RentalRate)),
|
||||||
|
).AS_DOUBLE()
|
||||||
|
|
||||||
query := Film.
|
query := Film.
|
||||||
SELECT(Film.AllColumns).
|
SELECT(Film.AllColumns).
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,6 @@ func newTimestampzExpressionWrap(expression Expression) TimestampzExpression {
|
||||||
return ×tampzExpressionWrap
|
return ×tampzExpressionWrap
|
||||||
}
|
}
|
||||||
|
|
||||||
func TimestampzExp(expression Expression) TimestampExpression {
|
func TimestampzExp(expression Expression) TimestampzExpression {
|
||||||
return newTimestampExpressionWrap(expression)
|
return newTimestampzExpressionWrap(expression)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,6 @@ func newTimezExpressionWrap(expression Expression) TimezExpression {
|
||||||
return &timezExpressionWrap
|
return &timezExpressionWrap
|
||||||
}
|
}
|
||||||
|
|
||||||
func TimezExp(expression Expression) TimeExpression {
|
func TimezExp(expression Expression) TimezExpression {
|
||||||
return newTimeExpressionWrap(expression)
|
return newTimezExpressionWrap(expression)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -122,19 +122,6 @@ func (u *updateStatementImpl) Sql() (sql string, args []interface{}, err error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//if len(u.returning) > 0 {
|
|
||||||
// out.newLine()
|
|
||||||
// out.writeString("RETURNING")
|
|
||||||
// out.increaseIdent()
|
|
||||||
// out.increaseIdent()
|
|
||||||
//
|
|
||||||
// err = serializeProjectionList(update_statement, u.returning, out)
|
|
||||||
//
|
|
||||||
// if err != nil {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
sql, args = out.finalize()
|
sql, args = out.finalize()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue