From 46903fc5c5068184c05bce03ead256fb0ecd7a64 Mon Sep 17 00:00:00 2001 From: Bryan Paxton <39971740+starbelly@users.noreply.github.com> Date: Mon, 24 Nov 2025 07:50:17 -0600 Subject: [PATCH 1/7] Always return ok or error tuple on connect --- lib/tds/protocol.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/tds/protocol.ex b/lib/tds/protocol.ex index 44c1057..800657b 100644 --- a/lib/tds/protocol.ex +++ b/lib/tds/protocol.ex @@ -373,8 +373,11 @@ defmodule Tds.Protocol do :gen_tcp.close(sock) {:error, error} + {:ok, _} = ret -> + ret + other -> - other + {:error, other} end else {:error, error} -> From 9448a14c9b96953a05ecfb0561ee14c0b01e2cae Mon Sep 17 00:00:00 2001 From: Bryan Paxton <39971740+starbelly@users.noreply.github.com> Date: Mon, 24 Nov 2025 08:21:11 -0600 Subject: [PATCH 2/7] Remove explicit match for :disconnect in msg_send/2 --- lib/tds/protocol.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/tds/protocol.ex b/lib/tds/protocol.ex index 800657b..40eef2a 100644 --- a/lib/tds/protocol.ex +++ b/lib/tds/protocol.ex @@ -855,7 +855,6 @@ defmodule Tds.Protocol do |> IO.iodata_to_binary() |> decode(s) else - {:disconnect, _ex, _s} = res -> {0, res} other -> other end end From cf04cfb6baf1bb3be58c92b05e8dacaf787acf26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 24 Nov 2025 15:36:30 +0100 Subject: [PATCH 3/7] Refactor TCP connection handling in protocol.ex --- lib/tds/protocol.ex | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/tds/protocol.ex b/lib/tds/protocol.ex index 40eef2a..e8d52bc 100644 --- a/lib/tds/protocol.ex +++ b/lib/tds/protocol.ex @@ -364,22 +364,22 @@ defmodule Tds.Protocol do s = %{s | opts: opts} # Initalize TCP connection with the SQL Server - with {:ok, sock} <- :gen_tcp.connect(host, port, sock_opts, timeout), - {:ok, buffers} <- :inet.getopts(sock, [:sndbuf, :recbuf, :buffer]), - :ok <- :inet.setopts(sock, buffer: max_buf_size(buffers)) do - # Send Prelogin message to SQL Server - case send_prelogin(%{s | sock: {:gen_tcp, sock}}) do - {:error, error, _state} -> - :gen_tcp.close(sock) - {:error, error} - - {:ok, _} = ret -> - ret - - other -> - {:error, other} - end - else + case :gen_tcp.connect(host, port, sock_opts, timeout) do + {:ok, sock} -> + with {:ok, buffers} <- :inet.getopts(sock, [:sndbuf, :recbuf, :buffer]), + :ok <- :inet.setopts(sock, buffer: max_buf_size(buffers)), + {:ok, s} <- send_prelogin(%{s | sock: {:gen_tcp, sock}}) do + {:ok, s} + else + {:disconnect, exception, _state} -> + :gen_tcp.close(sock) + {:error, exception} + + {:error, error} -> + :gen_tcp.close(sock) + {:error, %Tds.Error{message: "tcp connect: #{error}"}} + end + {:error, error} -> {:error, %Tds.Error{message: "tcp connect: #{error}"}} end @@ -854,8 +854,6 @@ defmodule Tds.Protocol do buffer |> IO.iodata_to_binary() |> decode(s) - else - other -> other end end From bf28a06d5402ca1347c6e65d9473e97dbbf69dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 24 Nov 2025 15:36:57 +0100 Subject: [PATCH 4/7] Update lib/tds/protocol.ex --- lib/tds/protocol.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tds/protocol.ex b/lib/tds/protocol.ex index e8d52bc..9fc62c4 100644 --- a/lib/tds/protocol.ex +++ b/lib/tds/protocol.ex @@ -375,9 +375,9 @@ defmodule Tds.Protocol do :gen_tcp.close(sock) {:error, exception} - {:error, error} -> - :gen_tcp.close(sock) - {:error, %Tds.Error{message: "tcp connect: #{error}"}} + {:error, error} -> + :gen_tcp.close(sock) + {:error, %Tds.Error{message: "tcp connect: #{error}"}} end {:error, error} -> From 9467f6a28fbcc1cbadd6c128ea54a62541822bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 24 Nov 2025 15:37:20 +0100 Subject: [PATCH 5/7] Update lib/tds/protocol.ex --- lib/tds/protocol.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tds/protocol.ex b/lib/tds/protocol.ex index 9fc62c4..dcff948 100644 --- a/lib/tds/protocol.ex +++ b/lib/tds/protocol.ex @@ -378,7 +378,7 @@ defmodule Tds.Protocol do {:error, error} -> :gen_tcp.close(sock) {:error, %Tds.Error{message: "tcp connect: #{error}"}} - end + end {:error, error} -> {:error, %Tds.Error{message: "tcp connect: #{error}"}} From 637b88e3452c851306d88b4fdbee9c8c4241bfca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 24 Nov 2025 15:39:10 +0100 Subject: [PATCH 6/7] Update lib/tds/protocol.ex --- lib/tds/protocol.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tds/protocol.ex b/lib/tds/protocol.ex index dcff948..aa7fb2b 100644 --- a/lib/tds/protocol.ex +++ b/lib/tds/protocol.ex @@ -371,7 +371,7 @@ defmodule Tds.Protocol do {:ok, s} <- send_prelogin(%{s | sock: {:gen_tcp, sock}}) do {:ok, s} else - {:disconnect, exception, _state} -> + {_error_or_disconnect, exception, _state} -> :gen_tcp.close(sock) {:error, exception} From 87a823e27fafb4a7293be1691365a955aec7dd2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 24 Nov 2025 15:45:30 +0100 Subject: [PATCH 7/7] Fix indentation and formatting in TCP connection logic --- lib/tds/protocol.ex | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/tds/protocol.ex b/lib/tds/protocol.ex index aa7fb2b..0d800ce 100644 --- a/lib/tds/protocol.ex +++ b/lib/tds/protocol.ex @@ -366,19 +366,19 @@ defmodule Tds.Protocol do # Initalize TCP connection with the SQL Server case :gen_tcp.connect(host, port, sock_opts, timeout) do {:ok, sock} -> - with {:ok, buffers} <- :inet.getopts(sock, [:sndbuf, :recbuf, :buffer]), - :ok <- :inet.setopts(sock, buffer: max_buf_size(buffers)), - {:ok, s} <- send_prelogin(%{s | sock: {:gen_tcp, sock}}) do - {:ok, s} - else - {_error_or_disconnect, exception, _state} -> - :gen_tcp.close(sock) - {:error, exception} - - {:error, error} -> - :gen_tcp.close(sock) - {:error, %Tds.Error{message: "tcp connect: #{error}"}} - end + with {:ok, buffers} <- :inet.getopts(sock, [:sndbuf, :recbuf, :buffer]), + :ok <- :inet.setopts(sock, buffer: max_buf_size(buffers)), + {:ok, s} <- send_prelogin(%{s | sock: {:gen_tcp, sock}}) do + {:ok, s} + else + {_error_or_disconnect, exception, _state} -> + :gen_tcp.close(sock) + {:error, exception} + + {:error, error} -> + :gen_tcp.close(sock) + {:error, %Tds.Error{message: "tcp connect: #{error}"}} + end {:error, error} -> {:error, %Tds.Error{message: "tcp connect: #{error}"}}