pgsql_wire.erl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. %%% Copyright (C) 2009 - Will Glozer. All rights reserved.
  2. %%% Copyright (C) 2011 - Anton Lebedevich. All rights reserved.
  3. -module(pgsql_wire).
  4. -export([decode_message/1,
  5. decode_error/1,
  6. decode_strings/1,
  7. decode_columns/2,
  8. encode/1,
  9. encode/2,
  10. decode_data/2,
  11. decode_complete/1,
  12. encode_types/1,
  13. encode_formats/1,
  14. format/1,
  15. encode_parameters/1]).
  16. -include("pgsql.hrl").
  17. -include("pgsql_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) ->
  80. decode_data(Columns, Bin, []).
  81. decode_data([], _Bin, Acc) ->
  82. list_to_tuple(lists:reverse(Acc));
  83. decode_data([_C | T], <<-1:?int32, Rest/binary>>, Acc) ->
  84. decode_data(T, Rest, [null | Acc]);
  85. decode_data([C | T], <<Len:?int32, Value:Len/binary, Rest/binary>>, Acc) ->
  86. case C of
  87. #column{type = Type, format = 1} -> Value2 = pgsql_binary:decode(Type, Value);
  88. #column{} -> Value2 = Value
  89. end,
  90. decode_data(T, Rest, [Value2 | Acc]).
  91. %% decode column information
  92. decode_columns(Count, Bin) ->
  93. decode_columns(Count, Bin, []).
  94. decode_columns(0, _Bin, Acc) ->
  95. lists:reverse(Acc);
  96. decode_columns(N, Bin, Acc) ->
  97. [Name, Rest] = decode_string(Bin),
  98. <<_Table_Oid:?int32, _Attrib_Num:?int16, Type_Oid:?int32,
  99. Size:?int16, Modifier:?int32, Format:?int16, Rest2/binary>> = Rest,
  100. Desc = #column{
  101. name = Name,
  102. type = pgsql_types:oid2type(Type_Oid),
  103. size = Size,
  104. modifier = Modifier,
  105. format = Format},
  106. decode_columns(N - 1, Rest2, [Desc | Acc]).
  107. %% decode command complete msg
  108. decode_complete(<<"SELECT", 0>>) -> select;
  109. decode_complete(<<"SELECT", _/binary>>) -> select;
  110. decode_complete(<<"BEGIN", 0>>) -> 'begin';
  111. decode_complete(<<"ROLLBACK", 0>>) -> rollback;
  112. decode_complete(Bin) ->
  113. [Str, _] = decode_string(Bin),
  114. case string:tokens(binary_to_list(Str), " ") of
  115. ["INSERT", _Oid, Rows] -> {insert, list_to_integer(Rows)};
  116. ["UPDATE", Rows] -> {update, list_to_integer(Rows)};
  117. ["DELETE", Rows] -> {delete, list_to_integer(Rows)};
  118. ["MOVE", Rows] -> {move, list_to_integer(Rows)};
  119. ["FETCH", Rows] -> {fetch, list_to_integer(Rows)};
  120. [Type | _Rest] -> lower_atom(Type)
  121. end.
  122. %% encode types
  123. encode_types(Types) ->
  124. encode_types(Types, 0, <<>>).
  125. encode_types([], Count, Acc) ->
  126. <<Count:?int16, Acc/binary>>;
  127. encode_types([Type | T], Count, Acc) ->
  128. case Type of
  129. undefined -> Oid = 0;
  130. _Any -> Oid = pgsql_types:type2oid(Type)
  131. end,
  132. encode_types(T, Count + 1, <<Acc/binary, Oid:?int32>>).
  133. %% encode column formats
  134. encode_formats(Columns) ->
  135. encode_formats(Columns, 0, <<>>).
  136. encode_formats([], Count, Acc) ->
  137. <<Count:?int16, Acc/binary>>;
  138. encode_formats([#column{format = Format} | T], Count, Acc) ->
  139. encode_formats(T, Count + 1, <<Acc/binary, Format:?int16>>).
  140. format(Type) ->
  141. case pgsql_binary:supports(Type) of
  142. true -> 1;
  143. false -> 0
  144. end.
  145. %% encode parameters
  146. encode_parameters(Parameters) ->
  147. encode_parameters(Parameters, 0, <<>>, <<>>).
  148. encode_parameters([], Count, Formats, Values) ->
  149. <<Count:?int16, Formats/binary, Count:?int16, Values/binary>>;
  150. encode_parameters([P | T], Count, Formats, Values) ->
  151. {Format, Value} = encode_parameter(P),
  152. Formats2 = <<Formats/binary, Format:?int16>>,
  153. Values2 = <<Values/binary, Value/binary>>,
  154. encode_parameters(T, Count + 1, Formats2, Values2).
  155. %% encode parameter
  156. encode_parameter({Type, Value}) ->
  157. case pgsql_binary:encode(Type, Value) of
  158. Bin when is_binary(Bin) -> {1, Bin};
  159. {error, unsupported} -> encode_parameter(Value)
  160. end;
  161. encode_parameter(A) when is_atom(A) -> {0, encode_list(atom_to_list(A))};
  162. encode_parameter(B) when is_binary(B) -> {0, <<(byte_size(B)):?int32, B/binary>>};
  163. encode_parameter(I) when is_integer(I) -> {0, encode_list(integer_to_list(I))};
  164. encode_parameter(F) when is_float(F) -> {0, encode_list(float_to_list(F))};
  165. encode_parameter(L) when is_list(L) -> {0, encode_list(L)}.
  166. encode_list(L) ->
  167. Bin = list_to_binary(L),
  168. <<(byte_size(Bin)):?int32, Bin/binary>>.