@@ -85,6 +85,7 @@ defmodule Postgrex.Protocol do
85
85
disconnect_on_error_codes = opts [ :disconnect_on_error_codes ] || [ ]
86
86
target_server_type = opts [ :target_server_type ] || :any
87
87
disable_composite_types = opts [ :disable_composite_types ] || false
88
+ parameters = opts [ :parameters ] || [ ]
88
89
89
90
{ ssl_opts , opts } =
90
91
case Keyword . pop ( opts , :ssl , false ) do
@@ -116,6 +117,20 @@ defmodule Postgrex.Protocol do
116
117
:unnamed -> :unnamed
117
118
end
118
119
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
+
119
134
s = % __MODULE__ {
120
135
timeout: timeout ,
121
136
ping_timeout: ping_timeout ,
@@ -128,15 +143,14 @@ defmodule Postgrex.Protocol do
128
143
connect_timeout = Keyword . get ( opts , :connect_timeout , timeout )
129
144
130
145
status = % {
131
- opts: opts ,
146
+ opts: Keyword . put ( opts , :parameters , parameters ) ,
132
147
types_mod: types_mod ,
133
148
types_key: nil ,
134
149
types_lock: nil ,
135
150
prepare: prepare ,
136
151
messages: [ ] ,
137
152
ssl: ssl_opts ,
138
- target_server_type: target_server_type ,
139
- search_path: opts [ :search_path ]
153
+ target_server_type: target_server_type
140
154
}
141
155
142
156
connect_endpoints ( endpoints , sock_opts ++ @ sock_opts , connect_timeout , s , status , [ ] )
@@ -916,7 +930,7 @@ defmodule Postgrex.Protocol do
916
930
init_recv ( % { s | connection_id: pid , connection_key: key } , status , buffer )
917
931
918
932
{ :ok , msg_ready ( ) , buffer } ->
919
- set_search_path ( s , status , buffer )
933
+ check_target_server_type ( s , status , buffer )
920
934
921
935
{ :ok , msg_error ( fields: fields ) , buffer } ->
922
936
disconnect ( s , Postgrex.Error . exception ( postgres: fields ) , buffer )
@@ -930,68 +944,6 @@ defmodule Postgrex.Protocol do
930
944
end
931
945
end
932
946
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
-
995
947
## check_target_server_type
996
948
997
949
defp check_target_server_type ( s , % { target_server_type: :any } = status , buffer ) ,
0 commit comments