Skip to content

Commit e851684

Browse files
author
spicychickensauce
committed
Use random name for temp table
1 parent 2b01d45 commit e851684

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,17 +1557,18 @@ defmodule Ecto.Adapters.SQLite3.Connection do
15571557
defp values_list(types, idx, num_rows) do
15581558
rows = :lists.seq(1, num_rows, 1)
15591559
col_names = Enum.map_join(types, ", ", &elem(&1, 0))
1560+
table_name = "temp_#{:rand.uniform(100_000)}"
15601561

15611562
[
1562-
"WITH xxx(",
1563+
"WITH #{table_name}(",
15631564
col_names,
15641565
") AS (VALUES ",
15651566
intersperse_reduce(rows, ?,, idx, fn _, idx ->
15661567
{value, idx} = values_expr(types, idx)
15671568
{[?(, value, ?)], idx}
15681569
end)
15691570
|> elem(0),
1570-
") SELECT * FROM xxx"
1571+
") SELECT * FROM #{table_name}"
15711572
]
15721573
end
15731574

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ 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+
142145
query =
143146
Schema
144147
|> join(
@@ -151,7 +154,7 @@ defmodule Ecto.Adapters.SQLite3.Connection.JoinTest do
151154
|> plan()
152155

153156
assert ~s{SELECT s0."id", v1."x" FROM "schema" AS s0 } <>
154-
~s{INNER JOIN (WITH xxx(y, x) AS (VALUES ($1,$2),($3,$4)) SELECT * FROM xxx) AS v1 } <>
157+
~s{INNER JOIN (WITH temp_78027(y, x) AS (VALUES ($1,$2),($3,$4)) SELECT * FROM temp_78027) AS v1 } <>
155158
~s{ON (v1."x" = s0."x") AND (v1."y" = s0."y")} ==
156159
all(query)
157160
end

0 commit comments

Comments
 (0)