epgsql_wire.erl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. encode/1,
  9. encode/2,
  10. decode_data/3,
  11. decode_complete/1,
  12. encode_types/2,
  13. encode_formats/1,
  14. format/1,
  15. encode_parameters/2]).
  16. -include("epgsql.hrl").
  17. -include("epgsql_binary.hrl").
  18. decode_message(<<Type:8, Len:?int32, Rest/binary>> = Bin) ->
  19. Len2 = Len - 4,
  20. case Rest of
  21. <<Data:Len2/binary, Tail/binary>> ->
  22. case Type of
  23. $E ->
  24. {{error, decode_error(Data)}, Tail};
  25. _ ->
  26. {{Type, Data}, Tail}
  27. end;
  28. _Other ->
  29. Bin
  30. end;
  31. decode_message(Bin) ->
  32. Bin.
  33. %% decode a single null-terminated string
  34. decode_string(Bin) ->
  35. binary:split(Bin, <<0>>).
  36. %% decode multiple null-terminated string
  37. decode_strings(Bin) ->
  38. [<<>> | T] = lists:reverse(binary:split(Bin, <<0>>, [global])),
  39. lists:reverse(T).
  40. %% decode field
  41. decode_fields(Bin) ->
  42. decode_fields(Bin, []).
  43. decode_fields(<<0>>, Acc) ->
  44. Acc;
  45. decode_fields(<<Type:8, Rest/binary>>, Acc) ->
  46. [Str, Rest2] = decode_string(Rest),
  47. decode_fields(Rest2, [{Type, Str} | Acc]).
  48. %% decode ErrorResponse
  49. %% TODO add fields from http://www.postgresql.org/docs/9.0/interactive/protocol-error-fields.html
  50. decode_error(Bin) ->
  51. Fields = decode_fields(Bin),
  52. Error = #error{
  53. severity = lower_atom(proplists:get_value($S, Fields)),
  54. code = proplists:get_value($C, Fields),
  55. message = proplists:get_value($M, Fields),
  56. extra = decode_error_extra(Fields)},
  57. Error.
  58. decode_error_extra(Fields) ->
  59. Types = [{$D, detail}, {$H, hint}, {$P, position}],
  60. decode_error_extra(Types, Fields, []).
  61. decode_error_extra([], _Fields, Extra) ->
  62. Extra;
  63. decode_error_extra([{Type, Name} | T], Fields, Extra) ->
  64. case proplists:get_value(Type, Fields) of
  65. undefined -> decode_error_extra(T, Fields, Extra);
  66. Value -> decode_error_extra(T, Fields, [{Name, Value} | Extra])
  67. end.
  68. lower_atom(Str) when is_binary(Str) ->
  69. lower_atom(binary_to_list(Str));
  70. lower_atom(Str) when is_list(Str) ->
  71. list_to_atom(string:to_lower(Str)).
  72. encode(Data) ->
  73. Bin = iolist_to_binary(Data),
  74. <<(byte_size(Bin) + 4):?int32, Bin/binary>>.
  75. encode(Type, Data) ->
  76. Bin = iolist_to_binary(Data),
  77. <<Type:8, (byte_size(Bin) + 4):?int32, Bin/binary>>.
  78. %% decode data
  79. decode_data(Columns, Bin, Codec) ->
  80. decode_data(Columns, Bin, [], Codec).
  81. decode_data([], _Bin, Acc, _Codec) ->
  82. list_to_tuple(lists:reverse(Acc));
  83. decode_data([_C | T], <<-1:?int32, Rest/binary>>, Acc, Codec) ->
  84. decode_data(T, Rest, [null | Acc], Codec);
  85. decode_data([C | T], <<Len:?int32, Value:Len/binary, Rest/binary>>, Acc, Codec) ->
  86. Value2 = case C of
  87. #column{type = Type, format = 1} ->
  88. epgsql_binary:decode(Type, Value, Codec);
  89. #column{} ->
  90. Value
  91. end,
  92. decode_data(T, Rest, [Value2 | Acc], Codec).
  93. %% decode column information
  94. decode_columns(Count, Bin, Codec) ->
  95. decode_columns(Count, Bin, [], Codec).
  96. decode_columns(0, _Bin, Acc, _Codec) ->
  97. lists:reverse(Acc);
  98. decode_columns(N, Bin, Acc, Codec) ->
  99. [Name, Rest] = decode_string(Bin),
  100. <<_Table_Oid:?int32, _Attrib_Num:?int16, Type_Oid:?int32,
  101. Size:?int16, Modifier:?int32, Format:?int16, Rest2/binary>> = Rest,
  102. Desc = #column{
  103. name = Name,
  104. type = epgsql_binary:oid2type(Type_Oid, Codec),
  105. size = Size,
  106. modifier = Modifier,
  107. format = Format},
  108. decode_columns(N - 1, Rest2, [Desc | Acc], Codec).
  109. %% decode command complete msg
  110. decode_complete(<<"SELECT", 0>>) -> select;
  111. decode_complete(<<"SELECT", _/binary>>) -> select;
  112. decode_complete(<<"BEGIN", 0>>) -> 'begin';
  113. decode_complete(<<"ROLLBACK", 0>>) -> rollback;
  114. decode_complete(Bin) ->
  115. [Str, _] = decode_string(Bin),
  116. case string:tokens(binary_to_list(Str), " ") of
  117. ["INSERT", _Oid, Rows] -> {insert, list_to_integer(Rows)};
  118. ["UPDATE", Rows] -> {update, list_to_integer(Rows)};
  119. ["DELETE", Rows] -> {delete, list_to_integer(Rows)};
  120. ["MOVE", Rows] -> {move, list_to_integer(Rows)};
  121. ["FETCH", Rows] -> {fetch, list_to_integer(Rows)};
  122. [Type | _Rest] -> lower_atom(Type)
  123. end.
  124. %% encode types
  125. encode_types(Types, Codec) ->
  126. encode_types(Types, 0, <<>>, Codec).
  127. encode_types([], Count, Acc, _Codec) ->
  128. <<Count:?int16, Acc/binary>>;
  129. encode_types([Type | T], Count, Acc, Codec) ->
  130. Oid = case Type of
  131. undefined -> 0;
  132. _Any -> epgsql_binary:type2oid(Type, Codec)
  133. end,
  134. encode_types(T, Count + 1, <<Acc/binary, Oid:?int32>>, Codec).
  135. %% encode column formats
  136. encode_formats(Columns) ->
  137. encode_formats(Columns, 0, <<>>).
  138. encode_formats([], Count, Acc) ->
  139. <<Count:?int16, Acc/binary>>;
  140. encode_formats([#column{format = Format} | T], Count, Acc) ->
  141. encode_formats(T, Count + 1, <<Acc/binary, Format:?int16>>).
  142. format(Type) ->
  143. case epgsql_binary:supports(Type) of
  144. true -> 1;
  145. false -> 0
  146. end.
  147. %% encode parameters
  148. encode_parameters(Parameters, Codec) ->
  149. encode_parameters(Parameters, 0, <<>>, <<>>, Codec).
  150. encode_parameters([], Count, Formats, Values, _Codec) ->
  151. <<Count:?int16, Formats/binary, Count:?int16, Values/binary>>;
  152. encode_parameters([P | T], Count, Formats, Values, Codec) ->
  153. {Format, Value} = encode_parameter(P, Codec),
  154. Formats2 = <<Formats/binary, Format:?int16>>,
  155. Values2 = <<Values/binary, Value/binary>>,
  156. encode_parameters(T, Count + 1, Formats2, Values2, Codec).
  157. %% encode parameter
  158. encode_parameter({Type, Value}, Codec) ->
  159. case epgsql_binary:encode(Type, Value, Codec) of
  160. Bin when is_binary(Bin) -> {1, Bin};
  161. {error, unsupported} -> encode_parameter(Value)
  162. end;
  163. encode_parameter(Value, _Codec) -> encode_parameter(Value).
  164. encode_parameter(A) when is_atom(A) -> {0, encode_list(atom_to_list(A))};
  165. encode_parameter(B) when is_binary(B) -> {0, <<(byte_size(B)):?int32, B/binary>>};
  166. encode_parameter(I) when is_integer(I) -> {0, encode_list(integer_to_list(I))};
  167. encode_parameter(F) when is_float(F) -> {0, encode_list(float_to_list(F))};
  168. encode_parameter(L) when is_list(L) -> {0, encode_list(L)}.
  169. encode_list(L) ->
  170. Bin = list_to_binary(L),
  171. <<(byte_size(Bin)):?int32, Bin/binary>>.