diff --git a/lib/tds/protocol.ex b/lib/tds/protocol.ex index eb25609..740f094 100644 --- a/lib/tds/protocol.ex +++ b/lib/tds/protocol.ex @@ -51,12 +51,11 @@ defmodule Tds.Protocol do end end - def disconnect(_err, %{sock: {:gen_tcp, sock}} = s) do - :ok = :gen_tcp.close(sock) + def disconnect(_err, %{sock: {mod, sock}} = s) do # If socket is active we flush any socket messages so the next # socket does not get the messages. _ = flush(s) - :ok + mod.close(sock) end def ping(s) do diff --git a/lib/tds/types.ex b/lib/tds/types.ex index 81b7a07..ac55ea1 100644 --- a/lib/tds/types.ex +++ b/lib/tds/types.ex @@ -980,33 +980,26 @@ defmodule Tds.Types do def encode_data(@tds_data_type_bigvarbinary = data_type, value, attr) when is_integer(value), do: encode_data(data_type, <>, attr) - - def encode_data(@tds_data_type_bigvarbinary, value, _) do - if value != nil do - <> <> value - else - <<@tds_plp_null::little-unsigned-64>> - end - end + def encode_data(@tds_data_type_bigvarbinary, nil, _), + do: <<@tds_plp_null::little-unsigned-64>> + def encode_data(@tds_data_type_bigvarbinary, value, _), + do: <> <> value @doc """ Data Encoding String Types """ + def encode_data(@tds_data_type_nvarchar, nil, _), + do: <<@tds_plp_null::little-unsigned-64>> def encode_data(@tds_data_type_nvarchar, value, _) do - if value == nil do - <<@tds_plp_null::little-unsigned-64>> - else - value = value |> to_little_ucs2 - value_size = byte_size(value) - - cond do - value_size == 0 -> - <<0x00::unsigned-64, 0x00::unsigned-32>> - value_size > 8000 -> - encode_plp(value) - true -> - <> <> value - end + value = to_little_ucs2(value) + value_size = byte_size(value) + cond do + value_size <= 0 -> + <<0x00::unsigned-64, 0x00::unsigned-32>> + value_size > 8000 -> + encode_plp(value) + true -> + <> <> value end end @@ -1057,8 +1050,8 @@ defmodule Tds.Types do value_size = byte_size(value_binary) len = cond do - precision <= 9 -> 4 - precision <= 19 -> 8 + precision <= 9 -> 4 + precision <= 19 -> 8 precision <= 28 -> 12 precision <= 38 -> 16 end @@ -1067,9 +1060,8 @@ defmodule Tds.Types do value_binary = value_binary <> <<0::size(padding)-unit(8)>> <> <> <> <> value_binary end - def encode_data(@tds_data_type_decimaln, nil, _) do - <<0x00, 0x00, 0x00, 0x00>> - end + def encode_data(@tds_data_type_decimaln, nil, _), + do: <<0x00::little-unsigned-32>> # <<0, 0, 0, 0> def encode_data(@tds_data_type_decimaln = data_type, value, attr) do encode_data(data_type, Decimal.new(value), attr) end @@ -1146,7 +1138,7 @@ defmodule Tds.Types do def encode_plp(data) do size = byte_size(data) - <> <> encode_plp_chunk(size, data, <<>>) <> <<0x00::32>> + <> <> encode_plp_chunk(size, data, <<>>) <> <<0x00::little-unsigned-32>> end def encode_plp_chunk(0, _, buf), do: buf