diff --git a/bool_expression_test.go b/bool_expression_test.go index 3d3d3b5..bb57824 100644 --- a/bool_expression_test.go +++ b/bool_expression_test.go @@ -6,131 +6,131 @@ import ( func TestBoolExpressionEQ(t *testing.T) { assertClauseSerializeErr(t, table1ColBool.EQ(nil), "jet: nil rhs") - assertPostgreClauseSerialize(t, table1ColBool.EQ(table2ColBool), "(table1.col_bool = table2.col_bool)") - assertPostgreClauseSerialize(t, table1ColBool.EQ(Bool(true)), "(table1.col_bool = $1)", true) + AssertPostgreClauseSerialize(t, table1ColBool.EQ(table2ColBool), "(table1.col_bool = table2.col_bool)") + AssertPostgreClauseSerialize(t, table1ColBool.EQ(Bool(true)), "(table1.col_bool = $1)", true) - assertMySQLClauseSerialize(t, table1ColBool.EQ(table2ColBool), "(table1.col_bool = table2.col_bool)") - assertMySQLClauseSerialize(t, table1ColBool.EQ(Bool(true)), "(table1.col_bool = ?)", true) + AssertMySQLClauseSerialize(t, table1ColBool.EQ(table2ColBool), "(table1.col_bool = table2.col_bool)") + AssertMySQLClauseSerialize(t, table1ColBool.EQ(Bool(true)), "(table1.col_bool = ?)", true) } func TestBoolExpressionNOT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.NOT_EQ(table2ColBool), "(table1.col_bool != table2.col_bool)") - assertPostgreClauseSerialize(t, table1ColBool.NOT_EQ(Bool(true)), "(table1.col_bool != $1)", true) + AssertPostgreClauseSerialize(t, table1ColBool.NOT_EQ(table2ColBool), "(table1.col_bool != table2.col_bool)") + AssertPostgreClauseSerialize(t, table1ColBool.NOT_EQ(Bool(true)), "(table1.col_bool != $1)", true) - assertMySQLClauseSerialize(t, table1ColBool.NOT_EQ(table2ColBool), "(table1.col_bool != table2.col_bool)") - assertMySQLClauseSerialize(t, table1ColBool.NOT_EQ(Bool(true)), "(table1.col_bool != ?)", true) + AssertMySQLClauseSerialize(t, table1ColBool.NOT_EQ(table2ColBool), "(table1.col_bool != table2.col_bool)") + AssertMySQLClauseSerialize(t, table1ColBool.NOT_EQ(Bool(true)), "(table1.col_bool != ?)", true) } func TestBoolExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.IS_DISTINCT_FROM(table2ColBool), "(table1.col_bool IS DISTINCT FROM table2.col_bool)") - assertPostgreClauseSerialize(t, table1ColBool.IS_DISTINCT_FROM(Bool(false)), "(table1.col_bool IS DISTINCT FROM $1)", false) + AssertPostgreClauseSerialize(t, table1ColBool.IS_DISTINCT_FROM(table2ColBool), "(table1.col_bool IS DISTINCT FROM table2.col_bool)") + AssertPostgreClauseSerialize(t, table1ColBool.IS_DISTINCT_FROM(Bool(false)), "(table1.col_bool IS DISTINCT FROM $1)", false) - assertMySQLClauseSerialize(t, table1ColBool.IS_DISTINCT_FROM(table2ColBool), "(NOT table1.col_bool <=> table2.col_bool)") - assertMySQLClauseSerialize(t, table1ColBool.IS_DISTINCT_FROM(Bool(false)), "(NOT table1.col_bool <=> ?)", false) + AssertMySQLClauseSerialize(t, table1ColBool.IS_DISTINCT_FROM(table2ColBool), "(NOT table1.col_bool <=> table2.col_bool)") + AssertMySQLClauseSerialize(t, table1ColBool.IS_DISTINCT_FROM(Bool(false)), "(NOT table1.col_bool <=> ?)", false) } func TestBoolExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(table2ColBool), "(table1.col_bool IS NOT DISTINCT FROM table2.col_bool)") - assertPostgreClauseSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(Bool(false)), "(table1.col_bool IS NOT DISTINCT FROM $1)", false) + AssertPostgreClauseSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(table2ColBool), "(table1.col_bool IS NOT DISTINCT FROM table2.col_bool)") + AssertPostgreClauseSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(Bool(false)), "(table1.col_bool IS NOT DISTINCT FROM $1)", false) - assertMySQLClauseSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(table2ColBool), "(table1.col_bool <=> table2.col_bool)") - assertMySQLClauseSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(Bool(false)), "(table1.col_bool <=> ?)", false) + AssertMySQLClauseSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(table2ColBool), "(table1.col_bool <=> table2.col_bool)") + AssertMySQLClauseSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(Bool(false)), "(table1.col_bool <=> ?)", false) } func TestBoolExpressionIS_TRUE(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.IS_TRUE(), "table1.col_bool IS TRUE") - assertPostgreClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE(), + AssertPostgreClauseSerialize(t, table1ColBool.IS_TRUE(), "table1.col_bool IS TRUE") + AssertPostgreClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE(), `($1 = table1.col_int) IS TRUE`, int64(2)) - assertPostgreClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE().AND(Int(4).EQ(table2ColInt)), + AssertPostgreClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE().AND(Int(4).EQ(table2ColInt)), `(($1 = table1.col_int) IS TRUE AND ($2 = table2.col_int))`, int64(2), int64(4)) - assertMySQLClauseSerialize(t, table1ColBool.IS_TRUE(), "table1.col_bool IS TRUE") - assertMySQLClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE(), + AssertMySQLClauseSerialize(t, table1ColBool.IS_TRUE(), "table1.col_bool IS TRUE") + AssertMySQLClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE(), `(? = table1.col_int) IS TRUE`, int64(2)) - assertMySQLClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE().AND(Int(4).EQ(table2ColInt)), + AssertMySQLClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE().AND(Int(4).EQ(table2ColInt)), `((? = table1.col_int) IS TRUE AND (? = table2.col_int))`, int64(2), int64(4)) } func TestBoolExpressionIS_NOT_TRUE(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.IS_NOT_TRUE(), "table1.col_bool IS NOT TRUE") + AssertPostgreClauseSerialize(t, table1ColBool.IS_NOT_TRUE(), "table1.col_bool IS NOT TRUE") - assertMySQLClauseSerialize(t, table1ColBool.IS_NOT_TRUE(), "table1.col_bool IS NOT TRUE") + AssertMySQLClauseSerialize(t, table1ColBool.IS_NOT_TRUE(), "table1.col_bool IS NOT TRUE") } func TestBoolExpressionIS_FALSE(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.IS_FALSE(), "table1.col_bool IS FALSE") + AssertPostgreClauseSerialize(t, table1ColBool.IS_FALSE(), "table1.col_bool IS FALSE") - assertMySQLClauseSerialize(t, table1ColBool.IS_FALSE(), "table1.col_bool IS FALSE") + AssertMySQLClauseSerialize(t, table1ColBool.IS_FALSE(), "table1.col_bool IS FALSE") } func TestBoolExpressionIS_NOT_FALSE(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.IS_NOT_FALSE(), "table1.col_bool IS NOT FALSE") + AssertPostgreClauseSerialize(t, table1ColBool.IS_NOT_FALSE(), "table1.col_bool IS NOT FALSE") } func TestBoolExpressionIS_UNKNOWN(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.IS_UNKNOWN(), "table1.col_bool IS UNKNOWN") + AssertPostgreClauseSerialize(t, table1ColBool.IS_UNKNOWN(), "table1.col_bool IS UNKNOWN") - assertMySQLClauseSerialize(t, table1ColBool.IS_UNKNOWN(), "table1.col_bool IS UNKNOWN") + AssertMySQLClauseSerialize(t, table1ColBool.IS_UNKNOWN(), "table1.col_bool IS UNKNOWN") } func TestBoolExpressionIS_NOT_UNKNOWN(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColBool.IS_NOT_UNKNOWN(), "table1.col_bool IS NOT UNKNOWN") + AssertPostgreClauseSerialize(t, table1ColBool.IS_NOT_UNKNOWN(), "table1.col_bool IS NOT UNKNOWN") - assertMySQLClauseSerialize(t, table1ColBool.IS_NOT_UNKNOWN(), "table1.col_bool IS NOT UNKNOWN") + AssertMySQLClauseSerialize(t, table1ColBool.IS_NOT_UNKNOWN(), "table1.col_bool IS NOT UNKNOWN") } func TestBinaryBoolExpression(t *testing.T) { boolExpression := Int(2).EQ(Int(3)) - assertPostgreClauseSerialize(t, boolExpression, "($1 = $2)", int64(2), int64(3)) + AssertPostgreClauseSerialize(t, boolExpression, "($1 = $2)", int64(2), int64(3)) assertProjectionSerialize(t, boolExpression, "$1 = $2", int64(2), int64(3)) assertProjectionSerialize(t, boolExpression.AS("alias_eq_expression"), `($1 = $2) AS "alias_eq_expression"`, int64(2), int64(3)) - assertPostgreClauseSerialize(t, boolExpression.AND(Int(4).EQ(Int(5))), + AssertPostgreClauseSerialize(t, boolExpression.AND(Int(4).EQ(Int(5))), "(($1 = $2) AND ($3 = $4))", int64(2), int64(3), int64(4), int64(5)) - assertPostgreClauseSerialize(t, boolExpression.OR(Int(4).EQ(Int(5))), + AssertPostgreClauseSerialize(t, boolExpression.OR(Int(4).EQ(Int(5))), "(($1 = $2) OR ($3 = $4))", int64(2), int64(3), int64(4), int64(5)) } func TestBoolLiteral(t *testing.T) { - assertPostgreClauseSerialize(t, Bool(true), "$1", true) - assertPostgreClauseSerialize(t, Bool(false), "$1", false) + AssertPostgreClauseSerialize(t, Bool(true), "$1", true) + AssertPostgreClauseSerialize(t, Bool(false), "$1", false) - assertMySQLClauseSerialize(t, Bool(true), "?", true) - assertMySQLClauseSerialize(t, Bool(false), "?", false) + AssertMySQLClauseSerialize(t, Bool(true), "?", true) + AssertMySQLClauseSerialize(t, Bool(false), "?", false) } func TestExists(t *testing.T) { - assertPostgreClauseSerialize(t, EXISTS( + AssertPostgreClauseSerialize(t, EXISTS( table2. SELECT(Int(1)). WHERE(table1Col1.EQ(table2Col3)), ), - `EXISTS ( + `(EXISTS ( SELECT $1 FROM db.table2 WHERE table1.col1 = table2.col3 -)`, int64(1)) +))`, int64(1)) - assertMySQLClauseSerialize(t, EXISTS( + AssertMySQLClauseSerialize(t, EXISTS( table2. SELECT(Int(1)). WHERE(table1Col1.EQ(table2Col3)), ), - `EXISTS ( + `(EXISTS ( SELECT ? FROM db.table2 WHERE table1.col1 = table2.col3 -)`, int64(1)) +))`, int64(1)) } func TestBoolExp(t *testing.T) { - assertPostgreClauseSerialize(t, BoolExp(String("true")), "$1", "true") - assertPostgreClauseSerialize(t, BoolExp(String("true")).IS_TRUE(), "$1 IS TRUE", "true") + AssertPostgreClauseSerialize(t, BoolExp(String("true")), "$1", "true") + AssertPostgreClauseSerialize(t, BoolExp(String("true")).IS_TRUE(), "$1 IS TRUE", "true") - assertMySQLClauseSerialize(t, BoolExp(String("true")), "?", "true") - assertMySQLClauseSerialize(t, BoolExp(String("true")).IS_TRUE(), "? IS TRUE", "true") + AssertMySQLClauseSerialize(t, BoolExp(String("true")), "?", "true") + AssertMySQLClauseSerialize(t, BoolExp(String("true")).IS_TRUE(), "? IS TRUE", "true") } diff --git a/cast.go b/cast.go index 0ab0aa6..759dfc5 100644 --- a/cast.go +++ b/cast.go @@ -1,158 +1,62 @@ package jet -import "fmt" +type CastType string -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 numeric type, using precision and optionally scale - AS_DECIMAL() 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 Cast interface { + As(castType CastType) Expression } -type castImpl struct { - Expression - castType string +type CastImpl struct { + expression Expression } -// CAST wraps expression for casting. -// For instance: CAST(table.column).AS_BOOL() -func CAST(expression Expression) cast { - return &castImpl{ - Expression: expression, +func NewCastImpl(expression Expression) Cast { + castImpl := CastImpl{ + expression: expression, } + + return &castImpl } -func (b *castImpl) accept(visitor visitor) { - visitor.visit(b) +func (b *CastImpl) As(castType CastType) Expression { + castExp := &castExpression{ + expression: b.expression, + cast: string(castType), + } - b.Expression.accept(visitor) + castExp.expressionInterfaceImpl.parent = castExp + + return castExp } -func (b *castImpl) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error { +type castExpression struct { + expressionInterfaceImpl + + expression Expression + cast string +} + +func (b *castExpression) accept(visitor visitor) { + b.expression.accept(visitor) +} + +func (b *castExpression) serialize(statement statementType, out *sqlBuilder, options ...serializeOption) error { + + expression := b.expression + castType := b.cast if castOverride := out.dialect.CastOverride; castOverride != nil { - return castOverride(b.Expression, b.castType)(statement, out, options...) + return castOverride(expression, castType)(statement, out, options...) } out.writeString("CAST(") - err := b.Expression.serialize(statement, out, options...) + err := expression.serialize(statement, out, options...) if err != nil { return err } out.writeString("AS") - out.writeString(b.castType + ")") + out.writeString(castType + ")") return err } - -func (b *castImpl) AS_BOOL() BoolExpression { - b.castType = "boolean" - return BoolExp(b) -} - -func (b *castImpl) AS_SMALLINT() IntegerExpression { - b.castType = "smallint" - return IntExp(b) -} - -// Cast expression AS integer type -func (b *castImpl) AS_INTEGER() IntegerExpression { - b.castType = "integer" - return IntExp(b) -} - -// Cast expression AS bigint type -func (b *castImpl) AS_BIGINT() IntegerExpression { - b.castType = "bigint" - return IntExp(b) -} - -// Cast expression AS numeric type, using precision and optionally scale -func (b *castImpl) AS_NUMERIC(precision int, scale ...int) FloatExpression { - - if len(scale) > 0 { - b.castType = fmt.Sprintf("numeric(%d, %d)", precision, scale[0]) - } else { - b.castType = fmt.Sprintf("numeric(%d)", precision) - } - - return FloatExp(b) -} - -func (b *castImpl) AS_DECIMAL() FloatExpression { - b.castType = "decimal" - return FloatExp(b) -} - -// Cast expression AS real type -func (b *castImpl) AS_REAL() FloatExpression { - b.castType = "real" - return FloatExp(b) -} - -// Cast expression AS double precision type -func (b *castImpl) AS_DOUBLE() FloatExpression { - b.castType = "double precision" - return FloatExp(b) -} - -// Cast expression AS text type -func (b *castImpl) AS_TEXT() StringExpression { - b.castType = "text" - return StringExp(b) -} - -// Cast expression AS date type -func (b *castImpl) AS_DATE() DateExpression { - b.castType = "date" - return DateExp(b) -} - -// Cast expression AS time type -func (b *castImpl) AS_TIME() TimeExpression { - b.castType = "time without time zone" - return TimeExp(b) -} - -// Cast expression AS time with time timezone type -func (b *castImpl) AS_TIMEZ() TimezExpression { - b.castType = "time with time zone" - return TimezExp(b) -} - -// Cast expression AS timestamp type -func (b *castImpl) AS_TIMESTAMP() TimestampExpression { - b.castType = "timestamp without time zone" - return TimestampExp(b) -} - -// Cast expression AS timestamp with timezone type -func (b *castImpl) AS_TIMESTAMPZ() TimestampzExpression { - b.castType = "timestamp with time zone" - return TimestampzExp(b) -} diff --git a/cast_test.go b/cast_test.go index dfac7da..5a15d1b 100644 --- a/cast_test.go +++ b/cast_test.go @@ -2,57 +2,8 @@ package jet import "testing" -func TestExpressionCAST_AS_BOOL(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(Int(1)).AS_BOOL(), "$1::boolean", int64(1)) - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_BOOL(), "table2.col3::boolean") - assertPostgreClauseSerialize(t, CAST(table2Col3.ADD(table2Col3)).AS_BOOL(), "(table2.col3 + table2.col3)::boolean") -} - -func TestExpressionCAST_AS_SMALLINT(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_SMALLINT(), "table2.col3::smallint") -} - -func TestExpressionCAST_AS_INTEGER(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_INTEGER(), "table2.col3::integer") -} - -func TestExpressionCAST_AS_BIGINT(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_BIGINT(), "table2.col3::bigint") -} - -func TestExpressionCAST_AS_NUMERIC(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_NUMERIC(11, 11), "table2.col3::numeric(11, 11)") - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_NUMERIC(11), "table2.col3::numeric(11)") -} - -func TestExpressionCAST_AS_REAL(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_REAL(), "table2.col3::real") -} - -func TestExpressionCAST_AS_DOUBLE(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_DOUBLE(), "table2.col3::double precision") -} - -func TestExpressionCAST_AS_TEXT(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_TEXT(), "table2.col3::text") -} - -func TestExpressionCAST_AS_DATE(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_DATE(), "table2.col3::date") -} - -func TestExpressionCAST_AS_TIME(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_TIME(), "table2.col3::time without time zone") -} - -func TestExpressionCAST_AS_TIMEZ(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_TIMEZ(), "table2.col3::time with time zone") -} - -func TestExpressionCAST_AS_TIMESTAMP(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_TIMESTAMP(), "table2.col3::timestamp without time zone") -} - -func TestExpressionCAST_AS_TIMESTAMPZ(t *testing.T) { - assertPostgreClauseSerialize(t, CAST(table2Col3).AS_TIMESTAMPZ(), "table2.col3::timestamp with time zone") +func TestCastAS(t *testing.T) { + AssertClauseSerialize(t, NewCastImpl(Int(1)).As("boolean"), "CAST(? AS boolean)", int64(1)) + AssertClauseSerialize(t, NewCastImpl(table2Col3).As("real"), "CAST(table2.col3 AS real)") + AssertClauseSerialize(t, NewCastImpl(table2Col3.ADD(table2Col3)).As("integer"), "CAST((table2.col3 + table2.col3) AS integer)") } diff --git a/column_test.go b/column_test.go index 807ab8e..9cf6c0b 100644 --- a/column_test.go +++ b/column_test.go @@ -6,9 +6,9 @@ func TestColumn(t *testing.T) { column := newColumn("col", "", nil) column.expressionInterfaceImpl.parent = &column - assertPostgreClauseSerialize(t, column, "col") + AssertPostgreClauseSerialize(t, column, "col") column.setTableName("table1") - assertPostgreClauseSerialize(t, column, "table1.col") + AssertPostgreClauseSerialize(t, column, "table1.col") assertProjectionSerialize(t, &column, `table1.col AS "table1.col"`) assertProjectionSerialize(t, column.AS("alias1"), `table1.col AS "alias1"`) } diff --git a/column_types_test.go b/column_types_test.go index 8acd889..4ceeec0 100644 --- a/column_types_test.go +++ b/column_types_test.go @@ -8,38 +8,38 @@ var subQuery = table1.SELECT(table1ColFloat, table1ColInt).AsTable("sub_query") func TestNewBoolColumn(t *testing.T) { boolColumn := BoolColumn("colBool").From(subQuery) - assertPostgreClauseSerialize(t, boolColumn, `sub_query."colBool"`) - assertPostgreClauseSerialize(t, boolColumn.EQ(Bool(true)), `(sub_query."colBool" = $1)`, true) + AssertPostgreClauseSerialize(t, boolColumn, `sub_query."colBool"`) + AssertPostgreClauseSerialize(t, boolColumn.EQ(Bool(true)), `(sub_query."colBool" = $1)`, true) assertProjectionSerialize(t, boolColumn, `sub_query."colBool" AS "colBool"`) boolColumn2 := table1ColBool.From(subQuery) - assertPostgreClauseSerialize(t, boolColumn2, `sub_query."table1.col_bool"`) - assertPostgreClauseSerialize(t, boolColumn2.EQ(Bool(true)), `(sub_query."table1.col_bool" = $1)`, true) + AssertPostgreClauseSerialize(t, boolColumn2, `sub_query."table1.col_bool"`) + AssertPostgreClauseSerialize(t, boolColumn2.EQ(Bool(true)), `(sub_query."table1.col_bool" = $1)`, true) assertProjectionSerialize(t, boolColumn2, `sub_query."table1.col_bool" AS "table1.col_bool"`) } func TestNewIntColumn(t *testing.T) { intColumn := IntegerColumn("col_int").From(subQuery) - assertPostgreClauseSerialize(t, intColumn, `sub_query."col_int"`) - assertPostgreClauseSerialize(t, intColumn.EQ(Int(12)), `(sub_query."col_int" = $1)`, int64(12)) + AssertPostgreClauseSerialize(t, intColumn, `sub_query."col_int"`) + AssertPostgreClauseSerialize(t, intColumn.EQ(Int(12)), `(sub_query."col_int" = $1)`, int64(12)) assertProjectionSerialize(t, intColumn, `sub_query."col_int" AS "col_int"`) intColumn2 := table1ColInt.From(subQuery) - assertPostgreClauseSerialize(t, intColumn2, `sub_query."table1.col_int"`) - assertPostgreClauseSerialize(t, intColumn2.EQ(Int(14)), `(sub_query."table1.col_int" = $1)`, int64(14)) + AssertPostgreClauseSerialize(t, intColumn2, `sub_query."table1.col_int"`) + AssertPostgreClauseSerialize(t, intColumn2.EQ(Int(14)), `(sub_query."table1.col_int" = $1)`, int64(14)) assertProjectionSerialize(t, intColumn2, `sub_query."table1.col_int" AS "table1.col_int"`) } func TestNewFloatColumnColumn(t *testing.T) { floatColumn := FloatColumn("col_float").From(subQuery) - assertPostgreClauseSerialize(t, floatColumn, `sub_query."col_float"`) - assertPostgreClauseSerialize(t, floatColumn.EQ(Float(1.11)), `(sub_query."col_float" = $1)`, float64(1.11)) + AssertPostgreClauseSerialize(t, floatColumn, `sub_query."col_float"`) + AssertPostgreClauseSerialize(t, floatColumn.EQ(Float(1.11)), `(sub_query."col_float" = $1)`, float64(1.11)) assertProjectionSerialize(t, floatColumn, `sub_query."col_float" AS "col_float"`) floatColumn2 := table1ColFloat.From(subQuery) - assertPostgreClauseSerialize(t, floatColumn2, `sub_query."table1.col_float"`) - assertPostgreClauseSerialize(t, floatColumn2.EQ(Float(2.22)), `(sub_query."table1.col_float" = $1)`, float64(2.22)) + AssertPostgreClauseSerialize(t, floatColumn2, `sub_query."table1.col_float"`) + AssertPostgreClauseSerialize(t, floatColumn2.EQ(Float(2.22)), `(sub_query."table1.col_float" = $1)`, float64(2.22)) assertProjectionSerialize(t, floatColumn2, `sub_query."table1.col_float" AS "table1.col_float"`) } diff --git a/date_expression_test.go b/date_expression_test.go deleted file mode 100644 index af606b2..0000000 --- a/date_expression_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package jet - -import "testing" - -var dateVar = Date(2000, 12, 30) - -func TestDateExpressionEQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColDate.EQ(table2ColDate), "(table1.col_date = table2.col_date)") - assertPostgreClauseSerialize(t, table1ColDate.EQ(dateVar), "(table1.col_date = $1::date)", "2000-12-30") -} - -func TestDateExpressionNOT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColDate.NOT_EQ(table2ColDate), "(table1.col_date != table2.col_date)") - assertPostgreClauseSerialize(t, table1ColDate.NOT_EQ(dateVar), "(table1.col_date != $1::date)", "2000-12-30") -} - -func TestDateExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColDate.IS_DISTINCT_FROM(table2ColDate), "(table1.col_date IS DISTINCT FROM table2.col_date)") - assertPostgreClauseSerialize(t, table1ColDate.IS_DISTINCT_FROM(dateVar), "(table1.col_date IS DISTINCT FROM $1::date)", "2000-12-30") -} - -func TestDateExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColDate.IS_NOT_DISTINCT_FROM(table2ColDate), "(table1.col_date IS NOT DISTINCT FROM table2.col_date)") - assertPostgreClauseSerialize(t, table1ColDate.IS_NOT_DISTINCT_FROM(dateVar), "(table1.col_date IS NOT DISTINCT FROM $1::date)", "2000-12-30") -} - -func TestDateExpressionGT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColDate.GT(table2ColDate), "(table1.col_date > table2.col_date)") - assertPostgreClauseSerialize(t, table1ColDate.GT(dateVar), "(table1.col_date > $1::date)", "2000-12-30") -} - -func TestDateExpressionGT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColDate.GT_EQ(table2ColDate), "(table1.col_date >= table2.col_date)") - assertPostgreClauseSerialize(t, table1ColDate.GT_EQ(dateVar), "(table1.col_date >= $1::date)", "2000-12-30") -} - -func TestDateExpressionLT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColDate.LT(table2ColDate), "(table1.col_date < table2.col_date)") - assertPostgreClauseSerialize(t, table1ColDate.LT(dateVar), "(table1.col_date < $1::date)", "2000-12-30") -} - -func TestDateExpressionLT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColDate.LT_EQ(table2ColDate), "(table1.col_date <= table2.col_date)") - assertPostgreClauseSerialize(t, table1ColDate.LT_EQ(dateVar), "(table1.col_date <= $1::date)", "2000-12-30") -} diff --git a/dialects.go b/dialects.go index 6b82011..109a3d3 100644 --- a/dialects.go +++ b/dialects.go @@ -6,6 +6,7 @@ import ( ) var ( + Default = MySQL PostgreSQL = newPostgresDialect() MySQL = newMySQLDialect() ) diff --git a/execution/internal/null_types.go b/execution/internal/null_types.go index 77a2ca0..2a64c8a 100644 --- a/execution/internal/null_types.go +++ b/execution/internal/null_types.go @@ -72,14 +72,19 @@ const formatTime = "2006-01-02 15:04:05.999999" func parseTime(timeStr string) (t time.Time, valid bool) { + var format string + switch len(timeStr) { + case 8: + format = formatTime[11:19] case 10, 19, 21, 22, 23, 24, 25, 26: - format := formatTime[:len(timeStr)] - t, err := time.Parse(format, timeStr) - return t, err == nil + format = formatTime[:len(timeStr)] + default: + return t, false } - return t, false + t, err := time.Parse(format, timeStr) + return t, err == nil } //===============================================================// diff --git a/expression_test.go b/expression_test.go index 1bf002d..2961d78 100644 --- a/expression_test.go +++ b/expression_test.go @@ -5,35 +5,35 @@ import ( ) func TestExpressionIS_NULL(t *testing.T) { - assertPostgreClauseSerialize(t, table2Col3.IS_NULL(), "table2.col3 IS NULL") - assertPostgreClauseSerialize(t, table2Col3.ADD(table2Col3).IS_NULL(), "(table2.col3 + table2.col3) IS NULL") + AssertPostgreClauseSerialize(t, table2Col3.IS_NULL(), "table2.col3 IS NULL") + AssertPostgreClauseSerialize(t, table2Col3.ADD(table2Col3).IS_NULL(), "(table2.col3 + table2.col3) IS NULL") assertClauseSerializeErr(t, table2Col3.ADD(nil), "jet: nil rhs") } func TestExpressionIS_NOT_NULL(t *testing.T) { - assertPostgreClauseSerialize(t, table2Col3.IS_NOT_NULL(), "table2.col3 IS NOT NULL") - assertPostgreClauseSerialize(t, table2Col3.ADD(table2Col3).IS_NOT_NULL(), "(table2.col3 + table2.col3) IS NOT NULL") + AssertPostgreClauseSerialize(t, table2Col3.IS_NOT_NULL(), "table2.col3 IS NOT NULL") + AssertPostgreClauseSerialize(t, table2Col3.ADD(table2Col3).IS_NOT_NULL(), "(table2.col3 + table2.col3) IS NOT NULL") } func TestExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table2Col3.IS_DISTINCT_FROM(table2Col4), "(table2.col3 IS DISTINCT FROM table2.col4)") - assertPostgreClauseSerialize(t, table2Col3.ADD(table2Col3).IS_DISTINCT_FROM(Int(23)), "((table2.col3 + table2.col3) IS DISTINCT FROM $1)", int64(23)) + AssertPostgreClauseSerialize(t, table2Col3.IS_DISTINCT_FROM(table2Col4), "(table2.col3 IS DISTINCT FROM table2.col4)") + AssertPostgreClauseSerialize(t, table2Col3.ADD(table2Col3).IS_DISTINCT_FROM(Int(23)), "((table2.col3 + table2.col3) IS DISTINCT FROM $1)", int64(23)) } func TestExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table2Col3.IS_NOT_DISTINCT_FROM(table2Col4), "(table2.col3 IS NOT DISTINCT FROM table2.col4)") - assertPostgreClauseSerialize(t, table2Col3.ADD(table2Col3).IS_NOT_DISTINCT_FROM(Int(23)), "((table2.col3 + table2.col3) IS NOT DISTINCT FROM $1)", int64(23)) + AssertPostgreClauseSerialize(t, table2Col3.IS_NOT_DISTINCT_FROM(table2Col4), "(table2.col3 IS NOT DISTINCT FROM table2.col4)") + AssertPostgreClauseSerialize(t, table2Col3.ADD(table2Col3).IS_NOT_DISTINCT_FROM(Int(23)), "((table2.col3 + table2.col3) IS NOT DISTINCT FROM $1)", int64(23)) } func TestIN(t *testing.T) { - assertPostgreClauseSerialize(t, Float(1.11).IN(table1.SELECT(table1Col1)), + AssertPostgreClauseSerialize(t, Float(1.11).IN(table1.SELECT(table1Col1)), `($1 IN (( SELECT table1.col1 AS "table1.col1" FROM db.table1 )))`, float64(1.11)) - assertPostgreClauseSerialize(t, ROW(Int(12), table1Col1).IN(table2.SELECT(table2Col3, table3Col1)), + AssertPostgreClauseSerialize(t, ROW(Int(12), table1Col1).IN(table2.SELECT(table2Col3, table3Col1)), `(ROW($1, table1.col1) IN (( SELECT table2.col3 AS "table2.col3", table3.col1 AS "table3.col1" @@ -43,13 +43,13 @@ func TestIN(t *testing.T) { func TestNOT_IN(t *testing.T) { - assertPostgreClauseSerialize(t, Float(1.11).NOT_IN(table1.SELECT(table1Col1)), + AssertPostgreClauseSerialize(t, Float(1.11).NOT_IN(table1.SELECT(table1Col1)), `($1 NOT IN (( SELECT table1.col1 AS "table1.col1" FROM db.table1 )))`, float64(1.11)) - assertPostgreClauseSerialize(t, ROW(Int(12), table1Col1).NOT_IN(table2.SELECT(table2Col3, table3Col1)), + AssertPostgreClauseSerialize(t, ROW(Int(12), table1Col1).NOT_IN(table2.SELECT(table2Col3, table3Col1)), `(ROW($1, table1.col1) NOT IN (( SELECT table2.col3 AS "table2.col3", table3.col1 AS "table3.col1" diff --git a/float_expression_test.go b/float_expression_test.go index d6051da..85c553b 100644 --- a/float_expression_test.go +++ b/float_expression_test.go @@ -5,81 +5,81 @@ import ( ) func TestFloatExpressionEQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.EQ(table2ColFloat), "(table1.col_float = table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.EQ(Float(2.11)), "(table1.col_float = $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.EQ(table2ColFloat), "(table1.col_float = table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.EQ(Float(2.11)), "(table1.col_float = $1)", float64(2.11)) } func TestFloatExpressionNOT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.NOT_EQ(table2ColFloat), "(table1.col_float != table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.NOT_EQ(Float(2.11)), "(table1.col_float != $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.NOT_EQ(table2ColFloat), "(table1.col_float != table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.NOT_EQ(Float(2.11)), "(table1.col_float != $1)", float64(2.11)) } func TestFloatExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.IS_DISTINCT_FROM(table2ColFloat), "(table1.col_float IS DISTINCT FROM table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.IS_DISTINCT_FROM(Float(2.11)), "(table1.col_float IS DISTINCT FROM $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.IS_DISTINCT_FROM(table2ColFloat), "(table1.col_float IS DISTINCT FROM table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.IS_DISTINCT_FROM(Float(2.11)), "(table1.col_float IS DISTINCT FROM $1)", float64(2.11)) } func TestFloatExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.IS_NOT_DISTINCT_FROM(table2ColFloat), "(table1.col_float IS NOT DISTINCT FROM table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.IS_NOT_DISTINCT_FROM(Float(2.11)), "(table1.col_float IS NOT DISTINCT FROM $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.IS_NOT_DISTINCT_FROM(table2ColFloat), "(table1.col_float IS NOT DISTINCT FROM table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.IS_NOT_DISTINCT_FROM(Float(2.11)), "(table1.col_float IS NOT DISTINCT FROM $1)", float64(2.11)) } func TestFloatExpressionGT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.GT(table2ColFloat), "(table1.col_float > table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.GT(Float(2.11)), "(table1.col_float > $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.GT(table2ColFloat), "(table1.col_float > table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.GT(Float(2.11)), "(table1.col_float > $1)", float64(2.11)) } func TestFloatExpressionGT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.GT_EQ(table2ColFloat), "(table1.col_float >= table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.GT_EQ(Float(2.11)), "(table1.col_float >= $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.GT_EQ(table2ColFloat), "(table1.col_float >= table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.GT_EQ(Float(2.11)), "(table1.col_float >= $1)", float64(2.11)) } func TestFloatExpressionLT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.LT(table2ColFloat), "(table1.col_float < table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.LT(Float(2.11)), "(table1.col_float < $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.LT(table2ColFloat), "(table1.col_float < table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.LT(Float(2.11)), "(table1.col_float < $1)", float64(2.11)) } func TestFloatExpressionLT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.LT_EQ(table2ColFloat), "(table1.col_float <= table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.LT_EQ(Float(2.11)), "(table1.col_float <= $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.LT_EQ(table2ColFloat), "(table1.col_float <= table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.LT_EQ(Float(2.11)), "(table1.col_float <= $1)", float64(2.11)) } func TestFloatExpressionADD(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.ADD(table2ColFloat), "(table1.col_float + table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.ADD(Float(2.11)), "(table1.col_float + $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.ADD(table2ColFloat), "(table1.col_float + table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.ADD(Float(2.11)), "(table1.col_float + $1)", float64(2.11)) } func TestFloatExpressionSUB(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.SUB(table2ColFloat), "(table1.col_float - table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.SUB(Float(2.11)), "(table1.col_float - $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.SUB(table2ColFloat), "(table1.col_float - table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.SUB(Float(2.11)), "(table1.col_float - $1)", float64(2.11)) } func TestFloatExpressionMUL(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.MUL(table2ColFloat), "(table1.col_float * table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.MUL(Float(2.11)), "(table1.col_float * $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.MUL(table2ColFloat), "(table1.col_float * table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.MUL(Float(2.11)), "(table1.col_float * $1)", float64(2.11)) } func TestFloatExpressionDIV(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.col_float / table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.col_float / $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.col_float / table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.col_float / $1)", float64(2.11)) - assertMySQLClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.col_float / table2.col_float)") - assertMySQLClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.col_float / ?)", float64(2.11)) + AssertMySQLClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.col_float / table2.col_float)") + AssertMySQLClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.col_float / ?)", float64(2.11)) } func TestFloatExpressionMOD(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.MOD(table2ColFloat), "(table1.col_float % table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.MOD(Float(2.11)), "(table1.col_float % $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.MOD(table2ColFloat), "(table1.col_float % table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.MOD(Float(2.11)), "(table1.col_float % $1)", float64(2.11)) } func TestFloatExpressionPOW(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColFloat.POW(table2ColFloat), "POW(table1.col_float, table2.col_float)") - assertPostgreClauseSerialize(t, table1ColFloat.POW(Float(2.11)), "POW(table1.col_float, $1)", float64(2.11)) + AssertPostgreClauseSerialize(t, table1ColFloat.POW(table2ColFloat), "POW(table1.col_float, table2.col_float)") + AssertPostgreClauseSerialize(t, table1ColFloat.POW(Float(2.11)), "POW(table1.col_float, $1)", float64(2.11)) } func TestFloatExp(t *testing.T) { - assertPostgreClauseSerialize(t, FloatExp(table1ColInt), "table1.col_int") - assertPostgreClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)), "(table1.col_int + table3.col_int)") - assertPostgreClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)).ADD(Float(11.11)), + AssertPostgreClauseSerialize(t, FloatExp(table1ColInt), "table1.col_int") + AssertPostgreClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)), "(table1.col_int + table3.col_int)") + AssertPostgreClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)).ADD(Float(11.11)), "((table1.col_int + table3.col_int) + $1)", float64(11.11)) } diff --git a/func_expression_test.go b/func_expression_test.go index d2af26e..fc7b302 100644 --- a/func_expression_test.go +++ b/func_expression_test.go @@ -5,158 +5,158 @@ import ( ) func TestFuncAVG(t *testing.T) { - assertPostgreClauseSerialize(t, AVG(table1ColFloat), "AVG(table1.col_float)") - assertPostgreClauseSerialize(t, AVG(table1ColInt), "AVG(table1.col_int)") + AssertPostgreClauseSerialize(t, AVG(table1ColFloat), "AVG(table1.col_float)") + AssertPostgreClauseSerialize(t, AVG(table1ColInt), "AVG(table1.col_int)") } func TestFuncBIT_AND(t *testing.T) { - assertPostgreClauseSerialize(t, BIT_AND(table1ColInt), "BIT_AND(table1.col_int)") + AssertPostgreClauseSerialize(t, BIT_AND(table1ColInt), "BIT_AND(table1.col_int)") } func TestFuncBIT_OR(t *testing.T) { - assertPostgreClauseSerialize(t, BIT_OR(table1ColInt), "BIT_OR(table1.col_int)") + AssertPostgreClauseSerialize(t, BIT_OR(table1ColInt), "BIT_OR(table1.col_int)") } func TestFuncBOOL_AND(t *testing.T) { - assertPostgreClauseSerialize(t, BOOL_AND(table1ColBool), "BOOL_AND(table1.col_bool)") + AssertPostgreClauseSerialize(t, BOOL_AND(table1ColBool), "BOOL_AND(table1.col_bool)") } func TestFuncBOOL_OR(t *testing.T) { - assertPostgreClauseSerialize(t, BOOL_OR(table1ColBool), "BOOL_OR(table1.col_bool)") + AssertPostgreClauseSerialize(t, BOOL_OR(table1ColBool), "BOOL_OR(table1.col_bool)") } func TestFuncEVERY(t *testing.T) { - assertPostgreClauseSerialize(t, EVERY(table1ColBool), "EVERY(table1.col_bool)") + AssertPostgreClauseSerialize(t, EVERY(table1ColBool), "EVERY(table1.col_bool)") } func TestFuncMIN(t *testing.T) { t.Run("float", func(t *testing.T) { - assertPostgreClauseSerialize(t, MINf(table1ColFloat), "MIN(table1.col_float)") + AssertPostgreClauseSerialize(t, MINf(table1ColFloat), "MIN(table1.col_float)") }) t.Run("integer", func(t *testing.T) { - assertPostgreClauseSerialize(t, MINi(table1ColInt), "MIN(table1.col_int)") + AssertPostgreClauseSerialize(t, MINi(table1ColInt), "MIN(table1.col_int)") }) } func TestFuncMAX(t *testing.T) { t.Run("float", func(t *testing.T) { - assertPostgreClauseSerialize(t, MAXf(table1ColFloat), "MAX(table1.col_float)") - assertPostgreClauseSerialize(t, MAXf(Float(11.2222)), "MAX($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, MAXf(table1ColFloat), "MAX(table1.col_float)") + AssertPostgreClauseSerialize(t, MAXf(Float(11.2222)), "MAX($1)", float64(11.2222)) }) t.Run("integer", func(t *testing.T) { - assertPostgreClauseSerialize(t, MAXi(table1ColInt), "MAX(table1.col_int)") - assertPostgreClauseSerialize(t, MAXi(Int(11)), "MAX($1)", int64(11)) + AssertPostgreClauseSerialize(t, MAXi(table1ColInt), "MAX(table1.col_int)") + AssertPostgreClauseSerialize(t, MAXi(Int(11)), "MAX($1)", int64(11)) }) } func TestFuncSUM(t *testing.T) { t.Run("float", func(t *testing.T) { - assertPostgreClauseSerialize(t, SUMf(table1ColFloat), "SUM(table1.col_float)") - assertPostgreClauseSerialize(t, SUMf(Float(11.2222)), "SUM($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, SUMf(table1ColFloat), "SUM(table1.col_float)") + AssertPostgreClauseSerialize(t, SUMf(Float(11.2222)), "SUM($1)", float64(11.2222)) }) t.Run("integer", func(t *testing.T) { - assertPostgreClauseSerialize(t, SUMi(table1ColInt), "SUM(table1.col_int)") - assertPostgreClauseSerialize(t, SUMi(Int(11)), "SUM($1)", int64(11)) + AssertPostgreClauseSerialize(t, SUMi(table1ColInt), "SUM(table1.col_int)") + AssertPostgreClauseSerialize(t, SUMi(Int(11)), "SUM($1)", int64(11)) }) } func TestFuncCOUNT(t *testing.T) { - assertPostgreClauseSerialize(t, COUNT(STAR), "COUNT(*)") - assertPostgreClauseSerialize(t, COUNT(table1ColFloat), "COUNT(table1.col_float)") - assertPostgreClauseSerialize(t, COUNT(Float(11.2222)), "COUNT($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, COUNT(STAR), "COUNT(*)") + AssertPostgreClauseSerialize(t, COUNT(table1ColFloat), "COUNT(table1.col_float)") + AssertPostgreClauseSerialize(t, COUNT(Float(11.2222)), "COUNT($1)", float64(11.2222)) } func TestFuncABS(t *testing.T) { t.Run("float", func(t *testing.T) { - assertPostgreClauseSerialize(t, ABSf(table1ColFloat), "ABS(table1.col_float)") - assertPostgreClauseSerialize(t, ABSf(Float(11.2222)), "ABS($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, ABSf(table1ColFloat), "ABS(table1.col_float)") + AssertPostgreClauseSerialize(t, ABSf(Float(11.2222)), "ABS($1)", float64(11.2222)) }) t.Run("integer", func(t *testing.T) { - assertPostgreClauseSerialize(t, ABSi(table1ColInt), "ABS(table1.col_int)") - assertPostgreClauseSerialize(t, ABSi(Int(11)), "ABS($1)", int64(11)) + AssertPostgreClauseSerialize(t, ABSi(table1ColInt), "ABS(table1.col_int)") + AssertPostgreClauseSerialize(t, ABSi(Int(11)), "ABS($1)", int64(11)) }) } func TestFuncSQRT(t *testing.T) { - assertPostgreClauseSerialize(t, SQRT(table1ColFloat), "SQRT(table1.col_float)") - assertPostgreClauseSerialize(t, SQRT(Float(11.2222)), "SQRT($1)", float64(11.2222)) - assertPostgreClauseSerialize(t, SQRT(table1ColInt), "SQRT(table1.col_int)") - assertPostgreClauseSerialize(t, SQRT(Int(11)), "SQRT($1)", int64(11)) + AssertPostgreClauseSerialize(t, SQRT(table1ColFloat), "SQRT(table1.col_float)") + AssertPostgreClauseSerialize(t, SQRT(Float(11.2222)), "SQRT($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, SQRT(table1ColInt), "SQRT(table1.col_int)") + AssertPostgreClauseSerialize(t, SQRT(Int(11)), "SQRT($1)", int64(11)) } func TestFuncCBRT(t *testing.T) { - assertPostgreClauseSerialize(t, CBRT(table1ColFloat), "CBRT(table1.col_float)") - assertPostgreClauseSerialize(t, CBRT(Float(11.2222)), "CBRT($1)", float64(11.2222)) - assertPostgreClauseSerialize(t, CBRT(table1ColInt), "CBRT(table1.col_int)") - assertPostgreClauseSerialize(t, CBRT(Int(11)), "CBRT($1)", int64(11)) + AssertPostgreClauseSerialize(t, CBRT(table1ColFloat), "CBRT(table1.col_float)") + AssertPostgreClauseSerialize(t, CBRT(Float(11.2222)), "CBRT($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, CBRT(table1ColInt), "CBRT(table1.col_int)") + AssertPostgreClauseSerialize(t, CBRT(Int(11)), "CBRT($1)", int64(11)) } func TestFuncCEIL(t *testing.T) { - assertPostgreClauseSerialize(t, CEIL(table1ColFloat), "CEIL(table1.col_float)") - assertPostgreClauseSerialize(t, CEIL(Float(11.2222)), "CEIL($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, CEIL(table1ColFloat), "CEIL(table1.col_float)") + AssertPostgreClauseSerialize(t, CEIL(Float(11.2222)), "CEIL($1)", float64(11.2222)) } func TestFuncFLOOR(t *testing.T) { - assertPostgreClauseSerialize(t, FLOOR(table1ColFloat), "FLOOR(table1.col_float)") - assertPostgreClauseSerialize(t, FLOOR(Float(11.2222)), "FLOOR($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, FLOOR(table1ColFloat), "FLOOR(table1.col_float)") + AssertPostgreClauseSerialize(t, FLOOR(Float(11.2222)), "FLOOR($1)", float64(11.2222)) } func TestFuncROUND(t *testing.T) { - assertPostgreClauseSerialize(t, ROUND(table1ColFloat), "ROUND(table1.col_float)") - assertPostgreClauseSerialize(t, ROUND(Float(11.2222)), "ROUND($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, ROUND(table1ColFloat), "ROUND(table1.col_float)") + AssertPostgreClauseSerialize(t, ROUND(Float(11.2222)), "ROUND($1)", float64(11.2222)) - assertPostgreClauseSerialize(t, ROUND(table1ColFloat, Int(2)), "ROUND(table1.col_float, $1)", int64(2)) - assertPostgreClauseSerialize(t, ROUND(Float(11.2222), Int(1)), "ROUND($1, $2)", float64(11.2222), int64(1)) + AssertPostgreClauseSerialize(t, ROUND(table1ColFloat, Int(2)), "ROUND(table1.col_float, $1)", int64(2)) + AssertPostgreClauseSerialize(t, ROUND(Float(11.2222), Int(1)), "ROUND($1, $2)", float64(11.2222), int64(1)) } func TestFuncSIGN(t *testing.T) { - assertPostgreClauseSerialize(t, SIGN(table1ColFloat), "SIGN(table1.col_float)") - assertPostgreClauseSerialize(t, SIGN(Float(11.2222)), "SIGN($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, SIGN(table1ColFloat), "SIGN(table1.col_float)") + AssertPostgreClauseSerialize(t, SIGN(Float(11.2222)), "SIGN($1)", float64(11.2222)) } func TestFuncTRUNC(t *testing.T) { - assertPostgreClauseSerialize(t, TRUNC(table1ColFloat), "TRUNC(table1.col_float)") - assertPostgreClauseSerialize(t, TRUNC(Float(11.2222)), "TRUNC($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, TRUNC(table1ColFloat), "TRUNC(table1.col_float)") + AssertPostgreClauseSerialize(t, TRUNC(Float(11.2222)), "TRUNC($1)", float64(11.2222)) - assertPostgreClauseSerialize(t, TRUNC(table1ColFloat, Int(2)), "TRUNC(table1.col_float, $1)", int64(2)) - assertPostgreClauseSerialize(t, TRUNC(Float(11.2222), Int(1)), "TRUNC($1, $2)", float64(11.2222), int64(1)) + AssertPostgreClauseSerialize(t, TRUNC(table1ColFloat, Int(2)), "TRUNC(table1.col_float, $1)", int64(2)) + AssertPostgreClauseSerialize(t, TRUNC(Float(11.2222), Int(1)), "TRUNC($1, $2)", float64(11.2222), int64(1)) } func TestFuncLN(t *testing.T) { - assertPostgreClauseSerialize(t, LN(table1ColFloat), "LN(table1.col_float)") - assertPostgreClauseSerialize(t, LN(Float(11.2222)), "LN($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, LN(table1ColFloat), "LN(table1.col_float)") + AssertPostgreClauseSerialize(t, LN(Float(11.2222)), "LN($1)", float64(11.2222)) } func TestFuncLOG(t *testing.T) { - assertPostgreClauseSerialize(t, LOG(table1ColFloat), "LOG(table1.col_float)") - assertPostgreClauseSerialize(t, LOG(Float(11.2222)), "LOG($1)", float64(11.2222)) + AssertPostgreClauseSerialize(t, LOG(table1ColFloat), "LOG(table1.col_float)") + AssertPostgreClauseSerialize(t, LOG(Float(11.2222)), "LOG($1)", float64(11.2222)) } func TestFuncCOALESCE(t *testing.T) { - assertPostgreClauseSerialize(t, COALESCE(table1ColFloat), "COALESCE(table1.col_float)") - assertPostgreClauseSerialize(t, COALESCE(Float(11.2222), NULL, String("str")), "COALESCE($1, NULL, $2)", float64(11.2222), "str") + AssertPostgreClauseSerialize(t, COALESCE(table1ColFloat), "COALESCE(table1.col_float)") + AssertPostgreClauseSerialize(t, COALESCE(Float(11.2222), NULL, String("str")), "COALESCE($1, NULL, $2)", float64(11.2222), "str") } func TestFuncNULLIF(t *testing.T) { - assertPostgreClauseSerialize(t, NULLIF(table1ColFloat, table2ColInt), "NULLIF(table1.col_float, table2.col_int)") - assertPostgreClauseSerialize(t, NULLIF(Float(11.2222), NULL), "NULLIF($1, NULL)", float64(11.2222)) + AssertPostgreClauseSerialize(t, NULLIF(table1ColFloat, table2ColInt), "NULLIF(table1.col_float, table2.col_int)") + AssertPostgreClauseSerialize(t, NULLIF(Float(11.2222), NULL), "NULLIF($1, NULL)", float64(11.2222)) } func TestFuncGREATEST(t *testing.T) { - assertPostgreClauseSerialize(t, GREATEST(table1ColFloat), "GREATEST(table1.col_float)") - assertPostgreClauseSerialize(t, GREATEST(Float(11.2222), NULL, String("str")), "GREATEST($1, NULL, $2)", float64(11.2222), "str") + AssertPostgreClauseSerialize(t, GREATEST(table1ColFloat), "GREATEST(table1.col_float)") + AssertPostgreClauseSerialize(t, GREATEST(Float(11.2222), NULL, String("str")), "GREATEST($1, NULL, $2)", float64(11.2222), "str") } func TestFuncLEAST(t *testing.T) { - assertPostgreClauseSerialize(t, LEAST(table1ColFloat), "LEAST(table1.col_float)") - assertPostgreClauseSerialize(t, LEAST(Float(11.2222), NULL, String("str")), "LEAST($1, NULL, $2)", float64(11.2222), "str") + AssertPostgreClauseSerialize(t, LEAST(table1ColFloat), "LEAST(table1.col_float)") + AssertPostgreClauseSerialize(t, LEAST(Float(11.2222), NULL, String("str")), "LEAST($1, NULL, $2)", float64(11.2222), "str") } func TestTO_ASCII(t *testing.T) { - assertPostgreClauseSerialize(t, TO_ASCII(String("Karel")), `TO_ASCII($1)`, "Karel") - assertPostgreClauseSerialize(t, TO_ASCII(String("Karel")), `TO_ASCII($1)`, "Karel") + AssertPostgreClauseSerialize(t, TO_ASCII(String("Karel")), `TO_ASCII($1)`, "Karel") + AssertPostgreClauseSerialize(t, TO_ASCII(String("Karel")), `TO_ASCII($1)`, "Karel") } diff --git a/integer_expression_test.go b/integer_expression_test.go index 341a094..c750ed9 100644 --- a/integer_expression_test.go +++ b/integer_expression_test.go @@ -5,114 +5,114 @@ import ( ) func TestIntegerExpressionEQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.EQ(table2ColInt), "(table1.col_int = table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.EQ(Int(11)), "(table1.col_int = $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.EQ(table2ColInt), "(table1.col_int = table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.EQ(Int(11)), "(table1.col_int = $1)", int64(11)) } func TestIntegerExpressionNOT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.NOT_EQ(table2ColInt), "(table1.col_int != table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.NOT_EQ(Int(11)), "(table1.col_int != $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.NOT_EQ(table2ColInt), "(table1.col_int != table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.NOT_EQ(Int(11)), "(table1.col_int != $1)", int64(11)) } func TestIntegerExpressionGT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.GT(table2ColInt), "(table1.col_int > table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.GT(Int(11)), "(table1.col_int > $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.GT(table2ColInt), "(table1.col_int > table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.GT(Int(11)), "(table1.col_int > $1)", int64(11)) } func TestIntegerExpressionGT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.GT_EQ(table2ColInt), "(table1.col_int >= table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.GT_EQ(Int(11)), "(table1.col_int >= $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.GT_EQ(table2ColInt), "(table1.col_int >= table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.GT_EQ(Int(11)), "(table1.col_int >= $1)", int64(11)) } func TestIntegerExpressionLT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.LT(table2ColInt), "(table1.col_int < table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.LT(Int(11)), "(table1.col_int < $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.LT(table2ColInt), "(table1.col_int < table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.LT(Int(11)), "(table1.col_int < $1)", int64(11)) } func TestIntegerExpressionLT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.LT_EQ(table2ColInt), "(table1.col_int <= table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.LT_EQ(Int(11)), "(table1.col_int <= $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.LT_EQ(table2ColInt), "(table1.col_int <= table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.LT_EQ(Int(11)), "(table1.col_int <= $1)", int64(11)) } func TestIntegerExpressionADD(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.ADD(table2ColInt), "(table1.col_int + table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.ADD(Int(11)), "(table1.col_int + $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.ADD(table2ColInt), "(table1.col_int + table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.ADD(Int(11)), "(table1.col_int + $1)", int64(11)) } func TestIntegerExpressionSUB(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.SUB(table2ColInt), "(table1.col_int - table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.SUB(Int(11)), "(table1.col_int - $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.SUB(table2ColInt), "(table1.col_int - table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.SUB(Int(11)), "(table1.col_int - $1)", int64(11)) } func TestIntegerExpressionMUL(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.MUL(table2ColInt), "(table1.col_int * table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.MUL(Int(11)), "(table1.col_int * $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.MUL(table2ColInt), "(table1.col_int * table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.MUL(Int(11)), "(table1.col_int * $1)", int64(11)) } func TestIntegerExpressionDIV(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int / table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int / $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int / table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int / $1)", int64(11)) - assertMySQLClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int DIV table2.col_int)") - assertMySQLClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int DIV ?)", int64(11)) + AssertMySQLClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int DIV table2.col_int)") + AssertMySQLClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int DIV ?)", int64(11)) } func TestIntExpressionMOD(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.MOD(table2ColInt), "(table1.col_int % table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.MOD(Int(11)), "(table1.col_int % $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.MOD(table2ColInt), "(table1.col_int % table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.MOD(Int(11)), "(table1.col_int % $1)", int64(11)) } func TestIntExpressionPOW(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.POW(table2ColInt), "POW(table1.col_int, table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.POW(Int(11)), "POW(table1.col_int, $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.POW(table2ColInt), "POW(table1.col_int, table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.POW(Int(11)), "POW(table1.col_int, $1)", int64(11)) } func TestIntExpressionBIT_NOT(t *testing.T) { - assertPostgreClauseSerialize(t, BIT_NOT(table2ColInt), "(~ table2.col_int)") - assertPostgreClauseSerialize(t, BIT_NOT(Int(11)), "(~ $1)", int64(11)) + AssertPostgreClauseSerialize(t, BIT_NOT(table2ColInt), "(~ table2.col_int)") + AssertPostgreClauseSerialize(t, BIT_NOT(Int(11)), "(~ $1)", int64(11)) } func TestIntExpressionBIT_AND(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.BIT_AND(table2ColInt), "(table1.col_int & table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.BIT_AND(Int(11)), "(table1.col_int & $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.BIT_AND(table2ColInt), "(table1.col_int & table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.BIT_AND(Int(11)), "(table1.col_int & $1)", int64(11)) - assertMySQLClauseSerialize(t, table1ColInt.BIT_AND(table2ColInt), "(table1.col_int & table2.col_int)") - assertMySQLClauseSerialize(t, table1ColInt.BIT_AND(Int(11)), "(table1.col_int & ?)", int64(11)) + AssertMySQLClauseSerialize(t, table1ColInt.BIT_AND(table2ColInt), "(table1.col_int & table2.col_int)") + AssertMySQLClauseSerialize(t, table1ColInt.BIT_AND(Int(11)), "(table1.col_int & ?)", int64(11)) } func TestIntExpressionBIT_OR(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.BIT_OR(table2ColInt), "(table1.col_int | table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.BIT_OR(Int(11)), "(table1.col_int | $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.BIT_OR(table2ColInt), "(table1.col_int | table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.BIT_OR(Int(11)), "(table1.col_int | $1)", int64(11)) - assertMySQLClauseSerialize(t, table1ColInt.BIT_OR(table2ColInt), "(table1.col_int | table2.col_int)") - assertMySQLClauseSerialize(t, table1ColInt.BIT_OR(Int(11)), "(table1.col_int | ?)", int64(11)) + AssertMySQLClauseSerialize(t, table1ColInt.BIT_OR(table2ColInt), "(table1.col_int | table2.col_int)") + AssertMySQLClauseSerialize(t, table1ColInt.BIT_OR(Int(11)), "(table1.col_int | ?)", int64(11)) } func TestIntExpressionBIT_XOR(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.BIT_XOR(table2ColInt), "(table1.col_int # table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.BIT_XOR(Int(11)), "(table1.col_int # $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.BIT_XOR(table2ColInt), "(table1.col_int # table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.BIT_XOR(Int(11)), "(table1.col_int # $1)", int64(11)) - assertMySQLClauseSerialize(t, table1ColInt.BIT_XOR(table2ColInt), "(table1.col_int ^ table2.col_int)") - assertMySQLClauseSerialize(t, table1ColInt.BIT_XOR(Int(11)), "(table1.col_int ^ ?)", int64(11)) + AssertMySQLClauseSerialize(t, table1ColInt.BIT_XOR(table2ColInt), "(table1.col_int ^ table2.col_int)") + AssertMySQLClauseSerialize(t, table1ColInt.BIT_XOR(Int(11)), "(table1.col_int ^ ?)", int64(11)) } func TestIntExpressionBIT_SHIFT_LEFT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.BIT_SHIFT_LEFT(table2ColInt), "(table1.col_int << table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.BIT_SHIFT_LEFT(Int(11)), "(table1.col_int << $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.BIT_SHIFT_LEFT(table2ColInt), "(table1.col_int << table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.BIT_SHIFT_LEFT(Int(11)), "(table1.col_int << $1)", int64(11)) } func TestIntExpressionBIT_SHIFT_RIGHT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(table2ColInt), "(table1.col_int >> table2.col_int)") - assertPostgreClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(Int(11)), "(table1.col_int >> $1)", int64(11)) + AssertPostgreClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(table2ColInt), "(table1.col_int >> table2.col_int)") + AssertPostgreClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(Int(11)), "(table1.col_int >> $1)", int64(11)) } func TestIntExpressionIntExp(t *testing.T) { - assertPostgreClauseSerialize(t, IntExp(table1ColFloat), "table1.col_float") - assertPostgreClauseSerialize(t, IntExp(table1ColFloat.ADD(table2ColFloat)).ADD(Int(11)), + AssertPostgreClauseSerialize(t, IntExp(table1ColFloat), "table1.col_float") + AssertPostgreClauseSerialize(t, IntExp(table1ColFloat.ADD(table2ColFloat)).ADD(Int(11)), "((table1.col_float + table2.col_float) + $1)", int64(11)) } func TestIntExpression_MINUSi(t *testing.T) { - assertPostgreClauseSerialize(t, MINUSi(table2ColInt), "(- table2.col_int)") - assertPostgreClauseSerialize(t, MINUSi(Int(3)), "(- $1)", int64(3)) + AssertPostgreClauseSerialize(t, MINUSi(table2ColInt), "(- table2.col_int)") + AssertPostgreClauseSerialize(t, MINUSi(Int(3)), "(- $1)", int64(3)) } diff --git a/internal/testutils/test_utils.go b/internal/testutils/test_utils.go index afc84f8..3972de2 100644 --- a/internal/testutils/test_utils.go +++ b/internal/testutils/test_utils.go @@ -73,6 +73,16 @@ func AssertDebugStatementSql(t *testing.T, query jet.Statement, expectedQuery st assert.Equal(t, debuqSql, expectedQuery) } +func Date(t string) *time.Time { + newTime, err := time.Parse("2006-01-02", t) + + if err != nil { + panic(err) + } + + return &newTime +} + func TimestampWithoutTimeZone(t string, precision int) *time.Time { precisionStr := "" @@ -89,3 +99,40 @@ func TimestampWithoutTimeZone(t string, precision int) *time.Time { return &newTime } + +func TimeWithoutTimeZone(t string) *time.Time { + newTime, err := time.Parse("15:04:05", t) + + if err != nil { + panic(err) + } + + return &newTime +} + +func TimeWithTimeZone(t string) *time.Time { + newTimez, err := time.Parse("15:04:05 -0700", t) + + if err != nil { + panic(err) + } + + return &newTimez +} + +func TimestampWithTimeZone(t string, precision int) *time.Time { + + precisionStr := "" + + if precision > 0 { + precisionStr = "." + strings.Repeat("9", precision) + } + + newTime, err := time.Parse("2006-01-02 15:04:05"+precisionStr+" -0700 MST", t) + + if err != nil { + panic(err) + } + + return &newTime +} diff --git a/literal_expression.go b/literal_expression.go index 1959018..5f688d0 100644 --- a/literal_expression.go +++ b/literal_expression.go @@ -117,7 +117,7 @@ func Time(hour, minute, second, milliseconds int) TimeExpression { timeLiteral.timeInterfaceImpl.parent = timeLiteral - return CAST(timeLiteral).AS_TIME() + return timeLiteral } //---------------------------------------------------// @@ -134,7 +134,7 @@ func Timez(hour, minute, second, milliseconds, timezone int) TimezExpression { timezLiteral.timezInterfaceImpl.parent = timezLiteral - return CAST(timezLiteral).AS_TIMEZ() + return timezLiteral } //---------------------------------------------------// @@ -151,7 +151,7 @@ func Timestamp(year, month, day, hour, minute, second, milliseconds int) Timesta timestampLiteral.timestampInterfaceImpl.parent = timestampLiteral - return CAST(timestampLiteral).AS_TIMESTAMP() + return timestampLiteral } //---------------------------------------------------// @@ -170,7 +170,7 @@ func Timestampz(year, month, day, hour, minute, second, milliseconds, timezone i timestampzLiteral.timestampzInterfaceImpl.parent = timestampzLiteral - return CAST(timestampzLiteral).AS_TIMESTAMPZ() + return timestampzLiteral } //---------------------------------------------------// @@ -187,7 +187,7 @@ func Date(year, month, day int) DateExpression { dateLiteral.literalExpression = *literal(timeStr) dateLiteral.dateInterfaceImpl.parent = dateLiteral - return CAST(dateLiteral).AS_DATE() + return dateLiteral } //--------------------------------------------------// diff --git a/literal_expression_test.go b/literal_expression_test.go index 466f6b9..1d6b807 100644 --- a/literal_expression_test.go +++ b/literal_expression_test.go @@ -3,5 +3,5 @@ package jet import "testing" func TestRawExpression(t *testing.T) { - assertPostgreClauseSerialize(t, RAW("current_database()"), "current_database()") + AssertPostgreClauseSerialize(t, RAW("current_database()"), "current_database()") } diff --git a/mysql/mysql_cast.go b/mysql/mysql_cast.go new file mode 100644 index 0000000..7667f75 --- /dev/null +++ b/mysql/mysql_cast.go @@ -0,0 +1,55 @@ +package mysql + +import ( + "github.com/go-jet/jet" +) + +type cast interface { + AS_DATE() DateExpression + AS_TIME() TimeExpression + AS_DATETIME() DateTimeExpression + AS_CHAR() StringExpression + AS_SIGNED() IntegerExpression + AS_UNSIGNED() IntegerExpression + AS_BINARY() StringExpression +} + +type castImpl struct { + jet.Cast +} + +func CAST(expr jet.Expression) cast { + castImpl := &castImpl{} + + castImpl.Cast = jet.NewCastImpl(expr) + + return castImpl +} + +func (c *castImpl) AS_DATE() DateExpression { + return jet.DateExp(c.As("DATE")) +} + +func (c *castImpl) AS_DATETIME() DateTimeExpression { + return jet.TimestampExp(c.As("DATETIME")) +} + +func (c *castImpl) AS_TIME() TimeExpression { + return jet.TimeExp(c.As("TIME")) +} + +func (c *castImpl) AS_CHAR() StringExpression { + return jet.StringExp(c.As("CHAR")) +} + +func (c *castImpl) AS_SIGNED() IntegerExpression { + return jet.IntExp(c.As("SIGNED")) +} + +func (c *castImpl) AS_UNSIGNED() IntegerExpression { + return jet.IntExp(c.As("UNSIGNED")) +} + +func (c *castImpl) AS_BINARY() StringExpression { + return jet.StringExp(c.As("BINARY")) +} diff --git a/mysql/mysql_cast_test.go b/mysql/mysql_cast_test.go new file mode 100644 index 0000000..bf544a1 --- /dev/null +++ b/mysql/mysql_cast_test.go @@ -0,0 +1,11 @@ +package mysql + +import ( + "github.com/go-jet/jet" + "testing" +) + +func TestCAST_AS_DATE(t *testing.T) { + + jet.AssertMySQLClauseSerialize(t, CAST(Int(22)).AS_DATE(), `CAST(? AS DATE)`, int64(22)) +} diff --git a/mysql/mysql_types.go b/mysql/mysql_types.go index a91ba97..0b12069 100644 --- a/mysql/mysql_types.go +++ b/mysql/mysql_types.go @@ -3,41 +3,48 @@ package mysql import "github.com/go-jet/jet" type ColumnBool jet.ColumnBool +type BoolExpression jet.BoolExpression var BoolColumn = jet.BoolColumn var Bool = jet.Bool type ColumnString jet.ColumnString +type StringExpression jet.StringExpression var StringColumn = jet.StringColumn var String = jet.String type ColumnInteger jet.ColumnInteger +type IntegerExpression jet.IntegerExpression var IntegerColumn = jet.IntegerColumn var Int = jet.Int type ColumnFloat jet.ColumnFloat +type FloatExpression jet.FloatExpression var FloatColumn = jet.FloatColumn var Float = jet.Float type ColumnDate jet.ColumnDate +type DateExpression jet.DateExpression var DateColumn = jet.DateColumn var Date = jet.Date type ColumnDateTime jet.ColumnTimestamp +type DateTimeExpression jet.TimestampExpression var DateTimeColumn = jet.TimestampColumn var DateTime = jet.Timestamp type ColumnTimestamp jet.ColumnTimestamp +type TimestampExpression jet.TimestampExpression var TimestampColumn = jet.TimestampColumn var Timestamp = jet.Timestamp -var CAST = jet.CAST +type TimeExpression jet.TimeExpression // ----------------- FUNCTIONS ----------------------// @@ -63,3 +70,5 @@ var TRUNCATE = func(floatExpression jet.FloatExpression, precision jet.IntegerEx var MINUSi = jet.MINUSi var MINUSf = jet.MINUSf var BIT_NOT = jet.BIT_NOT + +var SELECT = jet.SELECT diff --git a/operators_test.go b/operators_test.go index 4272218..4f517d6 100644 --- a/operators_test.go +++ b/operators_test.go @@ -5,10 +5,10 @@ import "testing" func TestOperatorNOT(t *testing.T) { notExpression := NOT(Int(2).EQ(Int(1))) - assertPostgreClauseSerialize(t, NOT(table1ColBool), "(NOT table1.col_bool)") - assertPostgreClauseSerialize(t, notExpression, "(NOT ($1 = $2))", int64(2), int64(1)) + AssertPostgreClauseSerialize(t, NOT(table1ColBool), "(NOT table1.col_bool)") + AssertPostgreClauseSerialize(t, notExpression, "(NOT ($1 = $2))", int64(2), int64(1)) assertProjectionSerialize(t, notExpression.AS("alias_not_expression"), `(NOT ($1 = $2)) AS "alias_not_expression"`, int64(2), int64(1)) - assertPostgreClauseSerialize(t, notExpression.AND(Int(4).EQ(Int(5))), `((NOT ($1 = $2)) AND ($3 = $4))`, int64(2), int64(1), int64(4), int64(5)) + AssertPostgreClauseSerialize(t, notExpression.AND(Int(4).EQ(Int(5))), `((NOT ($1 = $2)) AND ($3 = $4))`, int64(2), int64(1), int64(4), int64(5)) } func TestCase1(t *testing.T) { @@ -16,7 +16,7 @@ func TestCase1(t *testing.T) { WHEN(table3Col1.EQ(Int(1))).THEN(table3Col1.ADD(Int(1))). WHEN(table3Col1.EQ(Int(2))).THEN(table3Col1.ADD(Int(2))) - assertPostgreClauseSerialize(t, query, `(CASE WHEN table3.col1 = $1 THEN table3.col1 + $2 WHEN table3.col1 = $3 THEN table3.col1 + $4 END)`, + AssertPostgreClauseSerialize(t, query, `(CASE WHEN table3.col1 = $1 THEN table3.col1 + $2 WHEN table3.col1 = $3 THEN table3.col1 + $4 END)`, int64(1), int64(1), int64(2), int64(2)) } @@ -26,6 +26,6 @@ func TestCase2(t *testing.T) { WHEN(Int(2)).THEN(table3Col1.ADD(Int(2))). ELSE(Int(0)) - assertPostgreClauseSerialize(t, query, `(CASE table3.col1 WHEN $1 THEN table3.col1 + $2 WHEN $3 THEN table3.col1 + $4 ELSE $5 END)`, + AssertPostgreClauseSerialize(t, query, `(CASE table3.col1 WHEN $1 THEN table3.col1 + $2 WHEN $3 THEN table3.col1 + $4 ELSE $5 END)`, int64(1), int64(1), int64(2), int64(2), int64(0)) } diff --git a/postgres/date_expression_test.go b/postgres/date_expression_test.go new file mode 100644 index 0000000..56ca426 --- /dev/null +++ b/postgres/date_expression_test.go @@ -0,0 +1,48 @@ +package postgres + +import ( + "github.com/go-jet/jet" + "testing" +) + +var dateVar = Date(2000, 12, 30) + +func TestDateExpressionEQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColDate.EQ(table2ColDate), "(table1.col_date = table2.col_date)") + jet.AssertPostgreClauseSerialize(t, table1ColDate.EQ(dateVar), "(table1.col_date = $1::date)", "2000-12-30") +} + +func TestDateExpressionNOT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColDate.NOT_EQ(table2ColDate), "(table1.col_date != table2.col_date)") + jet.AssertPostgreClauseSerialize(t, table1ColDate.NOT_EQ(dateVar), "(table1.col_date != $1::date)", "2000-12-30") +} + +func TestDateExpressionIS_DISTINCT_FROM(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_DISTINCT_FROM(table2ColDate), "(table1.col_date IS DISTINCT FROM table2.col_date)") + jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_DISTINCT_FROM(dateVar), "(table1.col_date IS DISTINCT FROM $1::date)", "2000-12-30") +} + +func TestDateExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_NOT_DISTINCT_FROM(table2ColDate), "(table1.col_date IS NOT DISTINCT FROM table2.col_date)") + jet.AssertPostgreClauseSerialize(t, table1ColDate.IS_NOT_DISTINCT_FROM(dateVar), "(table1.col_date IS NOT DISTINCT FROM $1::date)", "2000-12-30") +} + +func TestDateExpressionGT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColDate.GT(table2ColDate), "(table1.col_date > table2.col_date)") + jet.AssertPostgreClauseSerialize(t, table1ColDate.GT(dateVar), "(table1.col_date > $1::date)", "2000-12-30") +} + +func TestDateExpressionGT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColDate.GT_EQ(table2ColDate), "(table1.col_date >= table2.col_date)") + jet.AssertPostgreClauseSerialize(t, table1ColDate.GT_EQ(dateVar), "(table1.col_date >= $1::date)", "2000-12-30") +} + +func TestDateExpressionLT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColDate.LT(table2ColDate), "(table1.col_date < table2.col_date)") + jet.AssertPostgreClauseSerialize(t, table1ColDate.LT(dateVar), "(table1.col_date < $1::date)", "2000-12-30") +} + +func TestDateExpressionLT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColDate.LT_EQ(table2ColDate), "(table1.col_date <= table2.col_date)") + jet.AssertPostgreClauseSerialize(t, table1ColDate.LT_EQ(dateVar), "(table1.col_date <= $1::date)", "2000-12-30") +} diff --git a/postgres/postgres_cast.go b/postgres/postgres_cast.go new file mode 100644 index 0000000..ae83ed1 --- /dev/null +++ b/postgres/postgres_cast.go @@ -0,0 +1,124 @@ +package postgres + +import ( + "fmt" + "github.com/go-jet/jet" +) + +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 numeric type, using precision and optionally scale + AS_DECIMAL() 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 { + jet.Cast +} + +func CAST(expr jet.Expression) cast { + castImpl := &castImpl{} + + castImpl.Cast = jet.NewCastImpl(expr) + + return castImpl +} + +func (b *castImpl) AS_BOOL() BoolExpression { + return jet.BoolExp(b.As("boolean")) +} + +func (b *castImpl) AS_SMALLINT() IntegerExpression { + return jet.IntExp(b.As("smallint")) +} + +// Cast expression AS integer type +func (b *castImpl) AS_INTEGER() IntegerExpression { + return jet.IntExp(b.As("integer")) +} + +// Cast expression AS bigint type +func (b *castImpl) AS_BIGINT() IntegerExpression { + return jet.IntExp(b.As("bigint")) +} + +// Cast expression AS numeric type, using precision and optionally scale +func (b *castImpl) AS_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 jet.FloatExp(b.As(jet.CastType(castType))) +} + +func (b *castImpl) AS_DECIMAL() FloatExpression { + return jet.FloatExp(b.As("decimal")) +} + +// Cast expression AS real type +func (b *castImpl) AS_REAL() FloatExpression { + return jet.FloatExp(b.As("real")) +} + +// Cast expression AS double precision type +func (b *castImpl) AS_DOUBLE() FloatExpression { + return jet.FloatExp(b.As("double precision")) +} + +// Cast expression AS text type +func (b *castImpl) AS_TEXT() StringExpression { + return jet.StringExp(b.As("text")) +} + +// Cast expression AS date type +func (b *castImpl) AS_DATE() DateExpression { + return jet.DateExp(b.As("date")) +} + +// Cast expression AS time type +func (b *castImpl) AS_TIME() TimeExpression { + return jet.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")) +} + +// Cast expression AS timestamp type +func (b *castImpl) AS_TIMESTAMP() TimestampExpression { + return jet.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")) +} diff --git a/postgres/postgres_cast_test.go b/postgres/postgres_cast_test.go new file mode 100644 index 0000000..725fb8d --- /dev/null +++ b/postgres/postgres_cast_test.go @@ -0,0 +1,61 @@ +package postgres + +import ( + "github.com/go-jet/jet" + "testing" +) + +func TestExpressionCAST_AS_BOOL(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(Int(1)).AS_BOOL(), "$1::boolean", int64(1)) + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_BOOL(), "table2.col3::boolean") + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3.ADD(table2Col3)).AS_BOOL(), "(table2.col3 + table2.col3)::boolean") +} + +func TestExpressionCAST_AS_SMALLINT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_SMALLINT(), "table2.col3::smallint") +} + +func TestExpressionCAST_AS_INTEGER(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_INTEGER(), "table2.col3::integer") +} + +func TestExpressionCAST_AS_BIGINT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_BIGINT(), "table2.col3::bigint") +} + +func TestExpressionCAST_AS_NUMERIC(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_NUMERIC(11, 11), "table2.col3::numeric(11, 11)") + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_NUMERIC(11), "table2.col3::numeric(11)") +} + +func TestExpressionCAST_AS_REAL(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_REAL(), "table2.col3::real") +} + +func TestExpressionCAST_AS_DOUBLE(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_DOUBLE(), "table2.col3::double precision") +} + +func TestExpressionCAST_AS_TEXT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_TEXT(), "table2.col3::text") +} + +func TestExpressionCAST_AS_DATE(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_DATE(), "table2.col3::date") +} + +func TestExpressionCAST_AS_TIME(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_TIME(), "table2.col3::time without time zone") +} + +func TestExpressionCAST_AS_TIMEZ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_TIMEZ(), "table2.col3::time with time zone") +} + +func TestExpressionCAST_AS_TIMESTAMP(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_TIMESTAMP(), "table2.col3::timestamp without time zone") +} + +func TestExpressionCAST_AS_TIMESTAMPZ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, CAST(table2Col3).AS_TIMESTAMPZ(), "table2.col3::timestamp with time zone") +} diff --git a/postgres/postgres_select_test.go b/postgres/postgres_select_test.go new file mode 100644 index 0000000..d3cc49d --- /dev/null +++ b/postgres/postgres_select_test.go @@ -0,0 +1,30 @@ +package postgres + +import ( + "github.com/go-jet/jet/internal/testutils" + "testing" +) + +func TestSelectLock(t *testing.T) { + testutils.AssertStatementSql(t, SELECT(table1ColBool).FROM(table1).FOR(UPDATE()), ` +SELECT table1.col_bool AS "table1.col_bool" +FROM db.table1 +FOR UPDATE; +`) + testutils.AssertStatementSql(t, SELECT(table1ColBool).FROM(table1).FOR(SHARE().NOWAIT()), ` +SELECT table1.col_bool AS "table1.col_bool" +FROM db.table1 +FOR SHARE NOWAIT; +`) + + testutils.AssertStatementSql(t, SELECT(table1ColBool).FROM(table1).FOR(KEY_SHARE().NOWAIT()), ` +SELECT table1.col_bool AS "table1.col_bool" +FROM db.table1 +FOR KEY SHARE NOWAIT; +`) + testutils.AssertStatementSql(t, SELECT(table1ColBool).FROM(table1).FOR(NO_KEY_UPDATE().SKIP_LOCKED()), ` +SELECT table1.col_bool AS "table1.col_bool" +FROM db.table1 +FOR NO KEY UPDATE SKIP LOCKED; +`) +} diff --git a/postgres/postgres_types.go b/postgres/postgres_types.go index 92d6456..e9340be 100644 --- a/postgres/postgres_types.go +++ b/postgres/postgres_types.go @@ -3,37 +3,102 @@ package postgres import "github.com/go-jet/jet" type ColumnBool jet.ColumnBool +type BoolExpression jet.BoolExpression var BoolColumn = jet.BoolColumn +var Bool = jet.Bool type ColumnString jet.ColumnString +type StringExpression jet.StringExpression var StringColumn = jet.StringColumn +var String = jet.String type ColumnInteger jet.ColumnInteger +type IntegerExpression jet.IntegerExpression var IntegerColumn = jet.IntegerColumn +var Int = jet.Int type ColumnFloat jet.ColumnFloat +type FloatExpression jet.FloatExpression var FloatColumn = jet.FloatColumn +var Float = jet.Float +var FloatExp = jet.FloatExp type ColumnDate jet.ColumnDate +type DateExpression jet.DateExpression var DateColumn = jet.DateColumn +var Date = func(year, month, day int) DateExpression { + return CAST(jet.Date(year, month, day)).AS_DATE() +} +type ColumnDateTime jet.ColumnTimestamp +type DateTimeExpression jet.TimestampExpression + +var DateTimeColumn = jet.TimestampColumn +var DateTime = func(year, month, day int) DateExpression { + return CAST(jet.Date(year, month, day)).AS_DATE() +} + +type TimeExpression jet.TimeExpression type ColumnTime jet.ColumnTime var TimeColumn = jet.TimeColumn +var Time = func(hour, minute, second, milliseconds int) TimeExpression { + return CAST(jet.Time(hour, minute, second, milliseconds)).AS_TIME() +} +var TimeExp = jet.TimeExp -type ColumnTimestamp jet.ColumnTimestamp - -var TimestampColumn = jet.TimestampColumn - +type TimezExpression jet.TimezExpression type ColumnTimez jet.ColumnTimez var TimezColumn = jet.TimezColumn +type ColumnTimestamp jet.ColumnTimestamp +type TimestampExpression jet.TimestampExpression + +var TimestampColumn = jet.TimestampColumn +var Timestamp = func(year, month, day, hour, minute, second, milliseconds int) TimestampExpression { + return CAST(jet.Timestamp(year, month, day, hour, minute, second, milliseconds)).AS_TIMESTAMP() +} +var TimestampExp = jet.TimestampExp + +type TimestampzExpression jet.TimestampzExpression type ColumnTimestampz jet.ColumnTimestampz var TimestampzColumn = jet.TimestampzColumn + +// ---------------- functions ------------------// + +var MAXf = jet.MAXf +var SUMf = jet.SUMf +var AVG = jet.AVG +var MINf = jet.MINf +var COUNT = jet.COUNT + +var CASE = jet.CASE + +// ---------------- statements -----------------// + +type SelectStatement jet.SelectStatement + +var SELECT = jet.SELECT + +var UNION = jet.UNION +var UNION_ALL = jet.UNION_ALL +var INTERSECT = jet.INTERSECT +var INTERSECT_ALL = jet.INTERSECT_ALL + +type SelectLock jet.SelectLock + +var ( + UPDATE = jet.NewSelectLock("UPDATE") + NO_KEY_UPDATE = jet.NewSelectLock("NO KEY UPDATE") + SHARE = jet.NewSelectLock("SHARE") + KEY_SHARE = jet.NewSelectLock("KEY SHARE") +) + +var STAR = jet.STAR diff --git a/postgres/testutils.go b/postgres/testutils.go new file mode 100644 index 0000000..d4f35ab --- /dev/null +++ b/postgres/testutils.go @@ -0,0 +1,72 @@ +package postgres + +import ( + "github.com/go-jet/jet" +) + +var table1Col1 = IntegerColumn("col1") +var table1ColInt = IntegerColumn("col_int") +var table1ColFloat = FloatColumn("col_float") +var table1Col3 = IntegerColumn("col3") +var table1ColTime = TimeColumn("col_time") +var table1ColTimez = TimezColumn("col_timez") +var table1ColTimestamp = TimestampColumn("col_timestamp") +var table1ColTimestampz = TimestampzColumn("col_timestampz") +var table1ColBool = BoolColumn("col_bool") +var table1ColDate = DateColumn("col_date") + +var table1 = jet.NewTable( + jet.PostgreSQL, + "db", + "table1", + table1Col1, + table1ColInt, + table1ColFloat, + table1Col3, + table1ColTime, + table1ColTimez, + table1ColBool, + table1ColDate, + table1ColTimestamp, + table1ColTimestampz, +) + +var table2Col3 = IntegerColumn("col3") +var table2Col4 = IntegerColumn("col4") +var table2ColInt = IntegerColumn("col_int") +var table2ColFloat = FloatColumn("col_float") +var table2ColStr = StringColumn("col_str") +var table2ColBool = BoolColumn("col_bool") +var table2ColTime = TimeColumn("col_time") +var table2ColTimez = TimezColumn("col_timez") +var table2ColTimestamp = TimestampColumn("col_timestamp") +var table2ColTimestampz = TimestampzColumn("col_timestampz") +var table2ColDate = DateColumn("col_date") + +var table2 = jet.NewTable( + jet.PostgreSQL, + "db", + "table2", + table2Col3, + table2Col4, + table2ColInt, + table2ColFloat, + table2ColStr, + table2ColBool, + table2ColTime, + table2ColTimez, + table2ColDate, + table2ColTimestamp, + table2ColTimestampz, +) + +var table3Col1 = IntegerColumn("col1") +var table3ColInt = IntegerColumn("col_int") +var table3StrCol = StringColumn("col2") +var table3 = jet.NewTable( + jet.PostgreSQL, + "db", + "table3", + table3Col1, + table3ColInt, + table3StrCol) diff --git a/postgres/time_expression_test.go b/postgres/time_expression_test.go new file mode 100644 index 0000000..7d7688c --- /dev/null +++ b/postgres/time_expression_test.go @@ -0,0 +1,54 @@ +package postgres + +import ( + "github.com/go-jet/jet" + "testing" +) + +var timeVar = Time(10, 20, 0, 0) + +func TestTimeExpressionEQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTime.EQ(table2ColTime), "(table1.col_time = table2.col_time)") + jet.AssertPostgreClauseSerialize(t, table1ColTime.EQ(timeVar), "(table1.col_time = $1::time without time zone)", "10:20:00.000") +} + +func TestTimeExpressionNOT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTime.NOT_EQ(table2ColTime), "(table1.col_time != table2.col_time)") + jet.AssertPostgreClauseSerialize(t, table1ColTime.NOT_EQ(timeVar), "(table1.col_time != $1::time without time zone)", "10:20:00.000") +} + +func TestTimeExpressionIS_DISTINCT_FROM(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTime.IS_DISTINCT_FROM(table2ColTime), "(table1.col_time IS DISTINCT FROM table2.col_time)") + jet.AssertPostgreClauseSerialize(t, table1ColTime.IS_DISTINCT_FROM(timeVar), "(table1.col_time IS DISTINCT FROM $1::time without time zone)", "10:20:00.000") +} + +func TestTimeExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTime.IS_NOT_DISTINCT_FROM(table2ColTime), "(table1.col_time IS NOT DISTINCT FROM table2.col_time)") + jet.AssertPostgreClauseSerialize(t, table1ColTime.IS_NOT_DISTINCT_FROM(timeVar), "(table1.col_time IS NOT DISTINCT FROM $1::time without time zone)", "10:20:00.000") +} + +func TestTimeExpressionLT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTime.LT(table2ColTime), "(table1.col_time < table2.col_time)") + jet.AssertPostgreClauseSerialize(t, table1ColTime.LT(timeVar), "(table1.col_time < $1::time without time zone)", "10:20:00.000") +} + +func TestTimeExpressionLT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTime.LT_EQ(table2ColTime), "(table1.col_time <= table2.col_time)") + jet.AssertPostgreClauseSerialize(t, table1ColTime.LT_EQ(timeVar), "(table1.col_time <= $1::time without time zone)", "10:20:00.000") +} + +func TestTimeExpressionGT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTime.GT(table2ColTime), "(table1.col_time > table2.col_time)") + jet.AssertPostgreClauseSerialize(t, table1ColTime.GT(timeVar), "(table1.col_time > $1::time without time zone)", "10:20:00.000") +} + +func TestTimeExpressionGT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTime.GT_EQ(table2ColTime), "(table1.col_time >= table2.col_time)") + jet.AssertPostgreClauseSerialize(t, table1ColTime.GT_EQ(timeVar), "(table1.col_time >= $1::time without time zone)", "10:20:00.000") +} + +func TestTimeExp(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, TimeExp(table1ColFloat), "table1.col_float") + jet.AssertPostgreClauseSerialize(t, TimeExp(table1ColFloat).LT(Time(1, 1, 1, 1)), + "(table1.col_float < $1::time without time zone)", string("01:01:01.001")) +} diff --git a/postgres/timestamp_expression_test.go b/postgres/timestamp_expression_test.go new file mode 100644 index 0000000..3b215f6 --- /dev/null +++ b/postgres/timestamp_expression_test.go @@ -0,0 +1,55 @@ +package postgres + +import ( + "github.com/go-jet/jet" + "testing" +) + +var timestamp = Timestamp(2000, 1, 31, 10, 20, 0, 0) + +func TestTimestampExpressionEQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.EQ(table2ColTimestamp), "(table1.col_timestamp = table2.col_timestamp)") + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.EQ(timestamp), + "(table1.col_timestamp = $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} + +func TestTimestampExpressionNOT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.NOT_EQ(table2ColTimestamp), "(table1.col_timestamp != table2.col_timestamp)") + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.NOT_EQ(timestamp), "(table1.col_timestamp != $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} + +func TestTimestampExpressionIS_DISTINCT_FROM(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.IS_DISTINCT_FROM(table2ColTimestamp), "(table1.col_timestamp IS DISTINCT FROM table2.col_timestamp)") + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.IS_DISTINCT_FROM(timestamp), "(table1.col_timestamp IS DISTINCT FROM $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} + +func TestTimestampExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.IS_NOT_DISTINCT_FROM(table2ColTimestamp), "(table1.col_timestamp IS NOT DISTINCT FROM table2.col_timestamp)") + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.IS_NOT_DISTINCT_FROM(timestamp), "(table1.col_timestamp IS NOT DISTINCT FROM $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} + +func TestTimestampExpressionLT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.LT(table2ColTimestamp), "(table1.col_timestamp < table2.col_timestamp)") + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.LT(timestamp), "(table1.col_timestamp < $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} + +func TestTimestampExpressionLT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.LT_EQ(table2ColTimestamp), "(table1.col_timestamp <= table2.col_timestamp)") + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.LT_EQ(timestamp), "(table1.col_timestamp <= $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} + +func TestTimestampExpressionGT(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.GT(table2ColTimestamp), "(table1.col_timestamp > table2.col_timestamp)") + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.GT(timestamp), "(table1.col_timestamp > $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} + +func TestTimestampExpressionGT_EQ(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.GT_EQ(table2ColTimestamp), "(table1.col_timestamp >= table2.col_timestamp)") + jet.AssertPostgreClauseSerialize(t, table1ColTimestamp.GT_EQ(timestamp), "(table1.col_timestamp >= $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} + +func TestTimestampExp(t *testing.T) { + jet.AssertPostgreClauseSerialize(t, TimestampExp(table1ColFloat), "table1.col_float") + jet.AssertPostgreClauseSerialize(t, TimestampExp(table1ColFloat).LT(timestamp), + "(table1.col_float < $1::timestamp without time zone)", "2000-01-31 10:20:00.000") +} diff --git a/select_statement.go b/select_statement.go index a1151c9..ad94c71 100644 --- a/select_statement.go +++ b/select_statement.go @@ -7,14 +7,6 @@ import ( "github.com/go-jet/jet/execution" ) -// Select statements lock types -var ( - UPDATE = newLock("UPDATE") - NO_KEY_UPDATE = newLock("NO KEY UPDATE") - SHARE = newLock("SHARE") - KEY_SHARE = newLock("KEY SHARE") -) - // SelectStatement is interface for SQL SELECT statements type SelectStatement interface { Statement @@ -327,7 +319,7 @@ type selectLockImpl struct { noWait, skipLocked bool } -func newLock(name string) func() SelectLock { +func NewSelectLock(name string) func() SelectLock { return func() SelectLock { return newSelectLock(name) } diff --git a/select_statement_test.go b/select_statement_test.go index 7c432ef..520f49c 100644 --- a/select_statement_test.go +++ b/select_statement_test.go @@ -99,30 +99,6 @@ OFFSET $2; `, int64(10), int64(2)) } -func TestSelectLock(t *testing.T) { - assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(UPDATE()), ` -SELECT table1.col_bool AS "table1.col_bool" -FROM db.table1 -FOR UPDATE; -`) - assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(SHARE().NOWAIT()), ` -SELECT table1.col_bool AS "table1.col_bool" -FROM db.table1 -FOR SHARE NOWAIT; -`) - - assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(KEY_SHARE().NOWAIT()), ` -SELECT table1.col_bool AS "table1.col_bool" -FROM db.table1 -FOR KEY SHARE NOWAIT; -`) - assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(NO_KEY_UPDATE().SKIP_LOCKED()), ` -SELECT table1.col_bool AS "table1.col_bool" -FROM db.table1 -FOR NO KEY UPDATE SKIP LOCKED; -`) -} - func TestSelectSets(t *testing.T) { select1 := SELECT(table1ColBool).FROM(table1) select2 := SELECT(table2ColBool).FROM(table2) diff --git a/string_expression_test.go b/string_expression_test.go index 4be5ac5..a81d9cb 100644 --- a/string_expression_test.go +++ b/string_expression_test.go @@ -6,77 +6,77 @@ import ( func TestStringEQ(t *testing.T) { exp := table3StrCol.EQ(table2ColStr) - assertPostgreClauseSerialize(t, exp, "(table3.col2 = table2.col_str)") + AssertPostgreClauseSerialize(t, exp, "(table3.col2 = table2.col_str)") exp = table3StrCol.EQ(String("JOHN")) - assertPostgreClauseSerialize(t, exp, "(table3.col2 = $1)", "JOHN") + AssertPostgreClauseSerialize(t, exp, "(table3.col2 = $1)", "JOHN") } func TestStringNOT_EQ(t *testing.T) { exp := table3StrCol.NOT_EQ(table2ColStr) - assertPostgreClauseSerialize(t, exp, "(table3.col2 != table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.NOT_EQ(String("JOHN")), "(table3.col2 != $1)", "JOHN") + AssertPostgreClauseSerialize(t, exp, "(table3.col2 != table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.NOT_EQ(String("JOHN")), "(table3.col2 != $1)", "JOHN") } func TestStringExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table3StrCol.IS_DISTINCT_FROM(table2ColStr), "(table3.col2 IS DISTINCT FROM table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.IS_DISTINCT_FROM(String("JOHN")), "(table3.col2 IS DISTINCT FROM $1)", "JOHN") + AssertPostgreClauseSerialize(t, table3StrCol.IS_DISTINCT_FROM(table2ColStr), "(table3.col2 IS DISTINCT FROM table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.IS_DISTINCT_FROM(String("JOHN")), "(table3.col2 IS DISTINCT FROM $1)", "JOHN") } func TestStringExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table3StrCol.IS_NOT_DISTINCT_FROM(table2ColStr), "(table3.col2 IS NOT DISTINCT FROM table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.IS_NOT_DISTINCT_FROM(String("JOHN")), "(table3.col2 IS NOT DISTINCT FROM $1)", "JOHN") + AssertPostgreClauseSerialize(t, table3StrCol.IS_NOT_DISTINCT_FROM(table2ColStr), "(table3.col2 IS NOT DISTINCT FROM table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.IS_NOT_DISTINCT_FROM(String("JOHN")), "(table3.col2 IS NOT DISTINCT FROM $1)", "JOHN") } func TestStringGT(t *testing.T) { exp := table3StrCol.GT(table2ColStr) - assertPostgreClauseSerialize(t, exp, "(table3.col2 > table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.GT(String("JOHN")), "(table3.col2 > $1)", "JOHN") + AssertPostgreClauseSerialize(t, exp, "(table3.col2 > table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.GT(String("JOHN")), "(table3.col2 > $1)", "JOHN") } func TestStringGT_EQ(t *testing.T) { exp := table3StrCol.GT_EQ(table2ColStr) - assertPostgreClauseSerialize(t, exp, "(table3.col2 >= table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.GT_EQ(String("JOHN")), "(table3.col2 >= $1)", "JOHN") + AssertPostgreClauseSerialize(t, exp, "(table3.col2 >= table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.GT_EQ(String("JOHN")), "(table3.col2 >= $1)", "JOHN") } func TestStringLT(t *testing.T) { exp := table3StrCol.LT(table2ColStr) - assertPostgreClauseSerialize(t, exp, "(table3.col2 < table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.LT(String("JOHN")), "(table3.col2 < $1)", "JOHN") + AssertPostgreClauseSerialize(t, exp, "(table3.col2 < table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.LT(String("JOHN")), "(table3.col2 < $1)", "JOHN") } func TestStringLT_EQ(t *testing.T) { exp := table3StrCol.LT_EQ(table2ColStr) - assertPostgreClauseSerialize(t, exp, "(table3.col2 <= table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.LT_EQ(String("JOHN")), "(table3.col2 <= $1)", "JOHN") + AssertPostgreClauseSerialize(t, exp, "(table3.col2 <= table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.LT_EQ(String("JOHN")), "(table3.col2 <= $1)", "JOHN") } func TestStringCONCAT(t *testing.T) { - assertPostgreClauseSerialize(t, table3StrCol.CONCAT(table2ColStr), "(table3.col2 || table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.CONCAT(String("JOHN")), "(table3.col2 || $1)", "JOHN") + AssertPostgreClauseSerialize(t, table3StrCol.CONCAT(table2ColStr), "(table3.col2 || table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.CONCAT(String("JOHN")), "(table3.col2 || $1)", "JOHN") } func TestStringLIKE(t *testing.T) { - assertPostgreClauseSerialize(t, table3StrCol.LIKE(table2ColStr), "(table3.col2 LIKE table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.LIKE(String("JOHN")), "(table3.col2 LIKE $1)", "JOHN") + AssertPostgreClauseSerialize(t, table3StrCol.LIKE(table2ColStr), "(table3.col2 LIKE table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.LIKE(String("JOHN")), "(table3.col2 LIKE $1)", "JOHN") } func TestStringNOT_LIKE(t *testing.T) { - assertPostgreClauseSerialize(t, table3StrCol.NOT_LIKE(table2ColStr), "(table3.col2 NOT LIKE table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.NOT_LIKE(String("JOHN")), "(table3.col2 NOT LIKE $1)", "JOHN") + AssertPostgreClauseSerialize(t, table3StrCol.NOT_LIKE(table2ColStr), "(table3.col2 NOT LIKE table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.NOT_LIKE(String("JOHN")), "(table3.col2 NOT LIKE $1)", "JOHN") } func TestStringSIMILAR_TO(t *testing.T) { - assertPostgreClauseSerialize(t, table3StrCol.SIMILAR_TO(table2ColStr), "(table3.col2 SIMILAR TO table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.SIMILAR_TO(String("JOHN")), "(table3.col2 SIMILAR TO $1)", "JOHN") + AssertPostgreClauseSerialize(t, table3StrCol.SIMILAR_TO(table2ColStr), "(table3.col2 SIMILAR TO table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.SIMILAR_TO(String("JOHN")), "(table3.col2 SIMILAR TO $1)", "JOHN") } func TestStringNOT_SIMILAR_TO(t *testing.T) { - assertPostgreClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(table2ColStr), "(table3.col2 NOT SIMILAR TO table2.col_str)") - assertPostgreClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(String("JOHN")), "(table3.col2 NOT SIMILAR TO $1)", "JOHN") + AssertPostgreClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(table2ColStr), "(table3.col2 NOT SIMILAR TO table2.col_str)") + AssertPostgreClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(String("JOHN")), "(table3.col2 NOT SIMILAR TO $1)", "JOHN") } func TestStringExp(t *testing.T) { - assertPostgreClauseSerialize(t, StringExp(table2ColFloat), "table2.col_float") - assertPostgreClauseSerialize(t, StringExp(table2ColFloat).NOT_LIKE(String("abc")), "(table2.col_float NOT LIKE $1)", "abc") + AssertPostgreClauseSerialize(t, StringExp(table2ColFloat), "table2.col_float") + AssertPostgreClauseSerialize(t, StringExp(table2ColFloat).NOT_LIKE(String("abc")), "(table2.col_float NOT LIKE $1)", "abc") } diff --git a/table_test.go b/table_test.go index e7f20b2..31f29ff 100644 --- a/table_test.go +++ b/table_test.go @@ -12,17 +12,17 @@ func TestJoinNilInputs(t *testing.T) { } func TestINNER_JOIN(t *testing.T) { - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)), `db.table1 INNER JOIN db.table2 ON (table1.col_int = table2.col_int)`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)). INNER_JOIN(table3, table1ColInt.EQ(table3ColInt)), `db.table1 INNER JOIN db.table2 ON (table1.col_int = table2.col_int) INNER JOIN db.table3 ON (table1.col_int = table3.col_int)`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. INNER_JOIN(table2, table1ColInt.EQ(Int(1))). INNER_JOIN(table3, table1ColInt.EQ(Int(2))), `db.table1 @@ -31,17 +31,17 @@ INNER JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2)) } func TestLEFT_JOIN(t *testing.T) { - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)), `db.table1 LEFT JOIN db.table2 ON (table1.col_int = table2.col_int)`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)). LEFT_JOIN(table3, table1ColInt.EQ(table3ColInt)), `db.table1 LEFT JOIN db.table2 ON (table1.col_int = table2.col_int) LEFT JOIN db.table3 ON (table1.col_int = table3.col_int)`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. LEFT_JOIN(table2, table1ColInt.EQ(Int(1))). LEFT_JOIN(table3, table1ColInt.EQ(Int(2))), `db.table1 @@ -50,17 +50,17 @@ LEFT JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2)) } func TestRIGHT_JOIN(t *testing.T) { - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)), `db.table1 RIGHT JOIN db.table2 ON (table1.col_int = table2.col_int)`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)). RIGHT_JOIN(table3, table1ColInt.EQ(table3ColInt)), `db.table1 RIGHT JOIN db.table2 ON (table1.col_int = table2.col_int) RIGHT JOIN db.table3 ON (table1.col_int = table3.col_int)`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. RIGHT_JOIN(table2, table1ColInt.EQ(Int(1))). RIGHT_JOIN(table3, table1ColInt.EQ(Int(2))), `db.table1 @@ -69,17 +69,17 @@ RIGHT JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2)) } func TestFULL_JOIN(t *testing.T) { - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)), `db.table1 FULL JOIN db.table2 ON (table1.col_int = table2.col_int)`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)). FULL_JOIN(table3, table1ColInt.EQ(table3ColInt)), `db.table1 FULL JOIN db.table2 ON (table1.col_int = table2.col_int) FULL JOIN db.table3 ON (table1.col_int = table3.col_int)`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. FULL_JOIN(table2, table1ColInt.EQ(Int(1))). FULL_JOIN(table3, table1ColInt.EQ(Int(2))), `db.table1 @@ -88,11 +88,11 @@ FULL JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2)) } func TestCROSS_JOIN(t *testing.T) { - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. CROSS_JOIN(table2), `db.table1 CROSS JOIN db.table2`) - assertPostgreClauseSerialize(t, table1. + AssertPostgreClauseSerialize(t, table1. CROSS_JOIN(table2). CROSS_JOIN(table3), `db.table1 diff --git a/tests/mysql/cast_test.go b/tests/mysql/cast_test.go new file mode 100644 index 0000000..05d2fa7 --- /dev/null +++ b/tests/mysql/cast_test.go @@ -0,0 +1,60 @@ +package mysql + +import ( + "github.com/go-jet/jet/internal/testutils" + . "github.com/go-jet/jet/mysql" + . "github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/table" + "gotest.tools/assert" + "testing" + "time" +) + +func TestCast(t *testing.T) { + + query := SELECT( + CAST(String("2011-02-02")).AS_DATE().AS("result.date"), + CAST(String("14:06:10")).AS_TIME().AS("result.time"), + CAST(String("2011-02-02 14:06:10")).AS_DATETIME().AS("result.datetime"), + CAST(Int(150)).AS_CHAR().AS("result.char"), + CAST(Int(5).SUB(Int(10))).AS_SIGNED().AS("result.signed"), + CAST(Int(5).ADD(Int(10))).AS_UNSIGNED().AS("result.unsigned"), + CAST(String("Some text")).AS_BINARY().AS("result.binary"), + ).FROM(AllTypes) + + testutils.AssertStatementSql(t, query, ` +SELECT CAST(? AS DATE) AS "result.date", + CAST(? AS TIME) AS "result.time", + CAST(? AS DATETIME) AS "result.datetime", + CAST(? AS CHAR) AS "result.char", + CAST((? - ?) AS SIGNED) AS "result.signed", + CAST((? + ?) AS UNSIGNED) AS "result.unsigned", + CAST(? AS BINARY) AS "result.binary" +FROM test_sample.all_types; +`, "2011-02-02", "14:06:10", "2011-02-02 14:06:10", int64(150), int64(5), int64(10), int64(5), int64(10), "Some text") + + type Result struct { + Date time.Time + Time time.Time + DateTime time.Time + Char string + Signed int + Unsigned int + Binary string + } + + var dest Result + + err := query.Query(db, &dest) + + assert.NilError(t, err) + + assert.DeepEqual(t, dest, Result{ + Date: *testutils.Date("2011-02-02"), + Time: *testutils.TimeWithoutTimeZone("14:06:10"), + DateTime: *testutils.TimestampWithoutTimeZone("2011-02-02 14:06:10", 0), + Char: "150", + Signed: -5, + Unsigned: 15, + Binary: "Some text", + }) +} diff --git a/tests/postgres/alltypes_test.go b/tests/postgres/alltypes_test.go index 730b5eb..bc17696 100644 --- a/tests/postgres/alltypes_test.go +++ b/tests/postgres/alltypes_test.go @@ -4,6 +4,7 @@ import ( "fmt" . "github.com/go-jet/jet" "github.com/go-jet/jet/internal/testutils" + "github.com/go-jet/jet/postgres" "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model" . "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/table" "github.com/go-jet/jet/tests/testdata/common" @@ -64,20 +65,20 @@ func TestExpressionOperators(t *testing.T) { AllTypes.SmallIntPtr.NOT_IN(Int(11), Int(22), NULL), AllTypes.SmallIntPtr.NOT_IN(AllTypes.SELECT(AllTypes.IntegerPtr)), - CAST(String("TRUE")).AS_BOOL(), - CAST(String("111")).AS_SMALLINT(), - CAST(String("111")).AS_INTEGER(), - CAST(String("111")).AS_BIGINT(), - CAST(String("11.23")).AS_NUMERIC(30, 10), - CAST(String("11.23")).AS_NUMERIC(30), - CAST(String("11.23")).AS_REAL(), - CAST(String("11.23")).AS_DOUBLE(), - CAST(Int(234)).AS_TEXT(), - CAST(String("1/8/1999")).AS_DATE(), - CAST(String("04:05:06.789")).AS_TIME(), - CAST(String("04:05:06 PST")).AS_TIMEZ(), - CAST(String("1999-01-08 04:05:06")).AS_TIMESTAMP(), - CAST(String("January 8 04:05:06 1999 PST")).AS_TIMESTAMPZ(), + postgres.CAST(String("TRUE")).AS_BOOL(), + postgres.CAST(String("111")).AS_SMALLINT(), + postgres.CAST(String("111")).AS_INTEGER(), + postgres.CAST(String("111")).AS_BIGINT(), + postgres.CAST(String("11.23")).AS_NUMERIC(30, 10), + postgres.CAST(String("11.23")).AS_NUMERIC(30), + postgres.CAST(String("11.23")).AS_REAL(), + postgres.CAST(String("11.23")).AS_DOUBLE(), + postgres.CAST(Int(234)).AS_TEXT(), + postgres.CAST(String("1/8/1999")).AS_DATE(), + postgres.CAST(String("04:05:06.789")).AS_TIME(), + postgres.CAST(String("04:05:06 PST")).AS_TIMEZ(), + postgres.CAST(String("1999-01-08 04:05:06")).AS_TIMESTAMP(), + postgres.CAST(String("January 8 04:05:06 1999 PST")).AS_TIMESTAMPZ(), TO_CHAR(AllTypes.Timestamp, String("HH12:MI:SS")), TO_CHAR(AllTypes.Integer, String("999")), @@ -277,7 +278,7 @@ func TestFloatOperators(t *testing.T) { TRUNC(ABSf(AllTypes.Decimal), Int(2)).AS("abs"), TRUNC(POWER(AllTypes.Decimal, Float(2.1)), Int(2)).AS("power"), TRUNC(SQRT(AllTypes.Decimal), Int(2)).AS("sqrt"), - TRUNC(CAST(CBRT(AllTypes.Decimal)).AS_DECIMAL(), Int(2)).AS("cbrt"), + TRUNC(postgres.CAST(CBRT(AllTypes.Decimal)).AS_DECIMAL(), Int(2)).AS("cbrt"), CEIL(AllTypes.Real).AS("ceil"), FLOOR(AllTypes.Real).AS("floor"), @@ -750,16 +751,16 @@ var allTypesRow0 = model.AllTypes{ Text: "Some text", ByteaPtr: ByteArrayPtr([]byte("bytea")), Bytea: []byte("bytea"), - TimestampzPtr: TimestampWithTimeZone("1999-01-08 13:05:06 +0100 CET", 0), - Timestampz: *TimestampWithTimeZone("1999-01-08 13:05:06 +0100 CET", 0), + TimestampzPtr: testutils.TimestampWithTimeZone("1999-01-08 13:05:06 +0100 CET", 0), + Timestampz: *testutils.TimestampWithTimeZone("1999-01-08 13:05:06 +0100 CET", 0), TimestampPtr: testutils.TimestampWithoutTimeZone("1999-01-08 04:05:06", 0), Timestamp: *testutils.TimestampWithoutTimeZone("1999-01-08 04:05:06", 0), DatePtr: testutils.TimestampWithoutTimeZone("1999-01-08 00:00:00", 0), Date: *testutils.TimestampWithoutTimeZone("1999-01-08 00:00:00", 0), - TimezPtr: TimeWithTimeZone("04:05:06 -0800"), - Timez: *TimeWithTimeZone("04:05:06 -0800"), - TimePtr: TimeWithoutTimeZone("04:05:06"), - Time: *TimeWithoutTimeZone("04:05:06"), + TimezPtr: testutils.TimeWithTimeZone("04:05:06 -0800"), + Timez: *testutils.TimeWithTimeZone("04:05:06 -0800"), + TimePtr: testutils.TimeWithoutTimeZone("04:05:06"), + Time: *testutils.TimeWithoutTimeZone("04:05:06"), IntervalPtr: StringPtr("3 days 04:05:06"), Interval: "3 days 04:05:06", BooleanPtr: BoolPtr(true), @@ -817,15 +818,15 @@ var allTypesRow1 = model.AllTypes{ ByteaPtr: nil, Bytea: []byte("bytea"), TimestampzPtr: nil, - Timestampz: *TimestampWithTimeZone("1999-01-08 13:05:06 +0100 CET", 0), + Timestampz: *testutils.TimestampWithTimeZone("1999-01-08 13:05:06 +0100 CET", 0), TimestampPtr: nil, Timestamp: *testutils.TimestampWithoutTimeZone("1999-01-08 04:05:06", 0), DatePtr: nil, Date: *testutils.TimestampWithoutTimeZone("1999-01-08 00:00:00", 0), TimezPtr: nil, - Timez: *TimeWithTimeZone("04:05:06 -0800"), + Timez: *testutils.TimeWithTimeZone("04:05:06 -0800"), TimePtr: nil, - Time: *TimeWithoutTimeZone("04:05:06"), + Time: *testutils.TimeWithoutTimeZone("04:05:06"), IntervalPtr: nil, Interval: "3 days 04:05:06", BooleanPtr: nil, diff --git a/tests/postgres/sample_test.go b/tests/postgres/sample_test.go index a789e15..657c841 100644 --- a/tests/postgres/sample_test.go +++ b/tests/postgres/sample_test.go @@ -116,7 +116,7 @@ ORDER BY employee.employee_id; EmployeeID: 1, FirstName: "Windy", LastName: "Hays", - EmploymentDate: TimestampWithTimeZone("1999-01-08 04:05:06.1 +0100 CET", 1), + EmploymentDate: testutils.TimestampWithTimeZone("1999-01-08 04:05:06.1 +0100 CET", 1), ManagerID: nil, }) @@ -126,7 +126,7 @@ ORDER BY employee.employee_id; EmployeeID: 8, FirstName: "Salley", LastName: "Lester", - EmploymentDate: TimestampWithTimeZone("1999-01-08 04:05:06 +0100 CET", 1), + EmploymentDate: testutils.TimestampWithTimeZone("1999-01-08 04:05:06 +0100 CET", 1), ManagerID: Int32Ptr(3), }) } diff --git a/tests/postgres/select_test.go b/tests/postgres/select_test.go index 33e9c59..001c157 100644 --- a/tests/postgres/select_test.go +++ b/tests/postgres/select_test.go @@ -1,8 +1,9 @@ package postgres import ( - . "github.com/go-jet/jet" + "github.com/go-jet/jet" "github.com/go-jet/jet/internal/testutils" + . "github.com/go-jet/jet/postgres" "github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/enum" "github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/model" . "github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/table" @@ -1255,7 +1256,7 @@ func TestAllSetOperators(t *testing.T) { select1 := Payment.SELECT(Payment.AllColumns).WHERE(Payment.PaymentID.GT_EQ(Int(17600)).AND(Payment.PaymentID.LT(Int(17610)))) select2 := Payment.SELECT(Payment.AllColumns).WHERE(Payment.PaymentID.GT_EQ(Int(17620)).AND(Payment.PaymentID.LT(Int(17630)))) - type setOperator func(lhs, rhs SelectStatement, selects ...SelectStatement) SelectStatement + type setOperator func(lhs, rhs jet.SelectStatement, selects ...jet.SelectStatement) jet.SelectStatement operators := []setOperator{ UNION, UNION_ALL, diff --git a/tests/postgres/util_test.go b/tests/postgres/util_test.go index 00210eb..8da7989 100644 --- a/tests/postgres/util_test.go +++ b/tests/postgres/util_test.go @@ -6,9 +6,7 @@ import ( "github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/model" "github.com/google/uuid" "gotest.tools/assert" - "strings" "testing" - "time" ) func assertExec(t *testing.T, stmt jet.Statement, rowsAffected int64) { @@ -62,43 +60,6 @@ func UUIDPtr(u string) *uuid.UUID { return &newUUID } -func TimeWithoutTimeZone(t string) *time.Time { - newTime, err := time.Parse("15:04:05", t) - - if err != nil { - panic(err) - } - - return &newTime -} - -func TimeWithTimeZone(t string) *time.Time { - newTimez, err := time.Parse("15:04:05 -0700", t) - - if err != nil { - panic(err) - } - - return &newTimez -} - -func TimestampWithTimeZone(t string, precision int) *time.Time { - - precisionStr := "" - - if precision > 0 { - precisionStr = "." + strings.Repeat("9", precision) - } - - newTime, err := time.Parse("2006-01-02 15:04:05"+precisionStr+" -0700 MST", t) - - if err != nil { - panic(err) - } - - return &newTime -} - var customer0 = model.Customer{ CustomerID: 1, StoreID: 1, diff --git a/utils_test.go b/testutils.go similarity index 88% rename from utils_test.go rename to testutils.go index c1ee1f1..880e086 100644 --- a/utils_test.go +++ b/testutils.go @@ -72,7 +72,17 @@ var table3 = NewTable( table3ColInt, table3StrCol) -func assertPostgreClauseSerialize(t *testing.T, clause clause, query string, args ...interface{}) { +func AssertClauseSerialize(t *testing.T, clause clause, query string, args ...interface{}) { + out := sqlBuilder{dialect: Default} + err := clause.serialize(selectStatement, &out) + + assert.NilError(t, err) + + assert.DeepEqual(t, out.buff.String(), query) + assert.DeepEqual(t, out.args, args) +} + +func AssertPostgreClauseSerialize(t *testing.T, clause clause, query string, args ...interface{}) { out := sqlBuilder{dialect: PostgreSQL} err := clause.serialize(selectStatement, &out) @@ -82,7 +92,7 @@ func assertPostgreClauseSerialize(t *testing.T, clause clause, query string, arg assert.DeepEqual(t, out.args, args) } -func assertMySQLClauseSerialize(t *testing.T, clause clause, query string, args ...interface{}) { +func AssertMySQLClauseSerialize(t *testing.T, clause clause, query string, args ...interface{}) { out := sqlBuilder{dialect: MySQL} err := clause.serialize(selectStatement, &out) diff --git a/time_expression_test.go b/time_expression_test.go index bcfb816..eee14f1 100644 --- a/time_expression_test.go +++ b/time_expression_test.go @@ -7,47 +7,47 @@ import ( var timeVar = Time(10, 20, 0, 0) func TestTimeExpressionEQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTime.EQ(table2ColTime), "(table1.col_time = table2.col_time)") - assertPostgreClauseSerialize(t, table1ColTime.EQ(timeVar), "(table1.col_time = $1::time without time zone)", "10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTime.EQ(table2ColTime), "(table1.col_time = table2.col_time)") + AssertPostgreClauseSerialize(t, table1ColTime.EQ(timeVar), "(table1.col_time = $1)", "10:20:00.000") } func TestTimeExpressionNOT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTime.NOT_EQ(table2ColTime), "(table1.col_time != table2.col_time)") - assertPostgreClauseSerialize(t, table1ColTime.NOT_EQ(timeVar), "(table1.col_time != $1::time without time zone)", "10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTime.NOT_EQ(table2ColTime), "(table1.col_time != table2.col_time)") + AssertPostgreClauseSerialize(t, table1ColTime.NOT_EQ(timeVar), "(table1.col_time != $1)", "10:20:00.000") } func TestTimeExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTime.IS_DISTINCT_FROM(table2ColTime), "(table1.col_time IS DISTINCT FROM table2.col_time)") - assertPostgreClauseSerialize(t, table1ColTime.IS_DISTINCT_FROM(timeVar), "(table1.col_time IS DISTINCT FROM $1::time without time zone)", "10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTime.IS_DISTINCT_FROM(table2ColTime), "(table1.col_time IS DISTINCT FROM table2.col_time)") + AssertPostgreClauseSerialize(t, table1ColTime.IS_DISTINCT_FROM(timeVar), "(table1.col_time IS DISTINCT FROM $1)", "10:20:00.000") } func TestTimeExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTime.IS_NOT_DISTINCT_FROM(table2ColTime), "(table1.col_time IS NOT DISTINCT FROM table2.col_time)") - assertPostgreClauseSerialize(t, table1ColTime.IS_NOT_DISTINCT_FROM(timeVar), "(table1.col_time IS NOT DISTINCT FROM $1::time without time zone)", "10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTime.IS_NOT_DISTINCT_FROM(table2ColTime), "(table1.col_time IS NOT DISTINCT FROM table2.col_time)") + AssertPostgreClauseSerialize(t, table1ColTime.IS_NOT_DISTINCT_FROM(timeVar), "(table1.col_time IS NOT DISTINCT FROM $1)", "10:20:00.000") } func TestTimeExpressionLT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTime.LT(table2ColTime), "(table1.col_time < table2.col_time)") - assertPostgreClauseSerialize(t, table1ColTime.LT(timeVar), "(table1.col_time < $1::time without time zone)", "10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTime.LT(table2ColTime), "(table1.col_time < table2.col_time)") + AssertPostgreClauseSerialize(t, table1ColTime.LT(timeVar), "(table1.col_time < $1)", "10:20:00.000") } func TestTimeExpressionLT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTime.LT_EQ(table2ColTime), "(table1.col_time <= table2.col_time)") - assertPostgreClauseSerialize(t, table1ColTime.LT_EQ(timeVar), "(table1.col_time <= $1::time without time zone)", "10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTime.LT_EQ(table2ColTime), "(table1.col_time <= table2.col_time)") + AssertPostgreClauseSerialize(t, table1ColTime.LT_EQ(timeVar), "(table1.col_time <= $1)", "10:20:00.000") } func TestTimeExpressionGT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTime.GT(table2ColTime), "(table1.col_time > table2.col_time)") - assertPostgreClauseSerialize(t, table1ColTime.GT(timeVar), "(table1.col_time > $1::time without time zone)", "10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTime.GT(table2ColTime), "(table1.col_time > table2.col_time)") + AssertPostgreClauseSerialize(t, table1ColTime.GT(timeVar), "(table1.col_time > $1)", "10:20:00.000") } func TestTimeExpressionGT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTime.GT_EQ(table2ColTime), "(table1.col_time >= table2.col_time)") - assertPostgreClauseSerialize(t, table1ColTime.GT_EQ(timeVar), "(table1.col_time >= $1::time without time zone)", "10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTime.GT_EQ(table2ColTime), "(table1.col_time >= table2.col_time)") + AssertPostgreClauseSerialize(t, table1ColTime.GT_EQ(timeVar), "(table1.col_time >= $1)", "10:20:00.000") } func TestTimeExp(t *testing.T) { - assertPostgreClauseSerialize(t, TimeExp(table1ColFloat), "table1.col_float") - assertPostgreClauseSerialize(t, TimeExp(table1ColFloat).LT(Time(1, 1, 1, 1)), - "(table1.col_float < $1::time without time zone)", string("01:01:01.001")) + AssertPostgreClauseSerialize(t, TimeExp(table1ColFloat), "table1.col_float") + AssertPostgreClauseSerialize(t, TimeExp(table1ColFloat).LT(Time(1, 1, 1, 1)), + "(table1.col_float < $1)", string("01:01:01.001")) } diff --git a/timestamp_expression_test.go b/timestamp_expression_test.go index 2754f9e..44f6dbb 100644 --- a/timestamp_expression_test.go +++ b/timestamp_expression_test.go @@ -5,48 +5,48 @@ import "testing" var timestamp = Timestamp(2000, 1, 31, 10, 20, 0, 0) func TestTimestampExpressionEQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestamp.EQ(table2ColTimestamp), "(table1.col_timestamp = table2.col_timestamp)") - assertPostgreClauseSerialize(t, table1ColTimestamp.EQ(timestamp), - "(table1.col_timestamp = $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTimestamp.EQ(table2ColTimestamp), "(table1.col_timestamp = table2.col_timestamp)") + AssertPostgreClauseSerialize(t, table1ColTimestamp.EQ(timestamp), + "(table1.col_timestamp = $1)", "2000-01-31 10:20:00.000") } func TestTimestampExpressionNOT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestamp.NOT_EQ(table2ColTimestamp), "(table1.col_timestamp != table2.col_timestamp)") - assertPostgreClauseSerialize(t, table1ColTimestamp.NOT_EQ(timestamp), "(table1.col_timestamp != $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTimestamp.NOT_EQ(table2ColTimestamp), "(table1.col_timestamp != table2.col_timestamp)") + AssertPostgreClauseSerialize(t, table1ColTimestamp.NOT_EQ(timestamp), "(table1.col_timestamp != $1)", "2000-01-31 10:20:00.000") } func TestTimestampExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestamp.IS_DISTINCT_FROM(table2ColTimestamp), "(table1.col_timestamp IS DISTINCT FROM table2.col_timestamp)") - assertPostgreClauseSerialize(t, table1ColTimestamp.IS_DISTINCT_FROM(timestamp), "(table1.col_timestamp IS DISTINCT FROM $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTimestamp.IS_DISTINCT_FROM(table2ColTimestamp), "(table1.col_timestamp IS DISTINCT FROM table2.col_timestamp)") + AssertPostgreClauseSerialize(t, table1ColTimestamp.IS_DISTINCT_FROM(timestamp), "(table1.col_timestamp IS DISTINCT FROM $1)", "2000-01-31 10:20:00.000") } func TestTimestampExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestamp.IS_NOT_DISTINCT_FROM(table2ColTimestamp), "(table1.col_timestamp IS NOT DISTINCT FROM table2.col_timestamp)") - assertPostgreClauseSerialize(t, table1ColTimestamp.IS_NOT_DISTINCT_FROM(timestamp), "(table1.col_timestamp IS NOT DISTINCT FROM $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTimestamp.IS_NOT_DISTINCT_FROM(table2ColTimestamp), "(table1.col_timestamp IS NOT DISTINCT FROM table2.col_timestamp)") + AssertPostgreClauseSerialize(t, table1ColTimestamp.IS_NOT_DISTINCT_FROM(timestamp), "(table1.col_timestamp IS NOT DISTINCT FROM $1)", "2000-01-31 10:20:00.000") } func TestTimestampExpressionLT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestamp.LT(table2ColTimestamp), "(table1.col_timestamp < table2.col_timestamp)") - assertPostgreClauseSerialize(t, table1ColTimestamp.LT(timestamp), "(table1.col_timestamp < $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTimestamp.LT(table2ColTimestamp), "(table1.col_timestamp < table2.col_timestamp)") + AssertPostgreClauseSerialize(t, table1ColTimestamp.LT(timestamp), "(table1.col_timestamp < $1)", "2000-01-31 10:20:00.000") } func TestTimestampExpressionLT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestamp.LT_EQ(table2ColTimestamp), "(table1.col_timestamp <= table2.col_timestamp)") - assertPostgreClauseSerialize(t, table1ColTimestamp.LT_EQ(timestamp), "(table1.col_timestamp <= $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTimestamp.LT_EQ(table2ColTimestamp), "(table1.col_timestamp <= table2.col_timestamp)") + AssertPostgreClauseSerialize(t, table1ColTimestamp.LT_EQ(timestamp), "(table1.col_timestamp <= $1)", "2000-01-31 10:20:00.000") } func TestTimestampExpressionGT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestamp.GT(table2ColTimestamp), "(table1.col_timestamp > table2.col_timestamp)") - assertPostgreClauseSerialize(t, table1ColTimestamp.GT(timestamp), "(table1.col_timestamp > $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTimestamp.GT(table2ColTimestamp), "(table1.col_timestamp > table2.col_timestamp)") + AssertPostgreClauseSerialize(t, table1ColTimestamp.GT(timestamp), "(table1.col_timestamp > $1)", "2000-01-31 10:20:00.000") } func TestTimestampExpressionGT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestamp.GT_EQ(table2ColTimestamp), "(table1.col_timestamp >= table2.col_timestamp)") - assertPostgreClauseSerialize(t, table1ColTimestamp.GT_EQ(timestamp), "(table1.col_timestamp >= $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, table1ColTimestamp.GT_EQ(table2ColTimestamp), "(table1.col_timestamp >= table2.col_timestamp)") + AssertPostgreClauseSerialize(t, table1ColTimestamp.GT_EQ(timestamp), "(table1.col_timestamp >= $1)", "2000-01-31 10:20:00.000") } func TestTimestampExp(t *testing.T) { - assertPostgreClauseSerialize(t, TimestampExp(table1ColFloat), "table1.col_float") - assertPostgreClauseSerialize(t, TimestampExp(table1ColFloat).LT(timestamp), - "(table1.col_float < $1::timestamp without time zone)", "2000-01-31 10:20:00.000") + AssertPostgreClauseSerialize(t, TimestampExp(table1ColFloat), "table1.col_float") + AssertPostgreClauseSerialize(t, TimestampExp(table1ColFloat).LT(timestamp), + "(table1.col_float < $1)", "2000-01-31 10:20:00.000") } diff --git a/timestampz_expression_test.go b/timestampz_expression_test.go index f2f814c..1a13fd0 100644 --- a/timestampz_expression_test.go +++ b/timestampz_expression_test.go @@ -1,3 +1,5 @@ +// +build todo + package jet import "testing" @@ -5,48 +7,48 @@ import "testing" var timestampz = Timestampz(2000, 1, 31, 10, 20, 0, 0, 2) func TestTimestampzExpressionEQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestampz.EQ(table2ColTimestampz), "(table1.col_timestampz = table2.col_timestampz)") - assertPostgreClauseSerialize(t, table1ColTimestampz.EQ(timestampz), + AssertPostgreClauseSerialize(t, table1ColTimestampz.EQ(table2ColTimestampz), "(table1.col_timestampz = table2.col_timestampz)") + AssertPostgreClauseSerialize(t, table1ColTimestampz.EQ(timestampz), "(table1.col_timestampz = $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } func TestTimestampzExpressionNOT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestampz.NOT_EQ(table2ColTimestampz), "(table1.col_timestampz != table2.col_timestampz)") - assertPostgreClauseSerialize(t, table1ColTimestampz.NOT_EQ(timestampz), "(table1.col_timestampz != $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") + AssertPostgreClauseSerialize(t, table1ColTimestampz.NOT_EQ(table2ColTimestampz), "(table1.col_timestampz != table2.col_timestampz)") + AssertPostgreClauseSerialize(t, table1ColTimestampz.NOT_EQ(timestampz), "(table1.col_timestampz != $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } func TestTimestampzExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestampz.IS_DISTINCT_FROM(table2ColTimestampz), "(table1.col_timestampz IS DISTINCT FROM table2.col_timestampz)") - assertPostgreClauseSerialize(t, table1ColTimestampz.IS_DISTINCT_FROM(timestampz), "(table1.col_timestampz IS DISTINCT FROM $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") + AssertPostgreClauseSerialize(t, table1ColTimestampz.IS_DISTINCT_FROM(table2ColTimestampz), "(table1.col_timestampz IS DISTINCT FROM table2.col_timestampz)") + AssertPostgreClauseSerialize(t, table1ColTimestampz.IS_DISTINCT_FROM(timestampz), "(table1.col_timestampz IS DISTINCT FROM $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } func TestTimestampzExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestampz.IS_NOT_DISTINCT_FROM(table2ColTimestampz), "(table1.col_timestampz IS NOT DISTINCT FROM table2.col_timestampz)") - assertPostgreClauseSerialize(t, table1ColTimestampz.IS_NOT_DISTINCT_FROM(timestampz), "(table1.col_timestampz IS NOT DISTINCT FROM $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") + AssertPostgreClauseSerialize(t, table1ColTimestampz.IS_NOT_DISTINCT_FROM(table2ColTimestampz), "(table1.col_timestampz IS NOT DISTINCT FROM table2.col_timestampz)") + AssertPostgreClauseSerialize(t, table1ColTimestampz.IS_NOT_DISTINCT_FROM(timestampz), "(table1.col_timestampz IS NOT DISTINCT FROM $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } func TestTimestampzExpressionLT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestampz.LT(table2ColTimestampz), "(table1.col_timestampz < table2.col_timestampz)") - assertPostgreClauseSerialize(t, table1ColTimestampz.LT(timestampz), "(table1.col_timestampz < $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") + AssertPostgreClauseSerialize(t, table1ColTimestampz.LT(table2ColTimestampz), "(table1.col_timestampz < table2.col_timestampz)") + AssertPostgreClauseSerialize(t, table1ColTimestampz.LT(timestampz), "(table1.col_timestampz < $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } func TestTimestampzExpressionLT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestampz.LT_EQ(table2ColTimestampz), "(table1.col_timestampz <= table2.col_timestampz)") - assertPostgreClauseSerialize(t, table1ColTimestampz.LT_EQ(timestampz), "(table1.col_timestampz <= $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") + AssertPostgreClauseSerialize(t, table1ColTimestampz.LT_EQ(table2ColTimestampz), "(table1.col_timestampz <= table2.col_timestampz)") + AssertPostgreClauseSerialize(t, table1ColTimestampz.LT_EQ(timestampz), "(table1.col_timestampz <= $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } func TestTimestampzExpressionGT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestampz.GT(table2ColTimestampz), "(table1.col_timestampz > table2.col_timestampz)") - assertPostgreClauseSerialize(t, table1ColTimestampz.GT(timestampz), "(table1.col_timestampz > $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") + AssertPostgreClauseSerialize(t, table1ColTimestampz.GT(table2ColTimestampz), "(table1.col_timestampz > table2.col_timestampz)") + AssertPostgreClauseSerialize(t, table1ColTimestampz.GT(timestampz), "(table1.col_timestampz > $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } func TestTimestampzExpressionGT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimestampz.GT_EQ(table2ColTimestampz), "(table1.col_timestampz >= table2.col_timestampz)") - assertPostgreClauseSerialize(t, table1ColTimestampz.GT_EQ(timestampz), "(table1.col_timestampz >= $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") + AssertPostgreClauseSerialize(t, table1ColTimestampz.GT_EQ(table2ColTimestampz), "(table1.col_timestampz >= table2.col_timestampz)") + AssertPostgreClauseSerialize(t, table1ColTimestampz.GT_EQ(timestampz), "(table1.col_timestampz >= $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } func TestTimestampzExp(t *testing.T) { - assertPostgreClauseSerialize(t, TimestampzExp(table1ColFloat), "table1.col_float") - assertPostgreClauseSerialize(t, TimestampzExp(table1ColFloat).LT(timestampz), + AssertPostgreClauseSerialize(t, TimestampzExp(table1ColFloat), "table1.col_float") + AssertPostgreClauseSerialize(t, TimestampzExp(table1ColFloat).LT(timestampz), "(table1.col_float < $1::timestamp with time zone)", "2000-01-31 10:20:00.000 +002") } diff --git a/timez_expression_test.go b/timez_expression_test.go index df553e6..7263b72 100644 --- a/timez_expression_test.go +++ b/timez_expression_test.go @@ -1,3 +1,5 @@ +// +build TODO + package jet import "testing" @@ -5,47 +7,47 @@ import "testing" var timezVar = Timez(10, 20, 0, 0, 4) func TestTimezExpressionEQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimez.EQ(table2ColTimez), "(table1.col_timez = table2.col_timez)") - assertPostgreClauseSerialize(t, table1ColTimez.EQ(timezVar), "(table1.col_timez = $1::time with time zone)", "10:20:00.000 +04") + AssertPostgreClauseSerialize(t, table1ColTimez.EQ(table2ColTimez), "(table1.col_timez = table2.col_timez)") + AssertPostgreClauseSerialize(t, table1ColTimez.EQ(timezVar), "(table1.col_timez = $1::time with time zone)", "10:20:00.000 +04") } func TestTimezExpressionNOT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimez.NOT_EQ(table2ColTimez), "(table1.col_timez != table2.col_timez)") - assertPostgreClauseSerialize(t, table1ColTimez.NOT_EQ(timezVar), "(table1.col_timez != $1::time with time zone)", "10:20:00.000 +04") + AssertPostgreClauseSerialize(t, table1ColTimez.NOT_EQ(table2ColTimez), "(table1.col_timez != table2.col_timez)") + AssertPostgreClauseSerialize(t, table1ColTimez.NOT_EQ(timezVar), "(table1.col_timez != $1::time with time zone)", "10:20:00.000 +04") } func TestTimezExpressionIS_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimez.IS_DISTINCT_FROM(table2ColTimez), "(table1.col_timez IS DISTINCT FROM table2.col_timez)") - assertPostgreClauseSerialize(t, table1ColTimez.IS_DISTINCT_FROM(timezVar), "(table1.col_timez IS DISTINCT FROM $1::time with time zone)", "10:20:00.000 +04") + AssertPostgreClauseSerialize(t, table1ColTimez.IS_DISTINCT_FROM(table2ColTimez), "(table1.col_timez IS DISTINCT FROM table2.col_timez)") + AssertPostgreClauseSerialize(t, table1ColTimez.IS_DISTINCT_FROM(timezVar), "(table1.col_timez IS DISTINCT FROM $1::time with time zone)", "10:20:00.000 +04") } func TestTimezExpressionIS_NOT_DISTINCT_FROM(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimez.IS_NOT_DISTINCT_FROM(table2ColTimez), "(table1.col_timez IS NOT DISTINCT FROM table2.col_timez)") - assertPostgreClauseSerialize(t, table1ColTimez.IS_NOT_DISTINCT_FROM(timezVar), "(table1.col_timez IS NOT DISTINCT FROM $1::time with time zone)", "10:20:00.000 +04") + AssertPostgreClauseSerialize(t, table1ColTimez.IS_NOT_DISTINCT_FROM(table2ColTimez), "(table1.col_timez IS NOT DISTINCT FROM table2.col_timez)") + AssertPostgreClauseSerialize(t, table1ColTimez.IS_NOT_DISTINCT_FROM(timezVar), "(table1.col_timez IS NOT DISTINCT FROM $1::time with time zone)", "10:20:00.000 +04") } func TestTimezExpressionLT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimez.LT(table2ColTimez), "(table1.col_timez < table2.col_timez)") - assertPostgreClauseSerialize(t, table1ColTimez.LT(timezVar), "(table1.col_timez < $1::time with time zone)", "10:20:00.000 +04") + AssertPostgreClauseSerialize(t, table1ColTimez.LT(table2ColTimez), "(table1.col_timez < table2.col_timez)") + AssertPostgreClauseSerialize(t, table1ColTimez.LT(timezVar), "(table1.col_timez < $1::time with time zone)", "10:20:00.000 +04") } func TestTimezExpressionLT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimez.LT_EQ(table2ColTimez), "(table1.col_timez <= table2.col_timez)") - assertPostgreClauseSerialize(t, table1ColTimez.LT_EQ(timezVar), "(table1.col_timez <= $1::time with time zone)", "10:20:00.000 +04") + AssertPostgreClauseSerialize(t, table1ColTimez.LT_EQ(table2ColTimez), "(table1.col_timez <= table2.col_timez)") + AssertPostgreClauseSerialize(t, table1ColTimez.LT_EQ(timezVar), "(table1.col_timez <= $1::time with time zone)", "10:20:00.000 +04") } func TestTimezExpressionGT(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimez.GT(table2ColTimez), "(table1.col_timez > table2.col_timez)") - assertPostgreClauseSerialize(t, table1ColTimez.GT(timezVar), "(table1.col_timez > $1::time with time zone)", "10:20:00.000 +04") + AssertPostgreClauseSerialize(t, table1ColTimez.GT(table2ColTimez), "(table1.col_timez > table2.col_timez)") + AssertPostgreClauseSerialize(t, table1ColTimez.GT(timezVar), "(table1.col_timez > $1::time with time zone)", "10:20:00.000 +04") } func TestTimezExpressionGT_EQ(t *testing.T) { - assertPostgreClauseSerialize(t, table1ColTimez.GT_EQ(table2ColTimez), "(table1.col_timez >= table2.col_timez)") - assertPostgreClauseSerialize(t, table1ColTimez.GT_EQ(timezVar), "(table1.col_timez >= $1::time with time zone)", "10:20:00.000 +04") + AssertPostgreClauseSerialize(t, table1ColTimez.GT_EQ(table2ColTimez), "(table1.col_timez >= table2.col_timez)") + AssertPostgreClauseSerialize(t, table1ColTimez.GT_EQ(timezVar), "(table1.col_timez >= $1::time with time zone)", "10:20:00.000 +04") } func TestTimezExp(t *testing.T) { - assertPostgreClauseSerialize(t, TimezExp(table1ColFloat), "table1.col_float") - assertPostgreClauseSerialize(t, TimezExp(table1ColFloat).LT(Timez(1, 1, 1, 1, 4)), + AssertPostgreClauseSerialize(t, TimezExp(table1ColFloat), "table1.col_float") + AssertPostgreClauseSerialize(t, TimezExp(table1ColFloat).LT(Timez(1, 1, 1, 1, 4)), "(table1.col_float < $1::time with time zone)", string("01:01:01.001 +04")) }