-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
queryable.ex
46 lines (39 loc) · 1.13 KB
/
queryable.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
defprotocol Ecto.Queryable do
@moduledoc """
Converts a data structure into an `Ecto.Query`.
"""
@doc """
Converts the given `data` into an `Ecto.Query`.
"""
def to_query(data)
end
defimpl Ecto.Queryable, for: Ecto.Query do
def to_query(query), do: query
end
defimpl Ecto.Queryable, for: Ecto.SubQuery do
def to_query(subquery), do: %Ecto.Query{from: subquery}
end
defimpl Ecto.Queryable, for: BitString do
def to_query(source) when is_binary(source),
do: %Ecto.Query{from: {source, nil}}
end
defimpl Ecto.Queryable, for: Atom do
def to_query(module) do
try do
module.__schema__(:query)
rescue
UndefinedFunctionError ->
message = if :code.is_loaded(module) do
"the given module does not provide a schema"
else
"the given module does not exist"
end
raise Protocol.UndefinedError,
protocol: @protocol, value: module, description: message
end
end
end
defimpl Ecto.Queryable, for: Tuple do
def to_query(from = {source, model}) when is_binary(source) and is_atom(model),
do: %Ecto.Query{from: from, prefix: model.__schema__(:prefix)}
end