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 |
|
|
20 |
|
-callback escape_binary(binary()) -> mongoose_rdbms:sql_query_part(). |
21 |
|
-callback escape_string(binary()|list()) -> mongoose_rdbms:sql_query_part(). |
22 |
|
|
23 |
|
-callback unescape_binary(binary()) -> binary(). |
24 |
|
-callback connect(Args :: any(), QueryTimeout :: non_neg_integer()) -> |
25 |
|
{ok, Connection :: term()} | {error, Reason :: any()}. |
26 |
|
-callback disconnect(Connection :: term()) -> any(). |
27 |
|
-callback query(Connection :: term(), Query :: any(), Timeout :: infinity | non_neg_integer()) -> |
28 |
|
mongoose_rdbms:query_result(). |
29 |
|
-callback prepare(Connection :: term(), Name :: atom(), |
30 |
|
Table :: binary(), Fields :: [binary()], Statement :: iodata()) -> |
31 |
|
{ok, Ref :: term()} | {error, Reason :: any()}. |
32 |
|
-callback execute(Connection :: term(), Ref :: term(), Parameters :: [term()], |
33 |
|
Timeout :: infinity | non_neg_integer()) -> mongoose_rdbms:query_result(). |
34 |
|
|
35 |
|
%% If not defined, generic escaping is used |
36 |
|
-optional_callbacks([escape_string/1]). |
37 |
|
|
38 |
|
|
39 |
|
-spec escape_binary(binary()) -> mongoose_rdbms:sql_query_part(). |
40 |
|
escape_binary(Binary) -> |
41 |
87 |
Args = [Binary], |
42 |
87 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
43 |
|
|
44 |
|
-spec escape_string(binary() | list()) -> mongoose_rdbms:sql_query_part(). |
45 |
|
escape_string(String) -> |
46 |
:-( |
Args = [String], |
47 |
:-( |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
48 |
|
|
49 |
|
-spec unescape_binary(binary()) -> binary(). |
50 |
|
unescape_binary(Binary) -> |
51 |
6076 |
Args = [Binary], |
52 |
6076 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
53 |
|
|
54 |
|
-spec connect(Settings :: tuple(), QueryTimeout :: non_neg_integer()) -> |
55 |
|
{ok, Connection :: term()} | {error, Reason :: any()}. |
56 |
|
connect(Settings, QueryTimeout) -> |
57 |
380 |
Args = [Settings, QueryTimeout], |
58 |
380 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
59 |
|
|
60 |
|
-spec disconnect(Connection :: term()) -> any(). |
61 |
|
disconnect(Connection) -> |
62 |
365 |
Args = [Connection], |
63 |
365 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
64 |
|
|
65 |
|
-spec query(Connection :: term(), Query :: any(), Timeout :: infinity | non_neg_integer()) -> |
66 |
|
mongoose_rdbms:query_result(). |
67 |
|
query(Connection, Query, Timeout) -> |
68 |
24453 |
Args = [Connection, Query, Timeout], |
69 |
24453 |
mongoose_backend:call_tracked(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
70 |
|
|
71 |
|
-spec prepare(Connection :: term(), Name :: atom(), |
72 |
|
Table :: binary(), Fields :: [binary()], Statement :: iodata()) -> |
73 |
|
{ok, Ref :: term()} | {error, Reason :: any()}. |
74 |
|
prepare(Connection, Name, Table, Fields, Statement) -> |
75 |
1347 |
Args = [Connection, Name, Table, Fields, Statement], |
76 |
1347 |
mongoose_backend:call(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
77 |
|
|
78 |
|
-spec execute(Connection :: term(), Ref :: term(), Parameters :: [term()], |
79 |
|
Timeout :: infinity | non_neg_integer()) -> mongoose_rdbms:query_result(). |
80 |
|
execute(Connection, Ref, Parameters, Timeout) -> |
81 |
94316 |
Args = [Connection, Ref, Parameters, Timeout], |
82 |
94316 |
mongoose_backend:call_tracked(global, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |