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