Skip to content

Commit c1edd09

Browse files
committed
maybe_paren_for_expr_inside_fragment
1 parent 9192a6d commit c1edd09

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

test/ecto/adapters/myxql_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,12 @@ defmodule Ecto.Adapters.MyXQLTest do
681681
assert_raise Ecto.QueryError, fn ->
682682
all(query)
683683
end
684+
685+
query = Schema |> select([r], fragment("CAST(? AS INT)", r.x and r.y)) |> plan()
686+
assert all(query) == ~s{SELECT CAST((s0.`x` AND s0.`y`) AS INT) FROM `schema` AS s0}
687+
688+
query = Schema |> select([r], fragment("CAST(? AS INT)", r.x or r.y)) |> plan()
689+
assert all(query) == ~s{SELECT CAST((s0.`x` OR s0.`y`) AS INT) FROM `schema` AS s0}
684690
end
685691

686692
test "literals" do

test/ecto/adapters/postgres_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,14 @@ defmodule Ecto.Adapters.PostgresTest do
861861
assert_raise Ecto.QueryError, fn ->
862862
all(query)
863863
end
864+
865+
query = Schema |> select([r], fragment("?::integer", r.x and r.y)) |> plan()
866+
# Boolean operations inside fragments should be wrapped in parentheses
867+
# to ensure correct precedence with surrounding SQL
868+
assert all(query) == ~s{SELECT (s0."x" AND s0."y")::integer FROM "schema" AS s0}
869+
870+
query = Schema |> select([r], fragment("?::integer", r.x or r.y)) |> plan()
871+
assert all(query) == ~s{SELECT (s0."x" OR s0."y")::integer FROM "schema" AS s0}
864872
end
865873

866874
test "literals" do

test/ecto/adapters/tds_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,12 @@ defmodule Ecto.Adapters.TdsTest do
719719
fn ->
720720
all(query)
721721
end
722+
723+
query = Schema |> select([r], fragment("CAST(? AS INT)", r.x and r.y)) |> plan()
724+
assert all(query) == ~s{SELECT CAST((s0.[x] AND s0.[y]) AS INT) FROM [schema] AS s0}
725+
726+
query = Schema |> select([r], fragment("CAST(? AS INT)", r.x or r.y)) |> plan()
727+
assert all(query) == ~s{SELECT CAST((s0.[x] OR s0.[y]) AS INT) FROM [schema] AS s0}
722728
end
723729

724730
test "literals" do

0 commit comments

Comments
 (0)