epgsql_codec.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. %%% @doc
  2. %%% Behaviour for postgresql datatype codecs.
  3. %%% XXX: this module and callbacks "know nothing" about OIDs.
  4. %%% XXX: state of codec shouldn't leave epgsql_sock process. If you need to
  5. %%% return "pointer" to data type/codec, it's better to return OID or type name.
  6. %%% @end
  7. %%% Created : 12 Oct 2017 by Sergey Prokhorov <me@seriyps.ru>
  8. -module(epgsql_codec).
  9. -export([init_mods/2, encode/4, decode/4, decode_text/4]).
  10. -export_type([codec_state/0, codec_mod/0, codec_entry/0]).
  11. %%
  12. %% Behaviour
  13. %%
  14. -type codec_state() :: any().
  15. -type codec_mod() :: module().
  16. -optional_callbacks([decode_text/3]).
  17. %% Called on connection start-up
  18. -callback init(any(), epgsql_sock:pg_sock()) -> codec_state().
  19. %% List of supported type names
  20. -callback names() -> [epgsql:type_name()].
  21. %% Encode Erlang representation to PG binary
  22. %% Called for each parameter, binary protocol (equery)
  23. -callback encode(Cell :: any(), epgsql:type_name(), codec_state()) -> iodata().
  24. %% Decode PG binary to erlang representation
  25. %% Called for each cell in each row, binary protocol (equery)
  26. -callback decode(Cell :: binary(), epgsql:type_name(), codec_state()) -> any().
  27. %% Decode PG string representation (text protocol) to erlang term.
  28. %% Called for each cell in each row, text protocol (squery)
  29. -callback decode_text(Cell :: binary(), epgsql:type_name(), codec_state()) ->
  30. any().
  31. %% ==========
  32. -type codec_entry() :: {epgsql:type_name(),
  33. Mod :: codec_mod(),
  34. CallbackState :: any()}.
  35. -spec init_mods([{codec_mod(), any()}], epgsql_sock:pg_sock()) ->
  36. ordsets:ordset(codec_entry()).
  37. init_mods(Codecs, PgSock) ->
  38. ModState = [{Mod, Mod:init(Opts, PgSock)} || {Mod, Opts} <- Codecs],
  39. build_mapping(ModState, sets:new(), []).
  40. build_mapping([{Mod, _State} = MS | ModStates], Set, Acc) ->
  41. Names = Mod:names(),
  42. {Set1, Acc1} = add_names(Names, MS, Set, Acc),
  43. build_mapping(ModStates, Set1, Acc1);
  44. build_mapping([], _, Acc) ->
  45. ordsets:from_list(Acc).
  46. add_names([Name | Names], {Mod, State} = MS, Set, Acc) ->
  47. case sets:is_element(Name, Set) of
  48. true ->
  49. add_names(Names, MS, Set, Acc);
  50. false ->
  51. Set1 = sets:add_element(Name, Set),
  52. Acc1 = [{Name, Mod, State} | Acc],
  53. add_names(Names, MS, Set1, Acc1)
  54. end;
  55. add_names([], _, Set, Acc) ->
  56. {Set, Acc}.
  57. -spec encode(codec_mod(), any(), epgsql:type_name(), codec_state()) -> iodata().
  58. encode(Mod, Cell, TypeName, CodecState) ->
  59. Mod:encode(Cell, TypeName, CodecState).
  60. -spec decode(codec_mod(), binary(), epgsql:type_name(), codec_state()) -> any().
  61. decode(Mod, Cell, TypeName, CodecState) ->
  62. Mod:decode(Cell, TypeName, CodecState).
  63. -spec decode_text(codec_mod(), binary(), epgsql:type_name(), codec_state()) -> any().
  64. decode_text(Mod, Cell, TypeName, CodecState) ->
  65. Mod:decode(Cell, TypeName, CodecState).