gpt4 book ai didi

elixir - 如何在 Elixir Repo.Transaction 中添加 COPY FROM STDIN

转载 作者:行者123 更新时间:2023-12-01 09:50:08 26 4
gpt4 key购买 nike

我正在试验 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)

如何根据需要转换它。我必须通过什么 pid?

Repo.transaction fn ->
query = "some query"
Ecto.Adapters.SQL.query!(Repo, query, [])
#copy from stdin, how??
end

最佳答案

根据 this postJames Fish在 Elixir 论坛上,应该可以:

It is possible to run a COPY FROM STDIN using Ecto.Adapters.SQL.query!/4 but can't use a collectable/stream:

Ecto.Adapters.SQL.query!(Repo, "COPY posts FROM STDIN",
[data_as_(final)_parameter], [copy_data: true])

..和:

From Ecto 2.1 the above no longer works. Instead must use built in streams:

stream = Ecto.Adapters.SQL.stream(TestRepo, "COPY posts FROM STDIN")
TestRepo.transaction(fn -> Enum.into(data_enum, stream) end)

关于elixir - 如何在 Elixir Repo.Transaction 中添加 COPY FROM STDIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39142851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com