| 1 |
|
%% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com> |
| 2 |
|
%% |
| 3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
|
%% you may not use this file except in compliance with the License. |
| 5 |
|
%% You may obtain a copy of the License at |
| 6 |
|
%% |
| 7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
%% |
| 9 |
|
%% Unless required by applicable law or agreed to in writing, software |
| 10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
|
%% See the License for the specific language governing permissions and |
| 13 |
|
%% limitations under the License. |
| 14 |
|
|
| 15 |
|
|
| 16 |
|
-module(pgmp_mm). |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
-export([bind/1]). |
| 20 |
|
-export([callback_mode/0]). |
| 21 |
|
-export([describe/1]). |
| 22 |
|
-export([execute/1]). |
| 23 |
|
-export([init/1]). |
| 24 |
|
-export([parameters/1]). |
| 25 |
|
-export([parse/1]). |
| 26 |
|
-export([query/1]). |
| 27 |
|
-export([recv/1]). |
| 28 |
|
-export([start_link/1]). |
| 29 |
|
-export([sync/1]). |
| 30 |
|
-export([terminate/3]). |
| 31 |
|
-import(pgmp_statem, [nei/1]). |
| 32 |
|
-import(pgmp_statem, [send_request/1]). |
| 33 |
|
-include_lib("kernel/include/logger.hrl"). |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
start_link(Arg) -> |
| 37 |
66 |
gen_statem:start_link(?MODULE, [Arg], envy_gen:options(?MODULE)). |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
recv(#{tag := Tag, message := Message} = Arg) -> |
| 41 |
35775 |
send_request( |
| 42 |
|
maps:without( |
| 43 |
|
[tag, message], |
| 44 |
|
maybe_label(Arg#{request => {?FUNCTION_NAME, {Tag, Message}}}))). |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
query(Arg) -> |
| 48 |
205 |
send_request(Arg, ?FUNCTION_NAME). |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
parse(Arg) -> |
| 52 |
3803 |
send_request(Arg, ?FUNCTION_NAME). |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
sync(Arg) -> |
| 56 |
2 |
send_request(Arg, ?FUNCTION_NAME). |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
bind(Arg) -> |
| 60 |
3953 |
send_request(Arg, ?FUNCTION_NAME). |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
describe(Arg) -> |
| 64 |
5 |
send_request(Arg, ?FUNCTION_NAME). |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
execute(Arg) -> |
| 68 |
3951 |
send_request(Arg, ?FUNCTION_NAME). |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
parameters(Arg) -> |
| 72 |
1 |
send_request(Arg, ?FUNCTION_NAME). |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
-type arg() :: atom() | {atom(), any()}. |
| 76 |
|
|
| 77 |
|
-type action() :: query |
| 78 |
|
| parameters |
| 79 |
|
| parse |
| 80 |
|
| sync |
| 81 |
|
| bind |
| 82 |
|
| describe |
| 83 |
|
| execute. |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
send_request(Arg, Action) -> |
| 87 |
11920 |
?FUNCTION_NAME(Arg, Action, args(Action)). |
| 88 |
|
|
| 89 |
|
send_request(Arg, Action, Config) -> |
| 90 |
11920 |
send_request( |
| 91 |
|
maps:without( |
| 92 |
|
keys(Config), |
| 93 |
|
maybe_label( |
| 94 |
|
Arg#{request => {request, |
| 95 |
|
#{action => Action, |
| 96 |
|
args => args(Arg, Config)}}}))). |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
-spec args(action()) -> [arg()]. |
| 100 |
|
|
| 101 |
|
args(query) -> |
| 102 |
205 |
[sql]; |
| 103 |
|
|
| 104 |
|
args(parse) -> |
| 105 |
3803 |
[{name, <<>>}, sql]; |
| 106 |
|
|
| 107 |
|
args(sync) -> |
| 108 |
2 |
[]; |
| 109 |
|
|
| 110 |
|
args(bind) -> |
| 111 |
3953 |
[{name, <<>>}, |
| 112 |
|
{portal, <<>>}, |
| 113 |
|
{args, []}, |
| 114 |
|
{parameter, binary}, |
| 115 |
|
{result, binary}]; |
| 116 |
|
|
| 117 |
|
args(describe) -> |
| 118 |
5 |
[type, {name, <<>>}]; |
| 119 |
|
|
| 120 |
|
args(execute) -> |
| 121 |
3951 |
[{portal, <<>>}, {max_rows, 0}]; |
| 122 |
|
|
| 123 |
|
args(parameters) -> |
| 124 |
1 |
[]. |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
maybe_label(#{requests := _, label := _} = Arg) -> |
| 128 |
75 |
Arg; |
| 129 |
|
|
| 130 |
|
maybe_label(#{requests := _} = Arg) -> |
| 131 |
35775 |
Arg#{label => ?MODULE}; |
| 132 |
|
|
| 133 |
|
maybe_label(Arg) -> |
| 134 |
11845 |
Arg. |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
keys(Config) -> |
| 138 |
11920 |
lists:map( |
| 139 |
|
fun |
| 140 |
|
({Key, _}) -> |
| 141 |
31475 |
Key; |
| 142 |
|
|
| 143 |
|
(Key) -> |
| 144 |
4013 |
Key |
| 145 |
|
end, |
| 146 |
|
Config). |
| 147 |
|
|
| 148 |
|
|
| 149 |
|
args(Arg, Config) -> |
| 150 |
11920 |
lists:map( |
| 151 |
|
fun |
| 152 |
|
({Parameter, Default}) -> |
| 153 |
31475 |
maps:get(Parameter, Arg, Default); |
| 154 |
|
|
| 155 |
|
(Parameter) -> |
| 156 |
4013 |
maps:get(Parameter, Arg) |
| 157 |
|
end, |
| 158 |
|
Config). |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
init([Arg]) -> |
| 162 |
66 |
process_flag(trap_exit, true), |
| 163 |
66 |
{ok, |
| 164 |
|
unready, |
| 165 |
|
#{cache => ets:new(?MODULE, []), |
| 166 |
|
requests => gen_statem:reqids_new(), |
| 167 |
|
types_ready => false, |
| 168 |
|
config => Arg, |
| 169 |
|
parameters => #{}}, |
| 170 |
|
[{change_callback_module, pgmp_mm_bootstrap}, |
| 171 |
|
nei(join), |
| 172 |
|
nei(types_when_ready), |
| 173 |
|
nei(peer)]}. |
| 174 |
|
|
| 175 |
|
|
| 176 |
|
callback_mode() -> |
| 177 |
66 |
[handle_event_function, state_enter]. |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
terminate(_Reason, _State, #{config := #{scope := Scope, group := Group}}) -> |
| 181 |
:-( |
pg:leave(Scope, Group, self()); |
| 182 |
|
|
| 183 |
|
terminate(_Reason, _State, _Data) -> |
| 184 |
:-( |
ok. |