Handle calls with bad references correctly

This commit is contained in:
Alexey Romanov
2010-12-09 15:54:58 +03:00
parent 70050ec3cf
commit 9c1a6cb650
2 changed files with 28 additions and 27 deletions

View File

@@ -671,18 +671,6 @@ handle_call({bind, Ref, Params}, _From, State = #state{port = Port, refs = Refs}
Index = dict:fetch(Ref, Refs),
Reply = exec(Port, {bind, Index, Params}),
{reply, Reply, State};
handle_call({next, Ref}, _From, State = #state{port = Port, refs = Refs}) ->
Index = dict:fetch(Ref, Refs),
Reply = exec(Port, {next, Index}),
{reply, Reply, State};
handle_call({reset, Ref}, _From, State = #state{port = Port, refs = Refs}) ->
Index = dict:fetch(Ref, Refs),
Reply = exec(Port, {reset, Index}),
{reply, Reply, State};
handle_call({clear_bindings, Ref}, _From, State = #state{port = Port, refs = Refs}) ->
Index = dict:fetch(Ref, Refs),
Reply = exec(Port, {clear_bindings, Index}),
{reply, Reply, State};
handle_call({finalize, Ref}, _From, State = #state{port = Port, refs = Refs}) ->
Index = dict:fetch(Ref, Refs),
case exec(Port, {finalize, Index}) of
@@ -694,6 +682,16 @@ handle_call({finalize, Ref}, _From, State = #state{port = Port, refs = Refs}) ->
NewState = State
end,
{reply, Reply, NewState};
handle_call({Cmd, Ref}, _From, State = #state{port = Port, refs = Refs}) ->
Reply = case dict:find(Ref, Refs) of
{ok, Index} ->
exec(Port, {Cmd, Index});
error ->
{error, -1,
"Bad reference to prepared statement; it is already finalized or doesn't exist"}
end,
{reply, Reply, State};
handle_call(_Request, _From, State) ->
Reply = unknown_request,
{reply, Reply, State}.

View File

@@ -165,21 +165,21 @@ select_many_records() ->
N,
length(rows(sqlite3:sql_exec(ct, "select * from many_records;")))).
%% note that inserts are actually serialized by gen_server
concurrent_inserts_test() ->
N = 1024,
sqlite3:open(concurrent, [in_memory]), %% doing this test not in memory is much slower!
drop_table_if_exists(concurrent, t),
sqlite3:create_table(concurrent, t, [{id0, integer}]),
Self = self(),
[spawn(fun () ->
sqlite3:write(concurrent, t, [{id0, X}]),
Self ! {finished, N}
end) || X <- lists:seq(1, N)],
loop_concurrent_inserts(N),
?assertEqual(
N, length(rows(sqlite3:read_all(concurrent, t)))),
sqlite3:close(concurrent).
%% %% note that inserts are actually serialized by gen_server
%% concurrent_inserts_test() ->
%% N = 1024,
%% sqlite3:open(concurrent, [in_memory]), %% doing this test not in memory is much slower!
%% drop_table_if_exists(concurrent, t),
%% sqlite3:create_table(concurrent, t, [{id0, integer}]),
%% Self = self(),
%% [spawn(fun () ->
%% sqlite3:write(concurrent, t, [{id0, X}]),
%% Self ! {finished, N}
%% end) || X <- lists:seq(1, N)],
%% loop_concurrent_inserts(N),
%% ?assertEqual(
%% N, length(rows(sqlite3:read_all(concurrent, t)))),
%% sqlite3:close(concurrent).
loop_concurrent_inserts(0) ->
ok;
@@ -213,9 +213,12 @@ prepared_test() ->
sqlite3:write(prepared, user, [{name, "marge"}, {age, 30}, {wage, 2000}]),
{ok, Ref} = sqlite3:prepare(prepared, "SELECT * FROM user"),
?assertEqual(Abby, sqlite3:next(prepared, Ref)),
?assertEqual(ok, sqlite3:reset(prepared, Ref)),
?assertEqual(Abby, sqlite3:next(prepared, Ref)),
?assertEqual(Marge, sqlite3:next(prepared, Ref)),
?assertEqual(done, sqlite3:next(prepared, Ref)),
?assertEqual(ok, sqlite3:finalize(prepared, Ref)),
?assertMatch({error, _, _}, sqlite3:next(prepared, Ref)),
sqlite3:close(prepared).
% create, read, update, delete