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
3 changes: 3 additions & 0 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,9 @@ if Code.ensure_loaded?(MyXQL) do
defp op_to_binary({:not, _, [_]} = expr, sources, query),
do: paren_expr(expr, sources, query)

defp op_to_binary({:in, _, [_, _]} = expr, sources, query),
do: paren_expr(expr, sources, query)

defp op_to_binary(expr, sources, query),
do: expr(expr, sources, query)

Expand Down
3 changes: 3 additions & 0 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,9 @@ if Code.ensure_loaded?(Postgrex) do
defp maybe_paren({:not, _, [_]} = expr, sources, query),
do: paren_expr(expr, sources, query)

defp maybe_paren({:in, _, [_, _]} = expr, sources, query),
do: paren_expr(expr, sources, query)

defp maybe_paren(expr, sources, query),
do: expr(expr, sources, query)

Expand Down
8 changes: 7 additions & 1 deletion test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,13 @@ defmodule Ecto.Adapters.MyXQLTest do
query = Schema |> select([e], e.x == ^0 or e.x in ^[1, 2, 3] or e.x == ^4) |> plan()

assert all(query) ==
~s{SELECT ((s0.`x` = ?) OR s0.`x` IN (?,?,?)) OR (s0.`x` = ?) FROM `schema` AS s0}
~s{SELECT ((s0.`x` = ?) OR (s0.`x` IN (?,?,?))) OR (s0.`x` = ?) FROM `schema` AS s0}

query = "schema" |> select([e], e.x in [1, 2] in [true]) |> plan()
assert all(query) == ~s{SELECT (s0.`x` IN (1,2)) IN (TRUE) FROM `schema` AS s0}

query = "schema" |> select([e], e.x in ^[1, 2] in [true]) |> plan()
assert all(query) == ~s{SELECT (s0.`x` IN (?,?)) IN (TRUE) FROM `schema` AS s0}
end

test "in subquery" do
Expand Down
11 changes: 10 additions & 1 deletion test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ defmodule Ecto.Adapters.PostgresTest do
query = Schema |> select([e], e.x == ^0 or e.x in ^[1, 2, 3] or e.x == ^4) |> plan()

assert all(query) ==
~s{SELECT ((s0."x" = $1) OR s0."x" = ANY($2)) OR (s0."x" = $3) FROM "schema" AS s0}
~s{SELECT ((s0."x" = $1) OR (s0."x" = ANY($2))) OR (s0."x" = $3) FROM "schema" AS s0}

query = "schema" |> select([e], (not e.x) in [true, false]) |> plan()
assert all(query) == ~s{SELECT (NOT (s0."x")) IN (TRUE,FALSE) FROM "schema" AS s0}
Expand All @@ -1133,6 +1133,15 @@ defmodule Ecto.Adapters.PostgresTest do

query = "schema" |> select([e], (not e.x) in e.w) |> plan()
assert all(query) == ~s{SELECT (NOT (s0."x")) = ANY(s0."w") FROM "schema" AS s0}

query = "schema" |> select([e], e.x == e.y in ^[1, 2]) |> plan()
assert all(query) == ~s{SELECT s0."x" = (s0."y" = ANY($1)) FROM "schema" AS s0}

query = "schema" |> select([e], e.x == e.y in e.w) |> plan()
assert all(query) == ~s{SELECT s0."x" = (s0."y" = ANY(s0."w")) FROM "schema" AS s0}

query = "schema" |> select([e], e.x in [1, 2] in [true]) |> plan()
assert all(query) == ~s{SELECT (s0."x" IN (1,2)) IN (TRUE) FROM "schema" AS s0}
end

test "in subquery" do
Expand Down
Loading