epgsql_cmd_sync.erl 927 B

123456789101112131415161718192021222324252627282930313233
  1. %% @doc Synchronize client and server states for multi-command combinations
  2. %%
  3. %% Should be executed if APIs start to return `{error, sync_required}'.
  4. %% See [https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY]
  5. %% ```
  6. %% > Sync
  7. %% < ReadyForQuery
  8. %% '''
  9. -module(epgsql_cmd_sync).
  10. -behaviour(epgsql_command).
  11. -export([init/1, execute/2, handle_message/4]).
  12. -export_type([response/0]).
  13. -type response() :: ok | {error, epgsql:query_error()}.
  14. -include("epgsql.hrl").
  15. -include("protocol.hrl").
  16. init(_) ->
  17. undefined.
  18. execute(Sock, St) ->
  19. Sock1 = epgsql_sock:set_attr(sync_required, false, Sock),
  20. {Type, Data} = epgsql_wire:encode_sync(),
  21. {send, Type, Data, Sock1, St}.
  22. handle_message(?READY_FOR_QUERY, _, Sock, _State) ->
  23. {finish, ok, ok, Sock};
  24. handle_message(?ERROR, Error, _Sock, _State) ->
  25. {sync_required, {error, Error}};
  26. handle_message(_, _, _, _) ->
  27. unknown.