MySQL cast expressions. Simplified.

This commit is contained in:
go-jet 2019-08-01 10:39:57 +02:00
parent 53dbcd9bfc
commit c342f296ca
10 changed files with 103 additions and 81 deletions

View file

@ -12,31 +12,40 @@ import (
func TestCast(t *testing.T) {
query := SELECT(
CAST(String("test")).AS("CHAR CHARACTER SET utf8").AS("result.AS1"),
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(150)).AS_CHAR().AS("result.char1"),
CAST(Int(150)).AS_CHAR(30).AS("result.char2"),
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",
SELECT CAST(? AS CHAR CHARACTER SET utf8) AS "result.AS1",
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 CHAR) AS "result.char1",
CAST(? AS CHAR(30)) AS "result.char2",
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")
`, "test", "2011-02-02", "14:06:10", "2011-02-02 14:06:10", int64(150), int64(150), int64(5),
int64(10), int64(5), int64(10), "Some text")
type Result struct {
As1 string
Date time.Time
Time time.Time
DateTime time.Time
Char string
Char1 string
Char2 string
Signed int
Unsigned int
Binary string
@ -49,10 +58,12 @@ FROM test_sample.all_types;
assert.NilError(t, err)
assert.DeepEqual(t, dest, Result{
As1: "test",
Date: *testutils.Date("2011-02-02"),
Time: *testutils.TimeWithoutTimeZone("14:06:10"),
DateTime: *testutils.TimestampWithoutTimeZone("2011-02-02 14:06:10", 0),
Char: "150",
Char1: "150",
Char2: "150",
Signed: -5,
Unsigned: 15,
Binary: "Some text",

View file

@ -65,6 +65,7 @@ func TestExpressionOperators(t *testing.T) {
AllTypes.SmallIntPtr.NOT_IN(Int(11), Int(22), NULL),
AllTypes.SmallIntPtr.NOT_IN(AllTypes.SELECT(AllTypes.IntegerPtr)),
postgres.CAST(Int(150)).AS_CHAR(12),
postgres.CAST(String("TRUE")).AS_BOOL(),
postgres.CAST(String("111")).AS_SMALLINT(),
postgres.CAST(String("111")).AS_INTEGER(),
@ -320,7 +321,7 @@ SELECT (all_types.numeric = all_types.numeric) AS "eq1",
TRUNC(ABS(all_types.decimal), $29) AS "abs",
TRUNC(POWER(all_types.decimal, $30), $31) AS "power",
TRUNC(SQRT(all_types.decimal), $32) AS "sqrt",
TRUNC(CBRT(all_types.decimal)::decimal, $33) AS "cbrt",
TRUNC(CBRT(all_types.decimal)::DECIMAL, $33) AS "cbrt",
CEIL(all_types.real) AS "ceil",
FLOOR(all_types.real) AS "floor",
ROUND(all_types.decimal) AS "round1",