epgsql_command.erl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. %%% Behaviour module for epgsql_sock commands.
  2. %%%
  3. %%% Copyright (C) 2017 - Sergey Prokhorov. All rights reserved.
  4. -module(epgsql_command).
  5. -export([init/2, execute/3, handle_message/5]).
  6. -export_type([command/0, state/0]).
  7. -type command() :: module().
  8. -type state() :: any().
  9. %% Initialize command's state. Called when command is received by epgsql_sock process.
  10. -callback init(any()) -> state().
  11. -type execute_return() ::
  12. {ok, epgsql_sock:pg_sock(), state()}
  13. | {stop, Reason :: any(), Response :: any(), epgsql_sock:pg_sock()}.
  14. %% Execute command. It should send commands to socket.
  15. %% May be called many times if 'handle_message' will return 'requeue'.
  16. -callback execute(epgsql_sock:pg_sock(), state()) -> execute_return().
  17. -type handle_message_return() ::
  18. {noaction, epgsql_sock:pg_sock()}
  19. %% Do nothing; remember changed state
  20. | {noaction, epgsql_sock:pg_sock(), state()}
  21. %% Add result to resultset (eg, `{ok, Count}' `{ok, Cols, Rows}', `{error, #error{}}'
  22. %% It may be returned many times for eg, `squery' with multiple
  23. %% queries separated by ';'
  24. %% See epgsql_sock:get_results/1
  25. | {add_result, Data :: any(), Notification :: any(), epgsql_sock:pg_sock(), state()}
  26. %% Add new row to current resultset;
  27. %% See epgsql_sock:get_rows/1
  28. | {add_row, tuple(), epgsql_sock:pg_sock(), state()}
  29. %% Finish command execution, reply to the client and go to next command
  30. | {finish, Result :: any(), Notification :: any(), epgsql_sock:pg_sock()}
  31. %% Stop `epgsql_sock' process
  32. | {stop, Reason :: any(), Response :: any(), epgsql_sock:pg_sock()}
  33. %% Call 'execute' and reschedule command.
  34. %% It's forbidden to call epgsql_sock:send from `handle_message'.
  35. %% If you need to do so, you should set some flag in state and
  36. %% reschedule command.
  37. %% See `epgsql_cmd_connect' for reference.
  38. | {requeue, epgsql_sock:pg_sock(), state()}
  39. %% Protocol synchronization error (eg, unexpected packet)
  40. %% Drop command queue and don't accept any command except 'sync'
  41. | {sync_required, Why :: any()}
  42. %% Unknown packet. Terminate `epgsql_sock' process
  43. | unknown.
  44. %% Handle incoming packet
  45. -callback handle_message(Type :: byte(), Payload :: binary() | epgsql:query_error(),
  46. epgsql_sock:pg_sock(), state()) -> handle_message_return().
  47. -spec init(command(), any()) -> state().
  48. init(Command, Args) ->
  49. Command:init(Args).
  50. -spec execute(command(), epgsql_sock:pg_sock(), state()) -> execute_return().
  51. execute(Command, PgSock, CmdState) ->
  52. Command:execute(PgSock, CmdState).
  53. -spec handle_message(command(), Type :: byte(), Payload :: binary() | epgsql:query_error(),
  54. epgsql_sock:pg_sock(), state()) -> handle_message_return().
  55. handle_message(Command, Type, Payload, PgSock, State) ->
  56. Command:handle_message(Type, Payload, PgSock, State).