feat scripts: jinja2 in sql
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.
I think it is somewhat ad-hoc feature. What are other use cases for the feature?
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?