Browse Source

fix encoding of text format parameters

Will 16 years ago
parent
commit
e22ae5940e
2 changed files with 16 additions and 2 deletions
  1. 1 2
      src/pgsql_connection.erl
  2. 15 0
      test_src/pgsql_tests.erl

+ 1 - 2
src/pgsql_connection.erl

@@ -579,9 +579,8 @@ encode_parameters([P | T], Count, Formats, Values) ->
 encode_parameter({Type, Value}) ->
     case pgsql_binary:encode(Type, Value) of
         Bin when is_binary(Bin) -> {1, Bin};
-        {error, unsupported}    -> {0, Value}
+        {error, unsupported}    -> encode_parameter(Value)
     end;
-encode_parameter(null)                 -> {1, pgsql_binary:encode(null, null)};
 encode_parameter(A) when is_atom(A)    -> {0, encode_list(atom_to_list(A))};
 encode_parameter(B) when is_binary(B)  -> {0, <<(byte_size(B)):?int32, B/binary>>};
 encode_parameter(I) when is_integer(I) -> {0, encode_list(integer_to_list(I))};

+ 15 - 0
test_src/pgsql_tests.erl

@@ -379,6 +379,21 @@ encode_binary_format_test() ->
               {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_varchar) values ($1)", ["hi"])
       end).
 
+text_format_test() ->
+    with_connection(
+      fun(C) ->
+              Select = fun(Type, V) ->
+                               V2 = list_to_binary(V),
+                               Query = "select $1::" ++ Type,
+                               {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V]),
+                               {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V2])
+                       end,
+              Select("timestamp", "2000-01-02 03:04:05"),
+              Select("date", "2000-01-02"),
+              Select("time", "03:04:05"),
+              Select("numeric", "123456")
+      end).
+
 %% -- run all tests --
 
 run_tests() ->