Phoenix API Toolkit v0.14.0 PhoenixApiToolkit.Ecto.GenericQueries View Source

This entire module is DEPRECATED.

Generic queries are applicable to any named binding in a query. By using generic queries, it is not necessary to implement standard queries for every Ecto model.

For example, instead of implementing in a User model:

def by_username(query, username) do
  from [user: user] in query, where: user.username == ^username
end

User.by_username(query, "some username")

...you can use generic query equals/4 instead:

GenericQueries.equals(query, :user, :username, "some username")

Such generic queries can be combined together in complex ways:

iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>

iex> base_query()
...> |> equals(:user, :name, "Peter")
...> |> smaller_than(:user, :balance, 50.00)
#Ecto.Query<from u0 in "users", as: :user, where: u0.name == ^"Peter", where: u0.balance < ^50.0>

Most of these generic queries rely on named bindings to do their work. That's why it's probably a good idea to always name all bindings in your queries, and not rely on positional bindings to separate models in your queries.

Link to this section Summary

Types

The directions supported by order_by/4

Functions

Narrow down the query to results in which the value of binding.field is equal to value. If value is a list, results that are equal to any list element are returned.

Narrow down the query to results in which the value contained in binding.field is greater than or equal to value.

limit(query, value) deprecated

Limit the query result set size to value.

Narrow down the query to results in which value is a member of the set of values contained in field.binding. Use with array-type Ecto fields.

Offset the query results by value.

Order the query by binding.field in direction.

Narrow down the query to results in which the value contained in binding.field is smaller than value.

Link to this section Types

Specs

order_directions() ::
  :asc
  | :asc_nulls_first
  | :asc_nulls_last
  | :desc
  | :desc_nulls_first
  | :desc_nulls_last

The directions supported by order_by/4

Link to this section Functions

Link to this function

equals(query, binding, field, value)

View Source
This function is deprecated. Same as Ecto.Query.where(query, [binding: bd], bd.field == ^value) or ...in ^value.

Specs

equals(Ecto.Query.t(), atom(), atom(), any()) :: Ecto.Query.t()

Narrow down the query to results in which the value of binding.field is equal to value. If value is a list, results that are equal to any list element are returned.

iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>

iex> equals(base_query(), :user, :name, "Peter")
#Ecto.Query<from u0 in "users", as: :user, where: u0.name == ^"Peter">

iex> equals(base_query(), :user, :name, ["Peter", "Patrick"])
#Ecto.Query<from u0 in "users", as: :user, where: u0.name in ^["Peter", "Patrick"]>
Link to this function

greater_than_or_equals(query, binding, field, value)

View Source
This function is deprecated. Same as Ecto.Query.where(query, [binding: bd], bd.field >= ^value).

Specs

greater_than_or_equals(Ecto.Query.t(), atom(), atom(), any()) :: Ecto.Query.t()

Narrow down the query to results in which the value contained in binding.field is greater than or equal to value.

iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>

iex> greater_than_or_equals(base_query(), :user, :balance, 50.00)
#Ecto.Query<from u0 in "users", as: :user, where: u0.balance >= ^50.0>
This function is deprecated. Same as Ecto.Query.limit(query, ^value).

Specs

limit(Ecto.Query.t(), integer()) :: Ecto.Query.t()

Limit the query result set size to value.

iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>

iex> limit(base_query(), 10)
#Ecto.Query<from u0 in "users", as: :user, limit: ^10>
Link to this function

member_of(query, binding, field, value)

View Source
This function is deprecated. Same as Ecto.Query.where(query, [binding: bd], ^value in bd.field).

Specs

member_of(Ecto.Query.t(), atom(), atom(), any()) :: Ecto.Query.t()

Narrow down the query to results in which value is a member of the set of values contained in field.binding. Use with array-type Ecto fields.

iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>

iex> member_of(base_query(), :user, :roles, "admin")
#Ecto.Query<from u0 in "users", as: :user, where: ^"admin" in u0.roles>
This function is deprecated. Same as Ecto.Query.offset(query, ^value).

Specs

offset(Ecto.Query.t(), integer()) :: Ecto.Query.t()

Offset the query results by value.

iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>

iex> offset(base_query(), 10)
#Ecto.Query<from u0 in "users", as: :user, offset: ^10>
Link to this function

order_by(query, binding, field, direction)

View Source
This function is deprecated. Same as Ecto.Query.order_by(query, [binding: bd], [{^direction, bd.field}]).

Specs

order_by(Ecto.Query.t(), atom(), atom(), order_directions()) :: Ecto.Query.t()

Order the query by binding.field in direction.

iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>

iex> order_by(base_query(), :user, :name, :asc_nulls_first)
#Ecto.Query<from u0 in "users", as: :user, order_by: [asc_nulls_first: u0.name]>
Link to this function

smaller_than(query, binding, field, value)

View Source
This function is deprecated. Same as Ecto.Query.where(query, [binding: bd], bd.field < ^value).

Specs

smaller_than(Ecto.Query.t(), atom(), atom(), any()) :: Ecto.Query.t()

Narrow down the query to results in which the value contained in binding.field is smaller than value.

iex> base_query()
#Ecto.Query<from u0 in "users", as: :user>

iex> smaller_than(base_query(), :user, :balance, 50.00)
#Ecto.Query<from u0 in "users", as: :user, where: u0.balance < ^50.0>