Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parametrize related field queries #1797

Merged

Conversation

henadzit
Copy link
Contributor

@henadzit henadzit commented Dec 3, 2024

Description

This is the final (hopefully) parametrization PR. After this is merged, all queries should be parameterized.

  • Updates relational field queries to use parametrized queries
  • Refactors QuerySets to use pypika's get_parameterized_query instead of doing parameterization itself. It shifts the responsibility of parameterization to pypika. This PR introduces QueryBuilder.get_parameterized_query.

Motivation and Context

Parameterized queries are crucial for preventing SQL injection attacks and but also can improve performance of database operations.

This PR should finally allow us to close #81.

This also checks one of the requirements for v1.0.0, related ticket.

How Has This Been Tested?

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@coveralls
Copy link

coveralls commented Dec 3, 2024

Pull Request Test Coverage Report for Build 12142120565

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 63 of 63 (100.0%) changed or added relevant lines in 7 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.006%) to 90.384%

Files with Coverage Reduction New Missed Lines %
tortoise/backends/base/client.py 1 93.48%
Totals Coverage Status
Change from base Build 12116630665: -0.006%
Covered Lines: 6308
Relevant Lines: 6873

💛 - Coveralls

@henadzit henadzit requested a review from abondar December 3, 2024 14:50
@Abdeldjalil-H
Copy link
Contributor

Abdeldjalil-H commented Dec 4, 2024

hi @henadzit. I have a question. I've noticed that if we call .sql(params_inline=True) on a query that has filters as subqueries, for example

MyModel.filter(id__in=Subquery(OtherModel.filter(field=some_value).values_list("id"))).sql(params_inline=True)

then the value of field is not included in the raw sql. Does this PR handle this case or we should have a separate issue for this?

Thank you for your time.

@henadzit
Copy link
Contributor Author

henadzit commented Dec 4, 2024

hi @henadzit. I have a question. I've noticed that if we call .sql(params_inline=True) on a query that has filters as subqueries, for example

MyModel.filter(id__in=Subquery(OtherModel.filter(field=some_value).values_list("id"))).sql(params_inline=True)

then the value of field is not included in the raw sql. Does this PR handle this case or we should have a separate issue for this?

Thank you for your time.

Great catch! This PR doesn't handle it and not related, please open a separate issue, thanks.

@henadzit henadzit merged commit b9fda6c into tortoise:develop Dec 5, 2024
7 checks passed
@henadzit henadzit mentioned this pull request Dec 8, 2024
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.

Change to all-parametrised queries
4 participants