From 6c58cc9ea297e581d30b7991ae7c23cbf2fe8b7c Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Thu, 22 Sep 2022 10:22:50 +0200 Subject: [PATCH] Use parens for bitstring macros --- lib/myxql/client.ex | 6 +-- lib/myxql/protocol.ex | 70 +++++++++++++-------------- lib/myxql/protocol/types.ex | 14 +++--- lib/myxql/protocol/values.ex | 93 ++++++++++++++++++++---------------- 4 files changed, 96 insertions(+), 87 deletions(-) diff --git a/lib/myxql/client.ex b/lib/myxql/client.ex index e973f6e4..e76c13cd 100644 --- a/lib/myxql/client.ex +++ b/lib/myxql/client.ex @@ -193,7 +193,7 @@ defmodule MyXQL.Client do defp recv_packets(data, decode, decoder_state, result_state, timeout, client, partial \\ <<>>) defp recv_packets( - <> = data, + <> = data, decoder, {:more_results, resultset}, result_state, @@ -215,7 +215,7 @@ defmodule MyXQL.Client do end defp recv_packets( - <>, + <>, decoder, decoder_state, result_state, @@ -242,7 +242,7 @@ defmodule MyXQL.Client do # If the packet size equals max packet size, save the payload, receive # more data and try again defp recv_packets( - <>, + <>, decoder, decoder_state, result_state, diff --git a/lib/myxql/protocol.ex b/lib/myxql/protocol.ex index 8f67076e..9b5a9176 100644 --- a/lib/myxql/protocol.ex +++ b/lib/myxql/protocol.ex @@ -37,7 +37,7 @@ defmodule MyXQL.Protocol do encode_packet(rest, rest_size, next_sequence_id, max_packet_size) ] else - [<>, payload] + [<>, payload] end end @@ -54,8 +54,8 @@ defmodule MyXQL.Protocol do {last_insert_id, rest} = take_int_lenenc(rest) << - status_flags::uint2, - num_warnings::uint2, + status_flags::uint2(), + num_warnings::uint2(), info::binary >> = rest @@ -69,7 +69,7 @@ defmodule MyXQL.Protocol do end defp decode_err_packet_body( - <> + <> ) do err_packet(code: code, message: message) end @@ -78,14 +78,14 @@ defmodule MyXQL.Protocol do decode_eof_packet_body(rest) end - defp decode_eof_packet_body(<>) do + defp decode_eof_packet_body(<>) do eof_packet( status_flags: status_flags, num_warnings: num_warnings ) end - defp decode_connect_err_packet_body(<>) do + defp decode_connect_err_packet_body(<>) do err_packet(code: code, message: message) end @@ -99,23 +99,23 @@ defmodule MyXQL.Protocol do {server_version, rest} = take_string_nul(rest) << - conn_id::uint4, + conn_id::uint4(), auth_plugin_data1::string(8), 0, - capability_flags1::uint2, - charset::uint1, - status_flags::uint2, - capability_flags2::uint2, + capability_flags1::uint2(), + charset::uint1(), + status_flags::uint2(), + capability_flags2::uint2(), rest::binary >> = rest - <> = <> + <> = <> # all set in servers since MySQL 4.1 required_capabilities = [:client_protocol_41, :client_plugin_auth, :client_secure_connection] with :ok <- ensure_capabilities(capability_flags, required_capabilities) do << - auth_plugin_data_length::uint1, + auth_plugin_data_length::uint1(), _::uint(10), rest::binary >> = rest @@ -198,8 +198,8 @@ defmodule MyXQL.Protocol do database = if database, do: <>, else: "" << - capability_flags::uint4, - max_packet_size::uint4, + capability_flags::uint4(), + max_packet_size::uint4(), charset, 0::uint(23), <>, @@ -217,8 +217,8 @@ defmodule MyXQL.Protocol do ) ) do << - capability_flags::uint4, - max_packet_size::uint4, + capability_flags::uint4(), + max_packet_size::uint4(), charset, 0::uint(23) >> @@ -276,12 +276,12 @@ defmodule MyXQL.Protocol do # https://dev.mysql.com/doc/internals/en/com-stmt-close.html def encode_com({:com_stmt_close, statement_id}) do - [0x19, <>] + [0x19, <>] end # https://dev.mysql.com/doc/internals/en/com-stmt-reset.html def encode_com({:com_stmt_reset, statement_id}) do - [0x1A, <>] + [0x1A, <>] end # https://dev.mysql.com/doc/internals/en/com-stmt-execute.html @@ -299,9 +299,9 @@ defmodule MyXQL.Protocol do << command, - statement_id::uint4, - flags::uint1, - iteration_count::uint4, + statement_id::uint4(), + flags::uint1(), + iteration_count::uint4(), params::binary >> end @@ -310,8 +310,8 @@ defmodule MyXQL.Protocol do def encode_com({:com_stmt_fetch, statement_id, num_rows}) do << 0x1C, - statement_id::uint4, - num_rows::uint4 + statement_id::uint4(), + num_rows::uint4() >> end @@ -329,8 +329,8 @@ defmodule MyXQL.Protocol do end def decode_com_stmt_prepare_response( - <<0x00, statement_id::uint4, num_columns::uint2, num_params::uint2, 0, - num_warnings::uint2>>, + <<0x00, statement_id::uint4(), num_columns::uint2(), num_params::uint2(), 0, + num_warnings::uint2()>>, next_data, :initial ) do @@ -423,7 +423,7 @@ defmodule MyXQL.Protocol do null_bitmap_size = div(count + 7, 8) new_params_bound_flag = 1 - <> end @@ -465,12 +465,12 @@ defmodule MyXQL.Protocol do << 0x0C, - _character_set::uint2, - column_length::uint4, - type::uint1, - flags::uint2, - _decimals::uint1, - 0::uint2 + _character_set::uint2(), + column_length::uint4(), + type::uint1(), + flags::uint2(), + _decimals::uint1(), + 0::uint2() >> = rest column_def( @@ -518,7 +518,7 @@ defmodule MyXQL.Protocol do end defp decode_resultset( - <<0xFE, num_warnings::uint2, status_flags::uint2>>, + <<0xFE, num_warnings::uint2(), status_flags::uint2()>>, next_data, {:column_defs_eof, column_defs}, _row_decoder @@ -541,7 +541,7 @@ defmodule MyXQL.Protocol do end defp decode_resultset( - <<0xFE, num_warnings::uint2, status_flags::uint2>>, + <<0xFE, num_warnings::uint2(), status_flags::uint2()>>, _next_data, {:rows, column_defs, num_rows, acc}, _row_decoder diff --git a/lib/myxql/protocol/types.ex b/lib/myxql/protocol/types.ex index b3b045eb..f5034b2a 100644 --- a/lib/myxql/protocol/types.ex +++ b/lib/myxql/protocol/types.ex @@ -22,19 +22,19 @@ defmodule MyXQL.Protocol.Types do # https://dev.mysql.com/doc/internals/en/integer.html#packet-Protocol::LengthEncodedInteger def encode_int_lenenc(int) when int < 251, do: <> - def encode_int_lenenc(int) when int < 0xFFFF, do: <<0xFC, int::uint2>> - def encode_int_lenenc(int) when int < 0xFFFFFF, do: <<0xFD, int::uint3>> - def encode_int_lenenc(int) when int < 0xFFFFFFFFFFFFFFFF, do: <<0xFE, int::uint8>> + def encode_int_lenenc(int) when int < 0xFFFF, do: <<0xFC, int::uint2()>> + def encode_int_lenenc(int) when int < 0xFFFFFF, do: <<0xFD, int::uint3()>> + def encode_int_lenenc(int) when int < 0xFFFFFFFFFFFFFFFF, do: <<0xFE, int::uint8()>> def decode_int_lenenc(binary) do {integer, ""} = take_int_lenenc(binary) integer end - def take_int_lenenc(<>) when int < 251, do: {int, rest} - def take_int_lenenc(<<0xFC, int::uint2, rest::binary>>), do: {int, rest} - def take_int_lenenc(<<0xFD, int::uint3, rest::binary>>), do: {int, rest} - def take_int_lenenc(<<0xFE, int::uint8, rest::binary>>), do: {int, rest} + def take_int_lenenc(<>) when int < 251, do: {int, rest} + def take_int_lenenc(<<0xFC, int::uint2(), rest::binary>>), do: {int, rest} + def take_int_lenenc(<<0xFD, int::uint3(), rest::binary>>), do: {int, rest} + def take_int_lenenc(<<0xFE, int::uint8(), rest::binary>>), do: {int, rest} # https://dev.mysql.com/doc/internals/en/string.html#packet-Protocol::FixedLengthString defmacro string(size) do diff --git a/lib/myxql/protocol/values.ex b/lib/myxql/protocol/values.ex index 46a42112..48826961 100644 --- a/lib/myxql/protocol/values.ex +++ b/lib/myxql/protocol/values.ex @@ -187,7 +187,7 @@ defmodule MyXQL.Protocol.Values do def encode_binary_value(value) when is_integer(value) and value >= -1 <<< 63 and value < 1 <<< 64 do - {:mysql_type_longlong, <>} + {:mysql_type_longlong, <>} end def encode_binary_value(value) when is_float(value) do @@ -202,11 +202,11 @@ defmodule MyXQL.Protocol.Values do end def encode_binary_value(%Date{year: year, month: month, day: day}) do - {:mysql_type_date, <<4, year::uint2, month::uint1, day::uint1>>} + {:mysql_type_date, <<4, year::uint2(), month::uint1(), day::uint1()>>} end def encode_binary_value(:zero_date) do - {:mysql_type_date, <<4, 0::uint2, 0::uint1, 0::uint1>>} + {:mysql_type_date, <<4, 0::uint2(), 0::uint1(), 0::uint1()>>} end def encode_binary_value(%Time{} = time), do: encode_binary_time(time) @@ -269,7 +269,7 @@ defmodule MyXQL.Protocol.Values do defp encode_geometry(geo) do srid = geo.srid || 0 binary = %{geo | srid: nil} |> Geo.WKB.encode_to_iodata(:ndr) |> IO.iodata_to_binary() - {:mysql_type_var_string, encode_string_lenenc(<>)} + {:mysql_type_var_string, encode_string_lenenc(<>)} end end @@ -283,7 +283,8 @@ defmodule MyXQL.Protocol.Values do end defp encode_binary_time(%Time{hour: hour, minute: minute, second: second, microsecond: {0, 0}}) do - {:mysql_type_time, <<8, 0::uint1, 0::uint4, hour::uint1, minute::uint1, second::uint1>>} + {:mysql_type_time, + <<8, 0::uint1(), 0::uint4(), hour::uint1(), minute::uint1(), second::uint1()>>} end defp encode_binary_time(%Time{ @@ -293,7 +294,8 @@ defmodule MyXQL.Protocol.Values do microsecond: {microsecond, _} }) do {:mysql_type_time, - <<12, 0::uint1, 0::uint4, hour::uint1, minute::uint1, second::uint1, microsecond::uint4>>} + <<12, 0::uint1(), 0::uint4(), hour::uint1(), minute::uint1(), second::uint1(), + microsecond::uint4()>>} end defp encode_binary_datetime(%NaiveDateTime{ @@ -306,7 +308,8 @@ defmodule MyXQL.Protocol.Values do microsecond: {0, 0} }) do {:mysql_type_datetime, - <<7, year::uint2, month::uint1, day::uint1, hour::uint1, minute::uint1, second::uint1>>} + <<7, year::uint2(), month::uint1(), day::uint1(), hour::uint1(), minute::uint1(), + second::uint1()>>} end defp encode_binary_datetime(%NaiveDateTime{ @@ -319,8 +322,8 @@ defmodule MyXQL.Protocol.Values do microsecond: {microsecond, _} }) do {:mysql_type_datetime, - <<11, year::uint2, month::uint1, day::uint1, hour::uint1, minute::uint1, second::uint1, - microsecond::uint4>>} + <<11, year::uint2(), month::uint1(), day::uint1(), hour::uint1(), minute::uint1(), + second::uint1(), microsecond::uint4()>>} end defp encode_binary_datetime(%DateTime{ @@ -334,8 +337,8 @@ defmodule MyXQL.Protocol.Values do time_zone: "Etc/UTC" }) do {:mysql_type_datetime, - <<11, year::uint2, month::uint1, day::uint1, hour::uint1, minute::uint1, second::uint1, - microsecond::uint4>>} + <<11, year::uint2(), month::uint1(), day::uint1(), hour::uint1(), minute::uint1(), + second::uint1(), microsecond::uint4()>>} end defp encode_binary_datetime(%DateTime{} = datetime) do @@ -418,7 +421,7 @@ defmodule MyXQL.Protocol.Values do if Code.ensure_loaded?(Geo) do # https://dev.mysql.com/doc/refman/8.0/en/gis-data-formats.html#gis-internal-format - defp decode_geometry(<>) do + defp decode_geometry(<>) do srid = if srid == 0, do: nil, else: srid r |> Geo.WKB.decode!() |> Map.put(:srid, srid) end @@ -434,28 +437,28 @@ defmodule MyXQL.Protocol.Values do end end - defp decode_int1(<>, null_bitmap, t, acc), + defp decode_int1(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) - defp decode_uint1(<>, null_bitmap, t, acc), + defp decode_uint1(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) - defp decode_int2(<>, null_bitmap, t, acc), + defp decode_int2(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) - defp decode_uint2(<>, null_bitmap, t, acc), + defp decode_uint2(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) - defp decode_int4(<>, null_bitmap, t, acc), + defp decode_int4(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) - defp decode_uint4(<>, null_bitmap, t, acc), + defp decode_uint4(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) - defp decode_int8(<>, null_bitmap, t, acc), + defp decode_int8(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) - defp decode_uint8(<>, null_bitmap, t, acc), + defp decode_uint8(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) defp decode_float(<>, null_bitmap, t, acc), @@ -466,10 +469,15 @@ defmodule MyXQL.Protocol.Values do # in theory it's supposed to be a `string_lenenc` field. However since MySQL decimals # maximum precision is 65 digits, the size of the string will always fir on one byte. - defp decode_decimal(<>, null_bitmap, t, acc), + defp decode_decimal(<>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [Decimal.new(string) | acc]) - defp decode_date(<<4, year::uint2, month::uint1, day::uint1, r::bits>>, null_bitmap, t, acc) do + defp decode_date( + <<4, year::uint2(), month::uint1(), day::uint1(), r::bits>>, + null_bitmap, + t, + acc + ) do v = %Date{year: year, month: month, day: day} decode_binary_row(r, null_bitmap >>> 1, t, [v | acc]) end @@ -480,7 +488,8 @@ defmodule MyXQL.Protocol.Values do end defp decode_time( - <<8, is_negative, days::uint4, hours::uint1, minutes::uint1, seconds::uint1, r::bits>>, + <<8, is_negative, days::uint4(), hours::uint1(), minutes::uint1(), seconds::uint1(), + r::bits>>, null_bitmap, t, acc @@ -490,8 +499,8 @@ defmodule MyXQL.Protocol.Values do end defp decode_time( - <<12, is_negative, days::uint4, hours::uint1, minutes::uint1, seconds::uint1, - microseconds::uint4, r::bits>>, + <<12, is_negative, days::uint4(), hours::uint1(), minutes::uint1(), seconds::uint1(), + microseconds::uint4(), r::bits>>, null_bitmap, t, acc @@ -521,7 +530,7 @@ defmodule MyXQL.Protocol.Values do end defp decode_datetime( - <<4, year::uint2, month::uint1, day::uint1, r::bits>>, + <<4, year::uint2(), month::uint1(), day::uint1(), r::bits>>, null_bitmap, t, acc, @@ -532,8 +541,8 @@ defmodule MyXQL.Protocol.Values do end defp decode_datetime( - <<7, year::uint2, month::uint1, day::uint1, hour::uint1, minute::uint1, second::uint1, - r::bits>>, + <<7, year::uint2(), month::uint1(), day::uint1(), hour::uint1(), minute::uint1(), + second::uint1(), r::bits>>, null_bitmap, t, acc, @@ -544,8 +553,8 @@ defmodule MyXQL.Protocol.Values do end defp decode_datetime( - <<11, year::uint2, month::uint1, day::uint1, hour::uint1, minute::uint1, second::uint1, - microsecond::uint4, r::bits>>, + <<11, year::uint2(), month::uint1(), day::uint1(), hour::uint1(), minute::uint1(), + second::uint1(), microsecond::uint4(), r::bits>>, null_bitmap, t, acc, @@ -594,12 +603,12 @@ defmodule MyXQL.Protocol.Values do } end - defp decode_string_lenenc(<>, null_bitmap, t, acc, decoder) + defp decode_string_lenenc(<>, null_bitmap, t, acc, decoder) when n < 251, do: decode_binary_row(r, null_bitmap >>> 1, t, [decoder.(v) | acc]) defp decode_string_lenenc( - <<0xFC, n::uint2, v::string(n), r::bits>>, + <<0xFC, n::uint2(), v::string(n), r::bits>>, null_bitmap, t, acc, @@ -608,7 +617,7 @@ defmodule MyXQL.Protocol.Values do do: decode_binary_row(r, null_bitmap >>> 1, t, [decoder.(v) | acc]) defp decode_string_lenenc( - <<0xFD, n::uint3, v::string(n), r::bits>>, + <<0xFD, n::uint3(), v::string(n), r::bits>>, null_bitmap, t, acc, @@ -617,7 +626,7 @@ defmodule MyXQL.Protocol.Values do do: decode_binary_row(r, null_bitmap >>> 1, t, [decoder.(v) | acc]) defp decode_string_lenenc( - <<0xFE, n::uint8, v::string(n), r::bits>>, + <<0xFE, n::uint8(), v::string(n), r::bits>>, null_bitmap, t, acc, @@ -625,16 +634,16 @@ defmodule MyXQL.Protocol.Values do ), do: decode_binary_row(r, null_bitmap >>> 1, t, [decoder.(v) | acc]) - defp decode_json(<>, null_bitmap, t, acc) when n < 251, + defp decode_json(<>, null_bitmap, t, acc) when n < 251, do: decode_binary_row(r, null_bitmap >>> 1, t, [decode_json(v) | acc]) - defp decode_json(<<0xFC, n::uint2, v::string(n), r::bits>>, null_bitmap, t, acc), + defp decode_json(<<0xFC, n::uint2(), v::string(n), r::bits>>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [decode_json(v) | acc]) - defp decode_json(<<0xFD, n::uint3, v::string(n), r::bits>>, null_bitmap, t, acc), + defp decode_json(<<0xFD, n::uint3(), v::string(n), r::bits>>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [decode_json(v) | acc]) - defp decode_json(<<0xFE, n::uint8, v::string(n), r::bits>>, null_bitmap, t, acc), + defp decode_json(<<0xFE, n::uint8(), v::string(n), r::bits>>, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [decode_json(v) | acc]) defp decode_json(string), do: json_library().decode!(string) @@ -643,16 +652,16 @@ defmodule MyXQL.Protocol.Values do Application.get_env(:myxql, :json_library, Jason) end - defp decode_bit(<>, size, null_bitmap, t, acc) when n < 251, + defp decode_bit(<>, size, null_bitmap, t, acc) when n < 251, do: decode_binary_row(r, null_bitmap >>> 1, t, [decode_bit(v, size) | acc]) - defp decode_bit(<<0xFC, n::uint2, v::string(n), r::bits>>, size, null_bitmap, t, acc), + defp decode_bit(<<0xFC, n::uint2(), v::string(n), r::bits>>, size, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [decode_bit(v, size) | acc]) - defp decode_bit(<<0xFD, n::uint3, v::string(n), r::bits>>, size, null_bitmap, t, acc), + defp decode_bit(<<0xFD, n::uint3(), v::string(n), r::bits>>, size, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [decode_bit(v, size) | acc]) - defp decode_bit(<<0xFE, n::uint8, v::string(n), r::bits>>, size, null_bitmap, t, acc), + defp decode_bit(<<0xFE, n::uint8(), v::string(n), r::bits>>, size, null_bitmap, t, acc), do: decode_binary_row(r, null_bitmap >>> 1, t, [decode_bit(v, size) | acc]) defp decode_bit(binary, size) do