Skip to content

Commit

Permalink
reserve known DbCommand properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Aug 14, 2023
1 parent 80d5203 commit b70f245
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Dapper.AOT.Analyzers/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ DAP030 | Library | Error | Diagnostics
DAP031 | Library | Error | Diagnostics
DAP032 | Library | Error | Diagnostics
DAP033 | Library | Warning | Diagnostics
DAP034 | Library | Warning | Diagnostics
DAP100 | Library | Error | Diagnostics
DAP101 | Library | Error | Diagnostics
DAP102 | Library | Error | Diagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,12 @@ private static void WriteCommandProperties(SourceProductionContext ctx, CodeWrit
bool firstForType = true; // defer starting the if-test in case all invalid
foreach (var prop in grp)
{
if (!HasPublicSettableInstanceMember(type, prop.Name))
if (IsReserved(prop.Name))
{
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.CommandPropertyReserved, prop.Location, prop.Name));
continue;
}
else if (!HasPublicSettableInstanceMember(type, prop.Name))
{
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.CommandPropertyNotFound, prop.Location, type.Name, prop.Name));
continue;
Expand Down Expand Up @@ -1259,6 +1264,28 @@ static bool HasPublicSettableInstanceMember(ITypeSymbol type, string name)
}
return false;
}

static bool IsReserved(string name)
{
switch (name)
{
// pretty much everything on DbCommand
case nameof(DbCommand.CommandText):
case nameof(DbCommand.CommandTimeout):
case nameof(DbCommand.CommandType):
case nameof(DbCommand.Connection):
case nameof(DbCommand.Parameters):
case nameof(DbCommand.Site):
case nameof(DbCommand.Transaction):
case nameof(DbCommand.UpdatedRowSource):
// see SpecialCommandFlags
case "InitialLONGFetchSize":
case "BindByName":
return true;
default:
return false;
}
}
}

private static void WriteRowFactory(CodeWriter sb, ITypeSymbol type, int index)
Expand Down
4 changes: 3 additions & 1 deletion src/Dapper.AOT.Analyzers/CodeAnalysis/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ internal static readonly DiagnosticDescriptor
MemberRowCountHintDuplicated = new("DAP032", "Member-level row-count hint duplicated",
"Only a single member should be marked [EstimatedRowCount]", Category.Library, DiagnosticSeverity.Error, true),
CommandPropertyNotFound = new("DAP033", "Command property not found",
"Command property {0}.{1} was not found or was not valid", Category.Library, DiagnosticSeverity.Warning, true),
"Command property {0}.{1} was not found or was not valid; attribute will be ignored", Category.Library, DiagnosticSeverity.Warning, true),
CommandPropertyReserved = new("DAP034", "Command property reserved",
"Command property {1} is reserved for internal usage; attribute will be ignored", Category.Library, DiagnosticSeverity.Warning, true),

// TypeAccessor
TypeAccessorCollectionTypeNotAllowed = new("DAP100", "TypeAccessors does not allow collection types",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Hidden DAP000 L1 C1
Dapper.AOT handled 6 of 6 enabled call-sites using 5 interceptors, 7 commands and 1 readers

Warning DAP033 Interceptors/CommandProperties.input.cs L12 C17
Command property SqlCommand.InvalidName was not found or was not valid
Command property SqlCommand.InvalidName was not found or was not valid; attribute will be ignored

Warning DAP033 Interceptors/CommandProperties.input.cs L12 C17
Command property SqlCommand.InvalidName was not found or was not valid
Command property SqlCommand.InvalidName was not found or was not valid; attribute will be ignored
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Hidden DAP000 L1 C1
Dapper.AOT handled 6 of 6 enabled call-sites using 5 interceptors, 7 commands and 1 readers

Warning DAP033 Interceptors/CommandProperties.input.cs L12 C17
Command property SqlCommand.InvalidName was not found or was not valid
Command property SqlCommand.InvalidName was not found or was not valid; attribute will be ignored

Warning DAP033 Interceptors/CommandProperties.input.cs L12 C17
Command property SqlCommand.InvalidName was not found or was not valid
Command property SqlCommand.InvalidName was not found or was not valid; attribute will be ignored

0 comments on commit b70f245

Please sign in to comment.