···127127 # SQLite does not support anything except a single column in DISTINCT
128128 :multicolumn_distinct,
129129130130- # Values list
131131- :values_list
130130+ # :location is not supported in elixir 1.15 and earlier, so we exclude all
131131+ if Version.match?(System.version(), "~> 1.16") do
132132+ # Run all with tag values_list, except for the "delete_all" test,
133133+ # as JOINS are not supported on DELETE statements by SQLite.
134134+ {:location, {"ecto/integration_test/cases/repo.exs", 2281}}
135135+ else
136136+ :values_list
137137+ end
132138]
133139134140ExUnit.configure(exclude: excludes)
+91
integration_test/values_test.exs
···11+defmodule Ecto.Integration.ValuesTest do
22+ use Ecto.Integration.Case, async: true
33+44+ import Ecto.Query, only: [from: 2, with_cte: 3]
55+66+ alias Ecto.Integration.Comment
77+ alias Ecto.Integration.Post
88+ alias Ecto.Integration.TestRepo
99+1010+ test "values works with datetime" do
1111+ TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:01:00]})
1212+ TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:02:00]})
1313+ TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:03:00]})
1414+1515+ params = [
1616+ %{id: 1, date: ~N[2000-01-01 00:00:00]},
1717+ %{id: 2, date: ~N[2000-01-01 00:01:00]},
1818+ %{id: 3, date: ~N[2000-01-01 00:02:00]},
1919+ %{id: 4, date: ~N[2000-01-01 00:03:00]}
2020+ ]
2121+2222+ types = %{id: :integer, date: :naive_datetime}
2323+2424+ results =
2525+ from(params in values(params, types),
2626+ left_join: p in Post,
2727+ on: p.inserted_at <= params.date,
2828+ group_by: params.id,
2929+ select: %{id: params.id, count: count(p.id)},
3030+ order_by: count(p.id)
3131+ )
3232+ |> TestRepo.all()
3333+3434+ assert results == [
3535+ %{count: 0, id: 1},
3636+ %{count: 1, id: 2},
3737+ %{count: 2, id: 3},
3838+ %{count: 3, id: 4}
3939+ ]
4040+ end
4141+4242+ test "join to values works" do
4343+ TestRepo.insert!(%Post{id: 1})
4444+ TestRepo.insert!(%Comment{post_id: 1, text: "short"})
4545+ TestRepo.insert!(%Comment{post_id: 1, text: "much longer text"})
4646+4747+ params = [%{id: 1, post_id: 1, n: 0}, %{id: 2, post_id: 1, n: 10}]
4848+ types = %{id: :integer, post_id: :integer, n: :integer}
4949+5050+ results =
5151+ from(p in Post,
5252+ right_join: params in values(params, types),
5353+ on: params.post_id == p.id,
5454+ left_join: c in Comment,
5555+ on: c.post_id == p.id and fragment("LENGTH(?)", c.text) > params.n,
5656+ group_by: params.id,
5757+ select: {params.id, count(c.id)}
5858+ )
5959+ |> TestRepo.all()
6060+6161+ assert [{1, 2}, {2, 1}] = results
6262+ end
6363+6464+ test "values can be used together with CTE" do
6565+ TestRepo.insert!(%Post{id: 1, visits: 42})
6666+ TestRepo.insert!(%Comment{post_id: 1, text: "short"})
6767+ TestRepo.insert!(%Comment{post_id: 1, text: "much longer text"})
6868+6969+ params = [%{id: 1, post_id: 1, n: 0}, %{id: 2, post_id: 1, n: 10}]
7070+ types = %{id: :integer, post_id: :integer, n: :integer}
7171+7272+ cte_query = from(p in Post, select: %{id: p.id, visits: coalesce(p.visits, 0)})
7373+7474+ q = Post |> with_cte("xxx", as: ^cte_query)
7575+7676+ results =
7777+ from(p in q,
7878+ right_join: params in values(params, types),
7979+ on: params.post_id == p.id,
8080+ left_join: c in Comment,
8181+ on: c.post_id == p.id and fragment("LENGTH(?)", c.text) > params.n,
8282+ left_join: cte in "xxx",
8383+ on: cte.id == p.id,
8484+ group_by: params.id,
8585+ select: {params.id, count(c.id), cte.visits}
8686+ )
8787+ |> TestRepo.all()
8888+8989+ assert [{1, 2, 42}, {2, 1, 42}] = results
9090+ end
9191+end
+32-8
lib/ecto/adapters/sqlite3/connection.ex
···10371037 message: "join hints are not supported by SQLite3"
10381038 end
1039103910401040- defp assert_valid_join(%JoinExpr{source: {:values, _, _}}, query) do
10411041- raise Ecto.QueryError,
10421042- query: query,
10431043- message: "SQLite3 adapter does not support values lists"
10441044- end
10451045-10461040 defp assert_valid_join(_join_expr, _query), do: :ok
1047104110481042 defp join_on(:cross, true, _sources, _query), do: []
···13681362 |> parens_for_select
13691363 end
1370136413711371- defp expr({:values, _, _}, _, _query) do
13721372- raise ArgumentError, "SQLite3 adapter does not support values lists"
13651365+ defp expr({:values, _, [types, idx, num_rows]}, _, _query) do
13661366+ [?(, values_list(types, idx + 1, num_rows), ?)]
13731367 end
1374136813751369 defp expr({:identifier, _, [literal]}, _sources, _query) do
···15601554 message: "unsupported expression #{inspect(expr)}"
15611555 end
1562155615571557+ defp values_list(types, idx, num_rows) do
15581558+ rows = :lists.seq(1, num_rows, 1)
15591559+15601560+ col_names =
15611561+ Enum.map_join(Enum.with_index(types), ", ", fn {{k, _v}, i} ->
15621562+ "column#{i + 1} AS #{k}"
15631563+ end)
15641564+15651565+ [
15661566+ "SELECT ",
15671567+ col_names,
15681568+ " FROM (VALUES ",
15691569+ intersperse_reduce(rows, ?,, idx, fn _, idx ->
15701570+ {value, idx} = values_expr(types, idx)
15711571+ {[?(, value, ?)], idx}
15721572+ end)
15731573+ |> elem(0),
15741574+ ")"
15751575+ ]
15761576+ end
15771577+15781578+ defp values_expr(types, idx) do
15791579+ intersperse_reduce(types, ?,, idx, fn {_field, type}, idx ->
15801580+ {[?$, Integer.to_string(idx), ?:, ?: | column_type(type, nil)], idx + 1}
15811581+ end)
15821582+ end
15831583+15631584 def interval(_, "microsecond", _sources) do
15641585 raise ArgumentError,
15651586 "SQLite does not support microsecond precision in datetime intervals"
···16171638 case elem(sources, pos) do
16181639 {:fragment, _, _} ->
16191640 {nil, as_prefix ++ [?f | Integer.to_string(pos)], nil}
16411641+16421642+ {:values, _, _} ->
16431643+ {nil, as_prefix ++ [?v | Integer.to_string(pos)], nil}
1620164416211645 {table, schema, prefix} ->
16221646 name = as_prefix ++ [create_alias(table) | Integer.to_string(pos)]