mysql_protocol.erl 50 KB

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