Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/kino_db/connection_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@ defmodule KinoDB.ConnectionCell do

defp missing_dep(%{"type" => "clickhouse"}) do
missing_many_deps([
{ReqCH, ~s|{:req_ch, "~> 0.1"}|},
{Explorer, ~s|{:explorer, "~> 0.10"}|}
{ReqCH, ~s|{:req_ch, "~> 0.2"}|},
{Adbc, ~s|{:adbc, "~> 0.12"}|}
])
end

Expand Down
17 changes: 3 additions & 14 deletions lib/kino_db/sql_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ defmodule KinoDB.SQLCell do
result_variable: Kino.SmartCell.prefixed_var_name("result", attrs["result_variable"]),
query: query,
timeout: attrs["timeout"],
cache_query: Map.get(attrs, "cache_query", true),
missing_dep: missing_dep(connection)
)

Expand All @@ -37,7 +36,6 @@ defmodule KinoDB.SQLCell do
connection: ctx.assigns.connection,
result_variable: ctx.assigns.result_variable,
timeout: ctx.assigns.timeout,
cache_query: ctx.assigns.cache_query,
missing_dep: ctx.assigns.missing_dep
}

Expand Down Expand Up @@ -93,12 +91,6 @@ defmodule KinoDB.SQLCell do
{:noreply, ctx}
end

def handle_event("update_cache_query", cache_query?, ctx) do
ctx = assign(ctx, cache_query: cache_query?)
broadcast_event(ctx, "update_cache_query", cache_query?)
{:noreply, ctx}
end

@impl true
def scan_binding(pid, binding, _env) do
connections =
Expand Down Expand Up @@ -185,8 +177,7 @@ defmodule KinoDB.SQLCell do
end,
"result_variable" => ctx.assigns.result_variable,
"query" => ctx.assigns.query,
"timeout" => ctx.assigns.timeout,
"cache_query" => ctx.assigns.cache_query
"timeout" => ctx.assigns.timeout
}
end

Expand All @@ -212,7 +203,7 @@ defmodule KinoDB.SQLCell do
to_quoted(attrs, quote(do: Tds), fn n -> "@#{n}" end)
end

# Explorer-based
# ADBC-based
defp to_quoted(%{"connection" => %{"type" => "snowflake"}} = attrs) do
to_quoted_adbc(attrs, fn n -> "?#{n}" end)
end
Expand Down Expand Up @@ -285,8 +276,6 @@ defmodule KinoDB.SQLCell do
unquote(params),
unquote_splicing(opts_args)
).body

Kino.DataTable.new(unquote(var))
end
end

Expand All @@ -308,7 +297,7 @@ defmodule KinoDB.SQLCell do
do: [[timeout: timeout * 1000]]

defp query_opts_args(%{"connection" => %{"type" => "clickhouse"}}),
do: [[format: :explorer]]
do: [[format: :adbc]]

defp query_opts_args(_attrs), do: []

Expand Down
89 changes: 15 additions & 74 deletions test/kino_db/sql_cell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ defmodule KinoDB.SQLCellTest do
"connection" => %{"variable" => "db", "type" => "postgres"},
"result_variable" => "ids_result",
"timeout" => nil,
"query" => "SELECT id FROM users",
"data_frame_alias" => Explorer.DataFrame
"query" => "SELECT id FROM users"
}

{_kino, source} = start_smart_cell!(SQLCell, attrs)
Expand Down Expand Up @@ -93,10 +92,8 @@ defmodule KinoDB.SQLCellTest do
attrs = %{
"connection" => %{"variable" => "conn", "type" => "postgres"},
"result_variable" => "result",
"cache_query" => true,
"timeout" => nil,
"query" => "SELECT id FROM users",
"data_frame_alias" => Explorer.DataFrame
"query" => "SELECT id FROM users"
}

assert SQLCell.to_source(attrs) == """
Expand All @@ -120,8 +117,7 @@ defmodule KinoDB.SQLCellTest do
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == """
result = ReqCH.query!(conn, ~S"SELECT id FROM users", [], format: :explorer).body
Kino.DataTable.new(result)\
result = ReqCH.query!(conn, ~S"SELECT id FROM users", [], format: :adbc).body\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == """
Expand All @@ -133,10 +129,8 @@ defmodule KinoDB.SQLCellTest do
attrs = %{
"connection" => %{"variable" => "conn", "type" => "postgres"},
"result_variable" => "result",
"cache_query" => true,
"timeout" => nil,
"query" => "SELECT id FROM users\nWHERE last_name = 'Sherlock'",
"data_frame_alias" => Explorer.DataFrame
"query" => "SELECT id FROM users\nWHERE last_name = 'Sherlock'"
}

assert SQLCell.to_source(attrs) == ~s'''
Expand Down Expand Up @@ -208,10 +202,8 @@ defmodule KinoDB.SQLCellTest do
WHERE last_name = 'Sherlock'
""",
[],
format: :explorer
).body

Kino.DataTable.new(result)\
format: :adbc
).body\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == ~s'''
Expand All @@ -231,10 +223,8 @@ defmodule KinoDB.SQLCellTest do
attrs = %{
"connection" => %{"variable" => "conn", "type" => "postgres"},
"result_variable" => "result",
"cache_query" => true,
"timeout" => nil,
"query" => ~s/SELECT id FROM users WHERE id {{user_id}} AND name LIKE {{search <> "%"}}/,
"data_frame_alias" => Explorer.DataFrame
"query" => ~s/SELECT id FROM users WHERE id {{user_id}} AND name LIKE {{search <> "%"}}/
}

assert SQLCell.to_source(attrs) == ~s'''
Expand Down Expand Up @@ -283,10 +273,8 @@ defmodule KinoDB.SQLCellTest do
conn,
~S"SELECT id FROM users WHERE id {user_id:String} AND name LIKE {param_2:String}",
[{"user_id", user_id}, {"param_2", search <> \"%\"}],
format: :explorer
).body

Kino.DataTable.new(result)\
format: :adbc
).body\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == ~s'''
Expand All @@ -302,14 +290,12 @@ defmodule KinoDB.SQLCellTest do
attrs = %{
"connection" => %{"variable" => "conn", "type" => "postgres"},
"result_variable" => "result",
"cache_query" => true,
"timeout" => nil,
"query" => """
SELECT id from users
-- WHERE id = {{user_id1}}
/* WHERE id = {{user_id2}} */ WHERE id = {{user_id3}}\
""",
"data_frame_alias" => Explorer.DataFrame
"""
}

assert SQLCell.to_source(attrs) == ~s'''
Expand Down Expand Up @@ -387,10 +373,8 @@ defmodule KinoDB.SQLCellTest do
/* WHERE id = {{user_id2}} */ WHERE id = {user_id3:String}
""",
[{"user_id3", user_id3}],
format: :explorer
).body

Kino.DataTable.new(result)\
format: :adbc
).body\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == ~s'''
Expand All @@ -411,10 +395,8 @@ defmodule KinoDB.SQLCellTest do
attrs = %{
"connection" => %{"variable" => "conn", "type" => "postgres"},
"result_variable" => "result",
"cache_query" => true,
"timeout" => 30,
"query" => "SELECT id FROM users",
"data_frame_alias" => Explorer.DataFrame
"query" => "SELECT id FROM users"
}

assert SQLCell.to_source(attrs) == """
Expand All @@ -438,61 +420,20 @@ defmodule KinoDB.SQLCellTest do
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == """
result = ReqCH.query!(conn, ~S"SELECT id FROM users", [], format: :explorer).body
Kino.DataTable.new(result)\
result = ReqCH.query!(conn, ~S"SELECT id FROM users", [], format: :adbc).body\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == """
result = Tds.query!(conn, ~S"SELECT id FROM users", [], timeout: 30000)\
"""
end

test "passes cache_query option when supported" do
attrs = %{
"connection" => %{"variable" => "conn", "type" => "postgres"},
"result_variable" => "result",
"cache_query" => true,
"query" => "SELECT id FROM users"
}

assert SQLCell.to_source(attrs) == """
result = Postgrex.query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "mysql")) == """
result = MyXQL.query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlite")) == """
result = Exqlite.query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "snowflake")) == """
result = Adbc.Connection.query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == """
result = ReqCH.query!(conn, ~S"SELECT id FROM users", [], format: :explorer).body
Kino.DataTable.new(result)\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "bigquery")) == """
result = Adbc.Connection.query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == """
result = Tds.query!(conn, ~S"SELECT id FROM users", [])\
"""
end

test "escapes interpolation" do
attrs = %{
"connection" => %{"variable" => "conn", "type" => "postgres"},
"result_variable" => "result",
"cache_query" => true,
"timeout" => nil,
"query" => "SELECT id FROM users WHERE last_name = '\#{user_id}'",
"data_frame_alias" => Explorer.DataFrame
"query" => "SELECT id FROM users WHERE last_name = '\#{user_id}'"
}

assert SQLCell.to_source(attrs) == """
Expand Down
Loading