epgsql_cmd_close.erl 967 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. %% @doc Closes statement / portal
  2. %%
  3. %% ```
  4. %% > Close
  5. %% < CloseComplete
  6. %% '''
  7. -module(epgsql_cmd_close).
  8. -behaviour(epgsql_command).
  9. -export([init/1, execute/2, handle_message/4]).
  10. -export_type([response/0]).
  11. -type response() :: ok | {error, epgsql:query_error()}.
  12. -include("epgsql.hrl").
  13. -include("protocol.hrl").
  14. -record(close,
  15. {type :: statement | portal,
  16. name :: iodata()}).
  17. init({Type, Name}) ->
  18. #close{type = Type, name = Name}.
  19. execute(Sock, #close{type = Type, name = Name} = St) ->
  20. Type2 = case Type of
  21. statement -> ?PREPARED_STATEMENT;
  22. portal -> ?PORTAL
  23. end,
  24. epgsql_sock:send_multi(
  25. Sock,
  26. [
  27. {?CLOSE, [Type2, Name, 0]},
  28. {?FLUSH, []}
  29. ]),
  30. {ok, Sock, St}.
  31. handle_message(?CLOSE_COMPLETE, <<>>, Sock, _St) ->
  32. {finish, ok, ok, Sock};
  33. handle_message(?ERROR, Error, _Sock, _State) ->
  34. {sync_required, {error, Error}};
  35. handle_message(_, _, _, _) ->
  36. unknown.