Skip to content
This repository was archived by the owner on May 4, 2026. It is now read-only.

Add sql() function for database queries in templates#2

Merged
vloothuis merged 2 commits into
developfrom
feature/sql-data-source
Mar 29, 2026
Merged

Add sql() function for database queries in templates#2
vloothuis merged 2 commits into
developfrom
feature/sql-data-source

Conversation

@vloothuis

Copy link
Copy Markdown
Contributor

Summary

  • Add sql() function to templates for querying SQL databases (MySQL, PostgreSQL, SQLite)
  • Add optional sqlalchemy>=2.0 dependency (pip install mdcomp[sql])
  • Add --db-url CLI option and MDCOMP_DB_URL env var support
  • Cache database engines to prevent connection pool exhaustion
  • Sanitize URLs in error messages to prevent credential leakage

Usage

{% for user in sql("SELECT name, email FROM users WHERE status = :status", {"status": "active"}) %}
- {{ user.name }} ({{ user.email }})
{% endfor %}

Test plan

  • Unit tests for run_sql() function (parameterized queries, edge cases)
  • Integration tests for sql() in templates
  • Tests for URL sanitization (credential hiding)
  • Tests for engine caching
  • Manual verification with SQLite

Enables templates to execute SQL queries against MySQL, PostgreSQL, or
SQLite databases. The sql() function returns results as a list of dicts
for easy iteration in Jinja2 templates.

- Add optional sqlalchemy dependency (pip install mdcomp[sql])
- Add --db-url CLI option and MDCOMP_DB_URL env var support
- Cache database engines to prevent connection pool exhaustion
- Sanitize URLs in error messages to prevent credential leakage
- Use named parameters only (:name syntax) for query safety
@vloothuis vloothuis force-pushed the feature/sql-data-source branch from 574a9de to 3a1ef50 Compare March 29, 2026 07:48
@vloothuis vloothuis merged commit 177c5fb into develop Mar 29, 2026
5 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant