Skip to content

Commit

Permalink
Fix bug #28 in handling quoted values in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
pguyot committed Dec 27, 2016
1 parent fa427d2 commit d7227cb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/pgsql.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[
{description, "pgsql driver"},
{id, "pgsql"},
{vsn, "26.0.0"},
{vsn, "26.0.1"},
{modules, [
pgsql_app,
pgsql_connection,
Expand Down
8 changes: 7 additions & 1 deletion src/pgsql.appup
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{"26.0.0",
{"26.0.1",
[
{"26.0.0", [
{load_module, pgsql_protocol, soft_purge, soft_purge, []}
]},
{"26", [
{load_module, pgsql_protocol, soft_purge, soft_purge, []}
]},
Expand Down Expand Up @@ -37,6 +40,9 @@
]}
],
[
{"26.0.0", [
{load_module, pgsql_protocol, soft_purge, soft_purge, []}
]},
{"26", [
{load_module, pgsql_protocol, soft_purge, soft_purge, []}
]},
Expand Down
30 changes: 18 additions & 12 deletions src/pgsql_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -934,19 +934,25 @@ decode_array_text(_Type, _OIDMap, <<"}", Next/binary>>, Acc) ->
{lists:reverse(Acc), Next};
decode_array_text(Type, OIDMap, <<",", Next/binary>>, Acc) ->
decode_array_text(Type, OIDMap, Next, Acc);
decode_array_text(Type, OIDMap, Content, Acc) ->
{Count, Next} = decode_array_text_find(Content, 0),
{Value, _} = split_binary(Content, Count),
decode_array_text(Type, OIDMap, Content0, Acc) ->
{Value, Rest} = decode_array_text0(Content0, false, []),
Element = decode_value_text(Type, Value, OIDMap),
decode_array_text(Type, OIDMap, Next, [Element|Acc]).

decode_array_text_find(<<",", Next/binary>>, Count) ->
{Count, Next};
decode_array_text_find(<<"}", _Next/binary>> = N, Count) ->
{Count, N};
decode_array_text_find(<<_, Next/binary>>, Count) ->
decode_array_text_find(Next, Count+1).

decode_array_text(Type, OIDMap, Rest, [Element|Acc]).

decode_array_text0(<<$", Rest/binary>>, false, []) ->
decode_array_text0(Rest, true, []);
decode_array_text0(<<$,, Rest/binary>>, false, Acc) ->
{list_to_binary(lists:reverse(Acc)), Rest};
decode_array_text0(<<$}, _/binary>> = Content, false, Acc) ->
{list_to_binary(lists:reverse(Acc)), Content};
decode_array_text0(<<$", $,, Rest/binary>>, true, Acc) ->
{list_to_binary(lists:reverse(Acc)), Rest};
decode_array_text0(<<$", $}, Rest/binary>>, true, Acc) ->
{list_to_binary(lists:reverse(Acc)), <<$}, Rest/binary>>};
decode_array_text0(<<$\\, C, Rest/binary>>, true, Acc) ->
decode_array_text0(Rest, true, [C | Acc]);
decode_array_text0(<<C, Rest/binary>>, Quoted, Acc) ->
decode_array_text0(Rest, Quoted, [C | Acc]).

decode_value_bin(?BOOLOID, <<0>>, _OIDMap, _IntegerDateTimes) -> false;
decode_value_bin(?BOOLOID, <<1>>, _OIDMap, _IntegerDateTimes) -> true;
Expand Down
23 changes: 23 additions & 0 deletions test/pgsql_connection_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,29 @@ array_types_test_() ->
end
}.

% https://github.com/semiocast/pgsql/issues/28
quoted_array_values_test_() ->
{setup,
fun() ->
{ok, SupPid} = pgsql_connection_sup:start_link(),
Conn = pgsql_connection:open("test", "test"),
{SupPid, Conn}
end,
fun({SupPid, Conn}) ->
pgsql_connection:close(Conn),
kill_sup(SupPid)
end,
fun({_SupPid, Conn}) ->
[
?_assertEqual({{select,1},[{{array,[<<"foo bar">>]}}]}, pgsql_connection:simple_query("select ARRAY['foo bar'];", Conn)),
?_assertEqual({{select,1},[{{array,[<<"foo, bar">>]}}]}, pgsql_connection:simple_query("select ARRAY['foo, bar'];", Conn)),
?_assertEqual({{select,1},[{{array,[<<"foo} bar">>]}}]}, pgsql_connection:simple_query("select ARRAY['foo} bar'];", Conn)),
?_assertEqual({{select,1},[{{array,[<<"foo \" bar">>]}}]}, pgsql_connection:simple_query("select ARRAY['foo \" bar'];", Conn)),
?_assertEqual({{select,1},[{{array,[{{2014,1,1},{12,12,12}}]}}]}, pgsql_connection:simple_query("select ARRAY['2014-01-01T12:12:12Z'::timestamp];", Conn))
]
end
}.

geometric_types_test_() ->
[{setup,
fun() ->
Expand Down

0 comments on commit d7227cb

Please sign in to comment.