userver icon indicating copy to clipboard operation
userver copied to clipboard

feat scripts: jinja2 in sql

Open danilshvalov opened this issue 8 months ago • 2 comments

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.

danilshvalov avatar Mar 31 '25 06:03 danilshvalov

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

segoon avatar Aug 18 '25 07:08 segoon

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?

danilshvalov avatar Aug 29 '25 09:08 danilshvalov