From 09fe45b09cc9dff7a0d62feeb055e3e66eb6d07b Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 20 Feb 2024 23:56:11 +0530 Subject: [PATCH 1/2] mysql: added a helper to compare UUID strings with uuid_to_bin --- mysql/literal.go | 15 ++++++++++++++- mysql/literal_test.go | 10 ++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mysql/literal.go b/mysql/literal.go index 1c69c31b..3c7c07a8 100644 --- a/mysql/literal.go +++ b/mysql/literal.go @@ -1,8 +1,10 @@ package mysql import ( - "github.com/go-jet/jet/v2/internal/jet" + "fmt" "time" + + "github.com/go-jet/jet/v2/internal/jet" ) // Keywords @@ -55,6 +57,17 @@ var String = jet.String // value can be any uuid type with a String method var UUID = jet.UUID +// UUIDToBin takes ay object with a String method and calls StringUUIDToBin. +func UUIDToBin(str fmt.Stringer) StringExpression { + return StringUUIDToBin(str.String()) +} + +// StringUUIDToBin is a helper function that calls "uuid_to_bin" function on the passed value. +func StringUUIDToBin(str string) StringExpression { + fn := Func("uuid_to_bin", String(str)) + return StringExp(fn) +} + // Date creates new date literal func Date(year int, month time.Month, day int) DateExpression { return CAST(jet.Date(year, month, day)).AS_DATE() diff --git a/mysql/literal_test.go b/mysql/literal_test.go index fb966417..d5831b6a 100644 --- a/mysql/literal_test.go +++ b/mysql/literal_test.go @@ -4,6 +4,8 @@ import ( "math" "testing" "time" + + "github.com/google/uuid" ) func TestBool(t *testing.T) { @@ -81,3 +83,11 @@ func TestTimestamp(t *testing.T) { assertSerialize(t, Timestamp(2010, time.March, 30, 10, 15, 30), `TIMESTAMP(?)`, "2010-03-30 10:15:30") assertSerialize(t, TimestampT(time.Now()), `TIMESTAMP(?)`) } + +func TestUUIDToBin(t *testing.T) { + assertSerialize(t, UUIDToBin(uuid.Nil), `uuid_to_bin(?)`, uuid.Nil.String()) +} + +func TestStringUUIDToBin(t *testing.T) { + assertSerialize(t, StringUUIDToBin(uuid.Nil.String()), `uuid_to_bin(?)`, uuid.Nil.String()) +} From 33ec120437b23edbc05a798491ca523c92beaad1 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 22 Feb 2024 17:23:14 +0530 Subject: [PATCH 2/2] replaced the UUIDToBin functions with a singular UUID_TO_BIN --- mysql/functions.go | 6 ++++++ mysql/functions_test.go | 11 +++++++++++ mysql/literal.go | 12 ------------ mysql/literal_test.go | 10 ---------- 4 files changed, 17 insertions(+), 22 deletions(-) create mode 100644 mysql/functions_test.go diff --git a/mysql/functions.go b/mysql/functions.go index 4eba9424..ca31d18d 100644 --- a/mysql/functions.go +++ b/mysql/functions.go @@ -222,6 +222,12 @@ var SUBSTR = jet.SUBSTR // REGEXP_LIKE Returns 1 if the string expr matches the regular expression specified by the pattern pat, 0 otherwise. var REGEXP_LIKE = jet.REGEXP_LIKE +// UUID_TO_BIN is a helper function that calls "uuid_to_bin" function on the passed value. +func UUID_TO_BIN(str StringExpression) StringExpression { + fn := Func("uuid_to_bin", str) + return StringExp(fn) +} + //----------------- Date/Time Functions and Operators ------------// // EXTRACT function retrieves subfields such as year or hour from date/time values diff --git a/mysql/functions_test.go b/mysql/functions_test.go new file mode 100644 index 00000000..197b515d --- /dev/null +++ b/mysql/functions_test.go @@ -0,0 +1,11 @@ +package mysql + +import ( + "testing" + + "github.com/google/uuid" +) + +func TestUUIDToBin(t *testing.T) { + assertSerialize(t, UUID_TO_BIN(String(uuid.Nil.String())), `uuid_to_bin(?)`, uuid.Nil.String()) +} diff --git a/mysql/literal.go b/mysql/literal.go index 3c7c07a8..ca720c82 100644 --- a/mysql/literal.go +++ b/mysql/literal.go @@ -1,7 +1,6 @@ package mysql import ( - "fmt" "time" "github.com/go-jet/jet/v2/internal/jet" @@ -57,17 +56,6 @@ var String = jet.String // value can be any uuid type with a String method var UUID = jet.UUID -// UUIDToBin takes ay object with a String method and calls StringUUIDToBin. -func UUIDToBin(str fmt.Stringer) StringExpression { - return StringUUIDToBin(str.String()) -} - -// StringUUIDToBin is a helper function that calls "uuid_to_bin" function on the passed value. -func StringUUIDToBin(str string) StringExpression { - fn := Func("uuid_to_bin", String(str)) - return StringExp(fn) -} - // Date creates new date literal func Date(year int, month time.Month, day int) DateExpression { return CAST(jet.Date(year, month, day)).AS_DATE() diff --git a/mysql/literal_test.go b/mysql/literal_test.go index d5831b6a..fb966417 100644 --- a/mysql/literal_test.go +++ b/mysql/literal_test.go @@ -4,8 +4,6 @@ import ( "math" "testing" "time" - - "github.com/google/uuid" ) func TestBool(t *testing.T) { @@ -83,11 +81,3 @@ func TestTimestamp(t *testing.T) { assertSerialize(t, Timestamp(2010, time.March, 30, 10, 15, 30), `TIMESTAMP(?)`, "2010-03-30 10:15:30") assertSerialize(t, TimestampT(time.Now()), `TIMESTAMP(?)`) } - -func TestUUIDToBin(t *testing.T) { - assertSerialize(t, UUIDToBin(uuid.Nil), `uuid_to_bin(?)`, uuid.Nil.String()) -} - -func TestStringUUIDToBin(t *testing.T) { - assertSerialize(t, StringUUIDToBin(uuid.Nil.String()), `uuid_to_bin(?)`, uuid.Nil.String()) -}