Filters

Composable SQL statement filters for building WHERE clauses, pagination, ordering, and search conditions. Filters can be applied to any SQL statement via apply_filter().

apply_filter

sqlspec.core.filters.apply_filter(statement, filter_obj)[source]

Apply a statement filter to a SQL query object.

Parameters:
  • statement (SQL) – The SQL query object to modify.

  • filter_obj (StatementFilter) – The filter to apply.

Return type:

SQL

Returns:

The modified query object.

Base

class sqlspec.core.filters.StatementFilter[source]

Bases: ABC

Abstract base class for filters that can be appended to a statement.

abstractmethod append_to_statement(statement)[source]

Append the filter to the statement.

This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().

Return type:

SQL

extract_parameters()[source]

Extract parameters that this filter contributes.

Returns:

  • positional_parameters: List of positional parameter values

  • named_parameters: Dict of parameter name to value

Return type:

Tuple of (positional_parameters, named_parameters) where

abstractmethod get_cache_key()[source]

Return a cache key for this filter’s configuration.

Return type:

tuple[typing.Any, ...]

Returns:

Tuple of hashable values representing the filter’s configuration

Date Filters

class sqlspec.core.filters.BeforeAfterFilter[source]

Bases: StatementFilter

Filter for datetime range queries.

Applies WHERE clauses for before/after datetime filtering.

__init__(field_name, before=None, after=None)[source]
get_param_names()[source]

Get parameter names without storing them.

Return type:

list[str]

extract_parameters()[source]

Extract filter parameters.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Apply filter to SQL expression only.

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

class sqlspec.core.filters.OnBeforeAfterFilter[source]

Bases: StatementFilter

Filter for inclusive datetime range queries.

Applies WHERE clauses for on-or-before/on-or-after datetime filtering.

__init__(field_name, on_or_before=None, on_or_after=None)[source]
get_param_names()[source]

Get parameter names without storing them.

Return type:

list[str]

extract_parameters()[source]

Extract filter parameters.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Append the filter to the statement.

This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

Collection Filters

class sqlspec.core.filters.InCollectionFilter[source]

Bases: InAnyFilter[T]

Filter for IN clause queries.

Constructs WHERE … IN (…) clauses.

__init__(field_name, values=None)[source]
get_param_names()[source]

Get parameter names without storing them.

Return type:

list[str]

extract_parameters()[source]

Extract filter parameters.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Append the filter to the statement.

This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

class sqlspec.core.filters.NotInCollectionFilter[source]

Bases: InAnyFilter[T]

Filter for NOT IN clause queries.

Constructs WHERE … NOT IN (…) clauses.

__init__(field_name, values=None)[source]
get_param_names()[source]

Get parameter names without storing them.

Return type:

list[str]

extract_parameters()[source]

Extract filter parameters.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Append the filter to the statement.

This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

class sqlspec.core.filters.AnyCollectionFilter[source]

Bases: InAnyFilter[T]

Filter for PostgreSQL-style ANY clause queries.

Constructs WHERE column_name = ANY (array_expression) clauses.

__init__(field_name, values=None)[source]
get_param_names()[source]

Get parameter names without storing them.

Return type:

list[str]

extract_parameters()[source]

Extract filter parameters.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Append the filter to the statement.

This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

class sqlspec.core.filters.NotAnyCollectionFilter[source]

Bases: InAnyFilter[T]

Filter for PostgreSQL-style NOT ANY clause queries.

Constructs WHERE NOT (column_name = ANY (array_expression)) clauses.

__init__(field_name, values=None)[source]
get_param_names()[source]

Get parameter names without storing them.

Return type:

list[str]

extract_parameters()[source]

Extract filter parameters.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Append the filter to the statement.

This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

Null Filters

class sqlspec.core.filters.NullFilter[source]

Bases: StatementFilter

Filter for IS NULL queries.

Constructs WHERE field_name IS NULL clauses.

__init__(field_name)[source]
extract_parameters()[source]

Extract filter parameters.

Returns empty parameters since IS NULL requires no values.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Apply IS NULL filter to SQL expression.

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

class sqlspec.core.filters.NotNullFilter[source]

Bases: StatementFilter

Filter for IS NOT NULL queries.

Constructs WHERE field_name IS NOT NULL clauses.

__init__(field_name)[source]
extract_parameters()[source]

Extract filter parameters.

Returns empty parameters since IS NOT NULL requires no values.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Apply IS NOT NULL filter to SQL expression.

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

Pagination

class sqlspec.core.filters.LimitOffsetFilter[source]

Bases: PaginationFilter

Filter for LIMIT and OFFSET clauses.

Adds pagination support through LIMIT/OFFSET SQL clauses.

__init__(limit, offset)[source]
get_param_names()[source]

Get parameter names without storing them.

Return type:

list[str]

extract_parameters()[source]

Extract filter parameters.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Append the filter to the statement.

This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

class sqlspec.core.filters.OffsetPagination[source]

Bases: Generic[T]

Container for data returned using limit/offset pagination.

__init__(items, limit, offset, total)[source]

Initialize OffsetPagination.

Parameters:
  • items (Sequence[TypeVar(T)]) – List of data being sent as part of the response.

  • limit (int) – Maximal number of items to send.

  • offset (int) – Offset from the beginning of the query. Identical to an index.

  • total (int) – Total number of items.

Ordering

class sqlspec.core.filters.OrderByFilter[source]

Bases: StatementFilter

Filter for ORDER BY clauses.

Adds sorting capability to SQL queries.

__init__(field_name, sort_order='asc')[source]
extract_parameters()[source]

Extract filter parameters.

Return type:

tuple[list[typing.Any], dict[str, typing.Any]]

append_to_statement(statement)[source]

Append the filter to the statement.

This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().

Return type:

SQL

get_cache_key()[source]

Return cache key for this filter configuration.

Return type:

tuple[typing.Any, ...]

Type Aliases

sqlspec.core.filters.FilterTypes

Union type of all filter classes.