Skip to content

Commit 2ab460a

Browse files
committed
Move rules to separate files
1 parent 7fca295 commit 2ab460a

11 files changed

+106
-105
lines changed

README.md

Lines changed: 7 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -9,105 +9,12 @@
99

1010
A Roslyn-based analyzer for SQL related stuff in .NET
1111

12-
1312
## Analyzers
1413

15-
### SQL001
16-
**SQL type is not specified**
17-
18-
Noncompliant Code Example:
19-
```csharp
20-
Query<Thing>("select * from Thing where Name = @Name", new { Name = abcde });
21-
```
22-
23-
Compliant Solution:
24-
```csharp
25-
Query<Thing>("select * from Thing where Name = @Name", new {Name = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true }});
26-
```
27-
28-
https://github.com/StackExchange/Dapper/blob/master/Readme.md#ansi-strings-and-varchar
29-
30-
31-
### SQL002
32-
**SQL parameters mismatch**
33-
34-
Noncompliant Code Example:
35-
Dapper
36-
```csharp
37-
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Id = guid });
38-
```
39-
40-
SqlCommand
41-
```csharp
42-
var sql = new SqlCommand("select Age = @Age, Id = @Id");
43-
sql.Parameters.AddWithValue("@Id", guid);
44-
sql.ExecuteNonQuery();
45-
```
46-
47-
Compliant Solution:
48-
Dapper
49-
```csharp
50-
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });
51-
```
52-
53-
SqlCommand
54-
```csharp
55-
var sql = new SqlCommand("select Age = @Age, Id = @Id");
56-
sql.Parameters.AddWithValue("@Id", guid);
57-
sql.Parameters.AddWithValue("@Age", 42);
58-
sql.ExecuteNonQuery();
59-
```
60-
61-
### SQL003
62-
**Using 'Query' method is not optimal here**
63-
64-
Noncompliant Code Example:
65-
```csharp
66-
var dog = connection.Query<Dog>("select * from dogs").Single();
67-
```
68-
69-
Compliant Solution:
70-
```csharp
71-
var dog = connection.QuerySingle<Dog>("select * from dogs");
72-
```
73-
74-
https://github.com/StackExchange/Dapper#performance
75-
76-
### SQL004
77-
**Using 'QueryMultiple' method is not optimal here**
78-
79-
Noncompliant Code Example:
80-
```csharp
81-
var multi = connection.QueryMultiple("select * from dogs");
82-
var dogs = multi.Read<Dog>();
83-
```
84-
85-
Compliant Solution:
86-
```csharp
87-
var dogs = connection.Query<Dog>("select * from dogs");
88-
```
89-
90-
### SQL005
91-
92-
**Using 'SaveChanges' method in a loop can affect performance**
93-
94-
Noncompliant Code Example:
95-
```csharp
96-
var dbContext = new DbContext("test");
97-
for (int i = 0; i < 100; i++)
98-
{
99-
dbContext.Entities.Add(new Entity(i));
100-
s.SaveChanges();
101-
}
102-
```
103-
104-
Compliant Solution:
105-
```csharp
106-
var dbContext = new DbContext("test");
107-
for (int i = 0; i < 100; i++)
108-
{
109-
dbContext.Entities.Add(new Entity(i));
110-
}
111-
112-
s.SaveChanges();
113-
```
14+
| Rule\Library | Dapper | ADO.NET | Entity Framework |
15+
|----------------------------------------------------------------------------------------|:------------------:|:------------------:|:------------------:|
16+
| [SQL001: SQL type is not specified](rules/SQL001.md) | :heavy_check_mark: | | |
17+
| [SQL002: SQL parameters mismatch](rules/SQL002.md) | :heavy_check_mark: | :heavy_check_mark: | |
18+
| [SQL003: Using 'Query' method is not optimal here](rules/SQL003.md) | :heavy_check_mark: | | |
19+
| [SQL004: Using 'QueryMultiple' method is not optimal here](rules/SQL004.md) | :heavy_check_mark: | | |
20+
| [SQL005: Using 'SaveChanges' method in a loop can affect performance](rules/SQL005.md) | | | :heavy_check_mark: |

rules/SQL001.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### SQL001
2+
**SQL type is not specified**
3+
4+
Noncompliant Code Example:
5+
```csharp
6+
Query<Thing>("select * from Thing where Name = @Name", new { Name = abcde });
7+
```
8+
9+
Compliant Solution:
10+
```csharp
11+
Query<Thing>("select * from Thing where Name = @Name", new {Name = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true }});
12+
```
13+
14+
https://github.com/StackExchange/Dapper/blob/master/Readme.md#ansi-strings-and-varchar

rules/SQL002.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### SQL002
2+
**SQL parameters mismatch**
3+
4+
Noncompliant Code Example:
5+
Dapper
6+
```csharp
7+
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Id = guid });
8+
```
9+
10+
SqlCommand
11+
```csharp
12+
var sql = new SqlCommand("select Age = @Age, Id = @Id");
13+
sql.Parameters.AddWithValue("@Id", guid);
14+
sql.ExecuteNonQuery();
15+
```
16+
17+
Compliant Solution:
18+
Dapper
19+
```csharp
20+
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });
21+
```
22+
23+
SqlCommand
24+
```csharp
25+
var sql = new SqlCommand("select Age = @Age, Id = @Id");
26+
sql.Parameters.AddWithValue("@Id", guid);
27+
sql.Parameters.AddWithValue("@Age", 42);
28+
sql.ExecuteNonQuery();
29+
```

rules/SQL003.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### SQL003
2+
**Using 'Query' method is not optimal here**
3+
4+
Noncompliant Code Example:
5+
```csharp
6+
var dog = connection.Query<Dog>("select * from dogs").Single();
7+
```
8+
9+
Compliant Solution:
10+
```csharp
11+
var dog = connection.QuerySingle<Dog>("select * from dogs");
12+
```
13+
14+
https://github.com/StackExchange/Dapper#performance

rules/SQL004.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### SQL004
2+
**Using 'QueryMultiple' method is not optimal here**
3+
4+
Noncompliant Code Example:
5+
```csharp
6+
var multi = connection.QueryMultiple("select * from dogs");
7+
var dogs = multi.Read<Dog>();
8+
```
9+
10+
Compliant Solution:
11+
```csharp
12+
var dogs = connection.Query<Dog>("select * from dogs");
13+
```

rules/SQL005.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### SQL005
2+
3+
**Using 'SaveChanges' method in a loop can affect performance**
4+
5+
Noncompliant Code Example:
6+
```csharp
7+
var dbContext = new DbContext("test");
8+
for (int i = 0; i < 100; i++)
9+
{
10+
dbContext.Entities.Add(new Entity(i));
11+
s.SaveChanges();
12+
}
13+
```
14+
15+
Compliant Solution:
16+
```csharp
17+
var dbContext = new DbContext("test");
18+
for (int i = 0; i < 100; i++)
19+
{
20+
dbContext.Entities.Add(new Entity(i));
21+
}
22+
23+
s.SaveChanges();
24+
```

src/SqlAnalyzer.Net/DapperQueryMisuseAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class DapperQueryMisuseAnalyzer : DiagnosticAnalyzer
3333
DiagnosticSeverity.Warning,
3434
true,
3535
Description,
36-
"https://github.com/olsh/sql-analyzer-net#sql003");
36+
"https://github.com/olsh/sql-analyzer-net/rules/SQL003.md");
3737

3838
private static readonly Regex DapperQueryRegex = new Regex(
3939
@"^(?<MethodPrefix>Query|Read).*",

src/SqlAnalyzer.Net/DapperQueryMultipleMisuseAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class DapperQueryMultipleMisuseAnalyzer : DiagnosticAnalyzer
3232
DiagnosticSeverity.Warning,
3333
true,
3434
Description,
35-
"https://github.com/olsh/sql-analyzer-net#sql004");
35+
"https://github.com/olsh/sql-analyzer-net/rules/SQL004.md");
3636

3737
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
3838

src/SqlAnalyzer.Net/DapperStringParameterAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class DapperStringParameterAnalyzer : DiagnosticAnalyzer
3030
DiagnosticSeverity.Warning,
3131
true,
3232
Description,
33-
"https://github.com/olsh/sql-analyzer-net#sql001");
33+
"https://github.com/olsh/sql-analyzer-net/rules/SQL001.md");
3434

3535
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
3636

src/SqlAnalyzer.Net/EntityFrameworkSaveChangesInLoopAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class EntityFrameworkSaveChangesInLoopAnalyzer : DiagnosticAnalyzer
3131
DiagnosticSeverity.Warning,
3232
true,
3333
Description,
34-
"https://github.com/olsh/sql-analyzer-net#sql005");
34+
"https://github.com/olsh/sql-analyzer-net/rules/SQL005.md");
3535

3636
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
3737

src/SqlAnalyzer.Net/Rules/ParametersMatchingRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static class ParametersMatchingRule
2525
DiagnosticSeverity.Warning,
2626
true,
2727
Title,
28-
"https://github.com/olsh/sql-analyzer-net#sql002");
28+
"https://github.com/olsh/sql-analyzer-net/rules/SQL002.md");
2929

3030
internal static readonly DiagnosticDescriptor SqlParameterNotFoundRule = new DiagnosticDescriptor(
3131
DiagnosticId,

0 commit comments

Comments
 (0)