combination.ex (1124B)
1 import Kernel, except: [apply: 2] 2 3 defmodule Ecto.Query.Builder.Combination do 4 @moduledoc false 5 6 alias Ecto.Query.Builder 7 8 @doc """ 9 Builds a quoted expression. 10 11 The quoted expression should evaluate to a query at runtime. 12 If possible, it does all calculations at compile time to avoid 13 runtime work. 14 """ 15 @spec build(atom, Macro.t, Macro.t, Macro.Env.t) :: Macro.t 16 def build(kind, query, {:^, _, [expr]}, env) do 17 expr = quote do: Ecto.Queryable.to_query(unquote(expr)) 18 Builder.apply_query(query, __MODULE__, [[{kind, expr}]], env) 19 end 20 21 def build(kind, _query, other, _env) do 22 Builder.error! "`#{Macro.to_string(other)}` is not a valid #{kind}. " <> 23 "#{kind} must always be an interpolated query, such as ^existing_query" 24 end 25 26 @doc """ 27 The callback applied by `build/4` to build the query. 28 """ 29 @spec apply(Ecto.Queryable.t, term) :: Ecto.Query.t 30 def apply(%Ecto.Query{combinations: combinations} = query, value) do 31 %{query | combinations: combinations ++ value} 32 end 33 def apply(query, value) do 34 apply(Ecto.Queryable.to_query(query), value) 35 end 36 end