Browse Source

Merge pull request #168 from egobrain/bugfix-nested-records-and-arrays

Bugfix decoding of nested records and arrays
Sergey Prokhorov 6 years ago
parent
commit
513211ed75
2 changed files with 24 additions and 8 deletions
  1. 2 8
      src/epgsql_binary.erl
  2. 22 0
      test/epgsql_SUITE.erl

+ 2 - 8
src/epgsql_binary.erl

@@ -200,14 +200,8 @@ decode_record(<<Size:?int32, Bin/binary>>, record, Codec) ->
 decode_record1(<<>>, 0, _Codec) -> [];
 decode_record1(<<_Type:?int32, -1:?int32, Rest/binary>>, Size, Codec) ->
     [null | decode_record1(Rest, Size - 1, Codec)];
-decode_record1(<<Oid:?int32, Len:?int32, ValueBin:Len/binary, Rest/binary>>, Size, #codec{oid_db = Db} = Codec) ->
-    Value =
-        case epgsql_oid_db:find_by_oid(Oid, Db) of
-            undefined -> ValueBin;
-            Type ->
-                {Name, Mod, State} = epgsql_oid_db:type_to_codec_entry(Type),
-                epgsql_codec:decode(Mod, ValueBin, Name, State)
-        end,
+decode_record1(<<Oid:?int32, Len:?int32, ValueBin:Len/binary, Rest/binary>>, Size, Codec) ->
+    Value = decode(ValueBin, oid_to_decoder(Oid, binary, Codec)),
     [Value | decode_record1(Rest, Size - 1, Codec)].
 
 

+ 22 - 0
test/epgsql_SUITE.erl

@@ -59,6 +59,7 @@ groups() ->
             hstore_type,
             net_type,
             array_type,
+            record_type,
             range_type,
             range8_type,
             date_time_range_type,
@@ -838,6 +839,27 @@ array_type(Config) ->
         Select(jsonb, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>])
     end).
 
+record_type(Config) ->
+    Module = ?config(module, Config),
+    epgsql_ct:with_connection(Config, fun(C) ->
+        Select = fun(Sql, Expected) ->
+            {ok, _Columns, [Row]} = Module:equery(C, Sql, []),
+            ?assertMatch(Expected, Row)
+        end,
+
+        %% Simple record
+        Select("select (1,2)", {{1, 2}}),
+
+        %% Record inside other record
+        Select("select (1, (select (2,3)))", {{1, {2, 3}}}),
+
+        %% Array inside record
+        Select("select (1, '{2,3}'::int[])", {{1, [2, 3]}}),
+
+        %% Array of records inside record
+        Select("select (0, ARRAY(select (id, value) from test_table1))", {{0,[{1,<<"one">>},{2,<<"two">>}]}})
+    end).
+
 custom_types(Config) ->
     Module = ?config(module, Config),
     epgsql_ct:with_connection(Config, fun(C) ->