Skip to content

Commit

Permalink
Use parens for bitstring macros
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Sep 22, 2022
1 parent b7bccab commit 6c58cc9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 87 deletions.
6 changes: 3 additions & 3 deletions lib/myxql/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ defmodule MyXQL.Client do
defp recv_packets(data, decode, decoder_state, result_state, timeout, client, partial \\ <<>>)

defp recv_packets(
<<size::uint3, _seq::uint1, payload::string(size), rest::binary>> = data,
<<size::uint3(), _seq::uint1(), payload::string(size), rest::binary>> = data,
decoder,
{:more_results, resultset},
result_state,
Expand All @@ -215,7 +215,7 @@ defmodule MyXQL.Client do
end

defp recv_packets(
<<size::uint3, _seq::uint1, payload::string(size), rest::binary>>,
<<size::uint3(), _seq::uint1(), payload::string(size), rest::binary>>,
decoder,
decoder_state,
result_state,
Expand All @@ -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(
<<size::uint3, _seq::uint1, payload::string(size), rest::binary>>,
<<size::uint3(), _seq::uint1(), payload::string(size), rest::binary>>,
decoder,
decoder_state,
result_state,
Expand Down
70 changes: 35 additions & 35 deletions lib/myxql/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule MyXQL.Protocol do
encode_packet(rest, rest_size, next_sequence_id, max_packet_size)
]
else
[<<payload_size::uint3, sequence_id::uint1>>, payload]
[<<payload_size::uint3(), sequence_id::uint1()>>, payload]
end
end

Expand All @@ -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

Expand All @@ -69,7 +69,7 @@ defmodule MyXQL.Protocol do
end

defp decode_err_packet_body(
<<code::uint2, _sql_state_marker::string(1), _sql_state::string(5), message::bits>>
<<code::uint2(), _sql_state_marker::string(1), _sql_state::string(5), message::bits>>
) do
err_packet(code: code, message: message)
end
Expand All @@ -78,14 +78,14 @@ defmodule MyXQL.Protocol do
decode_eof_packet_body(rest)
end

defp decode_eof_packet_body(<<num_warnings::uint2, status_flags::uint2>>) do
defp decode_eof_packet_body(<<num_warnings::uint2(), status_flags::uint2()>>) do
eof_packet(
status_flags: status_flags,
num_warnings: num_warnings
)
end

defp decode_connect_err_packet_body(<<code::uint2, message::bits>>) do
defp decode_connect_err_packet_body(<<code::uint2(), message::bits>>) do
err_packet(code: code, message: message)
end

Expand All @@ -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

<<capability_flags::uint4>> = <<capability_flags1::uint2, capability_flags2::uint2>>
<<capability_flags::uint4()>> = <<capability_flags1::uint2(), capability_flags2::uint2()>>
# 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
Expand Down Expand Up @@ -198,8 +198,8 @@ defmodule MyXQL.Protocol do
database = if database, do: <<database::binary, 0x00>>, else: ""

<<
capability_flags::uint4,
max_packet_size::uint4,
capability_flags::uint4(),
max_packet_size::uint4(),
charset,
0::uint(23),
<<username::binary, 0x00>>,
Expand All @@ -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)
>>
Expand Down Expand Up @@ -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, <<statement_id::uint4>>]
[0x19, <<statement_id::uint4()>>]
end

# https://dev.mysql.com/doc/internals/en/com-stmt-reset.html
def encode_com({:com_stmt_reset, statement_id}) do
[0x1A, <<statement_id::uint4>>]
[0x1A, <<statement_id::uint4()>>]
end

# https://dev.mysql.com/doc/internals/en/com-stmt-execute.html
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -423,7 +423,7 @@ defmodule MyXQL.Protocol do
null_bitmap_size = div(count + 7, 8)
new_params_bound_flag = 1

<<null_bitmap::uint(null_bitmap_size), new_params_bound_flag::uint1, types::binary,
<<null_bitmap::uint(null_bitmap_size), new_params_bound_flag::uint1(), types::binary,
values::binary>>
end

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions lib/myxql/protocol/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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: <<int>>
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(<<int::uint1, rest::binary>>) 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(<<int::uint1(), rest::binary>>) 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
Expand Down
Loading

0 comments on commit 6c58cc9

Please sign in to comment.