Add ability to subscribe to table updates.

This was implemented based on
[this](https://www.sqlite.org/c3ref/update_hook.html) document.
This commit is contained in:
Connor Rigby
2018-11-29 13:29:15 -08:00
parent ab25c2d2b6
commit 8fa063a3f1
4 changed files with 307 additions and 187 deletions

View File

@@ -20,6 +20,19 @@ open_multiple_different_databases_test() ->
{ok, _C2} = esqlite3:open("test2.db"),
ok.
update_hook_test() ->
{ok, Db} = esqlite3:open(":memory:"),
ok = esqlite3:set_update_hook(self(), Db),
ok = esqlite3:exec("CREATE TABLE test (id INTEGER PRIMARY KEY, val STRING);", Db),
ok = esqlite3:exec("INSERT INTO test (val) VALUES ('this is a test');", Db),
ok = receive {insert, "test", 1} -> ok after 150 -> no_message end,
ok = esqlite3:exec("UPDATE test SET val = 'a new test' WHERE id = 1;", Db),
ok = receive {update, "test", 1} -> ok after 150 -> no_message end,
ok = esqlite3:exec("DELETE FROM test WHERE id = 1;", Db),
ok = receive {delete, "test", 1} -> ok after 150 -> no_message end,
ok.
simple_query_test() ->
{ok, Db} = esqlite3:open(":memory:"),
ok = esqlite3:exec("begin;", Db),
@@ -38,7 +51,7 @@ simple_query_test() ->
ok = esqlite3:exec("delete from test_table;", Db),
{ok, 4} = esqlite3:changes(Db),
ok.
prepare_test() ->
@@ -46,11 +59,11 @@ prepare_test() ->
esqlite3:exec("begin;", Db),
esqlite3:exec("create table test_table(one varchar(10), two int);", Db),
{ok, Statement} = esqlite3:prepare("insert into test_table values(\"one\", 2)", Db),
'$done' = esqlite3:step(Statement),
{ok, 1} = esqlite3:changes(Db),
ok = esqlite3:exec(["insert into test_table values(", "\"hello4\"", ",", "13" ");"], Db),
ok = esqlite3:exec(["insert into test_table values(", "\"hello4\"", ",", "13" ");"], Db),
%% Check if the values are there.
[{<<"one">>, 2}, {<<"hello4">>, 13}] = esqlite3:q("select * from test_table order by two", Db),
@@ -61,7 +74,7 @@ prepare_test() ->
bind_test() ->
{ok, Db} = esqlite3:open(":memory:"),
ok = esqlite3:exec("begin;", Db),
ok = esqlite3:exec("create table test_table(one varchar(10), two int);", Db),
ok = esqlite3:exec("commit;", Db),
@@ -70,9 +83,9 @@ bind_test() ->
{ok, Statement} = esqlite3:prepare("insert into test_table values(?1, ?2)", Db),
esqlite3:bind(Statement, [one, 2]),
esqlite3:step(Statement),
esqlite3:bind(Statement, ["three", 4]),
esqlite3:bind(Statement, ["three", 4]),
esqlite3:step(Statement),
esqlite3:bind(Statement, ["five", 6]),
esqlite3:bind(Statement, ["five", 6]),
esqlite3:step(Statement),
esqlite3:bind(Statement, [[<<"se">>, $v, "en"], 8]), % iolist bound as text
esqlite3:step(Statement),
@@ -91,47 +104,47 @@ bind_test() ->
%% utf-8
esqlite3:bind(Statement, [[<<228,184,138,230,181,183>>], 100]),
esqlite3:bind(Statement, [[<<228,184,138,230,181,183>>], 100]),
esqlite3:step(Statement),
?assertEqual([{<<"one">>, 2}],
?assertEqual([{<<"one">>, 2}],
esqlite3:q("select one, two from test_table where two = '2'", Db)),
?assertEqual([{<<"three">>, 4}],
?assertEqual([{<<"three">>, 4}],
esqlite3:q("select one, two from test_table where two = 4", Db)),
?assertEqual([{<<"five">>, 6}],
?assertEqual([{<<"five">>, 6}],
esqlite3:q("select one, two from test_table where two = 6", Db)),
?assertEqual([{<<"seven">>, 8}],
?assertEqual([{<<"seven">>, 8}],
esqlite3:q("select one, two from test_table where two = 8", Db)),
?assertEqual([{<<"nine">>, 10}],
?assertEqual([{<<"nine">>, 10}],
esqlite3:q("select one, two from test_table where two = 10", Db)),
?assertEqual([{{blob, <<$e,$l,$e,$v,$e,$n,0>>}, 12}],
?assertEqual([{{blob, <<$e,$l,$e,$v,$e,$n,0>>}, 12}],
esqlite3:q("select one, two from test_table where two = 12", Db)),
?assertEqual([{<<"int64">>, 308553449069486081}],
?assertEqual([{<<"int64">>, 308553449069486081}],
esqlite3:q("select one, two from test_table where one = 'int64';", Db)),
?assertEqual([{<<"negative_int64">>, -308553449069486081}],
?assertEqual([{<<"negative_int64">>, -308553449069486081}],
esqlite3:q("select one, two from test_table where one = 'negative_int64';", Db)),
%% utf-8
?assertEqual([{<<228,184,138,230,181,183>>, 100}],
?assertEqual([{<<228,184,138,230,181,183>>, 100}],
esqlite3:q("select one, two from test_table where two = 100", Db)),
ok.
bind_for_queries_test() ->
{ok, Db} = esqlite3:open(":memory:"),
ok = esqlite3:exec("begin;", Db),
ok = esqlite3:exec("create table test_table(one varchar(10), two int);", Db),
ok = esqlite3:exec("commit;", Db),
?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
[test_table], Db)),
?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
["test_table"], Db)),
?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
[<<"test_table">>], Db)),
?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
[[<<"test_table">>]], Db)),
ok.
@@ -254,21 +267,21 @@ foreach_test() ->
ok = esqlite3:exec("commit;", Db),
F = fun(Row) ->
case Row of
case Row of
{Key, Value} ->
put(Key, Value);
_ ->
ok
end
end,
esqlite3:foreach(F, "select * from test_table;", Db),
10 = get(<<"hello1">>),
11 = get(<<"hello2">>),
12 = get(<<"hello3">>),
12 = get(<<"hello3">>),
13 = get(<<"hello4">>),
ok.
map_test() ->
@@ -282,12 +295,12 @@ map_test() ->
ok = esqlite3:exec("commit;", Db),
F = fun(Row) -> Row end,
[{<<"hello1">>,10},{<<"hello2">>,11},{<<"hello3">>,12},{<<"hello4">>,13}]
[{<<"hello1">>,10},{<<"hello2">>,11},{<<"hello3">>,12},{<<"hello4">>,13}]
= esqlite3:map(F, "select * from test_table", Db),
%% Test that when the row-names are added..
Assoc = fun(Names, Row) ->
Assoc = fun(Names, Row) ->
lists:zip(tuple_to_list(Names), tuple_to_list(Row))
end,
@@ -295,15 +308,15 @@ map_test() ->
[{one,<<"hello2">>},{two,11}],
[{one,<<"hello3">>},{two,12}],
[{one,<<"hello4">>},{two,13}]] = esqlite3:map(Assoc, "select * from test_table", Db),
ok.
error1_msg_test() ->
{ok, Db} = esqlite3:open(":memory:"),
%% Not sql.
{error, {sqlite_error, _Msg1}} = esqlite3:exec("dit is geen sql", Db),
%% Database test does not exist.
{error, {sqlite_error, _Msg2}} = esqlite3:exec("select * from test;", Db),
@@ -332,7 +345,7 @@ prepare_and_close_connection_test() ->
'$done' = esqlite3:step(Stmt),
ok.
sqlite_version_test() ->
{ok, Db} = esqlite3:open(":memory:"),
{ok, Stmt} = esqlite3:prepare("select sqlite_version() as sqlite_version;", Db),
@@ -346,7 +359,7 @@ sqlite_source_id_test() ->
{sqlite_source_id} = esqlite3:column_names(Stmt),
?assertEqual({row, {<<"2018-06-04 19:24:41 c7ee0833225bfd8c5ec2f9bf62b97c4e04d03bd9566366d5221ac8fb199a87ca">>}}, esqlite3:step(Stmt)),
ok.
garbage_collect_test() ->
F = fun() ->
{ok, Db} = esqlite3:open(":memory:"),
@@ -367,4 +380,3 @@ garbage_collect_test() ->