View Source QueryBuilder (Query Builder v1.4.2)

Summary

Functions

Allows to pass a list of operations through a keyword list.

A limit query expression. If multiple limit expressions are provided, the last expression is evaluated

Run QueryBuilder.order_by/2 only if given condition is met.

Run QueryBuilder.where/2 only if given condition is met.

A offset query expression. If multiple offset expressions are provided, the last expression is evaluated

An order by query expression.

An order by query expression.

Preloads the associations.

An AND where query expression.

Functions

Allows to pass a list of operations through a keyword list.

Example:

QueryBuilder.from_list(query, [
  where: [name: "John", city: "Anytown"],
  preload: [articles: :comments]
])
Link to this function

left_join(query, assoc_fields, filters \\ [], or_filters \\ [])

View Source

A join query expression.

Example:

QueryBuilder.left_join(query, :articles, title@articles: "Foo", or: [title@articles: "Bar"])

A limit query expression. If multiple limit expressions are provided, the last expression is evaluated

Example:

QueryBuilder.limit(query, 10)
Link to this function

maybe_order_by(query, bool, value)

View Source

Run QueryBuilder.order_by/2 only if given condition is met.

Link to this function

maybe_order_by(query, bool, assoc_fields, value)

View Source

Run QueryBuilder.order_by/3 only if given condition is met.

Link to this function

maybe_where(query, bool, filters)

View Source

Run QueryBuilder.where/2 only if given condition is met.

Link to this function

maybe_where(query, condition, assoc_fields, filters, or_filters \\ [])

View Source

Run QueryBuilder.where/4 only if given condition is met.

A offset query expression. If multiple offset expressions are provided, the last expression is evaluated

Example:

QueryBuilder.offset(query, 10)

An order by query expression.

Example:

QueryBuilder.order_by(query, asc: :lastname, asc: :firstname)
Link to this function

order_by(query, assoc_fields, value)

View Source

An order by query expression.

For more about the second argument, see where/3.

Example:

QueryBuilder.order_by(query, :articles, asc: :title@articles)
Link to this function

paginate(query, repo, opts \\ [])

View Source
Link to this function

preload(query, assoc_fields)

View Source

Preloads the associations.

Bindings are automatically set if joins have been made, or if it is preferable to join (i.e. one-to-one associations are preferable to include into the query result rather than emitting separate DB queries).

Example:

QueryBuilder.preload(query, [role: :permissions, articles: [:stars, comments: :user]])

An AND where query expression.

Example:

QueryBuilder.where(query, firstname: "John")
Link to this function

where(query, assoc_fields, filters, or_filters \\ [])

View Source

An AND where query expression.

Associations are passed in second argument; fields from these associations can then be referenced by writing the field name, followed by the "@" character and the association name, as an atom. For example: :name@users.

Example:

QueryBuilder.where(query, [role: :permissions], name@permissions: :write)

OR clauses may be passed through last argument opts. For example:

QueryBuilder.where(query, [], [firstname: "John"], or: [firstname: "Alice", lastname: "Doe"], or: [firstname: "Bob"])