epgsql_cmd_bind.erl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. %% > Bind
  2. %% < BindComplete
  3. -module(epgsql_cmd_bind).
  4. -behaviour(epgsql_command).
  5. -export([init/1, execute/2, handle_message/4]).
  6. -export_type([response/0]).
  7. -type response() :: ok | {error, epgsql:query_error()}.
  8. -include("epgsql.hrl").
  9. -include("../epgsql_binary.hrl").
  10. -record(bind,
  11. {stmt :: #statement{},
  12. portal :: iodata(),
  13. params :: list()}).
  14. init({Stmt, PortalName, Params}) ->
  15. #bind{stmt = Stmt, portal = PortalName, params = Params}.
  16. execute(Sock, #bind{stmt = Stmt, portal = PortalName, params = Params} = St) ->
  17. #statement{name = StatementName, columns = Columns, types = Types} = Stmt,
  18. Codec = epgsql_sock:get_codec(Sock),
  19. TypedParams = lists:zip(Types, Params),
  20. Bin1 = epgsql_wire:encode_parameters(TypedParams, Codec),
  21. Bin2 = epgsql_wire:encode_formats(Columns),
  22. epgsql_sock:send_multi(
  23. Sock,
  24. [
  25. {?BIND, [PortalName, 0, StatementName, 0, Bin1, Bin2]},
  26. {?FLUSH, []}
  27. ]),
  28. {ok, Sock, St}.
  29. handle_message(?BIND_COMPLETE, <<>>, Sock, _State) ->
  30. {finish, ok, ok, Sock};
  31. handle_message(?ERROR, Error, _Sock, _State) ->
  32. {sync_required, {error, Error}};
  33. handle_message(_, _, _, _) ->
  34. unknown.