| 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_connection_sync). |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
-export([bind/1]). |
| 20 |
|
-export([describe/1]). |
| 21 |
|
-export([execute/1]). |
| 22 |
|
-export([parameters/1]). |
| 23 |
|
-export([parse/1]). |
| 24 |
|
-export([query/1]). |
| 25 |
|
-export([sync/1]). |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
-type bind_request() :: #{name => iodata(), |
| 29 |
|
portal => iodata(), |
| 30 |
|
args => [any()], |
| 31 |
|
parameter => binary | text, |
| 32 |
|
result => binary | text}. |
| 33 |
|
-type bind_response() :: pgmp:bind_complete() |
| 34 |
|
| pgmp:error_response(). |
| 35 |
|
-spec bind(bind_request()) -> [bind_response(), ...]. |
| 36 |
|
|
| 37 |
|
bind(Arg) -> |
| 38 |
3938 |
receive_response(?FUNCTION_NAME, Arg). |
| 39 |
|
|
| 40 |
|
-type statement() :: $S. |
| 41 |
|
-type portal() :: $P. |
| 42 |
|
-type describe_type() :: statement() | portal(). |
| 43 |
|
-type describe_request() :: #{type := describe_type(), name => iodata()}. |
| 44 |
|
-type describe_response() :: pgmp:describe_row_description() |
| 45 |
|
| pgmp:parameter_description() |
| 46 |
|
| pgmp:error_response(). |
| 47 |
|
-spec describe(describe_request()) -> [describe_response(), ...]. |
| 48 |
|
|
| 49 |
|
describe(Arg) -> |
| 50 |
:-( |
receive_response(?FUNCTION_NAME, Arg). |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
-type execute_request() :: #{portal => iodata(), max_rows => non_neg_integer()}. |
| 54 |
|
-type execute_response() :: query_response() |
| 55 |
|
| pgmp:portal_suspended(). |
| 56 |
|
-spec execute(execute_request()) -> [execute_response(), ...]. |
| 57 |
|
|
| 58 |
|
execute(Arg) -> |
| 59 |
3936 |
receive_response(?FUNCTION_NAME, Arg). |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
-type parse_request() :: #{name => iodata(), sql := iodata()}. |
| 63 |
|
-type parse_response() :: pgmp:parse_complete() |
| 64 |
|
| pgmp:error_response(). |
| 65 |
|
-spec parse(parse_request()) -> [parse_response(), ...]. |
| 66 |
|
|
| 67 |
|
parse(Arg) -> |
| 68 |
3788 |
receive_response(?FUNCTION_NAME, Arg). |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
-type query_request() :: #{sql := iodata()}. |
| 72 |
|
-type query_response() :: pgmp:row_description() |
| 73 |
|
| pgmp:data_row() |
| 74 |
|
| pgmp:command_complete() |
| 75 |
|
| pgmp:error_response(). |
| 76 |
|
-spec query(query_request()) -> [query_response(), ...]. |
| 77 |
|
|
| 78 |
|
query(Arg) -> |
| 79 |
180 |
receive_response(?FUNCTION_NAME, Arg). |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
-type sync_request() :: #{}. |
| 83 |
|
-type sync_response() :: any(). |
| 84 |
|
-spec sync(sync_request()) -> sync_response(). |
| 85 |
|
|
| 86 |
|
sync(Arg) -> |
| 87 |
2 |
receive_response(?FUNCTION_NAME, Arg). |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
-type parameters_request() :: #{}. |
| 91 |
|
-type parameters_response() :: #{binary() => binary()}. |
| 92 |
|
-spec parameters(parameters_request()) -> parameters_response(). |
| 93 |
|
parameters(Arg) -> |
| 94 |
1 |
receive_response(?FUNCTION_NAME, Arg). |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
receive_response(Function, Arg) -> |
| 98 |
11845 |
case gen_statem:receive_response(pgmp_connection:Function(Arg)) of |
| 99 |
|
{reply, Reply} -> |
| 100 |
11845 |
Reply; |
| 101 |
|
|
| 102 |
|
{error, _} = Error -> |
| 103 |
:-( |
Error |
| 104 |
|
end. |