Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Rychlewski authored and Greg Rychlewski committed Jul 1, 2024
1 parent c44df85 commit 7057e83
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
8 changes: 7 additions & 1 deletion lib/postgrex/extensions/array.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ defmodule Postgrex.Extensions.Array do
data::binary>> = binary

# decode_list/2 defined by TypeModule
flat = decode_list(data, type, mod)
type =
case type do
{extension, sub_oids, sub_types} -> {extension, sub_oids, sub_types, mod}
extension -> {extension, mod}
end

flat = decode_list(data, type)

unquote(__MODULE__).decode(dims, flat)
end
Expand Down
9 changes: 8 additions & 1 deletion lib/postgrex/extensions/multirange.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ defmodule Postgrex.Extensions.Multirange do

def decode(_) do
quote location: :keep do
<<len::int32(), data::binary-size(len)>>, [_oid], [type] ->
<<len::int32(), data::binary-size(len)>>, [_oid], [type], _ ->
<<_num_ranges::int32(), ranges::binary>> = data

# decode_list/2 defined by TypeModule
type =
case type do
{extension, sub_oids, sub_types} -> {extension, sub_oids, sub_types, nil}
extension -> {extension, nil}
end

bound_decoder = &decode_list(&1, type)
unquote(__MODULE__).decode(ranges, bound_decoder, [])
end
Expand Down
9 changes: 8 additions & 1 deletion lib/postgrex/extensions/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ defmodule Postgrex.Extensions.Range do

def decode(_) do
quote location: :keep do
<<len::int32(), binary::binary-size(len)>>, [_oid], [type] ->
<<len::int32(), binary::binary-size(len)>>, [_oid], [type], _ ->
<<flags, data::binary>> = binary

# decode_list/2 defined by TypeModule
type =
case type do
{extension, sub_oids, sub_types} -> {extension, sub_oids, sub_types, nil}
extension -> {extension, nil}
end

case decode_list(data, type) do
[upper, lower] ->
unquote(__MODULE__).decode(flags, [lower, upper])
Expand Down
4 changes: 2 additions & 2 deletions lib/postgrex/extensions/record.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ defmodule Postgrex.Extensions.Record do

def decode(_) do
quote location: :keep do
<<len::int32(), binary::binary-size(len)>>, nil, types ->
<<len::int32(), binary::binary-size(len)>>, nil, types, _ ->
<<count::int32(), data::binary>> = binary
# decode_tuple/3 defined by TypeModule
decode_tuple(data, count, types)

<<len::int32(), binary::binary-size(len)>>, oids, types ->
<<len::int32(), binary::binary-size(len)>>, oids, types, _ ->
<<_::int32(), data::binary>> = binary
# decode_tuple/3 defined by TypeModule
decode_tuple(data, oids, types)
Expand Down
29 changes: 17 additions & 12 deletions lib/postgrex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1716,8 +1716,9 @@ defmodule Postgrex.Protocol do
end

defp describe_params(%{types: types}, query, param_oids) do
with {:ok, param_info} <- fetch_type_info(param_oids, types),
{param_formats, param_types} = Enum.unzip(param_info) do
with {:ok, param_info} <- fetch_type_info(param_oids, types) do
{param_formats, param_types} = Enum.unzip(param_info)

query = %Query{
query
| param_oids: param_oids,
Expand Down Expand Up @@ -1761,9 +1762,17 @@ defmodule Postgrex.Protocol do
end

defp describe_result(%{types: types}, query, result_oids, result_mods, columns) do
with {:ok, result_info} <- fetch_type_info(result_oids, types),
{result_formats, result_types} = Enum.unzip(result_info),
result_types = Enum.zip(result_types, result_mods) do
with {:ok, result_info} <- fetch_type_info(result_oids, types) do
{result_formats, result_types} = Enum.unzip(result_info)

result_types =
result_types
|> Enum.zip(result_mods)
|> Enum.map(fn
{{extension, sub_oids, sub_types}, mod} -> {extension, sub_oids, sub_types, mod}
{extension, mod} -> {extension, mod}
end)

query = %Query{
query
| ref: make_ref(),
Expand Down Expand Up @@ -3187,13 +3196,9 @@ defmodule Postgrex.Protocol do
end

defp columns(fields) do
{oids_mods, names} =
fields
|> Enum.map(fn row_field(type_oid: oid, type_mod: mod, name: name) -> {{oid, mod}, name} end)
|> :lists.unzip()

{oids, mods} = :lists.unzip(oids_mods)
{oids, names, mods}
fields
|> Enum.map(fn row_field(type_oid: oid, type_mod: mod, name: name) -> {oid, name, mod} end)
|> :lists.unzip3()
end

defp column_names(fields) do
Expand Down
14 changes: 4 additions & 10 deletions lib/postgrex/type_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ defmodule Postgrex.TypeModule do
defp decode_row_dispatch(extension, :super_binary, rest, acc, rem, full, rows) do
[clause] =
quote do
[{{unquote(extension), sub_oids, sub_types}, mod} | types] ->
[{unquote(extension), sub_oids, sub_types, mod} | types] ->
unquote(extension)(
unquote(rest),
sub_oids,
Expand Down Expand Up @@ -458,8 +458,8 @@ defmodule Postgrex.TypeModule do

quote do
@doc false
def decode_list(<<unquote(rest)::binary>>, type, mod \\ nil) do
case {type, mod} do
def decode_list(<<unquote(rest)::binary>>, type) do
case type do
unquote(dispatch)
end
end
Expand All @@ -469,7 +469,7 @@ defmodule Postgrex.TypeModule do
defp decode_list_dispatch(extension, :super_binary, rest) do
[clause] =
quote do
{{unquote(extension), sub_oids, sub_types}, mod} ->
{unquote(extension), sub_oids, sub_types, mod} ->
unquote(extension)(unquote(rest), sub_oids, sub_types, mod, [])
end

Expand Down Expand Up @@ -1023,12 +1023,6 @@ defmodule Postgrex.TypeModule do

defp split_super_decode({:->, _, [head, body]}) do
case head do
[{:when, _, [pattern, sub_oids, sub_types, guard]}] ->
{pattern, sub_oids, sub_types, quote(do: _), guard, body}

[pattern, sub_oids, sub_types] ->
{pattern, sub_oids, sub_types, quote(do: _), body}

[{:when, _, [pattern, sub_oids, sub_types, modifier, guard]}] ->
{pattern, sub_oids, sub_types, modifier, guard, body}

Expand Down

0 comments on commit 7057e83

Please sign in to comment.