Skip to content

Commit c5cc3c5

Browse files
author
spicychickensauce
committed
Simplify generated code
1 parent 99cec9a commit c5cc3c5

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,19 +1556,22 @@ defmodule Ecto.Adapters.SQLite3.Connection do
15561556

15571557
defp values_list(types, idx, num_rows) do
15581558
rows = :lists.seq(1, num_rows, 1)
1559-
col_names = Enum.map_join(types, ", ", &elem(&1, 0))
1560-
table_name = "temp_#{:rand.uniform(100_000)}"
1559+
1560+
col_names =
1561+
Enum.map_join(Enum.with_index(types), ", ", fn {{k, _v}, i} ->
1562+
"column#{i + 1} AS #{k}"
1563+
end)
15611564

15621565
[
1563-
"WITH #{table_name}(",
1566+
"SELECT ",
15641567
col_names,
1565-
") AS (VALUES ",
1568+
" FROM (VALUES ",
15661569
intersperse_reduce(rows, ?,, idx, fn _, idx ->
15671570
{value, idx} = values_expr(types, idx)
15681571
{[?(, value, ?)], idx}
15691572
end)
15701573
|> elem(0),
1571-
") SELECT * FROM #{table_name}"
1574+
")"
15721575
]
15731576
end
15741577

test/ecto/adapters/sqlite3/connection/join_test.exs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ defmodule Ecto.Adapters.SQLite3.Connection.JoinTest do
139139
rows = [%{x: 1, y: 1}, %{x: 2, y: 2}]
140140
types = %{x: :integer, y: :integer}
141141

142-
# Seeding rand ensures we get temp_78027 as the CTE name
143-
:rand.seed(:exsss, {1, 2, 3})
144-
145142
query =
146143
Schema
147144
|> join(
@@ -153,14 +150,9 @@ defmodule Ecto.Adapters.SQLite3.Connection.JoinTest do
153150
|> select([p, q], {p.id, q.x})
154151
|> plan()
155152

156-
# We can't hardcode the columns of the CTE here,
157-
# as the order changes for different Map implementations for OTP 25 and later
158-
col_names = Map.keys(types) |> Enum.join(", ")
159-
160153
assert ~s{SELECT s0."id", v1."x" FROM "schema" AS s0 } <>
161-
~s{INNER JOIN (WITH temp_78027(#{col_names}) AS (VALUES ($1::INTEGER,$2::INTEGER),($3::INTEGER,$4::INTEGER)) } <>
162-
~s{SELECT * FROM temp_78027) AS v1 } <>
163-
~s{ON (v1."x" = s0."x") AND (v1."y" = s0."y")} ==
154+
~s{INNER JOIN (SELECT column1 AS y, column2 AS x FROM (VALUES ($1::INTEGER,$2::INTEGER),($3::INTEGER,$4::INTEGER))) } <>
155+
~s{AS v1 ON (v1."x" = s0."x") AND (v1."y" = s0."y")} ==
164156
all(query)
165157
end
166158

0 commit comments

Comments
 (0)