epgsql_wire.erl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. %%% Copyright (C) 2009 - Will Glozer. All rights reserved.
  2. %%% Copyright (C) 2011 - Anton Lebedevich. All rights reserved.
  3. -module(epgsql_wire).
  4. -export([decode_message/1,
  5. decode_error/1,
  6. decode_strings/1,
  7. decode_columns/3,
  8. decode_parameters/2,
  9. encode_command/1,
  10. encode_command/2,
  11. build_decoder/2,
  12. decode_data/2,
  13. decode_complete/1,
  14. encode_types/2,
  15. encode_formats/1,
  16. format/2,
  17. encode_parameters/2,
  18. encode_standby_status_update/3]).
  19. -export_type([row_decoder/0]).
  20. -include("epgsql.hrl").
  21. -include("protocol.hrl").
  22. -opaque row_decoder() :: {[epgsql_binary:decoder()], [epgsql:column()], epgsql_binary:codec()}.
  23. -spec decode_message(binary()) -> {byte(), binary(), binary()} | binary().
  24. decode_message(<<Type:8, Len:?int32, Rest/binary>> = Bin) ->
  25. Len2 = Len - 4,
  26. case Rest of
  27. <<Data:Len2/binary, Tail/binary>> ->
  28. {Type, Data, Tail};
  29. _Other ->
  30. Bin
  31. end;
  32. decode_message(Bin) ->
  33. Bin.
  34. %% @doc decode a single null-terminated string
  35. -spec decode_string(binary()) -> [binary(), ...].
  36. decode_string(Bin) ->
  37. binary:split(Bin, <<0>>).
  38. %% @doc decode multiple null-terminated string
  39. -spec decode_strings(binary()) -> [binary(), ...].
  40. decode_strings(Bin) ->
  41. [<<>> | T] = lists:reverse(binary:split(Bin, <<0>>, [global])),
  42. lists:reverse(T).
  43. %% @doc decode error's field
  44. -spec decode_fields(binary()) -> [{byte(), binary()}].
  45. decode_fields(Bin) ->
  46. decode_fields(Bin, []).
  47. decode_fields(<<0>>, Acc) ->
  48. Acc;
  49. decode_fields(<<Type:8, Rest/binary>>, Acc) ->
  50. [Str, Rest2] = decode_string(Rest),
  51. decode_fields(Rest2, [{Type, Str} | Acc]).
  52. %% @doc decode ErrorResponse
  53. %% See http://www.postgresql.org/docs/current/interactive/protocol-error-fields.html
  54. -spec decode_error(binary()) -> epgsql:query_error().
  55. decode_error(Bin) ->
  56. Fields = decode_fields(Bin),
  57. ErrCode = proplists:get_value($C, Fields),
  58. ErrName = epgsql_errcodes:to_name(ErrCode),
  59. {ErrSeverity, Extra} = case proplists:get_value($V, Fields) of
  60. undefined ->
  61. {proplists:get_value($S, Fields), []};
  62. Severity ->
  63. {Severity, [{severity, proplists:get_value($S, Fields)}]}
  64. end,
  65. Error = #error{
  66. severity = lower_atom(ErrSeverity),
  67. code = ErrCode,
  68. codename = ErrName,
  69. message = proplists:get_value($M, Fields),
  70. extra = lists:sort(Extra ++ lists:foldl(fun decode_error_extra/2, [], Fields))},
  71. Error.
  72. %% consider updating #error.extra typespec when changing/adding extras
  73. decode_error_extra({$D, Val}, Acc) ->
  74. [{detail, Val} | Acc];
  75. decode_error_extra({$H, Val}, Acc) ->
  76. [{hint, Val} | Acc];
  77. decode_error_extra({$P, Val}, Acc) ->
  78. [{position, Val} | Acc];
  79. decode_error_extra({$p, Val}, Acc) ->
  80. [{internal_position, Val} | Acc];
  81. decode_error_extra({$q, Val}, Acc) ->
  82. [{internal_query, Val} | Acc];
  83. decode_error_extra({$W, Val}, Acc) ->
  84. [{where, Val} | Acc];
  85. decode_error_extra({$s, Val}, Acc) ->
  86. [{schema_name, Val} | Acc];
  87. decode_error_extra({$t, Val}, Acc) ->
  88. [{table_name, Val} | Acc];
  89. decode_error_extra({$c, Val}, Acc) ->
  90. [{column_name, Val} | Acc];
  91. decode_error_extra({$d, Val}, Acc) ->
  92. [{data_type_name, Val} | Acc];
  93. decode_error_extra({$n, Val}, Acc) ->
  94. [{constraint_name, Val} | Acc];
  95. decode_error_extra({$F, Val}, Acc) ->
  96. [{file, Val} | Acc];
  97. decode_error_extra({$L, Val}, Acc) ->
  98. [{line, Val} | Acc];
  99. decode_error_extra({$R, Val}, Acc) ->
  100. [{routine, Val} | Acc];
  101. decode_error_extra({_, _}, Acc) ->
  102. Acc.
  103. lower_atom(Str) when is_binary(Str) ->
  104. lower_atom(binary_to_list(Str));
  105. lower_atom(Str) when is_list(Str) ->
  106. list_to_atom(string:to_lower(Str)).
  107. %% @doc Build decoder for DataRow
  108. -spec build_decoder([epgsql:column()], epgsql_binary:codec()) -> row_decoder().
  109. build_decoder(Columns, Codec) ->
  110. Decoders = lists:map(
  111. fun(#column{oid = Oid, format = Format}) ->
  112. Fmt = case Format of
  113. 1 -> binary;
  114. 0 -> text
  115. end,
  116. epgsql_binary:oid_to_decoder(Oid, Fmt, Codec)
  117. end, Columns),
  118. {Decoders, Columns, Codec}.
  119. %% @doc decode row data
  120. -spec decode_data(binary(), row_decoder()) -> tuple().
  121. decode_data(Bin, {Decoders, Columns, Codec}) ->
  122. list_to_tuple(decode_data(Bin, Decoders, Columns, Codec)).
  123. decode_data(_, [], [], _) -> [];
  124. decode_data(<<-1:?int32, Rest/binary>>, [_Dec | Decs], [_Col | Cols], Codec) ->
  125. [null | decode_data(Rest, Decs, Cols, Codec)];
  126. decode_data(<<Len:?int32, Value:Len/binary, Rest/binary>>, [Decoder | Decs], [_Col | Cols], Codec) ->
  127. [epgsql_binary:decode(Value, Decoder)
  128. | decode_data(Rest, Decs, Cols, Codec)].
  129. %% @doc decode column information
  130. -spec decode_columns(non_neg_integer(), binary(), epgsql_binary:codec()) -> [epgsql:column()].
  131. decode_columns(0, _Bin, _Codec) -> [];
  132. decode_columns(Count, Bin, Codec) ->
  133. [Name, Rest] = decode_string(Bin),
  134. <<_TableOid:?int32, _AttribNum:?int16, TypeOid:?int32,
  135. Size:?int16, Modifier:?int32, Format:?int16, Rest2/binary>> = Rest,
  136. %% TODO: get rid of this 'type' (extra oid_db lookup)
  137. Type = epgsql_binary:oid_to_name(TypeOid, Codec),
  138. Desc = #column{
  139. name = Name,
  140. type = Type,
  141. oid = TypeOid,
  142. size = Size,
  143. modifier = Modifier,
  144. format = Format},
  145. [Desc | decode_columns(Count - 1, Rest2, Codec)].
  146. %% @doc decode ParameterDescription
  147. -spec decode_parameters(binary(), epgsql_binary:codec()) ->
  148. [epgsql_oid_db:type_info() | {unknown_oid, epgsql_oid_db:oid()}].
  149. decode_parameters(<<_Count:?int16, Bin/binary>>, Codec) ->
  150. [case epgsql_binary:oid_to_info(Oid, Codec) of
  151. undefined -> {unknown_oid, Oid};
  152. TypeInfo -> TypeInfo
  153. end || <<Oid:?int32>> <= Bin].
  154. %% @doc decode command complete msg
  155. decode_complete(<<"SELECT", 0>>) -> select;
  156. decode_complete(<<"SELECT", _/binary>>) -> select;
  157. decode_complete(<<"BEGIN", 0>>) -> 'begin';
  158. decode_complete(<<"ROLLBACK", 0>>) -> rollback;
  159. decode_complete(Bin) ->
  160. [Str, _] = decode_string(Bin),
  161. case string:tokens(binary_to_list(Str), " ") of
  162. ["INSERT", _Oid, Rows] -> {insert, list_to_integer(Rows)};
  163. ["UPDATE", Rows] -> {update, list_to_integer(Rows)};
  164. ["DELETE", Rows] -> {delete, list_to_integer(Rows)};
  165. ["MOVE", Rows] -> {move, list_to_integer(Rows)};
  166. ["FETCH", Rows] -> {fetch, list_to_integer(Rows)};
  167. [Type | _Rest] -> lower_atom(Type)
  168. end.
  169. %% @doc encode types
  170. encode_types(Types, Codec) ->
  171. encode_types(Types, 0, <<>>, Codec).
  172. encode_types([], Count, Acc, _Codec) ->
  173. <<Count:?int16, Acc/binary>>;
  174. encode_types([Type | T], Count, Acc, Codec) ->
  175. Oid = case Type of
  176. undefined -> 0;
  177. _Any -> epgsql_binary:type_to_oid(Type, Codec)
  178. end,
  179. encode_types(T, Count + 1, <<Acc/binary, Oid:?int32>>, Codec).
  180. %% @doc encode expected column formats
  181. -spec encode_formats([epgsql:column()]) -> binary().
  182. encode_formats(Columns) ->
  183. encode_formats(Columns, 0, <<>>).
  184. encode_formats([], Count, Acc) ->
  185. <<Count:?int16, Acc/binary>>;
  186. encode_formats([#column{format = Format} | T], Count, Acc) ->
  187. encode_formats(T, Count + 1, <<Acc/binary, Format:?int16>>).
  188. format({unknown_oid, _}, _) -> 0;
  189. format(#column{oid = Oid}, Codec) ->
  190. case epgsql_binary:supports(Oid, Codec) of
  191. true -> 1; %binary
  192. false -> 0 %text
  193. end.
  194. %% @doc encode parameters for 'Bind'
  195. -spec encode_parameters([], epgsql_binary:codec()) -> iolist().
  196. encode_parameters(Parameters, Codec) ->
  197. encode_parameters(Parameters, 0, <<>>, [], Codec).
  198. encode_parameters([], Count, Formats, Values, _Codec) ->
  199. [<<Count:?int16>>, Formats, <<Count:?int16>> | lists:reverse(Values)];
  200. encode_parameters([P | T], Count, Formats, Values, Codec) ->
  201. {Format, Value} = encode_parameter(P, Codec),
  202. Formats2 = <<Formats/binary, Format:?int16>>,
  203. Values2 = [Value | Values],
  204. encode_parameters(T, Count + 1, Formats2, Values2, Codec).
  205. %% @doc encode single 'typed' parameter
  206. -spec encode_parameter({Type, Val :: any()},
  207. epgsql_binary:codec()) -> {0..1, iolist()} when
  208. Type :: epgsql:type_name()
  209. | {array, epgsql:type_name()}
  210. | {unknown_oid, epgsql_oid_db:oid()}.
  211. encode_parameter({T, undefined}, Codec) ->
  212. encode_parameter({T, null}, Codec);
  213. encode_parameter({_, null}, _Codec) ->
  214. {1, <<-1:?int32>>};
  215. encode_parameter({{unknown_oid, _Oid}, Value}, _Codec) ->
  216. {0, encode_text(Value)};
  217. encode_parameter({Type, Value}, Codec) ->
  218. {1, epgsql_binary:encode(Type, Value, Codec)};
  219. encode_parameter(Value, _Codec) ->
  220. {0, encode_text(Value)}.
  221. encode_text(B) when is_binary(B) -> encode_bin(B);
  222. encode_text(A) when is_atom(A) -> encode_bin(atom_to_binary(A, utf8));
  223. encode_text(I) when is_integer(I) -> encode_bin(integer_to_binary(I));
  224. encode_text(F) when is_float(F) -> encode_bin(float_to_binary(F));
  225. encode_text(L) when is_list(L) -> encode_bin(list_to_binary(L)).
  226. encode_bin(Bin) ->
  227. <<(byte_size(Bin)):?int32, Bin/binary>>.
  228. %% @doc Encode iodata with size-prefix (used for `StartupMessage' and `SSLRequest' packets)
  229. encode_command(Data) ->
  230. Size = iolist_size(Data),
  231. [<<(Size + 4):?int32>> | Data].
  232. %% @doc Encode PG command with type and size prefix
  233. encode_command(Type, Data) ->
  234. Size = iolist_size(Data),
  235. [<<Type:8, (Size + 4):?int32>> | Data].
  236. %% @doc encode replication status message
  237. encode_standby_status_update(ReceivedLSN, FlushedLSN, AppliedLSN) ->
  238. {MegaSecs, Secs, MicroSecs} = os:timestamp(),
  239. %% microseconds since midnight on 2000-01-01
  240. Timestamp = ((MegaSecs * 1000000 + Secs) * 1000000 + MicroSecs) - 946684800*1000000,
  241. <<$r:8, ReceivedLSN:?int64, FlushedLSN:?int64, AppliedLSN:?int64, Timestamp:?int64, 0:8>>.