Skip to content
Draft
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
6 changes: 3 additions & 3 deletions lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ if Code.ensure_loaded?(MyXQL) do
end)

wheres =
for %JoinExpr{on: %QueryExpr{expr: value} = expr} <- joins,
for %JoinExpr{on: %BooleanExpr{expr: value} = expr} <- joins,
value != true,
do: expr |> Map.put(:__struct__, BooleanExpr) |> Map.put(:op, :and)
do: expr

{[?,, ?\s | froms], wheres}
end
Expand All @@ -513,7 +513,7 @@ if Code.ensure_loaded?(MyXQL) do

defp join(%{joins: joins} = query, sources) do
Enum.map(joins, fn
%JoinExpr{on: %QueryExpr{expr: expr}, qual: qual, ix: ix, source: source, hints: hints} ->
%JoinExpr{on: %{expr: expr}, qual: qual, ix: ix, source: source, hints: hints} ->
{join, name} = get_source(query, sources, ix, source)

[
Expand Down
10 changes: 5 additions & 5 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@ if Code.ensure_loaded?(Postgrex) do
join_clauses = join(%{query | joins: other_joins}, sources)

wheres =
for %JoinExpr{on: %QueryExpr{expr: value} = expr} <- inner_joins,
for %JoinExpr{on: %BooleanExpr{expr: value} = expr} <- inner_joins,
value != true,
do: expr |> Map.put(:__struct__, BooleanExpr) |> Map.put(:op, :and)
do: expr

{[?\s, prefix, ?\s, froms | join_clauses], wheres}
end
Expand All @@ -724,9 +724,9 @@ if Code.ensure_loaded?(Postgrex) do
end)

wheres =
for %JoinExpr{on: %QueryExpr{expr: value} = expr} <- joins,
for %JoinExpr{on: %BooleanExpr{expr: value} = expr} <- joins,
value != true,
do: expr |> Map.put(:__struct__, BooleanExpr) |> Map.put(:op, :and)
do: expr

{[?\s, prefix, ?\s | froms], wheres}
end
Expand All @@ -738,7 +738,7 @@ if Code.ensure_loaded?(Postgrex) do
?\s
| Enum.map_intersperse(joins, ?\s, fn
%JoinExpr{
on: %QueryExpr{expr: expr},
on: %{expr: expr},
qual: qual,
ix: ix,
source: source,
Expand Down
2 changes: 1 addition & 1 deletion lib/ecto/adapters/tds/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ if Code.ensure_loaded?(Tds) do
[
?\s,
Enum.map_intersperse(joins, ?\s, fn
%JoinExpr{on: %QueryExpr{expr: expr}, qual: qual, ix: ix, source: source, hints: hints} ->
%JoinExpr{on: %{expr: expr}, qual: qual, ix: ix, source: source, hints: hints} ->
{join, name} = get_source(query, sources, ix, source)
qual_text = join_qual(qual, query)
join = join || ["(", expr(source, sources, query) | ")"]
Expand Down
17 changes: 17 additions & 0 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
from(x in Schema,
order_by: [asc: :id],
limit: 10,
lock: "FOR UPDATE SKIP LOCKED",

Check failure on line 231 in test/ecto/adapters/myxql_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test CTE update_all (Ecto.Adapters.MyXQLTest)
select: %{id: x.id}
)

Expand Down Expand Up @@ -974,7 +974,7 @@

query = from(m in Schema, update: [set: [x: 0], inc: [y: 1, z: -3]]) |> plan(:update_all)

assert update_all(query) ==

Check failure on line 977 in test/ecto/adapters/myxql_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test update all (Ecto.Adapters.MyXQLTest)
~s{UPDATE `schema` AS s0 SET s0.`x` = 0, s0.`y` = s0.`y` + 1, s0.`z` = s0.`z` + -3}

query = from(e in Schema, where: e.x == 123, update: [set: [x: 0]]) |> plan(:update_all)
Expand Down Expand Up @@ -1023,7 +1023,7 @@
|> join(:inner, [p], p2 in subquery(sub), on: p.id == p2.id)
|> update([_], set: [x: ^100])
|> plan(:update_all)

Check failure on line 1026 in test/ecto/adapters/myxql_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test update all with subquery (Ecto.Adapters.MyXQLTest)
assert update_all(query) ==
~s{UPDATE `schema` AS s0, } <>
~s{(SELECT ss0.`id` AS `id`, ss0.`x` AS `x`, ss0.`y` AS `y`, ss0.`z` AS `z`, ss0.`meta` AS `meta` FROM `schema` AS ss0 WHERE (ss0.`x` > 10)) AS s1 } <>
Expand Down Expand Up @@ -1369,6 +1369,23 @@
"SELECT s0.`id`, s1.`id` FROM `schema` AS s0 LEFT OUTER JOIN `schema2` AS s1 ON TRUE"
end

test "update all with interpolated join query" do
inner = from(s in Schema2, where: s.z > 10)

query =
from(m in Schema, join: x in ^inner, on: m.x == x.z, update: [set: [x: 0]])
|> plan(:update_all)

assert update_all(query) == ~s{UPDATE `schema` AS s0, `schema2` AS s1 SET s0.`x` = 0 WHERE ((s1.`z` > 10) AND (s0.`x` = s1.`z`))}
end

Check failure on line 1380 in test/ecto/adapters/myxql_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test update all with interpolated join query (Ecto.Adapters.MyXQLTest)

test "delete all with interpolated join query" do
inner = from(s in Schema2, where: s.z > 10)
query = from(m in Schema, join: x in ^inner, on: m.x == x.z) |> plan(:delete_all)

assert delete_all(query) == ~s{DELETE s0.* FROM `schema` AS s0 INNER JOIN `schema2` AS s1 ON (s1.`z` > 10) AND (s0.`x` = s1.`z`)}
end

test "lateral join with fragment" do
query =
Schema
Expand Down Expand Up @@ -1620,7 +1637,7 @@
query =
from(s in "schema",
join: v in values(values, types),
on: s.x == v.num,

Check failure on line 1640 in test/ecto/adapters/myxql_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test values list: update_all (Ecto.Adapters.MyXQLTest)
where: v.num == ^2,
update: [set: [y: v.num]]
)
Expand Down
17 changes: 17 additions & 0 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@
query =
Schema
|> join(:inner, [p], q in Schema2, on: p.x == q.z)
|> update([_], set: [x: 0])

Check failure on line 1290 in test/ecto/adapters/postgres_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test update all (Ecto.Adapters.PostgresTest)
|> plan(:update_all)

assert update_all(query) ==
Expand Down Expand Up @@ -1395,7 +1395,7 @@

assert_raise Ecto.QueryError,
~r/Need at least one inner join at the beginning to use other joins with update_all/,
fn ->

Check failure on line 1398 in test/ecto/adapters/postgres_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test update all with left join (Ecto.Adapters.PostgresTest)
update_all(query)
end
end
Expand Down Expand Up @@ -1733,6 +1733,23 @@
"SELECT s0.\"id\", s1.\"id\" FROM \"schema\" AS s0 LEFT OUTER JOIN \"schema2\" AS s1 ON TRUE"
end

test "update all with interpolated join query" do
inner = from(s in Schema2, where: s.z > 10)

query =
from(m in Schema, join: x in ^inner, on: m.x == x.z, update: [set: [x: 0]])
|> plan(:update_all)

assert update_all(query) == ~s{UPDATE "schema" AS s0 SET "x" = 0 FROM "schema2" AS s1 WHERE ((s1."z" > 10) AND (s0."x" = s1."z"))}
end

test "delete all with interpolated join query" do
inner = from(s in Schema2, where: s.z > 10)
query = from(m in Schema, join: x in ^inner, on: m.x == x.z) |> plan(:delete_all)

assert delete_all(query) == ~s{DELETE FROM "schema" AS s0 USING "schema2" AS s1 WHERE ((s1."z" > 10) AND (s0."x" = s1."z"))}
end

test "lateral join with fragment" do
query =
Schema
Expand All @@ -1740,7 +1757,7 @@
:inner_lateral,
[p],
q in fragment("SELECT * FROM schema2 AS s2 WHERE s2.id = ? AND s2.field = ?", p.x, ^10),
on: true

Check failure on line 1760 in test/ecto/adapters/postgres_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test update all with interpolated join query (Ecto.Adapters.PostgresTest)
)
|> select([p, q], {p.id, q.z})
|> where([p], p.id > 0 and p.id < ^100)
Expand All @@ -1750,7 +1767,7 @@
~s{SELECT s0."id", f1."z" FROM "schema" AS s0 INNER JOIN LATERAL } <>
~s{(SELECT * FROM schema2 AS s2 WHERE s2.id = s0."x" AND s2.field = $1) AS f1 ON TRUE } <>
~s{WHERE ((s0."id" > 0) AND (s0."id" < $2))}
end

Check failure on line 1770 in test/ecto/adapters/postgres_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test delete all with interpolated join query (Ecto.Adapters.PostgresTest)

test "cross lateral join with fragment" do
query =
Expand Down Expand Up @@ -2058,7 +2075,7 @@
~s{FROM (#{values_text}) AS v1 (#{fields}) } <>
~s{WHERE (s0."x" = v1."num") AND (v1."num" = $5)}
end

Check failure on line 2078 in test/ecto/adapters/postgres_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.19.4, 26.0.1)

test values list: update_all (Ecto.Adapters.PostgresTest)
defp values_text(values, types, ix) do
types = Map.to_list(types)

Expand Down
17 changes: 17 additions & 0 deletions test/ecto/adapters/tds_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,23 @@ defmodule Ecto.Adapters.TdsTest do
"SELECT s0.[id], s1.[id] FROM [schema] AS s0 LEFT OUTER JOIN [schema2] AS s1 ON 1 = 1"
end

test "update all with interpolated join query" do
inner = from(s in Schema2, where: s.z > 10)

query =
from(m in Schema, join: x in ^inner, on: m.x == x.z, update: [set: [x: 0]])
|> plan(:update_all)

assert update_all(query) == ~s{UPDATE s0 SET s0.[x] = 0 FROM [schema] AS s0 INNER JOIN [schema2] AS s1 ON (s1.[z] > 10) AND (s0.[x] = s1.[z])}
end

test "delete all with interpolated join query" do
inner = from(s in Schema2, where: s.z > 10)
query = from(m in Schema, join: x in ^inner, on: m.x == x.z) |> plan(:delete_all)

assert delete_all(query) == ~s{DELETE s0 FROM [schema] AS s0 INNER JOIN [schema2] AS s1 ON (s1.[z] > 10) AND (s0.[x] = s1.[z])}
end

test "join produces correct bindings" do
query = from(p in Schema, join: c in Schema2, on: true)
query = from(p in query, join: c in Schema2, on: true, select: {p.id, c.id})
Expand Down
Loading