Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix socket_options instead using keywork merge, concat default options with assigned options #183

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/myxql/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule MyXQL.Client do

defstruct [:sock, :connection_id]

@sock_opts [mode: :binary, packet: :raw, active: false]

defmodule Config do
@moduledoc false

Expand All @@ -29,6 +31,8 @@ defmodule MyXQL.Client do
:enable_cleartext_plugin
]

@sock_opts [mode: :binary, packet: :raw, active: false]

def new(opts) do
{address, port} = address_and_port(opts)

Expand All @@ -43,8 +47,7 @@ defmodule MyXQL.Client do
ssl_opts: Keyword.get(opts, :ssl_opts, []),
connect_timeout: Keyword.get(opts, :connect_timeout, @default_timeout),
handshake_timeout: Keyword.get(opts, :handshake_timeout, @default_timeout),
socket_options:
Keyword.merge([mode: :binary, packet: :raw, active: false], opts[:socket_options] || []),
socket_options: @sock_opts ++ (opts[:socket_options] || []),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want the opposite order because what comes first wins. Maybe we can also write a test? You can set socket_options: [:inet] for the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I will do that.

charset: Keyword.get(opts, :charset),
collation: Keyword.get(opts, :collation),
enable_cleartext_plugin: Keyword.get(opts, :enable_cleartext_plugin, false)
Expand Down Expand Up @@ -295,7 +298,7 @@ defmodule MyXQL.Client do
buffer? = Keyword.has_key?(socket_options, :buffer)
client = %__MODULE__{connection_id: nil, sock: nil}

case :gen_tcp.connect(address, port, socket_options, connect_timeout) do
case :gen_tcp.connect(address, port, @sock_opts ++ socket_options, connect_timeout) do
josevalim marked this conversation as resolved.
Show resolved Hide resolved
{:ok, sock} when buffer? ->
{:ok, %{client | sock: {:gen_tcp, sock}}}

Expand Down
8 changes: 6 additions & 2 deletions test/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ defmodule MyXQLTest do

test "#{@protocol}: query with multiple rows", c do
%MyXQL.Result{num_rows: 2} =
MyXQL.query!(c.conn, "INSERT INTO integers VALUES (10), (20)", [], query_type: @protocol)
MyXQL.query!(c.conn, "INSERT INTO integers VALUES (10), (20)", [],
query_type: @protocol
)
josevalim marked this conversation as resolved.
Show resolved Hide resolved

assert {:ok, %MyXQL.Result{columns: ["x"], rows: [[10], [20]]}} =
MyXQL.query(c.conn, "SELECT * FROM integers")
Expand All @@ -168,7 +170,9 @@ defmodule MyXQLTest do
values = Enum.map_join(1..num, ", ", &"(#{&1})")

result =
MyXQL.query!(c.conn, "INSERT INTO integers VALUES " <> values, [], query_type: @protocol)
MyXQL.query!(c.conn, "INSERT INTO integers VALUES " <> values, [],
query_type: @protocol
)
josevalim marked this conversation as resolved.
Show resolved Hide resolved

assert result.num_rows == num

Expand Down