Skip to content

Commit

Permalink
MyXQL: Add back subquery parens inside of function calls (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski authored Oct 18, 2024
1 parent 1f631fc commit 6e7a49e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ if Code.ensure_loaded?(MyXQL) do
fun,
?(,
modifier,
Enum.map_intersperse(args, ", ", &top_level_expr(&1, sources, query)),
Enum.map_intersperse(args, ", ", &expr(&1, sources, query)),
?)
]
end
Expand Down
14 changes: 11 additions & 3 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ defmodule Ecto.Adapters.MyXQLTest do
assert all(query) == ~s{SELECT coalesce(s0.`x`, 5) FROM `schema` AS s0}
end

test "coalesce with subquery" do
squery = from s in Schema, select: s.x
query = Schema |> select([s], coalesce(subquery(squery), 5)) |> plan()

assert all(query) ==
~s{SELECT coalesce((SELECT ss0.`x` AS `x` FROM `schema` AS ss0), 5) FROM `schema` AS s0}
end

test "where" do
query = Schema |> where([r], r.x == 42) |> where([r], r.y != 43) |> select([r], r.x) |> plan()

Expand Down Expand Up @@ -478,7 +486,7 @@ defmodule Ecto.Adapters.MyXQLTest do
|> plan()

assert all(query) ==
~s{SELECT s0.`x` FROM `schema` AS s0 ORDER BY exists(SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`))}
~s{SELECT s0.`x` FROM `schema` AS s0 ORDER BY exists((SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`)))}
end

test "union and union all" do
Expand Down Expand Up @@ -882,7 +890,7 @@ defmodule Ecto.Adapters.MyXQLTest do
|> plan()

assert all(query) ==
~s{SELECT s0.`x` FROM `schema` AS s0 GROUP BY exists(SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`))}
~s{SELECT s0.`x` FROM `schema` AS s0 GROUP BY exists((SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`)))}
end

test "interpolated values" do
Expand Down Expand Up @@ -1089,7 +1097,7 @@ defmodule Ecto.Adapters.MyXQLTest do
|> plan

assert all(query) ==
~s{SELECT s0.`x` FROM `schema` AS s0 WINDOW `w` AS (ORDER BY exists(SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`)))}
~s{SELECT s0.`x` FROM `schema` AS s0 WINDOW `w` AS (ORDER BY exists((SELECT ss0.`x` AS `result` FROM `schema` AS ss0 WHERE (ss0.`x` = s0.`x`))))}
end

test "two windows" do
Expand Down
8 changes: 8 additions & 0 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,14 @@ defmodule Ecto.Adapters.PostgresTest do
assert all(query) == ~s{SELECT coalesce(s0."x", 5) FROM "schema" AS s0}
end

test "coalesce with subquery" do
squery = from s in Schema, select: s.x
query = Schema |> select([s], coalesce(subquery(squery), 5)) |> plan()

assert all(query) ==
~s{SELECT coalesce((SELECT ss0."x" AS "x" FROM "schema" AS ss0), 5) FROM "schema" AS s0}
end

test "where" do
query = Schema |> where([r], r.x == 42) |> where([r], r.y != 43) |> select([r], r.x) |> plan()

Expand Down

0 comments on commit 6e7a49e

Please sign in to comment.