diff --git a/integration_test/cases/repo.exs b/integration_test/cases/repo.exs index 50722c5492..68549b0c34 100644 --- a/integration_test/cases/repo.exs +++ b/integration_test/cases/repo.exs @@ -784,7 +784,8 @@ defmodule Ecto.Integration.RepoTest do test "fragment source mapped to schema" do query = from f in {fragment("select 1 as num"), Barebone} - assert %Barebone{num: 1} = TestRepo.one(query) + assert %Barebone{__meta__: meta, num: 1} = TestRepo.one(query) + assert meta.source == ~s[fragment("select 1 as num")] end test "fragment source mapped to schema with take" do diff --git a/lib/ecto/query.ex b/lib/ecto/query.ex index c3c145c62d..110331b23f 100644 --- a/lib/ecto/query.ex +++ b/lib/ecto/query.ex @@ -1090,6 +1090,9 @@ defmodule Ecto.Query do # Fragment with built-in function and undefined columns from(f in fragment("select generate_series(?::integer, ?::integer) as x", ^0, ^10), select: f.x) + # Fragment with schema + from(f in {fragment("my_table_valued_function(arg)"), Schema}) + ## Expressions examples # Schema diff --git a/lib/ecto/query/inspect.ex b/lib/ecto/query/inspect.ex index aec9323cdc..d5427ff0f0 100644 --- a/lib/ecto/query/inspect.ex +++ b/lib/ecto/query/inspect.ex @@ -96,6 +96,11 @@ defimpl Inspect, for: Ecto.Query do end) end + @doc false + def inspect_fragment({:fragment, _, parts}) do + Macro.to_string({:fragment, [], unmerge_fragments(parts, "", [])}) + end + defp to_list(query) do names = query diff --git a/lib/ecto/schema/loader.ex b/lib/ecto/schema/loader.ex index 8ee6471467..c96aec7047 100644 --- a/lib/ecto/schema/loader.ex +++ b/lib/ecto/schema/loader.ex @@ -14,6 +14,12 @@ defmodule Ecto.Schema.Loader do struct %{__meta__: %Metadata{} = metadata} = struct -> + source = + case source do + {:fragment, _, _} = frag -> Inspect.Ecto.Query.inspect_fragment(frag) + other -> other + end + Map.put(struct, :__meta__, %{metadata | source: source, prefix: prefix}) %{} = struct ->