gfw.common.query.Query#
- class Query[source]#
Abstract base class for SQL queries rendered via Jinja2 templates.
- Subclasses must define:
A Jinja2 template filename (via
template_filenameproperty).Variables to render into the template (via
template_varsproperty).
The base class handles Jinja2 environment setup, SQL rendering, and optional query formatting.
Methods
Returns SQL expression to cast a datetime field to TIMESTAMP.
Formats a SQL query string for better readability.
Builds the
SELECTclause fields from the output schema.Renders the Query using Jinja2.
Wraps each string in single quotes for safe SQL usage.
Injects a custom
jinja2.Environmentinto this query.Attributes
Default folder where Jinja2 templates are located.
Returns or lazily creates a Jinja2 environment for this query.
Optional schema for the query results.
Name of the Jinja2 template file for this query.
Variables to inject into the Jinja2 template.
Detects the top-level package name for this query class.
- classmethod datetime_to_timestamp(field)[source]#
Returns SQL expression to cast a datetime field to TIMESTAMP.
- property output_type: type[NamedTuple] | None#
Optional schema for the query results.
Subclasses may override this property to return a
NamedTupletype that models the expected rows. The fields of this type are automatically expanded into the generatedSELECTclause.- Returns:
A subclass of
NamedTupledescribing the result schema, orNoneif no schema is defined.
- abstract property template_filename: str#
Name of the Jinja2 template file for this query.
Subclasses must override this property to point to the SQL template file (relative to the
DEFAULT_JINJA_FOLDER).- Returns:
The filename of the template (e.g.,
messages.sql.j2).
- abstract property template_vars: dict[str, str]#
Variables to inject into the Jinja2 template.
Subclasses must override this property to return the dictionary of template context variables (e.g.,
start_date,end_date, …).- Returns:
A dictionary of key-value pairs for template rendering.
- property top_level_package: str#
Detects the top-level package name for this query class.
This is used to locate the query templates when using
EnvironmentLoader.- Returns:
The name of the top-level package as a string.
- property jinja_env: Environment#
Returns or lazily creates a Jinja2 environment for this query.
If no environment was explicitly set with
with_env(), one is created usingfrom_package()and the detected package name.
- with_env(env)[source]#
Injects a custom
jinja2.Environmentinto this query.This method enables dependency injection for testing or when using shared environments. Returns
selffor fluent chaining.- Parameters:
env (Environment) – A configured
jinja2.Environment.- Returns:
self (the same query instance).
- Return type:
- get_select_fields(convert_datetime_to_timestamp=False)[source]#
Builds the
SELECTclause fields from the output schema.- Parameters:
convert_datetime_to_timestamp (bool) – If True, fields typed as
datetimeare automatically cast to Unix float timestamps (viadatetime_to_timestamp()). All other fields are passed through.- Returns:
A comma-separated string of
SELECTfields.- Return type: