mysql_protocol.erl 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. %% MySQL/OTP – MySQL client library for Erlang/OTP
  2. %% Copyright (C) 2014 Viktor Söderqvist
  3. %%
  4. %% This file is part of MySQL/OTP.
  5. %%
  6. %% MySQL/OTP is free software: you can redistribute it and/or modify it under
  7. %% the terms of the GNU Lesser General Public License as published by the Free
  8. %% Software Foundation, either version 3 of the License, or (at your option)
  9. %% any later version.
  10. %%
  11. %% This program is distributed in the hope that it will be useful, but WITHOUT
  12. %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. %% more details.
  15. %%
  16. %% You should have received a copy of the GNU Lesser General Public License
  17. %% along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. %% @doc This module implements parts of the MySQL client/server protocol.
  19. %%
  20. %% The protocol is described in the document "MySQL Internals" which can be
  21. %% found under "MySQL Documentation: Expert Guides" on http://dev.mysql.com/.
  22. %%
  23. %% TCP communication is not handled in this module. Most of the public functions
  24. %% take funs for data communitaction as parameters.
  25. %% @private
  26. -module(mysql_protocol).
  27. -export([handshake/6, quit/2, ping/2,
  28. query/4, fetch_query_response/3,
  29. prepare/3, unprepare/3, execute/5, fetch_execute_response/3]).
  30. %% How much data do we want per packet?
  31. -define(MAX_BYTES_PER_PACKET, 16#1000000).
  32. -include("records.hrl").
  33. -include("protocol.hrl").
  34. -include("server_status.hrl").
  35. %% Macros for pattern matching on packets.
  36. -define(ok_pattern, <<?OK, _/binary>>).
  37. -define(error_pattern, <<?ERROR, _/binary>>).
  38. -define(eof_pattern, <<?EOF, _:4/binary>>).
  39. %% @doc Performs a handshake using the supplied functions for communication.
  40. %% Returns an ok or an error record. Raises errors when various unimplemented
  41. %% features are requested.
  42. -spec handshake(iodata(), iodata(), iodata() | undefined, atom(),
  43. term(), boolean()) -> #handshake{} | #error{}.
  44. handshake(Username, Password, Database, TcpModule, Socket, SetFoundRows) ->
  45. SeqNum0 = 0,
  46. {ok, HandshakePacket, SeqNum1} = recv_packet(TcpModule, Socket, SeqNum0),
  47. Handshake = parse_handshake(HandshakePacket),
  48. Response = build_handshake_response(Handshake, Username, Password,
  49. Database, SetFoundRows),
  50. {ok, SeqNum2} = send_packet(TcpModule, Socket, Response, SeqNum1),
  51. {ok, ConfirmPacket, _SeqNum3} = recv_packet(TcpModule, Socket, SeqNum2),
  52. case parse_handshake_confirm(ConfirmPacket) of
  53. #ok{status = OkStatus} ->
  54. OkStatus = Handshake#handshake.status,
  55. Handshake;
  56. Error ->
  57. Error
  58. end.
  59. -spec quit(atom(), term()) -> ok.
  60. quit(TcpModule, Socket) ->
  61. {ok, SeqNum1} = send_packet(TcpModule, Socket, <<?COM_QUIT>>, 0),
  62. case recv_packet(TcpModule, Socket, SeqNum1) of
  63. {error, closed} -> ok; %% MySQL 5.5.40 and more
  64. {ok, ?ok_pattern, _SeqNum2} -> ok %% Some older MySQL versions?
  65. end.
  66. -spec ping(atom(), term()) -> #ok{}.
  67. ping(TcpModule, Socket) ->
  68. {ok, SeqNum1} = send_packet(TcpModule, Socket, <<?COM_PING>>, 0),
  69. {ok, OkPacket, _SeqNum2} = recv_packet(TcpModule, Socket, SeqNum1),
  70. parse_ok_packet(OkPacket).
  71. -spec query(Query :: iodata(), atom(), term(), timeout()) ->
  72. {ok, [#ok{} | #resultset{} | #error{}]} | {error, timeout}.
  73. query(Query, TcpModule, Socket, Timeout) ->
  74. Req = <<?COM_QUERY, (iolist_to_binary(Query))/binary>>,
  75. SeqNum0 = 0,
  76. {ok, _SeqNum1} = send_packet(TcpModule, Socket, Req, SeqNum0),
  77. fetch_query_response(TcpModule, Socket, Timeout).
  78. %% @doc This is used by query/4. If query/4 returns {error, timeout}, this
  79. %% function can be called to retry to fetch the results of the query.
  80. fetch_query_response(TcpModule, Socket, Timeout) ->
  81. fetch_response(TcpModule, Socket, Timeout, text, []).
  82. %% @doc Prepares a statement.
  83. -spec prepare(iodata(), atom(), term()) -> #error{} | #prepared{}.
  84. prepare(Query, TcpModule, Socket) ->
  85. Req = <<?COM_STMT_PREPARE, (iolist_to_binary(Query))/binary>>,
  86. {ok, SeqNum1} = send_packet(TcpModule, Socket, Req, 0),
  87. {ok, Resp, SeqNum2} = recv_packet(TcpModule, Socket, SeqNum1),
  88. case Resp of
  89. ?error_pattern ->
  90. parse_error_packet(Resp);
  91. <<?OK,
  92. StmtId:32/little,
  93. NumColumns:16/little,
  94. NumParams:16/little,
  95. 0, %% reserved_1 -- [00] filler
  96. WarningCount:16/little>> ->
  97. %% This was the first packet.
  98. %% Now: Parameter Definition Block. The parameter definitions don't
  99. %% contain any useful data at all. They are always TYPE_VAR_STRING
  100. %% with charset 'binary' so we have to select a type ourselves for
  101. %% the parameters we have in execute/4.
  102. {_ParamDefs, SeqNum3} =
  103. fetch_column_definitions_if_any(NumParams, TcpModule, Socket,
  104. SeqNum2),
  105. %% Column Definition Block. We get column definitions in execute
  106. %% too, so we don't need them here. We *could* store them to be able
  107. %% to provide the user with some info about a prepared statement.
  108. {_ColDefs, _SeqNum4} =
  109. fetch_column_definitions_if_any(NumColumns, TcpModule, Socket,
  110. SeqNum3),
  111. #prepared{statement_id = StmtId,
  112. orig_query = Query,
  113. param_count = NumParams,
  114. warning_count = WarningCount}
  115. end.
  116. %% @doc Deallocates a prepared statement.
  117. -spec unprepare(#prepared{}, atom(), term()) -> ok.
  118. unprepare(#prepared{statement_id = Id}, TcpModule, Socket) ->
  119. {ok, _SeqNum} = send_packet(TcpModule, Socket,
  120. <<?COM_STMT_CLOSE, Id:32/little>>, 0),
  121. ok.
  122. %% @doc Executes a prepared statement.
  123. -spec execute(#prepared{}, [term()], atom(), term(), timeout()) ->
  124. {ok, [#ok{} | #resultset{} | #error{}]} | {error, timeout}.
  125. execute(#prepared{statement_id = Id, param_count = ParamCount}, ParamValues,
  126. TcpModule, Socket, Timeout) when ParamCount == length(ParamValues) ->
  127. %% Flags Constant Name
  128. %% 0x00 CURSOR_TYPE_NO_CURSOR
  129. %% 0x01 CURSOR_TYPE_READ_ONLY
  130. %% 0x02 CURSOR_TYPE_FOR_UPDATE
  131. %% 0x04 CURSOR_TYPE_SCROLLABLE
  132. Flags = 0,
  133. Req0 = <<?COM_STMT_EXECUTE, Id:32/little, Flags, 1:32/little>>,
  134. Req = case ParamCount of
  135. 0 ->
  136. Req0;
  137. _ ->
  138. %% We can't use the parameter types returned by the prepare call.
  139. %% They are all reported as ?TYPE_VAR_STRING with character
  140. %% set 'binary'.
  141. NullBitMap = build_null_bitmap(ParamValues),
  142. %% What does it mean to *not* bind new params? To use the same
  143. %% params as last time? Right now we always bind params each time.
  144. NewParamsBoundFlag = 1,
  145. Req1 = <<Req0/binary, NullBitMap/binary, NewParamsBoundFlag>>,
  146. %% For each value, first append type and signedness (16#80 signed or
  147. %% 00 unsigned) for all values and then the binary encoded values.
  148. EncodedParams = lists:map(fun encode_param/1, ParamValues),
  149. {TypesAndSigns, EncValues} = lists:unzip(EncodedParams),
  150. iolist_to_binary([Req1, TypesAndSigns, EncValues])
  151. end,
  152. {ok, _SeqNum1} = send_packet(TcpModule, Socket, Req, 0),
  153. fetch_execute_response(TcpModule, Socket, Timeout).
  154. %% @doc This is used by execute/5. If execute/5 returns {error, timeout}, this
  155. %% function can be called to retry to fetch the results of the query.
  156. fetch_execute_response(TcpModule, Socket, Timeout) ->
  157. fetch_response(TcpModule, Socket, Timeout, binary, []).
  158. %% --- internal ---
  159. %% @doc Parses a handshake. This is the first thing that comes from the server
  160. %% when connecting. If an unsupported version or variant of the protocol is used
  161. %% an error is raised.
  162. -spec parse_handshake(binary()) -> #handshake{}.
  163. parse_handshake(<<10, Rest/binary>>) ->
  164. %% Protocol version 10.
  165. {ServerVersion, Rest1} = nulterm_str(Rest),
  166. <<ConnectionId:32/little,
  167. AuthPluginDataPart1:8/binary-unit:8,
  168. 0, %% "filler" -- everything below is optional
  169. CapabilitiesLower:16/little,
  170. CharacterSet:8,
  171. StatusFlags:16/little,
  172. CapabilitiesUpper:16/little,
  173. AuthPluginDataLength:8, %% if cabab & CLIENT_PLUGIN_AUTH, otherwise 0
  174. _Reserved:10/binary-unit:8, %% 10 unused (reserved) bytes
  175. Rest3/binary>> = Rest1,
  176. Capabilities = CapabilitiesLower + 16#10000 * CapabilitiesUpper,
  177. Len = case AuthPluginDataLength of
  178. 0 -> 13; %% Server has not CLIENT_PLUGIN_AUTH
  179. K -> K - 8 %% Part 2 length = Total length minus the 8 bytes in part 1.
  180. end,
  181. <<AuthPluginDataPart2:Len/binary-unit:8, AuthPluginName/binary>> = Rest3,
  182. AuthPluginData = <<AuthPluginDataPart1/binary, AuthPluginDataPart2/binary>>,
  183. %% "Due to Bug#59453 the auth-plugin-name is missing the terminating
  184. %% NUL-char in versions prior to 5.5.10 and 5.6.2."
  185. %% Strip the final NUL byte if any.
  186. %% This may also be <<>> in older versions.
  187. L = byte_size(AuthPluginName) - 1,
  188. AuthPluginName1 = case AuthPluginName of
  189. <<AuthPluginNameTrimmed:L/binary, 0>> -> AuthPluginNameTrimmed;
  190. _ -> AuthPluginName
  191. end,
  192. #handshake{server_version = server_version_to_list(ServerVersion),
  193. connection_id = ConnectionId,
  194. capabilities = Capabilities,
  195. character_set = CharacterSet,
  196. status = StatusFlags,
  197. auth_plugin_data = AuthPluginData,
  198. auth_plugin_name = AuthPluginName1};
  199. parse_handshake(<<Protocol:8, _/binary>>) when Protocol /= 10 ->
  200. error(unknown_protocol).
  201. %% @doc Converts a version on the form `<<"5.6.21">' to a list `[5, 6, 21]'.
  202. -spec server_version_to_list(binary()) -> [integer()].
  203. server_version_to_list(ServerVersion) ->
  204. %% This must work with e.g. "5.5.40-0ubuntu0.12.04.1-log" and "5.5.33a".
  205. {match, Parts} = re:run(ServerVersion, <<"^(\\d+)\\.(\\d+)\\.(\\d+)">>,
  206. [{capture, all_but_first, binary}]),
  207. lists:map(fun binary_to_integer/1, Parts).
  208. %% @doc The response sent by the client to the server after receiving the
  209. %% initial handshake from the server
  210. -spec build_handshake_response(#handshake{}, iodata(), iodata(),
  211. iodata() | undefined, boolean()) -> binary().
  212. build_handshake_response(Handshake, Username, Password, Database, SetFoundRows) ->
  213. %% We require these capabilities. Make sure the server handles them.
  214. CapabilityFlags0 = ?CLIENT_PROTOCOL_41 bor
  215. ?CLIENT_TRANSACTIONS bor
  216. ?CLIENT_SECURE_CONNECTION,
  217. CapabilityFlags1 = case Database of
  218. undefined -> CapabilityFlags0;
  219. _ -> CapabilityFlags0 bor ?CLIENT_CONNECT_WITH_DB
  220. end,
  221. CapabilityFlags = case SetFoundRows of
  222. true -> CapabilityFlags1 bor ?CLIENT_FOUND_ROWS;
  223. _ -> CapabilityFlags1
  224. end,
  225. Handshake#handshake.capabilities band CapabilityFlags == CapabilityFlags
  226. orelse error(old_server_version),
  227. %% Add some extra capability flags only for signalling to the server what
  228. %% the client wants to do. The server doesn't say it handles them although
  229. %% it does. (http://bugs.mysql.com/bug.php?id=42268)
  230. ClientCapabilityFlags = CapabilityFlags bor
  231. ?CLIENT_MULTI_STATEMENTS bor
  232. ?CLIENT_MULTI_RESULTS bor
  233. ?CLIENT_PS_MULTI_RESULTS,
  234. Hash = case Handshake#handshake.auth_plugin_name of
  235. <<>> ->
  236. %% Server doesn't know auth plugins
  237. hash_password(Password, Handshake#handshake.auth_plugin_data);
  238. <<"mysql_native_password">> ->
  239. hash_password(Password, Handshake#handshake.auth_plugin_data);
  240. UnknownAuthMethod ->
  241. error({auth_method, UnknownAuthMethod})
  242. end,
  243. HashLength = size(Hash),
  244. CharacterSet = ?UTF8,
  245. UsernameUtf8 = unicode:characters_to_binary(Username),
  246. DbBin = case Database of
  247. undefined -> <<>>;
  248. _ -> <<(iolist_to_binary(Database))/binary, 0>>
  249. end,
  250. <<ClientCapabilityFlags:32/little,
  251. ?MAX_BYTES_PER_PACKET:32/little,
  252. CharacterSet:8,
  253. 0:23/unit:8, %% reserverd
  254. UsernameUtf8/binary,
  255. 0, %% NUL-terminator for the username
  256. HashLength,
  257. Hash/binary,
  258. DbBin/binary>>.
  259. %% @doc Handles the second packet from the server, when we have replied to the
  260. %% initial handshake. Returns an error if the server returns an error. Raises
  261. %% an error if unimplemented features are required.
  262. -spec parse_handshake_confirm(binary()) -> #ok{} | #error{}.
  263. parse_handshake_confirm(Packet) ->
  264. case Packet of
  265. ?ok_pattern ->
  266. %% Connection complete.
  267. parse_ok_packet(Packet);
  268. ?error_pattern ->
  269. %% Access denied, insufficient client capabilities, etc.
  270. parse_error_packet(Packet);
  271. <<?EOF>> ->
  272. %% "Old Authentication Method Switch Request Packet consisting of a
  273. %% single 0xfe byte. It is sent by server to request client to
  274. %% switch to Old Password Authentication if CLIENT_PLUGIN_AUTH
  275. %% capability is not supported (by either the client or the server)"
  276. error(old_auth);
  277. <<?EOF, _/binary>> ->
  278. %% "Authentication Method Switch Request Packet. If both server and
  279. %% client support CLIENT_PLUGIN_AUTH capability, server can send
  280. %% this packet to ask client to use another authentication method."
  281. error(auth_method_switch)
  282. end.
  283. %% -- both text and binary protocol --
  284. %% @doc Fetches one or more results and and parses the result set(s) using
  285. %% either the text format (for plain queries) or the binary format (for
  286. %% prepared statements).
  287. -spec fetch_response(atom(), term(), timeout(), text | binary, list()) ->
  288. {ok, [#ok{} | #resultset{} | #error{}]} | {error, timeout}.
  289. fetch_response(TcpModule, Socket, Timeout, Proto, Acc) ->
  290. case recv_packet(TcpModule, Socket, Timeout, any) of
  291. {ok, Packet, SeqNum2} ->
  292. Result = case Packet of
  293. ?ok_pattern ->
  294. parse_ok_packet(Packet);
  295. ?error_pattern ->
  296. parse_error_packet(Packet);
  297. ResultPacket ->
  298. %% The first packet in a resultset is only the column count.
  299. {ColCount, <<>>} = lenenc_int(ResultPacket),
  300. R0 = fetch_resultset(TcpModule, Socket, ColCount, SeqNum2),
  301. case R0 of
  302. #error{} = E ->
  303. %% TODO: Find a way to get here + testcase
  304. E;
  305. #resultset{} = R ->
  306. parse_resultset(R, ColCount, Proto)
  307. end
  308. end,
  309. Acc1 = [Result | Acc],
  310. case more_results_exists(Result) of
  311. true ->
  312. fetch_response(TcpModule, Socket, Timeout, Proto, Acc1);
  313. false ->
  314. {ok, lists:reverse(Acc1)}
  315. end;
  316. {error, timeout} ->
  317. {error, timeout}
  318. end.
  319. %% @doc Fetches packets for a result set. The column definitions are parsed but
  320. %% the rows are unparsed binary packages. This function is used for both the
  321. %% text protocol and the binary protocol. This affects the way the rows need to
  322. %% be parsed.
  323. -spec fetch_resultset(atom(), term(), integer(), integer()) ->
  324. #resultset{} | #error{}.
  325. fetch_resultset(TcpModule, Socket, FieldCount, SeqNum) ->
  326. {ok, ColDefs, SeqNum1} = fetch_column_definitions(TcpModule, Socket, SeqNum,
  327. FieldCount, []),
  328. {ok, DelimiterPacket, SeqNum2} = recv_packet(TcpModule, Socket, SeqNum1),
  329. #eof{status = S, warning_count = W} = parse_eof_packet(DelimiterPacket),
  330. case fetch_resultset_rows(TcpModule, Socket, SeqNum2, []) of
  331. {ok, Rows, _SeqNum3} ->
  332. ColDefs1 = lists:map(fun parse_column_definition/1, ColDefs),
  333. #resultset{cols = ColDefs1, rows = Rows,
  334. status = S, warning_count = W};
  335. #error{} = E ->
  336. E
  337. end.
  338. parse_resultset(#resultset{cols = ColDefs, rows = Rows} = R, ColumnCount, text) ->
  339. %% Parse the rows according to the 'text protocol' representation.
  340. Rows1 = [decode_text_row(ColumnCount, ColDefs, Row) || Row <- Rows],
  341. R#resultset{rows = Rows1};
  342. parse_resultset(#resultset{cols = ColDefs, rows = Rows} = R, ColumnCount, binary) ->
  343. %% Parse the rows according to the 'binary protocol' representation.
  344. Rows1 = [decode_binary_row(ColumnCount, ColDefs, Row) || Row <- Rows],
  345. R#resultset{rows = Rows1}.
  346. more_results_exists(#ok{status = S}) ->
  347. S band ?SERVER_MORE_RESULTS_EXISTS /= 0;
  348. more_results_exists(#error{}) ->
  349. false; %% No status bits for error
  350. more_results_exists(#resultset{status = S}) ->
  351. S band ?SERVER_MORE_RESULTS_EXISTS /= 0.
  352. %% @doc Receives NumLeft column definition packets. They are not parsed.
  353. %% @see parse_column_definition/1
  354. -spec fetch_column_definitions(atom(), term(), SeqNum :: integer(),
  355. NumLeft :: integer(), Acc :: [binary()]) ->
  356. {ok, ColDefPackets :: [binary()], NextSeqNum :: integer()}.
  357. fetch_column_definitions(TcpModule, Socket, SeqNum, NumLeft, Acc)
  358. when NumLeft > 0 ->
  359. {ok, Packet, SeqNum1} = recv_packet(TcpModule, Socket, SeqNum),
  360. fetch_column_definitions(TcpModule, Socket, SeqNum1, NumLeft - 1,
  361. [Packet | Acc]);
  362. fetch_column_definitions(_TcpModule, _Socket, SeqNum, 0, Acc) ->
  363. {ok, lists:reverse(Acc), SeqNum}.
  364. %% @doc Fetches rows in a result set. There is a packet per row. The row packets
  365. %% are not decoded. This function can be used for both the binary and the text
  366. %% protocol result sets.
  367. -spec fetch_resultset_rows(atom(), term(), SeqNum :: integer(), Acc) ->
  368. {ok, Rows, integer()} | #error{}
  369. when Acc :: [binary()],
  370. Rows :: [binary()].
  371. fetch_resultset_rows(TcpModule, Socket, SeqNum, Acc) ->
  372. {ok, Packet, SeqNum1} = recv_packet(TcpModule, Socket, SeqNum),
  373. case Packet of
  374. ?error_pattern ->
  375. parse_error_packet(Packet);
  376. ?eof_pattern ->
  377. {ok, lists:reverse(Acc), SeqNum1};
  378. Row ->
  379. fetch_resultset_rows(TcpModule, Socket, SeqNum1, [Row | Acc])
  380. end.
  381. %% Parses a packet containing a column definition (part of a result set)
  382. parse_column_definition(Data) ->
  383. {<<"def">>, Rest1} = lenenc_str(Data), %% catalog (always "def")
  384. {_Schema, Rest2} = lenenc_str(Rest1), %% schema-name
  385. {_Table, Rest3} = lenenc_str(Rest2), %% virtual table-name
  386. {_OrgTable, Rest4} = lenenc_str(Rest3), %% physical table-name
  387. {Name, Rest5} = lenenc_str(Rest4), %% virtual column name
  388. {_OrgName, Rest6} = lenenc_str(Rest5), %% physical column name
  389. {16#0c, Rest7} = lenenc_int(Rest6), %% length of the following fields
  390. %% (always 0x0c)
  391. <<Charset:16/little, %% column character set
  392. Length:32/little, %% maximum length of the field
  393. Type:8, %% type of the column as defined in Column Type
  394. Flags:16/little, %% flags
  395. Decimals:8, %% max shown decimal digits:
  396. 0, %% "filler" %% - 0x00 for integers and static strings
  397. 0, %% - 0x1f for dynamic strings, double, float
  398. Rest8/binary>> = Rest7, %% - 0x00 to 0x51 for decimals
  399. %% Here, if command was COM_FIELD_LIST {
  400. %% default values: lenenc_str
  401. %% }
  402. <<>> = Rest8,
  403. #col{name = Name, type = Type, charset = Charset, length = Length,
  404. decimals = Decimals, flags = Flags}.
  405. %% -- text protocol --
  406. -spec decode_text_row(NumColumns :: integer(),
  407. ColumnDefinitions :: [#col{}],
  408. Data :: binary()) -> [term()].
  409. decode_text_row(_NumColumns, ColumnDefs, Data) ->
  410. decode_text_row_acc(ColumnDefs, Data, []).
  411. %% parses Data using ColDefs and builds the values Acc.
  412. decode_text_row_acc([ColDef | ColDefs], Data, Acc) ->
  413. case Data of
  414. <<16#fb, Rest/binary>> ->
  415. %% NULL
  416. decode_text_row_acc(ColDefs, Rest, [null | Acc]);
  417. _ ->
  418. %% Every thing except NULL
  419. {Text, Rest} = lenenc_str(Data),
  420. Term = decode_text(ColDef, Text),
  421. decode_text_row_acc(ColDefs, Rest, [Term | Acc])
  422. end;
  423. decode_text_row_acc([], <<>>, Acc) ->
  424. lists:reverse(Acc).
  425. %% @doc When receiving data in the text protocol, we get everything as binaries
  426. %% (except NULL). This function is used to parse these string values.
  427. decode_text(#col{type = T}, Text)
  428. when T == ?TYPE_TINY; T == ?TYPE_SHORT; T == ?TYPE_LONG; T == ?TYPE_LONGLONG;
  429. T == ?TYPE_INT24; T == ?TYPE_YEAR ->
  430. binary_to_integer(Text);
  431. decode_text(#col{type = T}, Text)
  432. when T == ?TYPE_STRING; T == ?TYPE_VARCHAR; T == ?TYPE_VAR_STRING;
  433. T == ?TYPE_ENUM; T == ?TYPE_SET; T == ?TYPE_LONG_BLOB;
  434. T == ?TYPE_MEDIUM_BLOB; T == ?TYPE_BLOB; T == ?TYPE_TINY_BLOB;
  435. T == ?TYPE_GEOMETRY; T == ?TYPE_JSON ->
  436. %% As of MySQL 5.6.21 we receive SET and ENUM values as STRING, i.e. we
  437. %% cannot convert them to atom() or sets:set(), etc.
  438. Text;
  439. decode_text(#col{type = ?TYPE_BIT, length = Length}, Text) ->
  440. %% Convert to <<_:Length/bitstring>>
  441. decode_bitstring(Text, Length);
  442. decode_text(#col{type = T, decimals = S, length = L}, Text)
  443. when T == ?TYPE_DECIMAL; T == ?TYPE_NEWDECIMAL ->
  444. %% Length is the max number of symbols incl. dot and minus sign, e.g. the
  445. %% number of digits plus 2.
  446. decode_decimal(Text, L - 2, S);
  447. decode_text(#col{type = ?TYPE_DATE},
  448. <<Y:4/binary, "-", M:2/binary, "-", D:2/binary>>) ->
  449. {binary_to_integer(Y), binary_to_integer(M), binary_to_integer(D)};
  450. decode_text(#col{type = ?TYPE_TIME}, Text) ->
  451. {match, [Sign, Hbin, Mbin, Sbin, Frac]} =
  452. re:run(Text,
  453. <<"^(-?)(\\d+):(\\d+):(\\d+)(\\.?\\d*)$">>,
  454. [{capture, all_but_first, binary}]),
  455. H = binary_to_integer(Hbin),
  456. M = binary_to_integer(Mbin),
  457. S = binary_to_integer(Sbin),
  458. IsNeg = Sign == <<"-">>,
  459. Fraction = case Frac of
  460. <<>> -> 0;
  461. _ when not IsNeg -> binary_to_float(<<"0", Frac/binary>>);
  462. _ when IsNeg -> 1 - binary_to_float(<<"0", Frac/binary>>)
  463. end,
  464. Sec1 = H * 3600 + M * 60 + S,
  465. Sec2 = if IsNeg -> -Sec1; true -> Sec1 end,
  466. Sec3 = if IsNeg and (Fraction /= 0) -> Sec2 - 1;
  467. true -> Sec2
  468. end,
  469. {Days, {Hours, Minutes, Seconds}} = calendar:seconds_to_daystime(Sec3),
  470. {Days, {Hours, Minutes, Seconds + Fraction}};
  471. decode_text(#col{type = T},
  472. <<Y:4/binary, "-", M:2/binary, "-", D:2/binary, " ",
  473. H:2/binary, ":", Mi:2/binary, ":", S:2/binary>>)
  474. when T == ?TYPE_TIMESTAMP; T == ?TYPE_DATETIME ->
  475. %% Without fractions.
  476. {{binary_to_integer(Y), binary_to_integer(M), binary_to_integer(D)},
  477. {binary_to_integer(H), binary_to_integer(Mi), binary_to_integer(S)}};
  478. decode_text(#col{type = T},
  479. <<Y:4/binary, "-", M:2/binary, "-", D:2/binary, " ",
  480. H:2/binary, ":", Mi:2/binary, ":", FloatS/binary>>)
  481. when T == ?TYPE_TIMESTAMP; T == ?TYPE_DATETIME ->
  482. %% With fractions.
  483. {{binary_to_integer(Y), binary_to_integer(M), binary_to_integer(D)},
  484. {binary_to_integer(H), binary_to_integer(Mi), binary_to_float(FloatS)}};
  485. decode_text(#col{type = T}, Text) when T == ?TYPE_FLOAT;
  486. T == ?TYPE_DOUBLE ->
  487. try binary_to_float(Text)
  488. catch error:badarg ->
  489. try binary_to_integer(Text) of
  490. Int -> float(Int)
  491. catch error:badarg ->
  492. %% It is something like "4e75" that must be turned into "4.0e75"
  493. binary_to_float(binary:replace(Text, <<"e">>, <<".0e">>))
  494. end
  495. end.
  496. %% -- binary protocol --
  497. %% @doc If NumColumns is non-zero, fetches this number of column definitions
  498. %% and an EOF packet. Used by prepare/3.
  499. fetch_column_definitions_if_any(0, _TcpModule, _Socket, SeqNum) ->
  500. {[], SeqNum};
  501. fetch_column_definitions_if_any(N, TcpModule, Socket, SeqNum) ->
  502. {ok, Defs, SeqNum1} = fetch_column_definitions(TcpModule, Socket, SeqNum,
  503. N, []),
  504. {ok, ?eof_pattern, SeqNum2} = recv_packet(TcpModule, Socket, SeqNum1),
  505. {Defs, SeqNum2}.
  506. %% @doc Decodes a packet representing a row in a binary result set.
  507. %% It consists of a 0 byte, then a null bitmap, then the values.
  508. %% Returns a list of length NumColumns with terms of appropriate types for each
  509. %% MySQL type in ColumnTypes.
  510. -spec decode_binary_row(NumColumns :: integer(),
  511. ColumnDefs :: [#col{}],
  512. Data :: binary()) -> [term()].
  513. decode_binary_row(NumColumns, ColumnDefs, <<0, Data/binary>>) ->
  514. {NullBitMap, Rest} = null_bitmap_decode(NumColumns, Data, 2),
  515. decode_binary_row_acc(ColumnDefs, NullBitMap, Rest, []).
  516. %% @doc Accumulating helper for decode_binary_row/3.
  517. decode_binary_row_acc([_|ColDefs], <<1:1, NullBitMap/bitstring>>, Data, Acc) ->
  518. %% NULL
  519. decode_binary_row_acc(ColDefs, NullBitMap, Data, [null | Acc]);
  520. decode_binary_row_acc([ColDef | ColDefs], <<0:1, NullBitMap/bitstring>>, Data,
  521. Acc) ->
  522. %% Not NULL
  523. {Term, Rest} = decode_binary(ColDef, Data),
  524. decode_binary_row_acc(ColDefs, NullBitMap, Rest, [Term | Acc]);
  525. decode_binary_row_acc([], _, <<>>, Acc) ->
  526. lists:reverse(Acc).
  527. %% @doc Decodes a null bitmap as stored by MySQL and returns it in a strait
  528. %% bitstring counting bits from left to right in a tuple with remaining data.
  529. %%
  530. %% In the MySQL null bitmap the bits are stored counting bytes from the left and
  531. %% bits within each byte from the right. (Sort of little endian.)
  532. -spec null_bitmap_decode(NumColumns :: integer(), Data :: binary(),
  533. BitOffset :: integer()) ->
  534. {NullBitstring :: bitstring(), Rest :: binary()}.
  535. null_bitmap_decode(NumColumns, Data, BitOffset) ->
  536. %% Binary shift right by 3 is equivallent to integer division by 8.
  537. BitMapLength = (NumColumns + BitOffset + 7) bsr 3,
  538. <<NullBitstring0:BitMapLength/binary, Rest/binary>> = Data,
  539. <<_:BitOffset, NullBitstring:NumColumns/bitstring, _/bitstring>> =
  540. << <<(reverse_byte(B))/binary>> || <<B:1/binary>> <= NullBitstring0 >>,
  541. {NullBitstring, Rest}.
  542. %% @doc The reverse of null_bitmap_decode/3. The number of columns is taken to
  543. %% be the number of bits in NullBitstring. Returns the MySQL null bitmap as a
  544. %% binary (i.e. full bytes). BitOffset is the number of unused bits that should
  545. %% be inserted before the other bits.
  546. -spec null_bitmap_encode(bitstring(), integer()) -> binary().
  547. null_bitmap_encode(NullBitstring, BitOffset) ->
  548. PayloadLength = bit_size(NullBitstring) + BitOffset,
  549. %% Round up to a multiple of 8.
  550. BitMapLength = (PayloadLength + 7) band bnot 7,
  551. PadBitsLength = BitMapLength - PayloadLength,
  552. PaddedBitstring = <<0:BitOffset, NullBitstring/bitstring, 0:PadBitsLength>>,
  553. << <<(reverse_byte(B))/binary>> || <<B:1/binary>> <= PaddedBitstring >>.
  554. %% Reverses the bits in a byte.
  555. reverse_byte(<<A:1, B:1, C:1, D:1, E:1, F:1, G:1, H:1>>) ->
  556. <<H:1, G:1, F:1, E:1, D:1, C:1, B:1, A:1>>.
  557. %% @doc Used for executing prepared statements. The bit offset whould be 0 in
  558. %% this case.
  559. -spec build_null_bitmap([any()]) -> binary().
  560. build_null_bitmap(Values) ->
  561. Bits = << <<(case V of null -> 1; _ -> 0 end):1>> || V <- Values >>,
  562. null_bitmap_encode(Bits, 0).
  563. %% Decodes a value as received in the 'binary protocol' result set.
  564. %%
  565. %% The types are type constants for the binary protocol, such as
  566. %% ProtocolBinary::MYSQL_TYPE_STRING. In the guide "MySQL Internals" these are
  567. %% not listed, but we assume that are the same as for the text protocol.
  568. -spec decode_binary(ColDef :: #col{}, Data :: binary()) ->
  569. {Term :: term(), Rest :: binary()}.
  570. decode_binary(#col{type = T}, Data)
  571. when T == ?TYPE_STRING; T == ?TYPE_VARCHAR; T == ?TYPE_VAR_STRING;
  572. T == ?TYPE_ENUM; T == ?TYPE_SET; T == ?TYPE_LONG_BLOB;
  573. T == ?TYPE_MEDIUM_BLOB; T == ?TYPE_BLOB; T == ?TYPE_TINY_BLOB;
  574. T == ?TYPE_GEOMETRY; T == ?TYPE_JSON ->
  575. %% As of MySQL 5.6.21 we receive SET and ENUM values as STRING, i.e. we
  576. %% cannot convert them to atom() or sets:set(), etc.
  577. lenenc_str(Data);
  578. decode_binary(#col{type = ?TYPE_LONGLONG},
  579. <<Value:64/signed-little, Rest/binary>>) ->
  580. {Value, Rest};
  581. decode_binary(#col{type = T}, <<Value:32/signed-little, Rest/binary>>)
  582. when T == ?TYPE_LONG; T == ?TYPE_INT24 ->
  583. {Value, Rest};
  584. decode_binary(#col{type = T}, <<Value:16/signed-little, Rest/binary>>)
  585. when T == ?TYPE_SHORT; T == ?TYPE_YEAR ->
  586. {Value, Rest};
  587. decode_binary(#col{type = ?TYPE_TINY}, <<Value:8/signed, Rest/binary>>) ->
  588. {Value, Rest};
  589. decode_binary(#col{type = T, decimals = S, length = L}, Data)
  590. when T == ?TYPE_DECIMAL; T == ?TYPE_NEWDECIMAL ->
  591. %% Length is the max number of symbols incl. dot and minus sign, e.g. the
  592. %% number of digits plus 2.
  593. {Binary, Rest} = lenenc_str(Data),
  594. {decode_decimal(Binary, L - 2, S), Rest};
  595. decode_binary(#col{type = ?TYPE_DOUBLE},
  596. <<Value:64/float-little, Rest/binary>>) ->
  597. {Value, Rest};
  598. decode_binary(#col{type = ?TYPE_FLOAT}, <<0.0:32/float-little, Rest/binary>>) ->
  599. %% TYPE_FLOAT conversation fails on math:log10(0.0)
  600. {0.0, Rest};
  601. decode_binary(#col{type = ?TYPE_FLOAT},
  602. <<Value:32/float-little, Rest/binary>>) ->
  603. %% There is a precision loss when storing and fetching a 32-bit float.
  604. %% In the text protocol, it is obviously rounded. Storing 3.14 in a FLOAT
  605. %% column and fetching it using the text protocol, we get "3.14" which we
  606. %% parse to the Erlang double as close as possible to 3.14. Fetching the
  607. %% same value as a binary 32-bit float, we get 3.140000104904175. To achieve
  608. %% the same rounding after receiving it as a 32-bit float, we try to do the
  609. %% same rounding here as MySQL does when sending it over the text protocol.
  610. %%
  611. %% This comment explains the idea:
  612. %%
  613. %% Posted by Geoffrey Downs on March 10 2011 10:26am
  614. %%
  615. %% Following up... I *think* this is correct for the default float
  616. %% columns in mysql:
  617. %%
  618. %% var yourNumber = some floating point value
  619. %% max decimal precision = 10 ^ (-5 + floor(yourNumber log 10))
  620. %% So:
  621. %% 0 < x < 10 -> max precision is 0.00001
  622. %% 10 <= x < 100 -> max precision is 0.0001
  623. %% 100 <= x < 1000 -> max precision is 0.001
  624. %% etc.
  625. %%
  626. %% (From http://dev.mysql.com/doc/refman/5.7/en/problems-with-float.html
  627. %% fetched 10 Nov 2014)
  628. %%
  629. %% The above is almost correct, except for the example in the interval
  630. %% 0 < x < 1. There are 6 significant digits also for these numbers.
  631. %%
  632. %% Now, instead of P = 0.00001 we want the inverse 100000.0 but if we
  633. %% compute Factor = 1 / P we get a precision loss, so instead we do this:
  634. Factor = math:pow(10, floor(6 - math:log10(abs(Value)))),
  635. RoundedValue = round(Value * Factor) / Factor,
  636. {RoundedValue, Rest};
  637. decode_binary(#col{type = ?TYPE_BIT, length = Length}, Data) ->
  638. {Binary, Rest} = lenenc_str(Data),
  639. %% Convert to <<_:Length/bitstring>>
  640. {decode_bitstring(Binary, Length), Rest};
  641. decode_binary(#col{type = ?TYPE_DATE}, <<Length, Data/binary>>) ->
  642. %% Coded in the same way as DATETIME and TIMESTAMP below, but returned in
  643. %% a simple triple.
  644. case {Length, Data} of
  645. {0, _} -> {{0, 0, 0}, Data};
  646. {4, <<Y:16/little, M, D, Rest/binary>>} -> {{Y, M, D}, Rest}
  647. end;
  648. decode_binary(#col{type = T}, <<Length, Data/binary>>)
  649. when T == ?TYPE_DATETIME; T == ?TYPE_TIMESTAMP ->
  650. %% length (1) -- number of bytes following (valid values: 0, 4, 7, 11)
  651. case {Length, Data} of
  652. {0, _} ->
  653. {{{0, 0, 0}, {0, 0, 0}}, Data};
  654. {4, <<Y:16/little, M, D, Rest/binary>>} ->
  655. {{{Y, M, D}, {0, 0, 0}}, Rest};
  656. {7, <<Y:16/little, M, D, H, Mi, S, Rest/binary>>} ->
  657. {{{Y, M, D}, {H, Mi, S}}, Rest};
  658. {11, <<Y:16/little, M, D, H, Mi, S, Micro:32/little, Rest/binary>>} ->
  659. {{{Y, M, D}, {H, Mi, S + 0.000001 * Micro}}, Rest}
  660. end;
  661. decode_binary(#col{type = ?TYPE_TIME}, <<Length, Data/binary>>) ->
  662. %% length (1) -- number of bytes following (valid values: 0, 8, 12)
  663. %% is_negative (1) -- (1 if minus, 0 for plus)
  664. %% days (4) -- days
  665. %% hours (1) -- hours
  666. %% minutes (1) -- minutes
  667. %% seconds (1) -- seconds
  668. %% micro_seconds (4) -- micro-seconds
  669. case {Length, Data} of
  670. {0, _} ->
  671. {{0, {0, 0, 0}}, Data};
  672. {8, <<0, D:32/little, H, M, S, Rest/binary>>} ->
  673. {{D, {H, M, S}}, Rest};
  674. {12, <<0, D:32/little, H, M, S, Micro:32/little, Rest/binary>>} ->
  675. {{D, {H, M, S + 0.000001 * Micro}}, Rest};
  676. {8, <<1, D:32/little, H, M, S, Rest/binary>>} ->
  677. %% Negative time. Example: '-00:00:01' --> {-1,{23,59,59}}
  678. Seconds = ((D * 24 + H) * 60 + M) * 60 + S,
  679. %Seconds = D * 86400 + calendar:time_to_seconds({H, M, S}),
  680. {calendar:seconds_to_daystime(-Seconds), Rest};
  681. {12, <<1, D:32/little, H, M, S, Micro:32/little, Rest/binary>>}
  682. when Micro > 0 ->
  683. %% Negate and convert to seconds, excl fractions
  684. Seconds = -(((D * 24 + H) * 60 + M) * 60 + S),
  685. %Seconds = -D * 86400 - calendar:time_to_seconds({H, M, S}),
  686. %% Subtract 1 second for the fractions
  687. {Days, {Hours, Minutes, Sec}} =
  688. calendar:seconds_to_daystime(Seconds - 1),
  689. %% Adding the fractions to Sec again makes it a float
  690. {{Days, {Hours, Minutes, Sec + 1 - 0.000001 * Micro}}, Rest}
  691. end.
  692. %% @doc Like trunc/1 but towards negative infinity instead of towards zero.
  693. floor(Value) ->
  694. Trunc = trunc(Value),
  695. if
  696. Trunc =< Value -> Trunc;
  697. Trunc > Value -> Trunc - 1 %% for negative values
  698. end.
  699. %% @doc Encodes a term reprenting av value as a binary for use in the binary
  700. %% protocol. As this is used to encode parameters for prepared statements, the
  701. %% encoding is in its required form, namely `<<Type:8, Sign:8, Value/binary>>'.
  702. -spec encode_param(term()) -> {TypeAndSign :: binary(), Data :: binary()}.
  703. encode_param(null) ->
  704. {<<?TYPE_NULL, 0>>, <<>>};
  705. encode_param(Value) when is_binary(Value) ->
  706. EncLength = lenenc_int_encode(byte_size(Value)),
  707. {<<?TYPE_VAR_STRING, 0>>, <<EncLength/binary, Value/binary>>};
  708. encode_param(Value) when is_list(Value) ->
  709. encode_param(unicode:characters_to_binary(Value));
  710. encode_param(Value) when is_integer(Value), Value >= 0 ->
  711. %% We send positive integers with the 'unsigned' flag set.
  712. if
  713. Value =< 16#ff ->
  714. {<<?TYPE_TINY, 16#80>>, <<Value:8>>};
  715. Value =< 16#ffff ->
  716. {<<?TYPE_SHORT, 16#80>>, <<Value:16/little>>};
  717. Value =< 16#ffffffff ->
  718. {<<?TYPE_LONG, 16#80>>, <<Value:32/little>>};
  719. Value =< 16#ffffffffffffffff ->
  720. {<<?TYPE_LONGLONG, 16#80>>, <<Value:64/little>>};
  721. true ->
  722. %% If larger than a 64-bit int we send it as a string. MySQL does
  723. %% silently cast strings in aithmetic expressions. Also, DECIMALs
  724. %% are always sent as strings.
  725. encode_param(integer_to_binary(Value))
  726. end;
  727. encode_param(Value) when is_integer(Value), Value < 0 ->
  728. if
  729. Value >= -16#80 ->
  730. {<<?TYPE_TINY, 0>>, <<Value:8>>};
  731. Value >= -16#8000 ->
  732. {<<?TYPE_SHORT, 0>>, <<Value:16/little>>};
  733. Value >= -16#80000000 ->
  734. {<<?TYPE_LONG, 0>>, <<Value:32/little>>};
  735. Value >= -16#8000000000000000 ->
  736. {<<?TYPE_LONGLONG, 0>>, <<Value:64/little>>};
  737. true ->
  738. encode_param(integer_to_binary(Value))
  739. end;
  740. encode_param(Value) when is_float(Value) ->
  741. {<<?TYPE_DOUBLE, 0>>, <<Value:64/float-little>>};
  742. encode_param(Value) when is_bitstring(Value) ->
  743. Binary = encode_bitstring(Value),
  744. EncLength = lenenc_int_encode(byte_size(Binary)),
  745. {<<?TYPE_VAR_STRING, 0>>, <<EncLength/binary, Binary/binary>>};
  746. encode_param({Y, M, D}) ->
  747. %% calendar:date()
  748. {<<?TYPE_DATE, 0>>, <<4, Y:16/little, M, D>>};
  749. encode_param({{Y, M, D}, {0, 0, 0}}) ->
  750. %% Datetime at midnight
  751. {<<?TYPE_DATETIME, 0>>, <<4, Y:16/little, M, D>>};
  752. encode_param({{Y, M, D}, {H, Mi, S}}) when is_integer(S) ->
  753. %% calendar:datetime()
  754. {<<?TYPE_DATETIME, 0>>, <<7, Y:16/little, M, D, H, Mi, S>>};
  755. encode_param({{Y, M, D}, {H, Mi, S}}) when is_float(S) ->
  756. %% calendar:datetime() with a float for seconds. This way it looks very
  757. %% similar to a datetime. Microseconds in MySQL timestamps are possible but
  758. %% not very common.
  759. Sec = trunc(S),
  760. Micro = round(1000000 * (S - Sec)),
  761. {<<?TYPE_DATETIME, 0>>, <<11, Y:16/little, M, D, H, Mi, Sec,
  762. Micro:32/little>>};
  763. encode_param({D, {H, M, S}}) when is_integer(S), D >= 0 ->
  764. %% calendar:seconds_to_daystime()
  765. {<<?TYPE_TIME, 0>>, <<8, 0, D:32/little, H, M, S>>};
  766. encode_param({D, {H, M, S}}) when is_integer(S), D < 0 ->
  767. %% Convert to seconds, negate and convert back to daystime form.
  768. %% Then set the minus flag.
  769. Seconds = ((D * 24 + H) * 60 + M) * 60 + S,
  770. {D1, {H1, M1, S1}} = calendar:seconds_to_daystime(-Seconds),
  771. {<<?TYPE_TIME, 0>>, <<8, 1, D1:32/little, H1, M1, S1>>};
  772. encode_param({D, {H, M, S}}) when is_float(S), D >= 0 ->
  773. S1 = trunc(S),
  774. Micro = round(1000000 * (S - S1)),
  775. {<<?TYPE_TIME, 0>>, <<12, 0, D:32/little, H, M, S1, Micro:32/little>>};
  776. encode_param({D, {H, M, S}}) when is_float(S), S > 0.0, D < 0 ->
  777. IntS = trunc(S),
  778. Micro = round(1000000 * (1 - S + IntS)),
  779. Seconds = (D * 24 + H) * 3600 + M * 60 + IntS + 1,
  780. {D1, {M1, H1, S1}} = calendar:seconds_to_daystime(-Seconds),
  781. {<<?TYPE_TIME, 0>>, <<12, 1, D1:32/little, H1, M1, S1, Micro:32/little>>};
  782. encode_param({D, {H, M, 0.0}}) ->
  783. encode_param({D, {H, M, 0}}).
  784. %% -- Value representation in both the text and binary protocols --
  785. %% @doc Convert to `<<_:Length/bitstring>>'
  786. decode_bitstring(Binary, Length) ->
  787. PaddingLength = bit_size(Binary) - Length,
  788. <<_:PaddingLength/bitstring, Bitstring:Length/bitstring>> = Binary,
  789. Bitstring.
  790. encode_bitstring(Bitstring) ->
  791. Size = bit_size(Bitstring),
  792. PaddingSize = byte_size(Bitstring) * 8 - Size,
  793. <<0:PaddingSize, Bitstring:Size/bitstring>>.
  794. decode_decimal(Bin, _P, 0) ->
  795. binary_to_integer(Bin);
  796. decode_decimal(Bin, P, S) when P =< 15, S > 0 ->
  797. binary_to_float(Bin);
  798. decode_decimal(Bin, P, S) when P >= 16, S > 0 ->
  799. Bin.
  800. %% -- Protocol basics: packets --
  801. %% @doc Wraps Data in packet headers, sends it by calling TcpModule:send/2 with
  802. %% Socket and returns {ok, SeqNum1} where SeqNum1 is the next sequence number.
  803. -spec send_packet(atom(), term(), Data :: binary(), SeqNum :: integer()) ->
  804. {ok, NextSeqNum :: integer()}.
  805. send_packet(TcpModule, Socket, Data, SeqNum) ->
  806. {WithHeaders, SeqNum1} = add_packet_headers(Data, SeqNum),
  807. ok = TcpModule:send(Socket, WithHeaders),
  808. {ok, SeqNum1}.
  809. %% @see recv_packet/4
  810. recv_packet(TcpModule, Socket, SeqNum) ->
  811. recv_packet(TcpModule, Socket, infinity, SeqNum).
  812. %% @doc Receives data by calling TcpModule:recv/2 and removes the packet
  813. %% headers. Returns the packet contents and the next packet sequence number.
  814. -spec recv_packet(atom(), term(), timeout(), integer() | any) ->
  815. {ok, Data :: binary(), NextSeqNum :: integer()} | {error, term()}.
  816. recv_packet(TcpModule, Socket, Timeout, SeqNum) ->
  817. recv_packet(TcpModule, Socket, Timeout, SeqNum, <<>>).
  818. %% @doc Accumulating helper for recv_packet/4
  819. -spec recv_packet(atom(), term(), timeout(), integer() | any, binary()) ->
  820. {ok, Data :: binary(), NextSeqNum :: integer()} | {error, term()}.
  821. recv_packet(TcpModule, Socket, Timeout, ExpectSeqNum, Acc) ->
  822. case TcpModule:recv(Socket, 4, Timeout) of
  823. {ok, Header} ->
  824. {Size, SeqNum, More} = parse_packet_header(Header),
  825. true = SeqNum == ExpectSeqNum orelse ExpectSeqNum == any,
  826. {ok, Body} = TcpModule:recv(Socket, Size),
  827. Acc1 = <<Acc/binary, Body/binary>>,
  828. NextSeqNum = (SeqNum + 1) band 16#ff,
  829. case More of
  830. false -> {ok, Acc1, NextSeqNum};
  831. true -> recv_packet(TcpModule, Socket, Timeout, NextSeqNum,
  832. Acc1)
  833. end;
  834. {error, Reason} ->
  835. {error, Reason}
  836. end.
  837. %% @doc Parses a packet header (32 bits) and returns a tuple.
  838. %%
  839. %% The client should first read a header and parse it. Then read PacketLength
  840. %% bytes. If there are more packets, read another header and read a new packet
  841. %% length of payload until there are no more packets. The seq num should
  842. %% increment from 0 and may wrap around at 255 back to 0.
  843. %%
  844. %% When all packets are read and the payload of all packets are concatenated, it
  845. %% can be parsed using parse_response/1, etc. depending on what type of response
  846. %% is expected.
  847. -spec parse_packet_header(PackerHeader :: binary()) ->
  848. {PacketLength :: integer(),
  849. SeqNum :: integer(),
  850. MorePacketsExist :: boolean()}.
  851. parse_packet_header(<<PacketLength:24/little-integer, SeqNum:8/integer>>) ->
  852. {PacketLength, SeqNum, PacketLength == 16#ffffff}.
  853. %% @doc Splits a packet body into chunks and wraps them in headers. The
  854. %% resulting list is ready to sent to the socket.
  855. -spec add_packet_headers(PacketBody :: iodata(), SeqNum :: integer()) ->
  856. {PacketWithHeaders :: iodata(), NextSeqNum :: integer()}.
  857. add_packet_headers(PacketBody, SeqNum) ->
  858. Bin = iolist_to_binary(PacketBody),
  859. Size = size(Bin),
  860. SeqNum1 = (SeqNum + 1) band 16#ff,
  861. %% Todo: implement the case when Size >= 16#ffffff.
  862. if Size < 16#ffffff ->
  863. {[<<Size:24/little, SeqNum:8>>, Bin], SeqNum1}
  864. end.
  865. -spec parse_ok_packet(binary()) -> #ok{}.
  866. parse_ok_packet(<<?OK:8, Rest/binary>>) ->
  867. {AffectedRows, Rest1} = lenenc_int(Rest),
  868. {InsertId, Rest2} = lenenc_int(Rest1),
  869. <<StatusFlags:16/little, WarningCount:16/little, Msg/binary>> = Rest2,
  870. %% We have CLIENT_PROTOCOL_41 but not CLIENT_SESSION_TRACK enabled. The
  871. %% protocol is conditional. This is from the protocol documentation:
  872. %%
  873. %% if capabilities & CLIENT_PROTOCOL_41 {
  874. %% int<2> status_flags
  875. %% int<2> warning_count
  876. %% } elseif capabilities & CLIENT_TRANSACTIONS {
  877. %% int<2> status_flags
  878. %% }
  879. %% if capabilities & CLIENT_SESSION_TRACK {
  880. %% string<lenenc> info
  881. %% if status_flags & SERVER_SESSION_STATE_CHANGED {
  882. %% string<lenenc> session_state_changes
  883. %% }
  884. %% } else {
  885. %% string<EOF> info
  886. %% }
  887. #ok{affected_rows = AffectedRows,
  888. insert_id = InsertId,
  889. status = StatusFlags,
  890. warning_count = WarningCount,
  891. msg = Msg}.
  892. -spec parse_error_packet(binary()) -> #error{}.
  893. parse_error_packet(<<?ERROR:8, ErrNo:16/little, "#", SQLState:5/binary-unit:8,
  894. Msg/binary>>) ->
  895. %% Error, 4.1 protocol.
  896. %% (Older protocol: <<?ERROR:8, ErrNo:16/little, Msg/binary>>)
  897. #error{code = ErrNo, state = SQLState, msg = Msg}.
  898. -spec parse_eof_packet(binary()) -> #eof{}.
  899. parse_eof_packet(<<?EOF:8, NumWarnings:16/little, StatusFlags:16/little>>) ->
  900. %% EOF packet, 4.1 protocol.
  901. %% (Older protocol: <<?EOF:8>>)
  902. #eof{status = StatusFlags, warning_count = NumWarnings}.
  903. -spec hash_password(Password :: iodata(), Salt :: binary()) -> Hash :: binary().
  904. hash_password(Password, Salt) ->
  905. %% From the "MySQL Internals" manual:
  906. %% SHA1( password ) XOR SHA1( "20-bytes random data from server" <concat>
  907. %% SHA1( SHA1( password ) ) )
  908. %% ----
  909. %% Make sure the salt is exactly 20 bytes.
  910. %%
  911. %% The auth data is obviously nul-terminated. For the "native" auth
  912. %% method, it should be a 20 byte salt, so let's trim it in this case.
  913. PasswordBin = case erlang:is_binary(Password) of
  914. true -> Password;
  915. false -> erlang:iolist_to_binary(Password)
  916. end,
  917. case PasswordBin =:= <<>> of
  918. true -> <<>>;
  919. false -> hash_non_empty_password(Password, Salt)
  920. end.
  921. -spec hash_non_empty_password(Password :: iodata(), Salt :: binary()) -> Hash :: binary().
  922. hash_non_empty_password(Password, Salt) ->
  923. Salt1 = case Salt of
  924. <<SaltNoNul:20/binary-unit:8, 0>> -> SaltNoNul;
  925. _ when size(Salt) == 20 -> Salt
  926. end,
  927. %% Hash as described above.
  928. <<Hash1Num:160>> = Hash1 = crypto:hash(sha, Password),
  929. Hash2 = crypto:hash(sha, Hash1),
  930. <<Hash3Num:160>> = crypto:hash(sha, <<Salt1/binary, Hash2/binary>>),
  931. <<(Hash1Num bxor Hash3Num):160>>.
  932. %% --- Lowlevel: variable length integers and strings ---
  933. %% lenenc_int/1 decodes length-encoded-integer values
  934. -spec lenenc_int(Input :: binary()) -> {Value :: integer(), Rest :: binary()}.
  935. lenenc_int(<<Value:8, Rest/bits>>) when Value < 251 -> {Value, Rest};
  936. lenenc_int(<<16#fc:8, Value:16/little, Rest/binary>>) -> {Value, Rest};
  937. lenenc_int(<<16#fd:8, Value:24/little, Rest/binary>>) -> {Value, Rest};
  938. lenenc_int(<<16#fe:8, Value:64/little, Rest/binary>>) -> {Value, Rest}.
  939. %% Length-encoded-integer encode. Appends the encoded value to Acc.
  940. %% Values not representable in 64 bits are not accepted.
  941. -spec lenenc_int_encode(0..16#ffffffffffffffff) -> binary().
  942. lenenc_int_encode(Value) when Value >= 0 ->
  943. if Value < 251 -> <<Value>>;
  944. Value =< 16#ffff -> <<16#fc, Value:16/little>>;
  945. Value =< 16#ffffff -> <<16#fd, Value:24/little>>;
  946. Value =< 16#ffffffffffffffff -> <<16#fe, Value:64/little>>
  947. end.
  948. %% lenenc_str/1 decodes length-encoded-string values
  949. -spec lenenc_str(Input :: binary()) -> {String :: binary(), Rest :: binary()}.
  950. lenenc_str(Bin) ->
  951. {Length, Rest} = lenenc_int(Bin),
  952. <<String:Length/binary, Rest1/binary>> = Rest,
  953. {String, Rest1}.
  954. %% nts/1 decodes a nul-terminated string
  955. -spec nulterm_str(Input :: binary()) -> {String :: binary(), Rest :: binary()}.
  956. nulterm_str(Bin) ->
  957. [String, Rest] = binary:split(Bin, <<0>>),
  958. {String, Rest}.
  959. -ifdef(TEST).
  960. -include_lib("eunit/include/eunit.hrl").
  961. %% Testing some of the internal functions, mostly the cases we don't cover in
  962. %% other tests.
  963. decode_text_test() ->
  964. %% Int types
  965. lists:foreach(fun (T) ->
  966. ?assertEqual(1, decode_text(#col{type = T}, <<"1">>))
  967. end,
  968. [?TYPE_TINY, ?TYPE_SHORT, ?TYPE_LONG, ?TYPE_LONGLONG,
  969. ?TYPE_INT24, ?TYPE_YEAR]),
  970. %% BIT
  971. <<217>> = decode_text(#col{type = ?TYPE_BIT, length = 8}, <<217>>),
  972. %% Floating point and decimal numbers
  973. lists:foreach(fun (T) ->
  974. ?assertEqual(3.0, decode_text(#col{type = T}, <<"3.0">>))
  975. end,
  976. [?TYPE_FLOAT, ?TYPE_DOUBLE]),
  977. %% Decimal types
  978. lists:foreach(fun (T) ->
  979. ColDef = #col{type = T, decimals = 1, length = 4},
  980. ?assertMatch(3.0, decode_text(ColDef, <<"3.0">>))
  981. end,
  982. [?TYPE_DECIMAL, ?TYPE_NEWDECIMAL]),
  983. ?assertEqual(3.0, decode_text(#col{type = ?TYPE_FLOAT}, <<"3">>)),
  984. ?assertEqual(30.0, decode_text(#col{type = ?TYPE_FLOAT}, <<"3e1">>)),
  985. ?assertEqual(3, decode_text(#col{type = ?TYPE_LONG}, <<"3">>)),
  986. %% Date and time
  987. ?assertEqual({2014, 11, 01},
  988. decode_text(#col{type = ?TYPE_DATE}, <<"2014-11-01">>)),
  989. ?assertEqual({0, {23, 59, 01}},
  990. decode_text(#col{type = ?TYPE_TIME}, <<"23:59:01">>)),
  991. ?assertEqual({{2014, 11, 01}, {23, 59, 01}},
  992. decode_text(#col{type = ?TYPE_DATETIME},
  993. <<"2014-11-01 23:59:01">>)),
  994. ?assertEqual({{2014, 11, 01}, {23, 59, 01}},
  995. decode_text(#col{type = ?TYPE_TIMESTAMP},
  996. <<"2014-11-01 23:59:01">>)),
  997. %% Strings and blobs
  998. lists:foreach(fun (T) ->
  999. ColDef = #col{type = T},
  1000. ?assertEqual(<<"x">>, decode_text(ColDef, <<"x">>))
  1001. end,
  1002. [?TYPE_VARCHAR, ?TYPE_ENUM, ?TYPE_TINY_BLOB,
  1003. ?TYPE_MEDIUM_BLOB, ?TYPE_LONG_BLOB, ?TYPE_BLOB,
  1004. ?TYPE_VAR_STRING, ?TYPE_STRING, ?TYPE_GEOMETRY]),
  1005. ok.
  1006. decode_binary_test() ->
  1007. %% Test the special rounding we apply to (single precision) floats.
  1008. ?assertEqual({1.0, <<>>},
  1009. decode_binary(#col{type = ?TYPE_FLOAT},
  1010. <<1.0:32/float-little>>)),
  1011. ?assertEqual({0.2, <<>>},
  1012. decode_binary(#col{type = ?TYPE_FLOAT},
  1013. <<0.2:32/float-little>>)),
  1014. ?assertEqual({-33.3333, <<>>},
  1015. decode_binary(#col{type = ?TYPE_FLOAT},
  1016. <<-33.333333:32/float-little>>)),
  1017. ?assertEqual({0.000123457, <<>>},
  1018. decode_binary(#col{type = ?TYPE_FLOAT},
  1019. <<0.00012345678:32/float-little>>)),
  1020. ?assertEqual({1234.57, <<>>},
  1021. decode_binary(#col{type = ?TYPE_FLOAT},
  1022. <<1234.56789:32/float-little>>)),
  1023. ok.
  1024. null_bitmap_test() ->
  1025. ?assertEqual({<<0, 1:1>>, <<>>}, null_bitmap_decode(9, <<0, 4>>, 2)),
  1026. ?assertEqual(<<0, 4>>, null_bitmap_encode(<<0, 1:1>>, 2)),
  1027. ok.
  1028. lenenc_int_test() ->
  1029. %% decode
  1030. ?assertEqual({40, <<>>}, lenenc_int(<<40>>)),
  1031. ?assertEqual({16#ff, <<>>}, lenenc_int(<<16#fc, 255, 0>>)),
  1032. ?assertEqual({16#33aaff, <<>>}, lenenc_int(<<16#fd, 16#ff, 16#aa, 16#33>>)),
  1033. ?assertEqual({16#12345678, <<>>}, lenenc_int(<<16#fe, 16#78, 16#56, 16#34,
  1034. 16#12, 0, 0, 0, 0>>)),
  1035. %% encode
  1036. ?assertEqual(<<40>>, lenenc_int_encode(40)),
  1037. ?assertEqual(<<16#fc, 255, 0>>, lenenc_int_encode(255)),
  1038. ?assertEqual(<<16#fd, 16#ff, 16#aa, 16#33>>,
  1039. lenenc_int_encode(16#33aaff)),
  1040. ?assertEqual(<<16#fe, 16#78, 16#56, 16#34, 16#12, 0, 0, 0, 0>>,
  1041. lenenc_int_encode(16#12345678)),
  1042. ok.
  1043. lenenc_str_test() ->
  1044. ?assertEqual({<<"Foo">>, <<"bar">>}, lenenc_str(<<3, "Foobar">>)).
  1045. nulterm_test() ->
  1046. ?assertEqual({<<"Foo">>, <<"bar">>}, nulterm_str(<<"Foo", 0, "bar">>)).
  1047. parse_header_test() ->
  1048. %% Example from "MySQL Internals", revision 307, section 14.1.3.3 EOF_Packet
  1049. Packet = <<16#05, 16#00, 16#00, 16#05, 16#fe, 16#00, 16#00, 16#02, 16#00>>,
  1050. <<Header:4/binary-unit:8, Body/binary>> = Packet,
  1051. %% Check header contents and body length
  1052. ?assertEqual({size(Body), 5, false}, parse_packet_header(Header)),
  1053. ok.
  1054. add_packet_headers_test() ->
  1055. {Data, 43} = add_packet_headers(<<"foo">>, 42),
  1056. ?assertEqual(<<3, 0, 0, 42, "foo">>, list_to_binary(Data)).
  1057. parse_ok_test() ->
  1058. Body = <<0, 5, 1, 2, 0, 0, 0, "Foo">>,
  1059. ?assertEqual(#ok{affected_rows = 5,
  1060. insert_id = 1,
  1061. status = ?SERVER_STATUS_AUTOCOMMIT,
  1062. warning_count = 0,
  1063. msg = <<"Foo">>},
  1064. parse_ok_packet(Body)).
  1065. parse_error_test() ->
  1066. %% Protocol 4.1
  1067. Body = <<255, 42, 0, "#", "XYZxx", "Foo">>,
  1068. ?assertEqual(#error{code = 42, state = <<"XYZxx">>, msg = <<"Foo">>},
  1069. parse_error_packet(Body)),
  1070. ok.
  1071. parse_eof_test() ->
  1072. %% Example from "MySQL Internals", revision 307, section 14.1.3.3 EOF_Packet
  1073. Packet = <<16#05, 16#00, 16#00, 16#05, 16#fe, 16#00, 16#00, 16#02, 16#00>>,
  1074. <<_Header:4/binary-unit:8, Body/binary>> = Packet,
  1075. %% Ignore header. Parse body as an eof_packet.
  1076. ?assertEqual(#eof{warning_count = 0,
  1077. status = ?SERVER_STATUS_AUTOCOMMIT},
  1078. parse_eof_packet(Body)),
  1079. ok.
  1080. hash_password_test() ->
  1081. ?assertEqual(<<222,207,222,139,41,181,202,13,191,241,
  1082. 234,234,73,127,244,101,205,3,28,251>>,
  1083. hash_password(<<"foo">>, <<"abcdefghijklmnopqrst">>)),
  1084. ?assertEqual(<<>>, hash_password(<<>>, <<"abcdefghijklmnopqrst">>)).
  1085. -endif.