Skip to content

Commit 9c9efe9

Browse files
Raise if fragment source doesn't have select (#438)
1 parent c27adec commit 9c9efe9

File tree

8 files changed

+54
-11
lines changed

8 files changed

+54
-11
lines changed

lib/ecto/adapters/myxql/connection.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,11 @@ if Code.ensure_loaded?(MyXQL) do
271271
intersperse_map(fields, ", ", fn
272272
{:&, _, [idx]} ->
273273
case elem(sources, idx) do
274+
{nil, source, nil} ->
275+
error!(query, "MySQL adapter does not support selecting all fields from fragment #{source}. " <>
276+
"Please specify exactly which fields you want to select")
274277
{source, _, nil} ->
275-
error!(query, "MySQL does not support selecting all fields from #{source} without a schema. " <>
278+
error!(query, "MySQL adapter does not support selecting all fields from #{source} without a schema. " <>
276279
"Please specify a schema or specify exactly which fields you want to select")
277280
{_, source, _} ->
278281
source

lib/ecto/adapters/postgres/connection.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,11 @@ if Code.ensure_loaded?(Postgrex) do
366366
intersperse_map(fields, ", ", fn
367367
{:&, _, [idx]} ->
368368
case elem(sources, idx) do
369+
{nil, source, nil} ->
370+
error!(query, "PostgreSQL adapter does not support selecting all fields from fragment #{source}. " <>
371+
"Please specify exactly which fields you want to select")
369372
{source, _, nil} ->
370-
error!(query, "PostgreSQL does not support selecting all fields from #{source} without a schema. " <>
373+
error!(query, "PostgreSQL adapter does not support selecting all fields from #{source} without a schema. " <>
371374
"Please specify a schema or specify exactly which fields you want to select")
372375
{_, source, _} ->
373376
source

lib/ecto/adapters/tds/connection.ex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,13 @@ if Code.ensure_loaded?(Tds) do
395395
intersperse_map(fields, ", ", fn
396396
{:&, _, [idx]} ->
397397
case elem(sources, idx) do
398+
{nil, source, nil} ->
399+
error!(query, "Tds adapter does not support selecting all fields from fragment #{source}. " <>
400+
"Please specify exactly which fields you want to select")
401+
398402
{source, _, nil} ->
399-
error!(
400-
query,
401-
"Tds adapter does not support selecting all fields from #{source} without a schema. " <>
402-
"Please specify a schema or specify exactly which fields you want in projection"
403-
)
403+
error!(query, "Tds adapter does not support selecting all fields from #{source} without a schema. " <>
404+
"Please specify a schema or specify exactly which fields you want in projection")
404405

405406
{_, source, _} ->
406407
source

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ defmodule EctoSQL.MixProject do
7777
if path = System.get_env("ECTO_PATH") do
7878
{:ecto, path: path}
7979
else
80-
{:ecto, "~> 3.9.0-dev", github: "elixir-ecto/ecto"}
80+
{:ecto, "~> 3.9.0-dev", github: "greg-rychlewski/ecto", branch: "fragment_source"}
8181
end
8282
end
8383

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"decimal": {:hex, :decimal, "1.9.0", "83e8daf59631d632b171faabafb4a9f4242c514b0a06ba3df493951c08f64d07", [:mix], [], "hexpm", "b1f2343568eed6928f3e751cf2dffde95bfaa19dd95d09e8a9ea92ccfd6f7d85"},
77
"deep_merge": {:hex, :deep_merge, "0.2.0", "c1050fa2edf4848b9f556fba1b75afc66608a4219659e3311d9c9427b5b680b3", [:mix], [], "hexpm", "e3bf435a54ed27b0ba3a01eb117ae017988804e136edcbe8a6a14c310daa966e"},
88
"earmark_parser": {:hex, :earmark_parser, "1.4.25", "2024618731c55ebfcc5439d756852ec4e85978a39d0d58593763924d9a15916f", [:mix], [], "hexpm", "56749c5e1c59447f7b7a23ddb235e4b3defe276afc220a6227237f3efe83f51e"},
9-
"ecto": {:git, "https://github.com/elixir-ecto/ecto.git", "8831fe1fa8223c6db863559c41f68733eec301f6", []},
9+
"ecto": {:git, "https://github.com/greg-rychlewski/ecto.git", "31e8cd4d54897eb138abc52b658f1705fa10d845", [branch: "fragment_source"]},
1010
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
1111
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
1212
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},

test/ecto/adapters/myxql_test.exs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ defmodule Ecto.Adapters.MyXQLTest do
8888
query = "0posts" |> select([:x]) |> plan()
8989
assert all(query) == ~s{SELECT t0.`x` FROM `0posts` AS t0}
9090

91-
assert_raise Ecto.QueryError, ~r"MySQL does not support selecting all fields from `posts` without a schema", fn ->
91+
assert_raise Ecto.QueryError, ~r"MySQL adapter does not support selecting all fields from `posts` without a schema", fn ->
9292
all from(p in "posts", select: p) |> plan()
9393
end
9494
end
@@ -104,6 +104,18 @@ defmodule Ecto.Adapters.MyXQLTest do
104104
assert all(query) == ~s{SELECT s0.`x`, s0.`z` FROM (SELECT ss0.`x` AS `x`, ss0.`z` AS `z` FROM (SELECT ssp0.`x` AS `x`, ssp0.`y` AS `z` FROM `posts` AS ssp0) AS ss0) AS s0}
105105
end
106106

107+
test "from with fragment" do
108+
query = from(f in fragment("select ? as x", ^"abc"), select: f.x) |> plan()
109+
assert all(query) == ~s{SELECT f0.`x` FROM (select ? as x) AS f0}
110+
111+
query = from(fragment("select ? as x", ^"abc"), select: fragment("x")) |> plan()
112+
assert all(query) == ~s{SELECT x FROM (select ? as x) AS f0}
113+
114+
assert_raise Ecto.QueryError, ~r"MySQL adapter does not support selecting all fields from fragment", fn ->
115+
all from(f in fragment("select ? as x", ^"abc"), select: f) |> plan()
116+
end
117+
end
118+
107119
test "CTE" do
108120
initial_query =
109121
"categories"

test/ecto/adapters/postgres_test.exs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ defmodule Ecto.Adapters.PostgresTest do
9393
query = "0posts" |> select([:x]) |> plan()
9494
assert all(query) == ~s{SELECT t0."x" FROM "0posts" AS t0}
9595

96-
assert_raise Ecto.QueryError, ~r"PostgreSQL does not support selecting all fields from \"posts\" without a schema", fn ->
96+
assert_raise Ecto.QueryError, ~r"PostgreSQL adapter does not support selecting all fields from \"posts\" without a schema", fn ->
9797
all from(p in "posts", select: p) |> plan()
9898
end
9999
end
@@ -109,6 +109,18 @@ defmodule Ecto.Adapters.PostgresTest do
109109
assert all(query) == ~s{SELECT s0."x", s0."z" FROM (SELECT ss0."x" AS "x", ss0."z" AS "z" FROM (SELECT ssp0."x" AS "x", ssp0."y" AS "z" FROM "posts" AS ssp0) AS ss0) AS s0}
110110
end
111111

112+
test "from with fragment" do
113+
query = from(f in fragment("select ? as x", ^"abc"), select: f.x) |> plan()
114+
assert all(query) == ~s{SELECT f0."x" FROM (select $1 as x) AS f0}
115+
116+
query = from(fragment("select ? as x", ^"abc"), select: fragment("x")) |> plan()
117+
assert all(query) == ~s{SELECT x FROM (select $1 as x) AS f0}
118+
119+
assert_raise Ecto.QueryError, ~r"PostgreSQL adapter does not support selecting all fields from fragment", fn ->
120+
all from(f in fragment("select ? as x", ^"abc"), select: f) |> plan()
121+
end
122+
end
123+
112124
test "CTE" do
113125
initial_query =
114126
"categories"

test/ecto/adapters/tds_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ defmodule Ecto.Adapters.TdsTest do
142142
~s{SELECT s0.[x], s0.[z] FROM (SELECT ss0.[x] AS [x], ss0.[z] AS [z] FROM (SELECT ssp0.[x] AS [x], ssp0.[y] AS [z] FROM [posts] AS ssp0) AS ss0) AS s0}
143143
end
144144

145+
test "from with fragment" do
146+
query = from(f in fragment("select ? as x", ^"abc"), select: f.x) |> plan()
147+
assert all(query) == ~s{SELECT f0.[x] FROM (select @1 as x) AS f0}
148+
149+
query = from(fragment("select ? as x", ^"abc"), select: fragment("x")) |> plan()
150+
assert all(query) == ~s{SELECT x FROM (select @1 as x) AS f0}
151+
152+
assert_raise Ecto.QueryError, ~r"Tds adapter does not support selecting all fields from fragment", fn ->
153+
all from(f in fragment("select ? as x", ^"abc"), select: f) |> plan()
154+
end
155+
end
156+
145157
test "join with subquery" do
146158
posts = subquery("posts" |> where(title: ^"hello") |> select([r], %{x: r.x, y: r.y}))
147159

0 commit comments

Comments
 (0)