-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37317dc
commit e5c5b34
Showing
6 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Ch.Stream do | ||
@moduledoc """ | ||
Stream struct returned from stream commands. | ||
All of its fields are private. | ||
""" | ||
|
||
@derive {Inspect, only: []} | ||
defstruct [:conn, :query, :params, :opts] | ||
|
||
@type t :: %__MODULE__{ | ||
conn: DBConnection.conn(), | ||
query: Ch.Query.t(), | ||
params: Ch.params(), | ||
opts: [Ch.query_option()] | ||
} | ||
|
||
defimpl Enumerable do | ||
def reduce(stream, acc, fun) do | ||
%Ch.Stream{conn: conn, query: query, params: params, opts: opts} = stream | ||
stream = %DBConnection.Stream{conn: conn, query: query, params: params, opts: opts} | ||
DBConnection.reduce(stream, acc, fun) | ||
end | ||
|
||
def member?(_, _), do: {:error, __MODULE__} | ||
def count(_), do: {:error, __MODULE__} | ||
def slice(_), do: {:error, __MODULE__} | ||
end | ||
|
||
defimpl Collectable do | ||
def into(stream) do | ||
%Ch.Stream{conn: conn, query: query, params: params, opts: opts} = stream | ||
DBConnection.execute!(conn, query, params, opts) | ||
{stream, &collect/2} | ||
end | ||
|
||
defp collect(%{conn: conn, query: query} = stream, {:cont, data}) do | ||
DBConnection.execute!(conn, %{query | statement: data}, []) | ||
stream | ||
end | ||
|
||
defp collect(conn, :done), do: HTTP.stream_request_body(conn, ref(conn), :eof) | ||
Check failure on line 42 in lib/ch/stream.ex
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule Ch.StreamTest do | ||
use ExUnit.Case | ||
|
||
setup do | ||
pool = start_supervised!({Ch, database: Ch.Test.database()}) | ||
{:ok, pool: pool} | ||
end | ||
|
||
describe "enumerable" do | ||
test "works", %{pool: pool} do | ||
result = | ||
DBConnection.run(pool, fn conn -> | ||
conn | ||
|> Ch.stream("select * from system.numbers limit {limit:UInt32}", %{"limit" => 100}) | ||
|> Enum.into([]) | ||
end) | ||
|
||
assert [ | ||
%Ch.Result{ | ||
command: :select, | ||
data: [ | ||
<<1, 6, 110, 117, 109, 98, 101, 114, 6, 85, 73, 110, 116, 54, 52>>, | ||
<<_::6400>> | ||
] | ||
}, | ||
%Ch.Result{command: :select, data: []} | ||
] = result | ||
end | ||
end | ||
|
||
describe "collectable" do | ||
test "works", %{pool: pool} do | ||
Ch.query!(pool, "create table collectable_test(a UInt64, b String) engine Null") | ||
|
||
rows = | ||
Stream.repeatedly(fn -> [0, "0"] end) | ||
|> Stream.chunk_every(10000) | ||
|> Stream.map(fn chunk -> Ch.RowBinary.encode_rows(chunk, ["UInt64", "String"]) end) | ||
|> Stream.take(3) | ||
|
||
result = | ||
DBConnection.run(pool, fn conn -> | ||
Enum.into( | ||
rows, | ||
Ch.stream(conn, "insert into collectable_test(a, b) format RowBinary\n") | ||
) | ||
end) | ||
|
||
assert result == :asdlkfhajsdf | ||
end | ||
end | ||
end |