Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion integration_test/cases/repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/ecto/query/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/ecto/schema/loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Loading