Skip to content

Commit

Permalink
Finals (#57)
Browse files Browse the repository at this point in the history
* - fix DbConnection detection on VB
- implement DbCommand detection

* dap021

* DAP022, DAP023, DAP024

* nit

* DAP029,DAP030,DAP031,DAP032

* tweaks

* intermediate get args working again

* fix output variable usage

* DAP236

* DAP237, DAP238, DAP239
fix test setup (shared fixture)

* more fixture tweaks

* better param detection

* DAP018

* clarify error ids
  • Loading branch information
mgravell authored Sep 25, 2023
1 parent 5a46c94 commit 739dfd2
Show file tree
Hide file tree
Showing 117 changed files with 2,132 additions and 797 deletions.
3 changes: 3 additions & 0 deletions docs/rules/DAP000.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DAP000

This message is mostly for debugging and monitoring. But hey, the generator is at least loading, so; yay, I guess?
5 changes: 5 additions & 0 deletions docs/rules/DAP018.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# DAP018

It looks like you're passing parameters data to something that doesn't actually need
parameters. If you *intended* to use the values from the parameters: fix that. Otherwise,
consider not passing the parameters, to make the intent clear.
28 changes: 28 additions & 0 deletions docs/rules/DAP021.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# DAP021

Database parameters need to have unique names within a single type (case-insensitive); when in doubt, `[DbValue]` allows the name of individual members
to be specified explicitly.

Bad:

``` csharp
public int x {get;set;}
public string X {get;set;}
```

Better:

``` csharp
public int x {get;set;}
[DbValue(Name = "y")]
public string X {get;set;}
```

Best:

``` csharp
public int X {get;set;}
public string Y {get;set;}
```

The final option is preferred because avoiding ambiguity in the name solves a lot more problems than just Dapper!
22 changes: 22 additions & 0 deletions docs/rules/DAP022.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# DAP022

Only one member on a type can capture the return value of a command; remove the duplicate.

Bad:

``` csharp
[DbValue(Direction = ParameterDirection.ReturnVaue)]
public int X {get;set;}

[DbValue(Direction = ParameterDirection.ReturnVaue)]
public int Y {get;set;}
```

Good:

``` csharp
[DbValue(Direction = ParameterDirection.ReturnVaue)]
public int X {get;set;}

public int Y {get;set;}
```
22 changes: 22 additions & 0 deletions docs/rules/DAP023.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# DAP023

Only one member on a type can capture the number of rows affected by a command; remove the duplicate.

Bad:

``` csharp
[RowCount]
public int X {get;set;}

[RowCount]
public int Y {get;set;}
```

Good:

``` csharp
[RowCount]
public int X {get;set;}

public int Y {get;set;}
```
20 changes: 20 additions & 0 deletions docs/rules/DAP024.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# DAP024

A single member cannot be marked with both `[DbValue]` and `[RowCount]`; split out the two purposes.

Bad:

``` csharp
[RowCount, DbValue(...)]
public int X {get;set;}
```

Good:

``` csharp
[RowCount]
public int X {get;set;}

[DbValue(...)]
public int Y {get;set;}
```
45 changes: 45 additions & 0 deletions docs/rules/DAP029.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# DAP029

You can indicate an expected row-count via either a constant method-level `[RowCountHint(...)]` or a member-level `[RowCountHint]` on the parameter type (for variable hints),
but you should not specify both. The member-level value will be preferred, so you should *probably* remove the method-level hint.

Bad:

``` csharp
[RowCountHint(42)]
public void SomeMethod()
{
connection.Query("somesql", new SomeArgsType
{
//
SizeHint = 15,
}
}

class SomeArgsType
{
// ...
[RowCountHint]
public int SizeHint {get;set;}
}
```

Good:

``` csharp
public void SomeMethod()
{
connection.Query("somesql", new SomeArgsType
{
//
SizeHint = 15,
}
}

class SomeArgsType
{
// ...
[RowCountHint]
public int SizeHint {get;set;}
}
```
28 changes: 28 additions & 0 deletions docs/rules/DAP030.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# DAP030

When a constant method-level `[RowCountHint(...)]` is specified, it *must* include a positive integer to use as the estimated row count.

Bad:

``` csharp
[RowCountHint]
public void SomeMethod()
{ ... }
```


Also bad:

``` csharp
[RowCountHint(-15)]
public void SomeMethod()
{ ... }
```

Good:

``` csharp
[RowCountHint(100)]
public void SomeMethod()
{ ... }
```
17 changes: 17 additions & 0 deletions docs/rules/DAP031.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# DAP031

When a member-level `[RowCountHint(...)]` is specified (to allow a per-call estimated row count hint), it *must not* include a value.

Bad:

``` csharp
[RowCountHint(43)]
public int RowCount {get;set;}
```

Good:

``` csharp
[RowCountHint]
public int RowCount {get;set;}
```
22 changes: 22 additions & 0 deletions docs/rules/DAP032.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# DAP032

Only a single member-level `[RowCountHint(...)]` can be specified (to allow a per-call estimated row count hint); remove the duplicate.

Bad:

``` csharp
[RowCountHint]
public int X {get;set;}

[RowCountHint]
public int Y {get;set;}
```

Good:

``` csharp
[RowCountHint]
public int X {get;set;}

public int Y {get;set;}
```
9 changes: 9 additions & 0 deletions docs/rules/DAP033.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# DAP033

`[CommandProperty<T>]` allows additional properties to be specified against a particular command type, for example:

``` csharp
[CommandProperty<OracleCommand>(nameof(OracleCommand.FetchSize), 1024)]
```

Unfortunately, the property you specified *doesn't seem to exist*. So... we can't do that, sorry. Check again? `nameof` is highly recommended.
4 changes: 4 additions & 0 deletions docs/rules/DAP034.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# DAP034

`[CommandProperty<T>]` allows additional properties to be specified against a particular command type, but it is intended for things that Dapper.AOT
*doesn't* handle internally. The option you've specified should is reserved for internal handling. If this presents a problem, [log an issue](https://github.com/DapperLib/DapperAOT/issues).
9 changes: 9 additions & 0 deletions docs/rules/DAP236.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# DAP236

Dapper tries to only use parameters that are included in your query; it does this using basic
pattern matching, but it looks like when analyzed properly: this parameter is not actually used.
This might be because the parameter name is used in a comment or string literal.

Try to restructure the query, or rename the parameter members, so that there is no confusion.


5 changes: 5 additions & 0 deletions docs/rules/DAP237.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# DAP237

It looks like some calculation you are performing can be shown to produce a divide-by-zero.
That's not something that mathematics can do in any meaningful ways, so: maybe try not to do
that.
24 changes: 24 additions & 0 deletions docs/rules/DAP238.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# DAP238

It looks like you're doing something the hard way...

Bad:

``` sql
a + 0
b / 1
c * 0
d >> 0
e ^ -1
```

Good:

``` sql
a -- anything plus zero is itself
b -- anything divided by one is itself
0 -- anything multiplied by zero is zero
d -- anything right-shifted by zero is zero
~e -- anything xor'd with a full mask is the bitwise complement
```

13 changes: 13 additions & 0 deletions docs/rules/DAP239.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# DAP239

It is not *quite* true that any operation involving a `null` yields `null`. There *are exceptions*, in particular
when a null literal (or something that is *very close* to a null literal) is involved.

Examples:

- `null >> 2`
- `2 >> null`
- `null & null` (needs at least one operand that is not hard null)

Unusually this only applies to null literals; if you had a parameter/local that had a `null` *value*,
the expression would evaluate to `null` as expected.
30 changes: 30 additions & 0 deletions docs/rules/DAP240.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# DAP240

Dapper uses fairly simple parameter detection, and it looks like an available member on your
parameters object is being detected as a parameter, but this conflicts with
a variable declared in your SQL. Consider renaming the local, or renaming/removing the member
on your type, to avoid this confusion.

Bad:

``` csharp
conn.Execute("""
declare @id int = 42;
-- etc using @id
""", new Customer { Id = 14, ... });
```

Here `Customer.Id` will be interpreted as a parameter, because `@id` was
seen in the command - however, this is actually the declaration of a local
variable, which will cause a conflict at execution.

Good:

``` csharp
conn.Execute("""
declare @newid int = 42;
-- etc using @newid
""", new Customer { Id = 14, ... });
```

After renaming the local, there is no longer an accidental conflict.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public override void Initialize(AnalysisContext context)
// we won't register anything; all we really want here is to report our supported diagnostics
}

/// <summary>
/// Whether to emit interceptors even if the "interceptors" feature is not detected
/// </summary>
public bool OverrideFeatureEnabled { get; set; }

/// <inheritdoc/>
public abstract void Initialize(IncrementalGeneratorInitializationContext context);

Expand Down
Loading

0 comments on commit 739dfd2

Please sign in to comment.