Skip to content

Commit 693b0aa

Browse files
authored
add [QueryColumns] and [StrictTypes] (#149)
* add [StrictBind(...)] * add netfx output * split column-bind and type-bind logic; incomplete * finish split
1 parent 7566e7d commit 693b0aa

File tree

101 files changed

+1308
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1308
-148
lines changed

docs/rules/DAP049.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# DAP049
2+
3+
When using `[QueryColumns(...)]`, the elements should be the member names on the corresponding type. This error simply means that Dapper
4+
could not find a member you specified. You can skip unwanted columns by passing `null` or `""`.

src/Dapper.AOT.Analyzers/CodeAnalysis/DapperAnalyzer.Diagnostics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static readonly DiagnosticDescriptor
5656
AmbiguousProperties = LibraryWarning("DAP046", "Ambiguous properties", "Properties have same name '{0}' after normalization and can be conflated"),
5757
AmbiguousFields = LibraryWarning("DAP047", "Ambiguous fields", "Fields have same name '{0}' after normalization and can be conflated"),
5858
MoveFromDbString = LibraryWarning("DAP048", "Move from DbString to DbValue", "DbString achieves the same as [DbValue] does. Use it instead."),
59+
UnableToBindQueryColumns = LibraryError("DAP049", "Unable to bind query columns", "Something went terribly wrong"),
5960

6061
// SQL parse specific
6162
GeneralSqlError = SqlWarning("DAP200", "SQL error", "SQL error: {0}"),
@@ -103,7 +104,6 @@ public static readonly DiagnosticDescriptor
103104
ConcatenatedStringSqlExpression = SqlWarning("DAP242", "Concatenated string usage", "Data values should not be concatenated into SQL string - use parameters instead"),
104105
InvalidDatepartToken = SqlWarning("DAP243", "Valid datepart token expected", "Date functions require a recognized datepart argument"),
105106
SelectAggregateMismatch = SqlWarning("DAP244", "SELECT aggregate mismatch", "SELECT has mixture of aggregate and non-aggregate expressions"),
106-
PseudoPositionalParameter = SqlError("DAP245", "Avoid SQL pseudo-positional parameter", "It is more like Dapper will incorrectly treat this literal as a pseudo-positional parameter")
107-
;
107+
PseudoPositionalParameter = SqlError("DAP245", "Avoid SQL pseudo-positional parameter", "It is more like Dapper will incorrectly treat this literal as a pseudo-positional parameter");
108108
}
109109
}

src/Dapper.AOT.Analyzers/CodeAnalysis/DapperAnalyzer.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,11 @@ internal static Location SharedParseArgsAndFlags(in ParseState ctx, IInvocationO
665665
}
666666
}
667667

668+
if (flags.HasAny(OperationFlags.Query) && (IsEnabled(ctx, op, Types.StrictTypesAttribute, out _)))
669+
{
670+
flags |= OperationFlags.StrictTypes;
671+
}
672+
668673
if (exitFirstFailure && flags.HasAny(OperationFlags.DoNotGenerate))
669674
{
670675
resultType = null;
@@ -757,24 +762,28 @@ enum ParameterMode
757762

758763
if (reportDiagnostic is not null)
759764
{
760-
foreach (var attrib in member.Member.GetAttributes())
765+
if (member.IsMapped)
761766
{
762-
switch (attrib.AttributeClass!.Name)
767+
foreach (var attrib in member.Member.GetAttributes())
763768
{
764-
case Types.RowCountHintAttribute:
765-
if (attrib.ConstructorArguments.Length != 0)
766-
{
767-
reportDiagnostic.Invoke(Diagnostic.Create(Diagnostics.RowCountHintShouldNotSpecifyValue,
768-
attrib.ApplicationSyntaxReference?.GetSyntax().GetLocation() ?? location));
769-
}
770-
break;
769+
switch (attrib.AttributeClass!.Name)
770+
{
771+
case Types.RowCountHintAttribute:
772+
if (attrib.ConstructorArguments.Length != 0)
773+
{
774+
reportDiagnostic.Invoke(Diagnostic.Create(Diagnostics.RowCountHintShouldNotSpecifyValue,
775+
attrib.ApplicationSyntaxReference?.GetSyntax().GetLocation() ?? location));
776+
}
777+
break;
778+
}
771779
}
772780
}
773781
}
774782
}
775783
}
776784
}
777785

786+
ImmutableArray<string> queryColumns = default;
778787
int? batchSize = null;
779788
foreach (var attrib in methodAttribs)
780789
{
@@ -813,6 +822,9 @@ enum ParameterMode
813822
batchSize = batchTmp;
814823
}
815824
break;
825+
case Types.QueryColumnsAttribute:
826+
queryColumns = ParseQueryColumns(attrib, reportDiagnostic, location);
827+
break;
816828
}
817829
}
818830
}
@@ -841,8 +853,8 @@ enum ParameterMode
841853
}
842854

843855

844-
return cmdProps.IsDefaultOrEmpty && rowCountHint <= 0 && rowCountHintMember is null && batchSize is null
845-
? null : new(rowCountHint, rowCountHintMember?.Member.Name, batchSize, cmdProps);
856+
return cmdProps.IsDefaultOrEmpty && rowCountHint <= 0 && rowCountHintMember is null && batchSize is null && queryColumns.IsDefault
857+
? null : new(rowCountHint, rowCountHintMember?.Member?.Name, batchSize, cmdProps, queryColumns);
846858
}
847859

848860
static void ValidateParameters(MemberMap? parameters, OperationFlags flags, Action<Diagnostic> onDiagnostic)

src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.Single.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static void WriteSingleImplementation(
9292
break;
9393
}
9494
}
95-
sb.AppendReader(resultType, readers);
95+
sb.AppendReader(resultType, readers, flags, additionalCommandState?.QueryColumns ?? default);
9696
}
9797
else if (flags.HasAny(OperationFlags.Execute))
9898
{

0 commit comments

Comments
 (0)