Skip to content

Commit 6e7a49e

Browse files
MyXQL: Add back subquery parens inside of function calls (#643)
1 parent 1f631fc commit 6e7a49e

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lib/ecto/adapters/myxql/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ if Code.ensure_loaded?(MyXQL) do
826826
fun,
827827
?(,
828828
modifier,
829-
Enum.map_intersperse(args, ", ", &top_level_expr(&1, sources, query)),
829+
Enum.map_intersperse(args, ", ", &expr(&1, sources, query)),
830830
?)
831831
]
832832
end

test/ecto/adapters/myxql_test.exs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,14 @@ defmodule Ecto.Adapters.MyXQLTest do
416416
assert all(query) == ~s{SELECT coalesce(s0.`x`, 5) FROM `schema` AS s0}
417417
end
418418

419+
test "coalesce with subquery" do
420+
squery = from s in Schema, select: s.x
421+
query = Schema |> select([s], coalesce(subquery(squery), 5)) |> plan()
422+
423+
assert all(query) ==
424+
~s{SELECT coalesce((SELECT ss0.`x` AS `x` FROM `schema` AS ss0), 5) FROM `schema` AS s0}
425+
end
426+
419427
test "where" do
420428
query = Schema |> where([r], r.x == 42) |> where([r], r.y != 43) |> select([r], r.x) |> plan()
421429

@@ -478,7 +486,7 @@ defmodule Ecto.Adapters.MyXQLTest do
478486
|> plan()
479487

480488
assert all(query) ==
481-
~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`))}
489+
~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`)))}
482490
end
483491

484492
test "union and union all" do
@@ -882,7 +890,7 @@ defmodule Ecto.Adapters.MyXQLTest do
882890
|> plan()
883891

884892
assert all(query) ==
885-
~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`))}
893+
~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`)))}
886894
end
887895

888896
test "interpolated values" do
@@ -1089,7 +1097,7 @@ defmodule Ecto.Adapters.MyXQLTest do
10891097
|> plan
10901098

10911099
assert all(query) ==
1092-
~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`)))}
1100+
~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`))))}
10931101
end
10941102

10951103
test "two windows" do

test/ecto/adapters/postgres_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,14 @@ defmodule Ecto.Adapters.PostgresTest do
584584
assert all(query) == ~s{SELECT coalesce(s0."x", 5) FROM "schema" AS s0}
585585
end
586586

587+
test "coalesce with subquery" do
588+
squery = from s in Schema, select: s.x
589+
query = Schema |> select([s], coalesce(subquery(squery), 5)) |> plan()
590+
591+
assert all(query) ==
592+
~s{SELECT coalesce((SELECT ss0."x" AS "x" FROM "schema" AS ss0), 5) FROM "schema" AS s0}
593+
end
594+
587595
test "where" do
588596
query = Schema |> where([r], r.x == 42) |> where([r], r.y != 43) |> select([r], r.x) |> plan()
589597

0 commit comments

Comments
 (0)