Skip to content

Commit a5e5e15

Browse files
we could already support search path
1 parent 9f50c2b commit a5e5e15

File tree

1 file changed

+18
-66
lines changed

1 file changed

+18
-66
lines changed

lib/postgrex/protocol.ex

+18-66
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ defmodule Postgrex.Protocol do
8585
disconnect_on_error_codes = opts[:disconnect_on_error_codes] || []
8686
target_server_type = opts[:target_server_type] || :any
8787
disable_composite_types = opts[:disable_composite_types] || false
88+
parameters = opts[:parameters] || []
8889

8990
{ssl_opts, opts} =
9091
case Keyword.pop(opts, :ssl, false) do
@@ -116,6 +117,20 @@ defmodule Postgrex.Protocol do
116117
:unnamed -> :unnamed
117118
end
118119

120+
parameters =
121+
case opts[:search_path] do
122+
path when is_list(path) ->
123+
path = Enum.intersperse(path, ", ")
124+
Keyword.put(parameters, :search_path, path)
125+
126+
nil ->
127+
parameters
128+
129+
other ->
130+
raise ArgumentError,
131+
"expected :search_path to be a list of strings, got: #{inspect(other)}"
132+
end
133+
119134
s = %__MODULE__{
120135
timeout: timeout,
121136
ping_timeout: ping_timeout,
@@ -128,15 +143,14 @@ defmodule Postgrex.Protocol do
128143
connect_timeout = Keyword.get(opts, :connect_timeout, timeout)
129144

130145
status = %{
131-
opts: opts,
146+
opts: Keyword.put(opts, :parameters, parameters),
132147
types_mod: types_mod,
133148
types_key: nil,
134149
types_lock: nil,
135150
prepare: prepare,
136151
messages: [],
137152
ssl: ssl_opts,
138-
target_server_type: target_server_type,
139-
search_path: opts[:search_path]
153+
target_server_type: target_server_type
140154
}
141155

142156
connect_endpoints(endpoints, sock_opts ++ @sock_opts, connect_timeout, s, status, [])
@@ -916,7 +930,7 @@ defmodule Postgrex.Protocol do
916930
init_recv(%{s | connection_id: pid, connection_key: key}, status, buffer)
917931

918932
{:ok, msg_ready(), buffer} ->
919-
set_search_path(s, status, buffer)
933+
check_target_server_type(s, status, buffer)
920934

921935
{:ok, msg_error(fields: fields), buffer} ->
922936
disconnect(s, Postgrex.Error.exception(postgres: fields), buffer)
@@ -930,68 +944,6 @@ defmodule Postgrex.Protocol do
930944
end
931945
end
932946

933-
## set search path on connection startup
934-
935-
defp set_search_path(s, %{search_path: nil} = status, buffer),
936-
do: set_search_path_done(s, status, buffer)
937-
938-
defp set_search_path(s, %{search_path: search_path} = status, buffer)
939-
when is_list(search_path),
940-
do: set_search_path_send(s, status, buffer)
941-
942-
defp set_search_path(_, %{search_path: search_path}, _) do
943-
raise ArgumentError,
944-
"expected :search_path to be a list of strings, got: #{inspect(search_path)}"
945-
end
946-
947-
defp set_search_path_send(s, status, buffer) do
948-
search_path = Enum.intersperse(status.search_path, ",")
949-
msg = msg_query(statement: ["set search_path to " | search_path])
950-
951-
case msg_send(s, msg, buffer) do
952-
:ok ->
953-
set_search_path_recv(s, status, buffer)
954-
955-
{:disconnect, _, _} = dis ->
956-
dis
957-
end
958-
end
959-
960-
defp set_search_path_recv(s, status, buffer) do
961-
case msg_recv(s, :infinity, buffer) do
962-
{:ok, msg_row_desc(fields: fields), buffer} ->
963-
{[@text_type_oid], ["search_path"], _} = columns(fields)
964-
set_search_path_recv(s, status, buffer)
965-
966-
{:ok, msg_data_row(), buffer} ->
967-
set_search_path_recv(s, status, buffer)
968-
969-
{:ok, msg_command_complete(), buffer} ->
970-
set_search_path_recv(s, status, buffer)
971-
972-
{:ok, msg_ready(status: :idle), buffer} ->
973-
set_search_path_done(s, status, buffer)
974-
975-
{:ok, msg_ready(status: postgres), _buffer} ->
976-
err = %Postgrex.Error{message: "unexpected postgres status: #{postgres}"}
977-
{:disconnect, err, s}
978-
979-
{:ok, msg_error(fields: fields), buffer} ->
980-
err = Postgrex.Error.exception(postgres: fields)
981-
{:disconnect, err, %{s | buffer: buffer}}
982-
983-
{:ok, msg, buffer} ->
984-
s = handle_msg(s, status, msg)
985-
set_search_path_recv(s, status, buffer)
986-
987-
{:disconnect, _, _} = dis ->
988-
dis
989-
end
990-
end
991-
992-
defp set_search_path_done(s, status, buffer),
993-
do: check_target_server_type(s, status, buffer)
994-
995947
## check_target_server_type
996948

997949
defp check_target_server_type(s, %{target_server_type: :any} = status, buffer),

0 commit comments

Comments
 (0)