Browse Source

Fix bpcahar parameter is list. Fixes #159

Сергей Прохоров 7 years ago
parent
commit
ee5fc534fe
2 changed files with 12 additions and 2 deletions
  1. 8 1
      src/datatypes/epgsql_codec_bpchar.erl
  2. 4 1
      test/epgsql_SUITE.erl

+ 8 - 1
src/datatypes/epgsql_codec_bpchar.erl

@@ -24,7 +24,14 @@ names() ->
 encode(C, _, _) when is_integer(C), C =< 255 ->
     <<C:1/big-unsigned-unit:8>>;
 encode(Bin, bpchar, _) when is_binary(Bin) ->
-    Bin.
+    Bin;
+encode(Str, bpchar, _) when is_list(Str) ->
+    %% See epgsql_codec_text:encode/3
+    try iolist_size(Str) of
+        _ -> Str
+    catch error:badarg ->
+            unicode:characters_to_binary(Str)
+    end.
 
 decode(<<C:1/big-unsigned-unit:8>>, _, _) -> C;
 decode(Bin, bpchar, _) -> Bin.

+ 4 - 1
test/epgsql_SUITE.erl

@@ -699,7 +699,10 @@ character_type(Config) ->
                            Module:equery(C, "SELECT $1::varchar", [12345])),
               FloatBin = erlang:float_to_binary(1.2345),
               ?assertMatch({ok, _, [{FloatBin}]},
-                           Module:equery(C, "SELECT $1::varchar", [1.2345]))
+                           Module:equery(C, "SELECT $1::varchar", [1.2345])),
+              %% String bpchar
+              ?assertMatch({ok, _, [{<<"hello world">>}]},
+                           Module:equery(C, "SELECT $1::bpchar", ["hello world"]))
       end).
 
 uuid_type(Config) ->