Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyXQL: Add back subquery parens inside of function calls #643

Merged
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
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
Loading