From f3f460cdb7287a5a78bcac47943fa6196f9fdc4d Mon Sep 17 00:00:00 2001 From: Mykhailo Vstavskyi Date: Sun, 2 Jul 2023 11:16:01 +0300 Subject: [PATCH 1/5] Update jamdb_oracle_test.exs --- test/jamdb_oracle_test.exs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/jamdb_oracle_test.exs b/test/jamdb_oracle_test.exs index 76843d3..c7008af 100644 --- a/test/jamdb_oracle_test.exs +++ b/test/jamdb_oracle_test.exs @@ -523,6 +523,18 @@ defmodule Jamdb.OracleTest do assert all(query) == ~s{SELECT HEXTORAW(:1) FROM schema s0} end + test "in expression" do + query = "comments" |> where([c], c.post_id in ^[1, 2, 3]) |> select([c], c.x) |> plan() + assert all(query) == + ~s{SELECT c0.x FROM comments c0 } <> + ~s{WHERE (c0.post_id IN (:1,:2,:3))} + + query = "comments" |> where([c], c.post_id in [^1, ^2, ^3]) |> select([c], c.x) |> plan() + assert all(query) == + ~s{SELECT c0.x FROM comments c0 } <> + ~s{WHERE (c0.post_id IN (:1,:2,:3))} + end + test "in subquery" do posts = subquery("posts" |> where(title: ^"hello") |> select([p], p.id)) query = "comments" |> where([c], c.post_id in subquery(posts)) |> select([c], c.x) |> plan() From d591ddaf70e63f3d6bd60caa5133c83382e68683 Mon Sep 17 00:00:00 2001 From: Mykhailo Vstavskyi Date: Sun, 2 Jul 2023 11:16:22 +0300 Subject: [PATCH 2/5] Update mix.exs --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 38cdba2..3edbeb6 100644 --- a/mix.exs +++ b/mix.exs @@ -3,7 +3,7 @@ defmodule Jamdb.Oracle.Mixfile do def project do [app: :jamdb_oracle, - version: "0.5.4", + version: "0.5.5", elixir: "~> 1.10", description: description(), package: package(), From 8ef36e8ee6e7ff3551da18211aeec59bfa92f2ee Mon Sep 17 00:00:00 2001 From: Mykhailo Vstavskyi Date: Sun, 2 Jul 2023 11:16:37 +0300 Subject: [PATCH 3/5] Update jamdb_oracle.ex --- lib/jamdb_oracle.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jamdb_oracle.ex b/lib/jamdb_oracle.ex index 7441a3f..ba40400 100644 --- a/lib/jamdb_oracle.ex +++ b/lib/jamdb_oracle.ex @@ -1,5 +1,5 @@ defmodule Jamdb.Oracle do - @vsn "0.5.4" + @vsn "0.5.5" @moduledoc """ Adapter module for Oracle. `DBConnection` behaviour implementation. From b87ca9803057b3778704b33ef48bfbd8b352962b Mon Sep 17 00:00:00 2001 From: Mykhailo Vstavskyi Date: Sun, 2 Jul 2023 11:21:27 +0300 Subject: [PATCH 4/5] Update jamdb_oracle_test.exs --- test/jamdb_oracle_test.exs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/jamdb_oracle_test.exs b/test/jamdb_oracle_test.exs index c7008af..070098a 100644 --- a/test/jamdb_oracle_test.exs +++ b/test/jamdb_oracle_test.exs @@ -524,6 +524,11 @@ defmodule Jamdb.OracleTest do end test "in expression" do + query = "comments" |> where([c], c.post_id in [1, 2, 3]) |> select([c], c.x) |> plan() + assert all(query) == + ~s{SELECT c0.x FROM comments c0 } <> + ~s{WHERE (c0.post_id IN (1,2,3))} + query = "comments" |> where([c], c.post_id in ^[1, 2, 3]) |> select([c], c.x) |> plan() assert all(query) == ~s{SELECT c0.x FROM comments c0 } <> From eed09c7f93a45b5698c0c5bd73e3a60bc6f2f0d0 Mon Sep 17 00:00:00 2001 From: Mykhailo Vstavskyi Date: Sun, 2 Jul 2023 11:23:43 +0300 Subject: [PATCH 5/5] fix in operator --- lib/jamdb_oracle_query.ex | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/jamdb_oracle_query.ex b/lib/jamdb_oracle_query.ex index bd68681..c6e3c6b 100644 --- a/lib/jamdb_oracle_query.ex +++ b/lib/jamdb_oracle_query.ex @@ -369,8 +369,8 @@ defmodule Jamdb.Oracle.Query do [?(, expr(expr, sources, query), ?)] end - defp expr({:^, [], [ix]}, _sources, _query) do - [?: | Integer.to_string(ix + 1)] + defp expr({:^, [], [idx]}, _sources, _query) do + ":#{idx + 1}" end defp expr({{:., _, [{:parent_as, _, [as]}, field]}, _, []}, _sources, query) @@ -393,17 +393,17 @@ defmodule Jamdb.Oracle.Query do end defp expr({:in, _, [left, right]}, sources, query) when is_list(right) do - args = - intersperse_map(right, ?,, fn - elem when is_list(elem) -> [?(, intersperse_map(elem, ?,, &expr(&1, sources, query)), ?)] - elem -> expr(elem, sources, query) - end) + args = Enum.map_join(right, ",", &expr(&1, sources, query)) [expr(left, sources, query), " IN (", args, ?)] end - defp expr({:in, _, [left, {:^, _, [_, length]}]}, sources, query) do - right = for ix <- 1..length, do: {:^, [], [ix]} - expr({:in, [], [left, right]}, sources, query) + defp expr({:in, _, [_, {:^, _, [_, 0]}]}, _sources, _query) do + "0" + end + + defp expr({:in, _, [left, {:^, _, [idx, length]}]}, sources, query) do + args = Enum.map_join(1..length, ",", &":#{idx + &1}") + [expr(left, sources, query), " IN (", args, ?)] end defp expr({:in, _, [left, %Ecto.SubQuery{} = subquery]}, sources, query) do @@ -495,8 +495,8 @@ defmodule Jamdb.Oracle.Query do end end - defp expr(%Ecto.Query.Tagged{value: {:^, [], [ix]}, type: :binary}, _sources, _query) do - [?: | Integer.to_string(ix + 1)] + defp expr(%Ecto.Query.Tagged{value: {:^, [], [idx]}, type: :binary}, _sources, _query) do + ":#{idx + 1}" end defp expr(%Ecto.Query.Tagged{value: binary, type: :binary}, _sources, _query) do ["'", Base.encode16(binary, case: :upper), "'"]