1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% @copyright 2021, Erlang Solutions Ltd. |
3 |
|
%%% @doc Proxy module for rdbms backends. |
4 |
|
%%% |
5 |
|
%%% @end |
6 |
|
%%%------------------------------------------------------------------- |
7 |
|
-module(mongoose_rdbms_backend). |
8 |
|
-export([escape_binary/1, |
9 |
|
escape_string/1, |
10 |
|
unescape_binary/1, |
11 |
|
connect/2, |
12 |
|
disconnect/1, |
13 |
|
query/3, |
14 |
|
prepare/5, |
15 |
|
execute/4]). |
16 |
|
|
17 |
|
-define(MAIN_MODULE, mongoose_rdbms). |
18 |
|
|
19 |
|
-type options() :: mongoose_rdbms:options(). |
20 |
|
|
21 |
|
-callback escape_binary(binary()) -> mongoose_rdbms:sql_query_part(). |
22 |
|
-callback escape_string(binary()|list()) -> mongoose_rdbms:sql_query_part(). |
23 |
|
|
24 |
|
-callback unescape_binary(binary()) -> binary(). |
25 |
|
-callback connect(options(), QueryTimeout :: non_neg_integer()) -> |
26 |
|
{ok, Connection :: term()} | {error, Reason :: any()}. |
27 |
|
-callback disconnect(Connection :: term()) -> any(). |
28 |
|
-callback query(Connection :: term(), Query :: any(), Timeout :: infinity | non_neg_integer()) -> |
29 |
|
mongoose_rdbms:query_result(). |
30 |
|
-callback prepare(Connection :: term(), Name :: atom(), |
31 |
|
Table :: binary(), Fields :: [binary()], Statement :: iodata()) -> |
32 |
|
{ok, Ref :: term()} | {error, Reason :: any()}. |
33 |
|
-callback execute(Connection :: term(), Ref :: term(), Parameters :: [term()], |
34 |
|
Timeout :: infinity | non_neg_integer()) -> mongoose_rdbms:query_result(). |
35 |
|
|
36 |
|
%% If not defined, generic escaping is used |
37 |
|
-optional_callbacks([escape_string/1]). |
38 |
|
|
39 |
|
|
40 |
|
-spec escape_binary(binary()) -> mongoose_rdbms:sql_query_part(). |
41 |
|
escape_binary(Binary) -> |
42 |
87 |
Args = [Binary], |
43 |
87 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
44 |
|
|
45 |
|
-spec escape_string(binary() | list()) -> mongoose_rdbms:sql_query_part(). |
46 |
|
escape_string(String) -> |
47 |
57 |
Args = [String], |
48 |
57 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
49 |
|
|
50 |
|
-spec unescape_binary(binary()) -> binary(). |
51 |
|
unescape_binary(Binary) -> |
52 |
6539 |
Args = [Binary], |
53 |
6539 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
54 |
|
|
55 |
|
-spec connect(options(), QueryTimeout :: non_neg_integer()) -> |
56 |
|
{ok, Connection :: term()} | {error, Reason :: any()}. |
57 |
|
connect(Options, QueryTimeout) -> |
58 |
420 |
Args = [Options, QueryTimeout], |
59 |
420 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
60 |
|
|
61 |
|
-spec disconnect(Connection :: term()) -> any(). |
62 |
|
disconnect(Connection) -> |
63 |
410 |
Args = [Connection], |
64 |
410 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
65 |
|
|
66 |
|
-spec query(Connection :: term(), Query :: any(), Timeout :: infinity | non_neg_integer()) -> |
67 |
|
mongoose_rdbms:query_result(). |
68 |
|
query(Connection, Query, Timeout) -> |
69 |
35702 |
Args = [Connection, Query, Timeout], |
70 |
35702 |
mongoose_backend:call_tracked(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
71 |
|
|
72 |
|
-spec prepare(Connection :: term(), Name :: atom(), |
73 |
|
Table :: binary(), Fields :: [binary()], Statement :: iodata()) -> |
74 |
|
{ok, Ref :: term()} | {error, Reason :: any()}. |
75 |
|
prepare(Connection, Name, Table, Fields, Statement) -> |
76 |
3059 |
Args = [Connection, Name, Table, Fields, Statement], |
77 |
3059 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
78 |
|
|
79 |
|
-spec execute(Connection :: term(), Ref :: term(), Parameters :: [term()], |
80 |
|
Timeout :: infinity | non_neg_integer()) -> mongoose_rdbms:query_result(). |
81 |
|
execute(Connection, Ref, Parameters, Timeout) -> |
82 |
148135 |
Args = [Connection, Ref, Parameters, Timeout], |
83 |
148135 |
mongoose_backend:call_tracked(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |