Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mjaric committed Aug 26, 2017
1 parent 65dc482 commit 52a4a8e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
5 changes: 2 additions & 3 deletions lib/tds/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 20 additions & 28 deletions lib/tds/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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, <<value>>, attr)

def encode_data(@tds_data_type_bigvarbinary, value, _) do
if value != nil do
<<byte_size(value)::little-unsigned-16>> <> 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: <<byte_size(value)::little-unsigned-16>> <> 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_size::little-size(2)-unit(8)>> <> 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_size::little-size(2)-unit(8)>> <> value
end
end

Expand Down Expand Up @@ -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
Expand All @@ -1067,9 +1060,8 @@ defmodule Tds.Types do
value_binary = value_binary <> <<0::size(padding)-unit(8)>>
<<byte_len>> <> <<sign>> <> 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
Expand Down Expand Up @@ -1146,7 +1138,7 @@ defmodule Tds.Types do

def encode_plp(data) do
size = byte_size(data)
<<size::little-unsigned-64>> <> encode_plp_chunk(size, data, <<>>) <> <<0x00::32>>
<<size::little-unsigned-64>> <> encode_plp_chunk(size, data, <<>>) <> <<0x00::little-unsigned-32>>
end

def encode_plp_chunk(0, _, buf), do: buf
Expand Down

0 comments on commit 52a4a8e

Please sign in to comment.