Current section

Files

Jump to
ecto lib ecto query builder having.ex
Raw

lib/ecto/query/builder/having.ex

defmodule Ecto.Query.Builder.Having do
@moduledoc false
alias Ecto.Query.Builder
@doc """
Builds a quoted expression.
The quoted expression should evaluate to a query at runtime.
If possible, it does all calculations at compile time to avoid
runtime work.
"""
@spec build(Macro.t, [Macro.t], Macro.t, Macro.Env.t) :: Macro.t
def build(query, binding, expr, env) do
binding = Builder.escape_binding(binding)
{expr, params} = Builder.escape(expr, :boolean, %{}, binding)
params = Builder.escape_params(params)
having = quote do: %Ecto.Query.QueryExpr{
expr: unquote(expr),
params: unquote(params),
file: unquote(env.file),
line: unquote(env.line)}
Builder.apply_query(query, __MODULE__, [having], env)
end
@doc """
The callback applied by `build/4` to build the query.
"""
@spec apply(Ecto.Queryable.t, term) :: Ecto.Query.t
def apply(query, expr) do
query = Ecto.Queryable.to_query(query)
%{query | havings: query.havings ++ [expr]}
end
end