epgsql_cmd_bind.erl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. %% @doc Binds placeholder parameters to prepared statement, creating a "portal"
  2. %%
  3. %% ```
  4. %% > Bind
  5. %% < BindComplete
  6. %% '''
  7. %% @see epgsql_cmd_parse
  8. %% @see epgsql_cmd_execute
  9. -module(epgsql_cmd_bind).
  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. -record(bind,
  17. {stmt :: #statement{},
  18. portal :: iodata(),
  19. params :: list()}).
  20. init({Stmt, PortalName, Params}) ->
  21. #bind{stmt = Stmt, portal = PortalName, params = Params}.
  22. execute(Sock, #bind{stmt = Stmt, portal = PortalName, params = Params} = St) ->
  23. #statement{name = StatementName, columns = Columns, types = Types} = Stmt,
  24. Codec = epgsql_sock:get_codec(Sock),
  25. TypedParams = lists:zip(Types, Params),
  26. Bin1 = epgsql_wire:encode_parameters(TypedParams, Codec),
  27. Bin2 = epgsql_wire:encode_formats(Columns),
  28. Commands = [
  29. epgsql_wire:encode_bind(PortalName, StatementName, Bin1, Bin2),
  30. epgsql_wire:encode_flush()
  31. ],
  32. {send_multi, Commands, Sock, St}.
  33. handle_message(?BIND_COMPLETE, <<>>, Sock, _State) ->
  34. {finish, ok, ok, Sock};
  35. handle_message(?ERROR, Error, _Sock, _State) ->
  36. {sync_required, {error, Error}};
  37. handle_message(_, _, _, _) ->
  38. unknown.