Skip to content

Commit

Permalink
Merge pull request #141 from erlangbureau/stage
Browse files Browse the repository at this point in the history
fix in operator
  • Loading branch information
vstavskyi authored Jul 2, 2023
2 parents 0949f39 + eed09c7 commit 421129e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/jamdb_oracle.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Jamdb.Oracle do
@vsn "0.5.4"
@vsn "0.5.5"
@moduledoc """
Adapter module for Oracle. `DBConnection` behaviour implementation.
Expand Down
24 changes: 12 additions & 12 deletions lib/jamdb_oracle_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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), "'"]
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
17 changes: 17 additions & 0 deletions test/jamdb_oracle_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,23 @@ 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))}

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()
Expand Down

0 comments on commit 421129e

Please sign in to comment.