Skip to content

Commit

Permalink
Implement Table.Reader for Tds.Result (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcconnell authored Sep 2, 2023
1 parent 61ec77f commit 1b249ea
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/tds/result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ defmodule Tds.Result do
}

defstruct columns: nil, rows: nil, num_rows: 0

if Code.ensure_loaded?(Table.Reader) do
defimpl Table.Reader, for: Tds.Result do
def init(%{columns: columns}) when columns in [nil, []] do
{:rows, %{columns: [], count: 0}, []}
end

def init(result) do
{:rows, %{columns: result.columns, count: result.num_rows}, result.rows}
end
end
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule Tds.Mixfile do
{:db_connection, "~> 2.0"},
{:ex_doc, "~> 0.19", only: :docs},
{:excoding, "~> 0.1", optional: true, only: :test},
{:tzdata, "~> 1.0", optional: true, only: :test}
{:tzdata, "~> 1.0", optional: true, only: :test},
{:table, "~> 0.1.0", optional: true}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"rustler_precompiled": {:hex, :rustler_precompiled, "0.5.2", "7619fff0309a012eac7441993da4f6e257022bd456449a366756696a9a18fb19", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, "~> 0.23", [hex: :rustler, repo: "hexpm", optional: true]}], "hexpm", "4e3716fd7cf6fbb806a9ed2b1449c987cfe578b24e3deb3ca4b8645638cc644c"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"table": {:hex, :table, "0.1.2", "87ad1125f5b70c5dea0307aa633194083eb5182ec537efc94e96af08937e14a8", [:mix], [], "hexpm", "7e99bc7efef806315c7e65640724bf165c3061cdc5d854060f74468367065029"},
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
"tzdata": {:hex, :tzdata, "1.1.1", "20c8043476dfda8504952d00adac41c6eda23912278add38edc140ae0c5bcc46", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a69cec8352eafcd2e198dea28a34113b60fdc6cb57eb5ad65c10292a6ba89787"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
Expand Down
24 changes: 24 additions & 0 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,28 @@ defmodule QueryTest do
} = query("SELECT 1 WHERE 1 = @1", params, opts)
end
end

test "table reader integration", context do
assert {:ok, result} =
Tds.query(
context[:pid],
"SELECT * FROM (VALUES (1, 'a'), (2, 'b'), (3, 'c')) AS tab (x, y)",
[]
)

assert [
%{"x" => 1, "y" => "a"},
%{"x" => 2, "y" => "b"},
%{"x" => 3, "y" => "c"}
] ==
result
|> Table.to_rows()
|> Enum.to_list()

columns = Table.to_columns(result)
assert Enum.to_list(columns["x"]) == [1, 2, 3]
assert Enum.to_list(columns["y"]) == ["a", "b", "c"]

assert {_, %{count: 3}, _} = Table.Reader.init(result)
end
end

0 comments on commit 1b249ea

Please sign in to comment.