From 5238291b09e197b933973beeb8fb1a5a1410650b Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Mon, 26 Aug 2024 19:10:03 -0400 Subject: [PATCH 1/2] range fix --- lib/postgrex/extensions/multirange.ex | 1 + lib/postgrex/extensions/range.ex | 1 + test/calendar_test.exs | 6 ++++++ test/query_test.exs | 21 +++++++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/lib/postgrex/extensions/multirange.ex b/lib/postgrex/extensions/multirange.ex index 11bb2c20..b23624f6 100644 --- a/lib/postgrex/extensions/multirange.ex +++ b/lib/postgrex/extensions/multirange.ex @@ -34,6 +34,7 @@ defmodule Postgrex.Extensions.Multirange do type = case type do {extension, sub_oids, sub_types} -> {extension, sub_oids, sub_types, nil} + {extension, _} -> {extension, nil} extension -> {extension, nil} end diff --git a/lib/postgrex/extensions/range.ex b/lib/postgrex/extensions/range.ex index 92c0278f..091b348b 100644 --- a/lib/postgrex/extensions/range.ex +++ b/lib/postgrex/extensions/range.ex @@ -42,6 +42,7 @@ defmodule Postgrex.Extensions.Range do type = case type do {extension, sub_oids, sub_types} -> {extension, sub_oids, sub_types, nil} + {extension, _} -> {extension, nil} extension -> {extension, nil} end diff --git a/test/calendar_test.exs b/test/calendar_test.exs index eef776da..36c0df6a 100644 --- a/test/calendar_test.exs +++ b/test/calendar_test.exs @@ -27,6 +27,12 @@ defmodule CalendarTest do test "decode time with precision", context do assert [[~T[00:00:00.000]]] = query("SELECT time(3) '00:00:00'", []) assert [[[~T[00:00:00.000]]]] = query("SELECT ARRAY[time(3) '00:00:00']", []) + + assert [[[[~T[00:00:00.000], ~T[00:00:00.000]], [~T[01:00:00.000], ~T[01:00:00.000]]]]] = + query( + "SELECT ARRAY[ARRAY[time(3) '00:00:00', time(3) '00:00:00'], ARRAY[time(3) '01:00:00', time(3) '01:00:00']]", + [] + ) end test "decode timetz", context do diff --git a/test/query_test.exs b/test/query_test.exs index 1eb4b2c0..72240411 100644 --- a/test/query_test.exs +++ b/test/query_test.exs @@ -531,6 +531,27 @@ defmodule QueryTest do } ] ] = query("SELECT '[2014-1-1,2014-12-31)'::tsrange", []) + + assert [ + [ + [ + %Postgrex.Range{ + lower: ~N[2014-01-01 00:00:00.000000], + upper: ~N[2014-12-31 00:00:00.000000], + lower_inclusive: true, + upper_inclusive: false + }, + %Postgrex.Range{ + lower: ~N[2014-01-01 00:00:00.000000], + upper: ~N[2014-12-31 00:00:00.000000], + lower_inclusive: true, + upper_inclusive: false + } + ] + ] + ] + + query("SELECT ARRAY['[2014-1-1,2014-12-31)'::tsrange, '[2014-1-1,2014-12-31)'::tsrange]", []) end @tag min_pg_version: "14.0" From 53e65b49865899e9c0b07f40cec52929662601e5 Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Mon, 26 Aug 2024 23:10:57 -0400 Subject: [PATCH 2/2] oh boy --- lib/postgrex/extensions/array.ex | 4 ++-- lib/postgrex/extensions/multirange.ex | 5 ++--- lib/postgrex/extensions/range.ex | 5 ++--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/postgrex/extensions/array.ex b/lib/postgrex/extensions/array.ex index 92d3a959..da3df290 100644 --- a/lib/postgrex/extensions/array.ex +++ b/lib/postgrex/extensions/array.ex @@ -33,13 +33,13 @@ defmodule Postgrex.Extensions.Array do data::binary>> = binary # decode_list/2 defined by TypeModule - type = + sub_type_with_mod = case type do {extension, sub_oids, sub_types} -> {extension, sub_oids, sub_types, var!(mod)} extension -> {extension, var!(mod)} end - flat = decode_list(data, type) + flat = decode_list(data, sub_type_with_mod) unquote(__MODULE__).decode(dims, flat) end diff --git a/lib/postgrex/extensions/multirange.ex b/lib/postgrex/extensions/multirange.ex index b23624f6..f644c253 100644 --- a/lib/postgrex/extensions/multirange.ex +++ b/lib/postgrex/extensions/multirange.ex @@ -31,14 +31,13 @@ defmodule Postgrex.Extensions.Multirange do <<_num_ranges::int32(), ranges::binary>> = data # decode_list/2 defined by TypeModule - type = + sub_type_with_mod = case type do {extension, sub_oids, sub_types} -> {extension, sub_oids, sub_types, nil} - {extension, _} -> {extension, nil} extension -> {extension, nil} end - bound_decoder = &decode_list(&1, type) + bound_decoder = &decode_list(&1, sub_type_with_mod) unquote(__MODULE__).decode(ranges, bound_decoder, []) end end diff --git a/lib/postgrex/extensions/range.ex b/lib/postgrex/extensions/range.ex index 091b348b..e6ab46e6 100644 --- a/lib/postgrex/extensions/range.ex +++ b/lib/postgrex/extensions/range.ex @@ -39,14 +39,13 @@ defmodule Postgrex.Extensions.Range do <> = binary # decode_list/2 defined by TypeModule - type = + sub_type_with_mod = case type do {extension, sub_oids, sub_types} -> {extension, sub_oids, sub_types, nil} - {extension, _} -> {extension, nil} extension -> {extension, nil} end - case decode_list(data, type) do + case decode_list(data, sub_type_with_mod) do [upper, lower] -> unquote(__MODULE__).decode(flags, [lower, upper])