From 792f89a8571cf054c02daecc9341a634d567d557 Mon Sep 17 00:00:00 2001 From: Karl Blomster Date: Fri, 3 Jun 2022 16:11:24 +0200 Subject: [PATCH 1/3] mysql: export some conditional functions COALESCE, NULLIF, GREATEST and LEAST already existed and were available in the postgres dialect, but not in MySQL. --- mysql/functions.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/mysql/functions.go b/mysql/functions.go index 2a8e227..24cd73c 100644 --- a/mysql/functions.go +++ b/mysql/functions.go @@ -261,10 +261,22 @@ func UNIX_TIMESTAMP(str StringExpression) TimestampExpression { return jet.NewTimestampFunc("UNIX_TIMESTAMP", str) } -//----------- Comparison operators ---------------// +// --------------- Conditional Expressions Functions -------------// // EXISTS checks for existence of the rows in subQuery var EXISTS = jet.EXISTS // CASE create CASE operator with optional list of expressions var CASE = jet.CASE + +// COALESCE function returns the first of its arguments that is not null. +var COALESCE = jet.COALESCE + +// NULLIF function returns a null value if value1 equals value2; otherwise it returns value1. +var NULLIF = jet.NULLIF + +// GREATEST selects the largest value from a list of expressions, or null if any of the expressions is null. +var GREATEST = jet.GREATEST + +// LEAST selects the smallest value from a list of expressions, or null if any of the expressions is null. +var LEAST = jet.LEAST From 0425e8895c398eaf649f2dc7c4b4be472b52af72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Zahradn=C3=ADk?= Date: Sat, 4 Jun 2022 14:12:08 +0200 Subject: [PATCH 2/3] add postgres json literal --- postgres/literal.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/postgres/literal.go b/postgres/literal.go index 7b1bd19..929e394 100644 --- a/postgres/literal.go +++ b/postgres/literal.go @@ -65,6 +65,11 @@ func String(value string) StringExpression { return CAST(jet.String(value)).AS_TEXT() } +// Json creates new json literal expression +func Json(value string) StringExpression { + return StringExp(CAST(jet.String(value)).AS("json")) +} + // 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 From 11b0a6858a8a84c5a4a4bbbe4bf401a32503b517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Zahradn=C3=ADk?= Date: Sat, 4 Jun 2022 14:22:33 +0200 Subject: [PATCH 3/3] Better json func, and tests --- postgres/literal.go | 9 +++++++-- postgres/literal_test.go | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/postgres/literal.go b/postgres/literal.go index 929e394..e3a95b3 100644 --- a/postgres/literal.go +++ b/postgres/literal.go @@ -66,8 +66,13 @@ func String(value string) StringExpression { } // Json creates new json literal expression -func Json(value string) StringExpression { - return StringExp(CAST(jet.String(value)).AS("json")) +func Json(value interface{}) StringExpression { + switch value.(type) { + case string, []byte: + default: + panic("Bytea parameter value has to be of the type string or []byte") + } + return StringExp(CAST(jet.Literal(value)).AS("json")) } // UUID is a helper function to create string literal expression from uuid object diff --git a/postgres/literal_test.go b/postgres/literal_test.go index 5c5160e..9fef055 100644 --- a/postgres/literal_test.go +++ b/postgres/literal_test.go @@ -67,6 +67,11 @@ func TestBytea(t *testing.T) { assertSerialize(t, Bytea([]byte("Some byte array")), `$1::bytea`, []byte("Some byte array")) } +func TestJson(t *testing.T) { + assertSerialize(t, Json("{\"key\": \"value\"}"), `$1::json`, "{\"key\": \"value\"}") + assertSerialize(t, Json([]byte("{\"key\": \"value\"}")), `$1::json`, []byte("{\"key\": \"value\"}")) +} + func TestDate(t *testing.T) { assertSerialize(t, Date(2014, time.January, 2), `$1::date`, "2014-01-02") assertSerialize(t, DateT(time.Now()), `$1::date`)