我正在尝试Elixir Ecto,现在我需要在代码中实现COPY FROM STDIN。我在github上的postgrex.ex中找到了示例:
Postgrex.transaction(pid, fn(conn) ->
query = Postgrex.prepare!(conn, "", "COPY posts FROM STDIN",[copy_data:true])
stream = Postgrex.stream(conn, query, [])
Enum.into(File.stream!("posts"), stream)
end)
Run Code Online (Sandbox Code Playgroud)
我如何才能将其转换为我的需求。我必须通过什么pid?
Repo.transaction fn ->
query = "some query"
Ecto.Adapters.SQL.query!(Repo, query, [])
#copy from stdin, how??
end
Run Code Online (Sandbox Code Playgroud)
可以使用Ecto.Adapters.SQL.query!/ 4运行来自STDIN的COPY,但不能使用collectable / stream:
Run Code Online (Sandbox Code Playgroud)Ecto.Adapters.SQL.query!(Repo, "COPY posts FROM STDIN", [data_as_(final)_parameter], [copy_data: true])
..和:
从Ecto 2.1开始,以上不再有效。相反,必须使用内置流:
Run Code Online (Sandbox Code Playgroud)stream = Ecto.Adapters.SQL.stream(TestRepo, "COPY posts FROM STDIN") TestRepo.transaction(fn -> Enum.into(data_enum, stream) end)