SQL Templating¶
SQL Glider supports templating for SQL files, letting you parameterize queries with variables, conditionals, and loops before running any analysis. This is useful when your SQL references environment-specific schemas, toggleable columns, or other dynamic values.
Quick Start¶
Given a SQL file query.sql:
Render it with:
Output:
Templating works with all commands — not just template. Pass --templater jinja to lineage, tables overview, graph build, dissect, and others:
Built-in Templaters¶
SQL Glider ships with two templaters:
| Name | Description |
|---|---|
jinja |
Full Jinja2 templating (default for template command) |
none |
Pass-through, returns SQL unchanged |
List available templaters (including any installed plugins):
Jinja2 Syntax¶
The built-in Jinja templater supports the full Jinja2 feature set.
Variables¶
Conditionals¶
Loops¶
Comments¶
Includes¶
Templates can include other SQL files. Paths resolve relative to the source file's directory:
Providing Variables¶
Variables can come from four sources. When the same variable is defined in multiple sources, higher-priority sources win.
Priority (highest to lowest):
- CLI arguments (
--var key=value) - Variables file (
--vars-file vars.json) - Config file (
sqlglider.tomlinline variables) - Environment variables (
SQLGLIDER_VAR_*)
CLI Arguments¶
Values are type-inferred: true/false become booleans, numeric strings become integers or floats, everything else stays a string.
Variables File¶
JSON, YAML, or TOML files are supported:
Config File¶
Set defaults in sqlglider.toml:
[sqlglider]
templater = "jinja"
[sqlglider.templating]
variables_file = "vars.json"
[sqlglider.templating.variables]
schema = "default_schema"
Environment Variables¶
Any environment variable prefixed with SQLGLIDER_VAR_ is available as a template variable (prefix stripped, name lowercased):
Piping and Chaining¶
Templating works with stdin, so you can chain commands:
# Render a template then analyze lineage
cat query.sql | sqlglider template --var schema=prod | sqlglider lineage
# Inline SQL
echo "SELECT * FROM {{ schema }}.users" | sqlglider template --var schema=prod
Writing a Custom Templater¶
You can create your own templater as a Python package and register it as a plugin.
1. Implement the Templater Class¶
Subclass sqlglider.templating.base.Templater and implement name and render:
from pathlib import Path
from typing import Any, Dict, Optional
from sqlglider.templating.base import Templater, TemplaterError
class DbtStyleTemplater(Templater):
@property
def name(self) -> str:
return "dbt-style"
def render(
self,
sql: str,
variables: Optional[Dict[str, Any]] = None,
source_path: Optional[Path] = None,
) -> str:
variables = variables or {}
try:
# Your custom rendering logic here
for key, value in variables.items():
sql = sql.replace(f"{{{{ var('{key}') }}}}", str(value))
return sql
except Exception as e:
raise TemplaterError(f"dbt-style templater error: {e}") from e
The render method receives:
sql— the raw SQL string to processvariables— merged dictionary from all variable sources (already resolved by priority)source_path— path to the SQL file, useful for resolving relative includes
Raise TemplaterError on failure so SQL Glider can report it cleanly.
2. Register via Entry Points¶
In your package's pyproject.toml, add an entry point under the sqlglider.templaters group:
The key (dbt-style) is the name users pass to --templater.
3. Use It¶
After installing your package, the templater appears in --list and can be used like any built-in:
Or set it as the default in sqlglider.toml: