Skip to content

Commit

Permalink
Merge pull request #3643 from esl/rdbms_request_transactions
Browse files Browse the repository at this point in the history
Add transaction async requests
  • Loading branch information
chrzaszcz authored May 10, 2022
2 parents 72db059 + 199523f commit a5d00fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions big_tests/tests/rdbms_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ rdbms_queries_cases() ->
insert_batch_with_null_case,
test_cast_insert,
test_request_insert,
test_request_transaction,
test_incremental_upsert,
arguments_from_two_tables].

Expand Down Expand Up @@ -374,6 +375,18 @@ test_request_insert(Config) ->
selected_to_sorted(SelectResult))
end, ok, #{name => request_queries}).

test_request_transaction(Config) ->
erase_table(Config),
Queries = [<<"INSERT INTO test_types(unicode) VALUES ('check1')">>,
<<"INSERT INTO test_types(unicode) VALUES ('check2')">>],
sql_transaction_request(Config, Queries),
mongoose_helper:wait_until(
fun() ->
SelectResult = sql_query(Config, "SELECT unicode FROM test_types"),
?assertEqual({selected, [{<<"check1">>}, {<<"check2">>}]},
selected_to_sorted(SelectResult))
end, ok, #{name => request_queries}).

test_incremental_upsert(Config) ->
case is_odbc() of
true ->
Expand Down Expand Up @@ -438,6 +451,9 @@ sql_execute_upsert(_Config, Name, Insert, Update, Unique) ->
sql_query_request(_Config, Query) ->
slow_rpc(mongoose_rdbms, sql_query_request, [host_type(), Query]).

sql_transaction_request(_Config, Query) ->
slow_rpc(mongoose_rdbms, sql_transaction_request, [host_type(), Query]).

escape_null(_Config) ->
escalus_ejabberd:rpc(mongoose_rdbms, escape_null, []).

Expand Down
14 changes: 13 additions & 1 deletion src/rdbms/mongoose_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
sql_query_request/2,
sql_query_t/1,
sql_transaction/2,
sql_transaction_request/2,
transaction_with_delayed_retry/3,
sql_dirty/2,
to_bool/1,
Expand Down Expand Up @@ -117,7 +118,9 @@
terminate/2,
code_change/3]).

-ignore_xref([sql_query_cast/2, sql_query_request/2, execute_cast/3, execute_request/3,
-ignore_xref([sql_query_cast/2, sql_query_request/2,
execute_cast/3, execute_request/3,
sql_transaction_request/2,
sql_query_t/1, use_escaped/1,
escape_like/1, escape_like_prefix/1, use_escaped_like/1,
escape_binary/2, use_escaped_binary/1,
Expand Down Expand Up @@ -255,6 +258,15 @@ sql_transaction(HostType, Queries) when is_list(Queries) ->
sql_transaction(HostType, F) when is_function(F) ->
sql_call(HostType, {sql_transaction, F}).

%% @doc SQL transaction based on a list of queries
-spec sql_transaction_request(server(), fun() | maybe_improper_list()) -> transaction_result().
sql_transaction_request(HostType, Queries) when is_list(Queries) ->
F = fun() -> lists:map(fun sql_query_t/1, Queries) end,
sql_transaction_request(HostType, F);
%% SQL transaction, based on a erlang anonymous function (F = fun)
sql_transaction_request(HostType, F) when is_function(F) ->
sql_request(HostType, {sql_transaction, F}).

%% This function allows to specify delay between retries.
-spec transaction_with_delayed_retry(server(), fun() | maybe_improper_list(), map()) -> transaction_result().
transaction_with_delayed_retry(HostType, F, Info) ->
Expand Down

0 comments on commit a5d00fc

Please sign in to comment.