Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion include/emysql.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@
-define(CONNECT_WITH_DB, 8).
-define(CONN_TEST_PERIOD, 28000).
-define(TCP_RECV_BUFFER, 8192).

-define(SOCKET_OPTIONS, [binary,
{packet, raw},
{active, false},
{recbuf, ?TCP_RECV_BUFFER}]).

%% MYSQL TYPES
-define(FIELD_TYPE_DECIMAL, 16#00).
Expand Down
18 changes: 17 additions & 1 deletion src/emysql_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ open_connection(#pool{pool_id=PoolId, host=Host, port=Port, user=User,
start_cmds=StartCmds, connect_timeout=ConnectTimeout} = Pool) ->
%-% io:format("~p open connection for pool ~p host ~p port ~p user ~p base ~p~n", [self(), PoolId, Host, Port, User, Database]),
%-% io:format("~p open connection: ... connect ... ~n", [self()]),
case gen_tcp:connect(Host, Port, [binary, {packet, raw}, {active, false}, {recbuf, ?TCP_RECV_BUFFER}], ConnectTimeout) of
IpFamily = host_ipfamily(Host),
case gen_tcp:connect(Host, Port, [IpFamily | ?SOCKET_OPTIONS], ConnectTimeout) of
{ok, Sock} ->
#greeting {
server_version = Version,
Expand Down Expand Up @@ -220,6 +221,21 @@ open_connection(#pool{pool_id=PoolId, host=Host, port=Port, user=User,
exit({failed_to_connect_to_database, Reason})
end.

host_ipfamily(Host) ->
case inet_parse:address(Host) of
{ok, {_, _, _, _, _, _, _, _}} -> inet6;
{ok, {_, _, _, _}} -> inet;
_ ->
case inet:getaddr(Host, inet6) of
{ok, _} -> inet6;
_ ->
case inet:getaddr(Host, inet) of
{ok, _} -> inet;
{error, Reason} -> exit(Reason)
end
end
end.

handshake(Sock, User, Password) ->
case emysql_auth:handshake(Sock, User, Password) of
{ok, #greeting{} = G} -> G;
Expand Down