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

Reproducer for S1192 FP: SQL Named Parameters #9593

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: I think those test cases could be in a dedicated .Dapper.cs file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the test case and moved it to a dedicated test file.

Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,47 @@ public class SpecialChar
+ "Say \"hello\""; // Secondary
}
}

namespace SqlNamedParameters
{
public class Program
{
public void ExecuteSqlCommands()
{
var userCommand = new SqlCommand("SELECT * FROM Users WHERE Name = @Name");
userCommand.AddParameter(new SqlParameter("@Name", "John Doe")); // Noncompliant - FP: @Name refers to parameters in different SQL tables.
var users = userCommand.ExecuteQuery(); // Renaming one does not necessitate renaming of parameters with the same name from other tables.

var companyCommand = new SqlCommand("SELECT * FROM Companies WHERE Name = @Name");
companyCommand.AddParameter(new SqlParameter("@Name", "Contosco")); // Secondary - FP
var companies = companyCommand.ExecuteQuery();

var productCommand = new SqlCommand("SELECT * FROM Products WHERE Name = @Name");
productCommand.AddParameter(new SqlParameter("@Name", "CleanBot 9000")); // Secondary - FP
var products = productCommand.ExecuteQuery();

var countryCommand = new SqlCommand("SELECT * FROM Countries WHERE Name = @Name");
countryCommand.AddParameter(new SqlParameter("@Name", "Norway")); // Secondary - FP
var countries = countryCommand.ExecuteQuery();
}
}

public class SqlCommand
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
{
public string CommandText { get; }
public SqlCommand(string commandText) => CommandText = commandText;
public void AddParameter(SqlParameter parameter) { }
public object ExecuteQuery() => null;
}

public class SqlParameter
{
public string Name { get; }
public string Value { get; }
public SqlParameter(string name, string value)
{
Name = name;
Value = value;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be sure, shouldn't those be in the Dapper namespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally wanted to create some dummy classes, so that the UT doesn't need to load Dapper and its dependencies.
But a real-world example is probably better. I changed the test case and moved it to a dedicated test file.

}
Loading