Skip to content

Commit

Permalink
Merge pull request #18 from nakagami/issue17
Browse files Browse the repository at this point in the history
fix over 4 bytes int parameter #17
  • Loading branch information
nakagami authored May 8, 2024
2 parents c9e4e6b + 8038359 commit 0597660
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/efirebirdsql_conv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ byte4(N, little) ->
byte4(N) ->
byte4(N, big).

%%% big endian number list fill 8 byte alignment
-spec byte8(integer(), atom()) -> list().
byte8(N, big) ->
LB = binary:encode_unsigned(N, big),
LB8 = case size(LB) of
1 -> << <<0,0,0,0,0,0,0>>/binary, LB/binary >>;
2 -> << <<0,0,0,0,0,0>>/binary, LB/binary >>;
3 -> << <<0,0,0,0,0>>/binary, LB/binary >>;
4 -> << <<0,0,0,0>>/binary, LB/binary >>;
5 -> << <<0,0,0>>/binary, LB/binary >>;
6 -> << <<0,0>>/binary, LB/binary >>;
7 -> << <<0>>/binary, LB/binary >>;
8 -> LB
end,
binary_to_list(LB8).

byte8(N) ->
byte8(N, big).

%%% 4 byte padding
-spec pad4(list()) -> list().
pad4(L) ->
Expand Down Expand Up @@ -155,8 +174,10 @@ param_to_date(Year, Month, Day) ->
param_to_time(Hour, Minute, Second, Microsecond) ->
byte4((Hour*3600 + Minute*60 + Second) * 10000 + Microsecond div 100).

param_to_blr(V, _) when is_integer(V) ->
param_to_blr(V, _) when is_integer(V) and (V =< 16#7FFFFFFF) and (V >= -16#80000000) ->
{[8, 0, 7, 0], byte4(V)};
param_to_blr(V, _) when is_integer(V) ->
{[16, 0, 7, 0], byte8(V)};
param_to_blr(V, _) when is_binary(V) ->
B = binary_to_list(V),
{lists:flatten([14, byte2(length(B)), 7, 0]),
Expand Down
12 changes: 8 additions & 4 deletions test/efirebirdsql_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ create_test_tables(C) ->
h1 BLOB SUB_TYPE 1,
i DOUBLE PRECISION DEFAULT 1.0,
j FLOAT DEFAULT 2.0,
k BIGINT DEFAULT 123456789999999999,
PRIMARY KEY (a),
CONSTRAINT CHECK_A CHECK (a <> 0)
)
">>),
ok = efirebirdsql:execute(C, <<"insert into foo(a, b, c, h0, h1) values (?,?,?,?,?)">>, [1, <<"b">>, <<"c">>, nil, <<"blob">>]),
ok = efirebirdsql:execute(C, <<"insert into foo(a, b, c, h1) values (?,?,?,?)">>, [2, <<"B">>, <<"C">>, <<"BLOB">>]),
ok = efirebirdsql:execute(C, <<"insert into foo(a, b, c, h1, k) values (?,?,?,?,?)">>, [2, <<"B">>, <<"C">>, <<"BLOB">>, 123456780000000000]),

ok = efirebirdsql:execute(C, <<"
CREATE PROCEDURE foo_proc
Expand Down Expand Up @@ -70,7 +71,8 @@ description() ->
{<<"H0">>,blob,0,8,true},
{<<"H1">>,blob,4,8,true},
{<<"I">>,double,0,8,true},
{<<"J">>,float,0,4,true}].
{<<"J">>,float,0,4,true},
{<<"K">>,int64,0,8,true}].

alias_description() ->
[{<<"ALIAS_NAME">>,long,0,4,false}].
Expand All @@ -86,7 +88,8 @@ result1() ->
{<<"H0">>,nil},
{<<"H1">>,<<"blob">>},
{<<"I">>,1.0},
{<<"J">>,2.0}].
{<<"J">>,2.0},
{<<"K">>,123456789999999999}].

result2() ->
[{<<"A">>,2},
Expand All @@ -99,7 +102,8 @@ result2() ->
{<<"H0">>,nil},
{<<"H1">>,<<"BLOB">>},
{<<"I">>,1.0},
{<<"J">>,2.0}].
{<<"J">>,2.0},
{<<"K">>,123456780000000000}].

basic_test() ->
%% connect to bad database
Expand Down

0 comments on commit 0597660

Please sign in to comment.