Skip to content

Conversation

danilshvalov
Copy link
Contributor

SQL queries often have duplicate elements, such as fields in SELECT. I would like to put such elements in a separate file to reduce duplication.

This Pull Request adds the ability to use Jinja2 substitution in SQL queries. The following is an example of usage.

_fields.sql
id,
first_name,
last_name
select_user_by_id.sql
SELECT
{% include '_fields.sql' %}
FROM users
WHERE id = $1
select_users.sql
SELECT
{% include '_fields.sql' %}
FROM users
ORDER BY id

Note: by creating a PR or an issue you automatically agree to the CLA. See CONTRIBUTING.md. Feel free to remove this note, the agreement holds.

@segoon
Copy link
Collaborator

segoon commented Aug 18, 2025

I think it is somewhat ad-hoc feature. What are other use cases for the feature?

@danilshvalov
Copy link
Contributor Author

I think it is somewhat ad-hoc feature. What are other use cases for the feature?

As I see it, any scenarios for substituting any duplicate values are applicable here, for example, for WHERE or JOIN.

Are there any obvious disadvantages of this solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants