Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions docs/rules/DAP049.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# DAP049

When using `[QueryColumns(...)]`, the elements should be the member names on the corresponding type. This error simply means that Dapper
could not find a member you specified. You can skip unwanted columns by passing `null` or `""`.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static readonly DiagnosticDescriptor
AmbiguousProperties = LibraryWarning("DAP046", "Ambiguous properties", "Properties have same name '{0}' after normalization and can be conflated"),
AmbiguousFields = LibraryWarning("DAP047", "Ambiguous fields", "Fields have same name '{0}' after normalization and can be conflated"),
MoveFromDbString = LibraryWarning("DAP048", "Move from DbString to DbValue", "DbString achieves the same as [DbValue] does. Use it instead."),
UnableToBindQueryColumns = LibraryError("DAP049", "Unable to bind query columns", "Something went terribly wrong"),

// SQL parse specific
GeneralSqlError = SqlWarning("DAP200", "SQL error", "SQL error: {0}"),
Expand Down Expand Up @@ -103,7 +104,6 @@ public static readonly DiagnosticDescriptor
ConcatenatedStringSqlExpression = SqlWarning("DAP242", "Concatenated string usage", "Data values should not be concatenated into SQL string - use parameters instead"),
InvalidDatepartToken = SqlWarning("DAP243", "Valid datepart token expected", "Date functions require a recognized datepart argument"),
SelectAggregateMismatch = SqlWarning("DAP244", "SELECT aggregate mismatch", "SELECT has mixture of aggregate and non-aggregate expressions"),
PseudoPositionalParameter = SqlError("DAP245", "Avoid SQL pseudo-positional parameter", "It is more like Dapper will incorrectly treat this literal as a pseudo-positional parameter")
;
PseudoPositionalParameter = SqlError("DAP245", "Avoid SQL pseudo-positional parameter", "It is more like Dapper will incorrectly treat this literal as a pseudo-positional parameter");
}
}
34 changes: 23 additions & 11 deletions src/Dapper.AOT.Analyzers/CodeAnalysis/DapperAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,11 @@ internal static Location SharedParseArgsAndFlags(in ParseState ctx, IInvocationO
}
}

if (flags.HasAny(OperationFlags.Query) && (IsEnabled(ctx, op, Types.StrictTypesAttribute, out _)))
{
flags |= OperationFlags.StrictTypes;
}

if (exitFirstFailure && flags.HasAny(OperationFlags.DoNotGenerate))
{
resultType = null;
Expand Down Expand Up @@ -757,24 +762,28 @@ enum ParameterMode

if (reportDiagnostic is not null)
{
foreach (var attrib in member.Member.GetAttributes())
if (member.IsMapped)
{
switch (attrib.AttributeClass!.Name)
foreach (var attrib in member.Member.GetAttributes())
{
case Types.RowCountHintAttribute:
if (attrib.ConstructorArguments.Length != 0)
{
reportDiagnostic.Invoke(Diagnostic.Create(Diagnostics.RowCountHintShouldNotSpecifyValue,
attrib.ApplicationSyntaxReference?.GetSyntax().GetLocation() ?? location));
}
break;
switch (attrib.AttributeClass!.Name)
{
case Types.RowCountHintAttribute:
if (attrib.ConstructorArguments.Length != 0)
{
reportDiagnostic.Invoke(Diagnostic.Create(Diagnostics.RowCountHintShouldNotSpecifyValue,
attrib.ApplicationSyntaxReference?.GetSyntax().GetLocation() ?? location));
}
break;
}
}
}
}
}
}
}

ImmutableArray<string> queryColumns = default;
int? batchSize = null;
foreach (var attrib in methodAttribs)
{
Expand Down Expand Up @@ -813,6 +822,9 @@ enum ParameterMode
batchSize = batchTmp;
}
break;
case Types.QueryColumnsAttribute:
queryColumns = ParseQueryColumns(attrib, reportDiagnostic, location);
break;
}
}
}
Expand Down Expand Up @@ -841,8 +853,8 @@ enum ParameterMode
}


return cmdProps.IsDefaultOrEmpty && rowCountHint <= 0 && rowCountHintMember is null && batchSize is null
? null : new(rowCountHint, rowCountHintMember?.Member.Name, batchSize, cmdProps);
return cmdProps.IsDefaultOrEmpty && rowCountHint <= 0 && rowCountHintMember is null && batchSize is null && queryColumns.IsDefault
? null : new(rowCountHint, rowCountHintMember?.Member?.Name, batchSize, cmdProps, queryColumns);
}

static void ValidateParameters(MemberMap? parameters, OperationFlags flags, Action<Diagnostic> onDiagnostic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static void WriteSingleImplementation(
break;
}
}
sb.AppendReader(resultType, readers);
sb.AppendReader(resultType, readers, flags, additionalCommandState?.QueryColumns ?? default);
}
else if (flags.HasAny(OperationFlags.Execute))
{
Expand Down
Loading