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

feat: trivial parameterization anti-patterns analysis support #61

Merged

Conversation

DeagleGross
Copy link
Collaborator

@DeagleGross DeagleGross commented Oct 22, 2023

PR adds the analysis of how sql parameter is passed:

  1. if like an interpolated string $"select * from customers where Id={id}", reports DAP241;
  2. if like an interpolated raw string literal $"""select * from customers where Id={id}""", reports DAP241;
  3. if like an concatenated string "select * from customers where Id=" + id, reports DAP242;
  4. also added a special case support for string.Format (reports DAP242 as in the case of concatenated strings);

Also supported lookup for local variables: in case sql argument is not passed directly, but via the local variable. Example:

int id = 1;
var sqlQuery = "select Id from Customers where Id = " + id;
_ = connection.Query<int>(sqlQuery);
DiagnosticId Title Message
DAP241 Interpolated string usage Data values should not be interpolated into SQL string - use parameters instead
DAP242 Concatenated string usage Data values should not be concatenated into SQL string - use parameters instead

Closes #35

@DeagleGross DeagleGross requested a review from mgravell October 22, 2023 00:41
@DeagleGross DeagleGross self-assigned this Oct 22, 2023
@mgravell
Copy link
Member

mgravell commented Oct 22, 2023

Looks amazing! An additional possible test scenarios: does it work for interpolated raw string literals? i.e.

$$"""
select * from
customers where Id = {{id}}
"""

@DeagleGross
Copy link
Collaborator Author

@mgravell addressed PR comments

@mgravell mgravell merged commit 207f229 into DapperLib:main Oct 23, 2023
1 check passed
@mgravell
Copy link
Member

Many thanks.

@DeagleGross DeagleGross deleted the dmkorolev/parameterization-antipatterns branch November 12, 2023 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Analyzer: parameterization anti-patterns
2 participants