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
filter(selecto, filters)
@spec filter(Selecto.Types.t(), [Selecto.Types.filter()]) :: Selecto.Types.t()
@spec filter(Selecto.Types.t(), Selecto.Types.filter()) :: Selecto.Types.t()
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}}])
group_by(selecto, groups)
@spec group_by(Selecto.Types.t(), [Selecto.Types.field_name()]) :: Selecto.Types.t()
@spec group_by(Selecto.Types.t(), Selecto.Types.field_name()) :: Selecto.Types.t()
Add to the Group By clause.
examples
Examples
selecto
|> Selecto.Query.group_by(["category", "region"])
limit(selecto, limit_value)
@spec limit(Selecto.Types.t(), non_neg_integer()) :: Selecto.Types.t()
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)
offset(selecto, offset_value)
@spec offset(Selecto.Types.t(), non_neg_integer()) :: Selecto.Types.t()
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)
order_by(selecto, orders)
@spec order_by(Selecto.Types.t(), [Selecto.Types.order_spec()]) :: Selecto.Types.t()
@spec order_by(Selecto.Types.t(), Selecto.Types.order_spec()) :: Selecto.Types.t()
Add to the Order By clause.
examples
Examples
selecto
|> Selecto.Query.order_by(["created_at", {:desc, "name"}])
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.
post_pivot_filters(selecto)
@spec post_pivot_filters(Selecto.Types.t()) :: [Selecto.Types.filter()]
Return only post-pivot filters currently attached to the query.
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.
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.
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- includeset.post_pivot_filters(default:true)
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.
select(selecto, fields)
@spec select(Selecto.Types.t(), [Selecto.Types.selector()]) :: Selecto.Types.t()
@spec select(Selecto.Types.t(), Selecto.Types.selector()) :: Selecto.Types.t()
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", ["*"]})
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 (:ansior:markdown):indent- indentation string used by pretty formatter