epgsql_cmd_sync.erl 814 B

1234567891011121314151617181920212223242526272829303132
  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. %% ```
  5. %% > Sync
  6. %% < ReadyForQuery
  7. %% '''
  8. -module(epgsql_cmd_sync).
  9. -behaviour(epgsql_command).
  10. -export([init/1, execute/2, handle_message/4]).
  11. -export_type([response/0]).
  12. -type response() :: ok | {error, epgsql:query_error()}.
  13. -include("epgsql.hrl").
  14. -include("protocol.hrl").
  15. init(_) ->
  16. undefined.
  17. execute(Sock, St) ->
  18. epgsql_sock:send(Sock, ?SYNC, []),
  19. Sock1 = epgsql_sock:set_attr(sync_required, false, Sock),
  20. {ok, Sock1, St}.
  21. handle_message(?READY_FOR_QUERY, _, Sock, _State) ->
  22. {finish, ok, ok, Sock};
  23. handle_message(?ERROR, Error, _Sock, _State) ->
  24. {sync_required, {error, Error}};
  25. handle_message(_, _, _, _) ->
  26. unknown.