From f30cbb9e89a4db4fb301fcf676f387306266ad05 Mon Sep 17 00:00:00 2001 From: go-jet Date: Tue, 11 May 2021 13:20:07 +0200 Subject: [PATCH] Add UUID helper function UUID creates string literal expression from uuid object. uuid can be any uuid type with a String method. --- internal/jet/literal_expression.go | 6 ++++++ mysql/literal.go | 4 ++++ postgres/literal.go | 4 ++++ tests/mysql/alltypes_test.go | 2 +- tests/postgres/sample_test.go | 5 ++++- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/jet/literal_expression.go b/internal/jet/literal_expression.go index b86cb8e..29560e4 100644 --- a/internal/jet/literal_expression.go +++ b/internal/jet/literal_expression.go @@ -409,3 +409,9 @@ func Raw(raw string, parent ...Expression) Expression { return rawExp } + +// UUID is a helper function to create string literal expression from uuid object +// value can be any uuid type with a String method +func UUID(value fmt.Stringer) StringExpression { + return String(value.String()) +} diff --git a/mysql/literal.go b/mysql/literal.go index 4a6544c..0a66eb3 100644 --- a/mysql/literal.go +++ b/mysql/literal.go @@ -51,6 +51,10 @@ var Decimal = jet.Decimal // String creates new string literal expression var String = jet.String +// UUID is a helper function to create string literal expression from uuid object +// value can be any uuid type with a String method +var UUID = jet.UUID + // Date creates new date literal var Date = func(year int, month time.Month, day int) DateExpression { return CAST(jet.Date(year, month, day)).AS_DATE() diff --git a/postgres/literal.go b/postgres/literal.go index ebfd85c..efba9ba 100644 --- a/postgres/literal.go +++ b/postgres/literal.go @@ -44,6 +44,10 @@ var Decimal = jet.Decimal // String creates new string literal expression var String = jet.String +// UUID is a helper function to create string literal expression from uuid object +// value can be any uuid type with a String method +var UUID = jet.UUID + // Bytea craates new bytea literal expression var Bytea = func(value string) StringExpression { return CAST(jet.String(value)).AS_BYTEA() diff --git a/tests/mysql/alltypes_test.go b/tests/mysql/alltypes_test.go index 8c1539a..70ca277 100644 --- a/tests/mysql/alltypes_test.go +++ b/tests/mysql/alltypes_test.go @@ -78,7 +78,7 @@ func TestUUID(t *testing.T) { require.NoError(t, err) require.True(t, dest.StrUUID != nil) require.True(t, dest.UUID.String() != uuid.UUID{}.String()) - require.True(t, dest.StrUUID.String() != uuid.UUID{}.String()) + require.Equal(t, dest.StrUUID.String(), "dc8daae3-b83b-11e9-8eb4-98ded00c39c6") require.Equal(t, dest.StrUUID.String(), dest.BinUUID.String()) requireLogged(t, query) } diff --git a/tests/postgres/sample_test.go b/tests/postgres/sample_test.go index bc82c7e..b554431 100644 --- a/tests/postgres/sample_test.go +++ b/tests/postgres/sample_test.go @@ -15,9 +15,12 @@ import ( ) func TestUUIDType(t *testing.T) { + + id := uuid.MustParse("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11") + query := AllTypes. SELECT(AllTypes.UUID, AllTypes.UUIDPtr). - WHERE(AllTypes.UUID.EQ(String("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"))) + WHERE(AllTypes.UUID.EQ(UUID(id))) testutils.AssertDebugStatementSql(t, query, ` SELECT all_types.uuid AS "all_types.uuid",