Skip to content

Commit 68b9229

Browse files
Allow function sources whose name begins with 'select' (#444)
1 parent cf8e924 commit 68b9229

4 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/ecto/adapters/postgres/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ if Code.ensure_loaded?(Postgrex) do
599599
defp operator_to_boolean(:or), do: " OR "
600600

601601
defp parens_for_select([first_expr | _] = expr) do
602-
if is_binary(first_expr) and String.match?(first_expr, ~r/^\s*select/i) do
602+
if is_binary(first_expr) and String.match?(first_expr, ~r/^\s*select\s/i) do
603603
[?(, expr, ?)]
604604
else
605605
expr

lib/ecto/adapters/tds/connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ if Code.ensure_loaded?(Tds) do
646646
defp operator_to_boolean(:or), do: " OR "
647647

648648
defp parens_for_select([first_expr | _] = expr) do
649-
if is_binary(first_expr) and String.match?(first_expr, ~r/^\s*select/i) do
649+
if is_binary(first_expr) and String.match?(first_expr, ~r/^\s*select\s/i) do
650650
[?(, expr, ?)]
651651
else
652652
expr

test/ecto/adapters/postgres_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ defmodule Ecto.Adapters.PostgresTest do
116116
query = from(fragment("select ? as x", ^"abc"), select: fragment("x")) |> plan()
117117
assert all(query) == ~s{SELECT x FROM (select $1 as x) AS f0}
118118

119+
query = from(f in fragment("select_rows(arg)"), select: f.x) |> plan()
120+
assert all(query) == ~s{SELECT f0."x" FROM select_rows(arg) AS f0}
121+
119122
assert_raise Ecto.QueryError, ~r"PostgreSQL adapter does not support selecting all fields from fragment", fn ->
120123
all from(f in fragment("select ? as x", ^"abc"), select: f) |> plan()
121124
end

test/ecto/adapters/tds_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ defmodule Ecto.Adapters.TdsTest do
149149
query = from(fragment("select ? as x", ^"abc"), select: fragment("x")) |> plan()
150150
assert all(query) == ~s{SELECT x FROM (select @1 as x) AS f0}
151151

152+
query = from(f in fragment("select_rows(arg)"), select: f.x) |> plan()
153+
assert all(query) == ~s{SELECT f0.[x] FROM select_rows(arg) AS f0}
154+
152155
assert_raise Ecto.QueryError, ~r"Tds adapter does not support selecting all fields from fragment", fn ->
153156
all from(f in fragment("select ? as x", ^"abc"), select: f) |> plan()
154157
end

0 commit comments

Comments
 (0)