Skip to content

Commit

Permalink
Merge remote-tracking branch 'wg/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Maier committed Jun 28, 2013
2 parents f24cee4 + 3318bd5 commit 0f1eb3f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Erlang PostgreSQL Database Client
ok = pgsql:close(C).

The timeout parameter will trigger an {error, timeout} result when the
server fails to respond within Timeout milliseconds.
server fails to respond within Timeout milliseconds. This timeout applies
to the initial connection attempt and any subsequent queries.

* Simple Query

Expand Down
8 changes: 7 additions & 1 deletion src/pgsql_binary.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ encode(boolarray, L) when is_list(L) -> encode_array(bool, L);
encode(int2array, L) when is_list(L) -> encode_array(int2, L);
encode(int4array, L) when is_list(L) -> encode_array(int4, L);
encode(int8array, L) when is_list(L) -> encode_array(int8, L);
encode(float4array, L) when is_list(L) -> encode_array(float4, L);
encode(float8array, L) when is_list(L) -> encode_array(float8, L);
encode(chararray, L) when is_list(L) -> encode_array(bpchar, L);
encode(textarray, L) when is_list(L) -> encode_array(text, L);
encode(Type, L) when is_list(L) -> encode(Type, list_to_binary(L));
Expand All @@ -54,14 +56,16 @@ decode(boolarray, B) -> decode_array(B);
decode(int2array, B) -> decode_array(B);
decode(int4array, B) -> decode_array(B);
decode(int8array, B) -> decode_array(B);
decode(float4array, B) -> decode_array(B);
decode(float8array, B) -> decode_array(B);
decode(chararray, B) -> decode_array(B);
decode(textarray, B) -> decode_array(B);
decode(_Other, Bin) -> Bin.

encode_array(Type, A) ->
{Data, {NDims, Lengths}} = encode_array(Type, A, 0, []),
Oid = pgsql_types:type2oid(Type),
Lens = [<<N:?int32, 0:?int32>> || N <- lists:reverse(Lengths)],
Lens = [<<N:?int32, 1:?int32>> || N <- lists:reverse(Lengths)],
Hdr = <<NDims:?int32, 0:?int32, Oid:?int32>>,
Bin = iolist_to_binary([Hdr, Lens, Data]),
<<(byte_size(Bin)):?int32, Bin/binary>>.
Expand Down Expand Up @@ -130,6 +134,8 @@ supports(boolarray) -> true;
supports(int2array) -> true;
supports(int4array) -> true;
supports(int8array) -> true;
supports(float4array) -> true;
supports(float8array) -> true;
supports(chararray) -> true;
supports(textarray) -> true;
supports(_Type) -> false.
2 changes: 2 additions & 0 deletions src/pgsql_types.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ oid2type(1009) -> textarray;
oid2type(1014) -> chararray;
oid2type(1016) -> int8array;
oid2type(1021) -> float4array;
oid2type(1022) -> float8array;
oid2type(1033) -> aclitem;
oid2type(1263) -> cstringarray;
oid2type(1042) -> bpchar;
Expand Down Expand Up @@ -129,6 +130,7 @@ type2oid(textarray) -> 1009;
type2oid(chararray) -> 1014;
type2oid(int8array) -> 1016;
type2oid(float4array) -> 1021;
type2oid(float8array) -> 1022;
type2oid(aclitem) -> 1033;
type2oid(cstringarray) -> 1263;
type2oid(bpchar) -> 1042;
Expand Down
31 changes: 19 additions & 12 deletions test_src/pgsql_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -427,19 +427,26 @@ misc_type_test() ->
array_type_test() ->
with_connection(
fun(C) ->
Select = fun(Type, V) ->
Query = "select $1::" ++ Type,
{ok, _Cols, [{V}]} = pgsql:equery(C, Query, [V])
{ok, _, [{[1, 2]}]} = pgsql:equery(C, "select ($1::int[])[1:2]", [[1, 2, 3]]),
Select = fun(Type, A) ->
Query = "select $1::" ++ atom_to_list(Type) ++ "[]",
{ok, _Cols, [{A2}]} = pgsql:equery(C, Query, [A]),
case lists:all(fun({V, V2}) -> compare(Type, V, V2) end, lists:zip(A, A2)) of
true -> ok;
false -> ?assertMatch(A, A2)
end
end,
Select("int2[]", []),
Select("int2[]", [1, 2, 3, 4]),
Select("int2[]", [[1], [2], [3], [4]]),
Select("int2[]", [[[[[[1, 2]]]]]]),
Select("bool[]", [true]),
Select("char[]", [$a, $b, $c]),
Select("int4[]", [[1, 2]]),
Select("int8[]", [[[[1, 2]], [[3, 4]]]]),
Select("text[]", [<<"one">>, <<"two>">>])
Select(int2, []),
Select(int2, [1, 2, 3, 4]),
Select(int2, [[1], [2], [3], [4]]),
Select(int2, [[[[[[1, 2]]]]]]),
Select(bool, [true]),
Select(char, [$a, $b, $c]),
Select(int4, [[1, 2]]),
Select(int8, [[[[1, 2]], [[3, 4]]]]),
Select(text, [<<"one">>, <<"two>">>]),
Select(float4, [0.0, 1.0, 0.123]),
Select(float8, [0.0, 1.0, 0.123])
end).

text_format_test() ->
Expand Down

0 comments on commit 0f1eb3f

Please sign in to comment.