Selecto.Query (Selecto v0.3.16)

Core query building operations for Selecto.

This module contains the basic query building functions like select, filter, order_by, group_by, limit, and offset.

Link to this section Summary

Functions

Add a filter to selecto. Send in a tuple with field name and filter value.

Add to the Group By clause.

Limit the number of rows returned by the query.

Set the offset for the query results.

Add to the Order By clause.

Explicitly append filters to the post-pivot filter list (set.post_pivot_filters).

Return only post-pivot filters currently attached to the query.

Explicitly append filters to the pre-pivot filter list (set.filtered).

Return only pre-pivot filters currently attached to the query.

Return query filters across current buckets as a flat list.

Return required filters currently attached to the query.

Add a field to the Select list. Send in one or a list of field names or selectable tuples.

Generate SQL without executing - useful for debugging and caching.

Link to this section Functions

Link to this function

filter(selecto, filters)

Add a filter to selecto. Send in a tuple with field name and filter value.

examples

Examples

selecto
|> Selecto.Query.filter([{"active", true}, {"age", {:gt, 18}}])
Link to this function

group_by(selecto, groups)

Add to the Group By clause.

examples

Examples

selecto
|> Selecto.Query.group_by(["category", "region"])
Link to this function

limit(selecto, limit_value)

Limit the number of rows returned by the query.

examples

Examples

# Limit to 10 rows
selecto |> Selecto.Query.limit(10)

# Limit with offset for pagination
selecto |> Selecto.Query.limit(10) |> Selecto.Query.offset(20)
Link to this function

offset(selecto, offset_value)

Set the offset for the query results.

examples

Examples

# Skip first 20 rows
selecto |> Selecto.Query.offset(20)

# Pagination: page 3 with 10 items per page
selecto |> Selecto.Query.limit(10) |> Selecto.Query.offset(20)
Link to this function

order_by(selecto, orders)

Add to the Order By clause.

examples

Examples

selecto
|> Selecto.Query.order_by(["created_at", {:desc, "name"}])
Link to this function

post_pivot_filter(selecto, filters)

@spec post_pivot_filter(Selecto.Types.t(), [Selecto.Types.filter()]) ::
  Selecto.Types.t()
@spec post_pivot_filter(Selecto.Types.t(), Selecto.Types.filter()) ::
  Selecto.Types.t()

Explicitly append filters to the post-pivot filter list (set.post_pivot_filters).

Use this when constraints should apply to the pivoted target root.

Link to this function

post_pivot_filters(selecto)

@spec post_pivot_filters(Selecto.Types.t()) :: [Selecto.Types.filter()]

Return only post-pivot filters currently attached to the query.

Link to this function

pre_pivot_filter(selecto, filters)

@spec pre_pivot_filter(Selecto.Types.t(), [Selecto.Types.filter()]) ::
  Selecto.Types.t()
@spec pre_pivot_filter(Selecto.Types.t(), Selecto.Types.filter()) :: Selecto.Types.t()

Explicitly append filters to the pre-pivot filter list (set.filtered).

Use this when you want filters to be preserved as source-root constraints even when composing a pivoted query.

Link to this function

pre_pivot_filters(selecto)

@spec pre_pivot_filters(Selecto.Types.t()) :: [Selecto.Types.filter()]

Return only pre-pivot filters currently attached to the query.

This reads set.filtered and does not include post-pivot buckets.

Link to this function

query_filters(selecto, opts \\ [])

@spec query_filters(
  Selecto.Types.t(),
  keyword()
) :: [Selecto.Types.filter()]

Return query filters across current buckets as a flat list.

This is useful for integrations that need to copy filters between Selecto and other query/update builders.

options

Options

  • :include_post_pivot - include set.post_pivot_filters (default: true)
Link to this function

required_filters(selecto)

@spec required_filters(Selecto.Types.t()) :: [Selecto.Types.filter()]

Return required filters currently attached to the query.

This includes domain-level required filters and query-level required filters added at runtime.

Link to this function

select(selecto, fields)

Add a field to the Select list. Send in one or a list of field names or selectable tuples.

examples

Examples

selecto
|> Selecto.Query.select(["name", "email"])
|> Selecto.Query.select({:func, "COUNT", ["*"]})
Link to this function

to_sql(selecto, opts \\ [])

@spec to_sql(
  Selecto.Types.t(),
  keyword()
) :: {String.t(), list()}

Generate SQL without executing - useful for debugging and caching.

examples

Examples

{sql, params} = Selecto.Query.to_sql(selecto)
IO.puts(sql)

options

Options

  • :pretty - format SQL for readability
  • :highlight - apply highlighting (:ansi or :markdown)
  • :indent - indentation string used by pretty formatter