Selecto.Query (Selecto v0.4.5)

Copy Markdown

Core query building operations for Selecto.

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

Summary

Functions

Add filters to the query.

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-retarget filter list (set.post_retarget_filters).

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

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

Return only pre-retarget 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 fields to the select list.

Generate SQL without executing - useful for debugging and caching.

Functions

filter(selecto, filters)

Add filters to the query.

For macro-free query composition, prefer importing Selecto.Expr and using runtime filter constructors like eq/2, gte/2, and compact_and/1.

Examples

import Selecto.Expr

selecto
|> Selecto.Query.filter(eq("active", true))
|> Selecto.Query.filter(compact_and([gte("age", 18), not_null("email")]))

group_by(selecto, groups)

Add to the Group By clause.

Examples

import Selecto.Expr

selecto
|> Selecto.Query.group_by(["category", "region"])
|> Selecto.Query.group_by(rollup(["status"]))

limit(selecto, limit_value)

Limit the number of rows returned by the query.

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)

Set the offset for the query results.

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)

Add to the Order By clause.

Examples

import Selecto.Expr

selecto
|> Selecto.Query.order_by([asc("created_at"), desc("name")])

post_retarget_filter(selecto, filters)

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

Explicitly append filters to the post-retarget filter list (set.post_retarget_filters).

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

post_retarget_filters(selecto)

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

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

pre_retarget_filter(selecto, filters)

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

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

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

pre_retarget_filters(selecto)

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

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

This reads set.filtered and does not include post-retarget 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

  • :include_post_retarget - include set.post_retarget_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)

Add fields to the select list.

For macro-free query composition, prefer importing Selecto.Expr and passing string field paths plus runtime helper constructors.

Examples

import Selecto.Expr

selecto
|> Selecto.Query.select(["name", "email", as(count(), "total")])
|> Selecto.Query.select(avg("price"))

to_sql(selecto, opts \\ [])

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

Generate SQL without executing - useful for debugging and caching.

Examples

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

Options

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