-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathDapperAnalyzer.cs
More file actions
1138 lines (1045 loc) · 50.6 KB
/
DapperAnalyzer.cs
File metadata and controls
1138 lines (1045 loc) · 50.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Dapper.Internal;
using Dapper.Internal.Roslyn;
using Dapper.SqlAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Data;
using System.Linq;
using System.Threading;
using static Dapper.Internal.Inspection;
namespace Dapper.CodeAnalysis;
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed partial class DapperAnalyzer : DiagnosticAnalyzer
{
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => DiagnosticsBase.All<Diagnostics>();
[System.Diagnostics.CodeAnalysis.SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1026:Enable concurrent execution", Justification = "Only disabled for debugging")]
public override void Initialize(AnalysisContext context)
{
#if !DEBUG
context.EnableConcurrentExecution();
#endif
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCompilationStartAction(OnCompilationStart);
}
private void OnCompilationStart(CompilationStartAnalysisContext context)
{
// per-run state (in particular, so we can have "first time only" diagnostics)
var state = new AnalyzerState(context);
context.RegisterOperationAction(state.OnOperation,
OperationKind.Invocation, // for Dapper method invocations
OperationKind.SimpleAssignment, // for assignments of query
OperationKind.ObjectCreation // for instantiating Command objects
);
// final actions
context.RegisterCompilationEndAction(state.OnCompilationEndAction);
}
private sealed class AnalyzerState
{
private readonly List<Location> _missedOpportunities = [];
private int _dapperHits; // this isn't a true count; it'll usually be 0 or 1, no matter the number of calls, because we stop tracking
private void OnDapperAotHit()
{
if (Thread.VolatileRead(ref _dapperHits) == 0 && IsDapperAotAvailable) // fast short-circuit if we know we're all good
{
lock (_missedOpportunities)
{
if (++_dapperHits == 1) // no point using Interlocked here; we need the lock anyway, to reset the list
{
// we only report about enabling Dapper.AOT if it isn't done anywhere
// (i.e. probably not known about), so: if they've *used* Dapper:
// we can stop tracking our failures
_missedOpportunities.Clear();
}
}
}
}
private void OnDapperAotMiss(Location location)
{
if (Thread.VolatileRead(ref _dapperHits) == 0 // fast short-circuit if we know we're all good
&& IsDapperAotAvailable) // don't warn if not available!
{
lock (_missedOpportunities)
{
if (_dapperHits == 0)
{
// see comment in OnDapperAotHit
_missedOpportunities.Add(location);
}
}
}
}
internal void OnCompilationEndAction(CompilationAnalysisContext ctx)
{
try
{
lock (_missedOpportunities)
{
var count = _missedOpportunities.Count;
if (count != 0)
{
var location = _missedOpportunities[0];
Location[]? additionalLocations = null;
if (count > 1)
{
additionalLocations = new Location[count - 1];
_missedOpportunities.CopyTo(1, additionalLocations, 0, count - 1);
}
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.DapperAotNotEnabled, location, additionalLocations, count));
}
}
}
catch (Exception ex)
{
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticsBase.UnknownError, null, ex.Message, ex.StackTrace));
}
}
public void OnOperation(OperationAnalysisContext ctx)
{
try
{
// we'll look for:
// - method calls with a parameter called "sql" or marked [Sql]
// - property assignments to "CommandText"
// - allocation of SqlCommand (i.e. `new SqlCommand(queryString, ...)` )
switch (ctx.Operation.Kind)
{
case OperationKind.Invocation when ctx.Operation is IInvocationOperation invoke:
int index = 0;
foreach (var p in invoke.TargetMethod.Parameters)
{
if (p.Type.SpecialType == SpecialType.System_String && (
p.Name == "sql" || GetDapperAttribute(p, Types.SqlAttribute) is not null))
{
if (Inspection.IsDapperMethod(invoke, out var dapperFlags))
{
ValidateDapperMethod(ctx, invoke.Arguments[index], dapperFlags);
}
else
{
ValidateParameterUsage(ctx, invoke.Arguments[index]);
}
}
index++;
}
break;
case OperationKind.SimpleAssignment when ctx.Operation is ISimpleAssignmentOperation assignment
&& assignment.Target is IPropertyReferenceOperation propRef:
if (propRef.Member is IPropertySymbol
{
Type.SpecialType: SpecialType.System_String,
IsStatic: false,
IsIndexer: false,
// check for DbConnection?
} prop)
{
if (prop.Name == "CommandText" && IsCommand(prop.ContainingType))
{
// write to CommandText
ValidatePropertyUsage(ctx, assignment.Value, true);
}
else if (GetDapperAttribute(prop, Types.SqlAttribute) is not null)
{
// write SQL to a string property
ValidatePropertyUsage(ctx, assignment.Value, false);
}
}
break;
case OperationKind.ObjectCreation when ctx.Operation is IObjectCreationOperation objectCreationOperation:
var ctor = objectCreationOperation.Constructor;
var receiverType = ctor?.ReceiverType;
if (ctor is not null && IsSqlClient(receiverType))
{
var sqlParam = ctor.Parameters.FirstOrDefault();
if (sqlParam is not null && sqlParam.Type.SpecialType == SpecialType.System_String && sqlParam.Name == "cmdText")
{
ValidateParameterUsage(ctx, objectCreationOperation.Arguments.First(), sqlUsage: objectCreationOperation);
}
}
break;
}
}
catch (Exception ex)
{
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticsBase.UnknownError, null, ex.Message, ex.StackTrace));
}
}
private void ValidateDapperMethod(in OperationAnalysisContext ctx, IOperation sqlSource, OperationFlags flags)
{
Action<Diagnostic> onDiagnostic = ctx.ReportDiagnostic;
if (ctx.Operation is not IInvocationOperation invoke) return;
// check the args
var parseState = new ParseState(ctx);
bool aotEnabled = IsEnabled(in parseState, invoke, Types.DapperAotAttribute, out var aotAttribExists);
if (!aotEnabled) flags |= OperationFlags.DoNotGenerate;
var location = SharedParseArgsAndFlags(parseState, invoke, ref flags, out var sql, out var argExpression, onDiagnostic, out _, exitFirstFailure: false);
// report our AOT readiness
if (aotEnabled)
{
OnDapperAotHit(); // all good for AOT
if (flags.HasAny(OperationFlags.NotAotSupported))
{
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.UnsupportedMethod, location, invoke.GetSignature()));
}
}
else if (!aotAttribExists && !flags.HasAny(OperationFlags.NotAotSupported))
{
// we might have been able to do more, but Dapper.AOT wasn't enabled
OnDapperAotMiss(location);
}
// check the types
// resultType can be an array, so let's unwrap it to determine actual type
var resultType = invoke.GetResultType(flags);
if (resultType is not null && IdentifyDbType(resultType, out _) is null && !IsSpecialCaseAllowedResultType(resultType)) // don't warn if handled as an inbuilt
{
var resultMap = MemberMap.CreateForResults(resultType, location);
if (resultMap is not null)
{
ValidateMembers(resultMap, onDiagnostic);
// check for single-row value-type usage
if (flags.HasAny(OperationFlags.SingleRow) && !flags.HasAny(OperationFlags.AtLeastOne)
&& resultMap.ElementType.IsValueType && !CouldBeNullable(resultMap.ElementType))
{
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.ValueTypeSingleFirstOrDefaultUsage, location, resultMap.ElementType.Name, invoke.TargetMethod.Name));
}
// check for constructors and factoryMethods on the materialized type
var ctorFault = ChooseConstructor(resultMap.ElementType, out var ctor) switch
{
ConstructorResult.FailMultipleExplicit => Diagnostics.ConstructorMultipleExplicit,
ConstructorResult.FailMultipleImplicit when aotEnabled => Diagnostics.ConstructorAmbiguous,
_ => null,
};
var factoryMethodFault = ChooseFactoryMethod(resultMap.ElementType, out var factoryMethod) switch
{
FactoryMethodResult.FailMultipleExplicit => Diagnostics.FactoryMethodMultipleExplicit,
_ => null,
};
// we cant use both ctor and factoryMethod, so reporting a warning that ctor is prioritized
if (ctor is not null && factoryMethod is not null)
{
var loc = factoryMethod?.Locations.FirstOrDefault() ?? resultMap.ElementType.Locations.FirstOrDefault();
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.ConstructorOverridesFactoryMethod, loc, resultMap.ElementType.GetDisplayString()));
}
if (ctorFault is not null)
{
var loc = ctor?.Locations.FirstOrDefault() ?? resultMap.ElementType.Locations.FirstOrDefault();
ctx.ReportDiagnostic(Diagnostic.Create(ctorFault, loc, resultMap.ElementType.GetDisplayString()));
}
else if (factoryMethodFault is not null)
{
var loc = factoryMethod?.Locations.FirstOrDefault() ?? resultMap.ElementType.Locations.FirstOrDefault();
ctx.ReportDiagnostic(Diagnostic.Create(factoryMethodFault, loc, resultMap.ElementType.GetDisplayString()));
}
else if (resultMap.Members.IsDefaultOrEmpty && IsPublicOrAssemblyLocal(resultType, parseState, out _))
{
// there are so settable members + there is no constructor to use
// (note: generator uses Inspection.GetMembers(type, dapperAotConstructor: constructor)
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.UserTypeNoSettableMembersFound, resultMap.Location, resultType.Name));
}
}
}
var parameters = MemberMap.CreateForParameters(argExpression);
if (aotEnabled)
{
_ = AdditionalCommandState.Parse(GetSymbol(parseState, invoke), parameters, onDiagnostic);
}
ValidateParameters(parameters, flags, onDiagnostic);
var args = SharedGetParametersToInclude(parameters, ref flags, sql, onDiagnostic, out var parseFlags);
ValidateSql(ctx, sqlSource, GetModeFlags(flags), SqlParameters.From(args), location);
ValidateSurroundingLinqUsage(ctx, flags);
}
static SqlParseInputFlags GetModeFlags(OperationFlags flags)
{
var modeFlags = SqlParseInputFlags.None;
// if (caseSensitive) modeFlags |= ModeFlags.CaseSensitive;
if ((flags & OperationFlags.BindResultsByName) != 0) modeFlags |= SqlParseInputFlags.ValidateSelectNames;
if ((flags & OperationFlags.SingleRow) != 0) modeFlags |= SqlParseInputFlags.SingleRow;
if ((flags & OperationFlags.AtMostOne) != 0) modeFlags |= SqlParseInputFlags.AtMostOne;
if ((flags & OperationFlags.KnownParameters) != 0) modeFlags |= SqlParseInputFlags.KnownParameters;
if ((flags & OperationFlags.Scalar) != 0)
{
modeFlags |= SqlParseInputFlags.ExpectQuery | SqlParseInputFlags.SingleRow | SqlParseInputFlags.SingleQuery | SqlParseInputFlags.SingleColumn;
}
else if ((flags & OperationFlags.Query) != 0)
{
modeFlags |= SqlParseInputFlags.ExpectQuery;
if ((flags & OperationFlags.QueryMultiple) == 0) modeFlags |= SqlParseInputFlags.SingleQuery;
}
else if ((flags & (OperationFlags.Execute)) != 0) modeFlags |= SqlParseInputFlags.ExpectNoQuery;
return modeFlags;
}
private void ValidateSurroundingLinqUsage(in OperationAnalysisContext ctx, OperationFlags flags)
{
if (flags.HasAny(OperationFlags.Query) && !flags.HasAny(OperationFlags.SingleRow)
&& ctx.Operation.Parent is IArgumentOperation arg
&& arg.Parent is IInvocationOperation parent && parent.TargetMethod is
{
IsExtensionMethod: true,
Parameters.Length: 1, Arity: 1, ContainingType:
{
Name: nameof(Enumerable),
ContainingType: null,
ContainingNamespace:
{
Name: "Linq",
ContainingNamespace:
{
Name: "System",
ContainingNamespace.IsGlobalNamespace: true
}
}
}
} target)
{
bool useSingleRowQuery = parent.TargetMethod.Name switch
{
nameof(Enumerable.First) => true,
nameof(Enumerable.Single) => true,
nameof(Enumerable.FirstOrDefault) => true,
nameof(Enumerable.SingleOrDefault) => true,
_ => false,
};
if (useSingleRowQuery)
{
var name = parent.TargetMethod.Name;
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.UseSingleRowQuery, parent.GetMemberLocation(),
"Query" + name, name));
}
else if (parent.TargetMethod.Name == nameof(Enumerable.ToList))
{
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.UseQueryAsList, parent.GetMemberLocation()));
}
}
}
private void ValidateParameterUsage(in OperationAnalysisContext ctx, IOperation sqlSource, IOperation? sqlUsage = null)
{
// TODO: check other parameters for special markers like command type?
var flags = SqlParseInputFlags.None;
ValidateSql(ctx, sqlSource, flags, SqlParameters.None, sqlUsageOperation: sqlUsage);
}
private void ValidatePropertyUsage(in OperationAnalysisContext ctx, IOperation sqlSource, bool isCommand)
{
var flags = SqlParseInputFlags.None;
if (isCommand)
{
// TODO: check other parameters for special markers like command type?
// check the method being invoked - scalar, reader, etc
}
ValidateSql(ctx, sqlSource, flags, SqlParameters.None);
}
private readonly SqlSyntax? DefaultSqlSyntax;
private readonly SqlParseInputFlags? DebugSqlFlags;
private readonly Compilation _compilation;
private bool? _isDapperAotAvailable;
internal bool IsDapperAotAvailable => _isDapperAotAvailable ?? LazyIsDapperAotAvailable();
private bool LazyIsDapperAotAvailable()
{
_isDapperAotAvailable = _compilation.Language == LanguageNames.CSharp && _compilation.GetTypeByMetadataName("Dapper." + Types.DapperAotAttribute) is not null;
return _isDapperAotAvailable.Value;
}
#pragma warning disable RS1012 // Start action has no registered actions
public AnalyzerState(CompilationStartAnalysisContext context)
#pragma warning restore RS1012 // Start action has no registered actions
{
_compilation = context.Compilation;
if (context.Options.TryGetSqlSyntax(out var syntax))
{
DefaultSqlSyntax = syntax;
}
if (context.Options.TryGetDebugModeFlags(out var flags))
{
DebugSqlFlags = flags;
}
}
private void ValidateSql(in OperationAnalysisContext ctx, IOperation sqlSource, SqlParseInputFlags flags,
ImmutableArray<SqlParameter> parameters, Location? location = null, IOperation? sqlUsageOperation = null)
{
var parseState = new ParseState(ctx);
// should we consider this as a syntax we can handle?
var syntax = IdentifySqlSyntax(parseState, sqlUsageOperation ?? ctx.Operation, out var caseSensitive) ?? DefaultSqlSyntax ?? SqlSyntax.General;
switch (syntax)
{
case SqlSyntax.SqlServer:
// other known types here
break;
default:
return;
}
if (syntax != SqlSyntax.SqlServer) return;
if (caseSensitive) flags |= SqlParseInputFlags.CaseSensitive;
// can we get the SQL itself?
if (!TryGetConstantValueWithSyntax(sqlSource, out string? sql, out var sqlSyntax, out var stringSyntaxKind))
{
DiagnosticDescriptor? descriptor = stringSyntaxKind switch
{
StringSyntaxKind.InterpolatedString
=> Diagnostics.InterpolatedStringSqlExpression,
StringSyntaxKind.ConcatenatedString or StringSyntaxKind.FormatString
=> Diagnostics.ConcatenatedStringSqlExpression,
_ => null
};
if (descriptor is not null)
{
ctx.ReportDiagnostic(Diagnostic.Create(descriptor, sqlSource.Syntax.GetLocation()));
}
return;
}
if (string.IsNullOrWhiteSpace(sql) || !CompiledRegex.WhitespaceOrReserved.IsMatch(sql)) return; // need non-trivial content to validate
location ??= ctx.Operation.Syntax.GetLocation();
if (DebugSqlFlags is not null)
{
var debugModeFlags = DebugSqlFlags.Value;
if ((debugModeFlags & SqlParseInputFlags.DebugMode) != 0)
{
// debug mode flags **replace** all other flags
flags = debugModeFlags;
}
else
{
// otherwise we *extend* the flags
flags |= debugModeFlags;
}
}
bool forgiveSyntaxErrors = false;
try
{
SqlParseOutputFlags parseFlags;
switch (syntax)
{
case SqlSyntax.GeneralWithAtParameters:
case SqlSyntax.SQLite:
case SqlSyntax.MySql:
forgiveSyntaxErrors = true; // we're just taking a punt, honestly
goto case SqlSyntax.SqlServer;
case SqlSyntax.SqlServer:
var proc = new OperationAnalysisContextTSqlProcessor(ctx, null, flags, location, sqlSyntax);
proc.Execute(sql!, parameters);
parseFlags = proc.Flags;
// paramMembers);
//parseFlags = proc.Flags;
//paramNames = (from var in proc.Variables
// where var.IsParameter
// select var.Name.StartsWith("@") ? var.Name.Substring(1) : var.Name
// ).ToImmutableHashSet();
//diagnostics = proc.DiagnosticsObject;
break;
default:
parseFlags = SqlParseOutputFlags.None;
break;
}
// if we're sure we understood the SQL (reliable and no syntax error), we can check our query expectations
// (note some other similar rules are enforced *inside* the parser - single-row, for example)
if ((parseFlags & (SqlParseOutputFlags.SyntaxError | SqlParseOutputFlags.Reliable)) == SqlParseOutputFlags.Reliable)
{
switch (flags & (SqlParseInputFlags.ExpectQuery | SqlParseInputFlags.ExpectNoQuery))
{
case SqlParseInputFlags.ExpectQuery when ((parseFlags & (SqlParseOutputFlags.Query | SqlParseOutputFlags.MaybeQuery)) == 0):
// definitely do not have a query
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.QueryCommandMissingQuery, location));
break;
case SqlParseInputFlags.ExpectNoQuery when ((parseFlags & SqlParseOutputFlags.Query) != 0):
// definitely have a query
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.ExecuteCommandWithQuery, location));
break;
}
}
}
catch (Exception ex)
{
if (!forgiveSyntaxErrors)
{
ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.GeneralSqlError, location, ex.Message));
}
}
}
}
// we want a common understanding of the setup between the analyzer and generator
internal static Location SharedParseArgsAndFlags(in ParseState ctx, IInvocationOperation op, ref OperationFlags flags, out string? sql,
out IOperation? argExpression, Action<Diagnostic>? reportDiagnostic, out ITypeSymbol? resultType, bool exitFirstFailure)
{
var callLocation = op.GetMemberLocation();
argExpression = null;
sql = null;
bool? buffered = null;
// check the args
foreach (var arg in op.Arguments)
{
switch (arg.Parameter?.Name)
{
case "sql":
if (TryGetConstantValueWithSyntax(arg, out string? s, out _, out _))
{
sql = s;
}
break;
case "buffered":
if (TryGetConstantValue(arg, out bool b))
{
buffered = b;
}
break;
case "param":
if (arg.Value is not IDefaultValueOperation)
{
var expr = arg.Value;
while (expr is IConversionOperation conv && expr.Type?.SpecialType == SpecialType.System_Object)
{
expr = conv.Operand;
}
if (expr.ConstantValue.HasValue && expr.ConstantValue.Value is null)
{
// another way of saying null, especially in VB
}
else
{
argExpression = expr;
flags |= OperationFlags.HasParameters;
}
}
break;
case "cnn":
case "commandTimeout":
case "transaction":
case "reader":
case "startIndex":
case "length":
case "returnNullIfFirstMissing":
case "concreteType" when arg.Value is IDefaultValueOperation || (arg.ConstantValue.HasValue && arg.ConstantValue.Value is null):
// nothing to do
break;
case "commandType":
if (TryGetConstantValue(arg, out int? ct))
{
switch (ct)
{
case null when !string.IsNullOrWhiteSpace(sql):
// if no spaces: interpret as stored proc, else: text
flags |= CompiledRegex.WhitespaceOrReserved.IsMatch(sql) ? OperationFlags.Text : OperationFlags.StoredProcedure;
break;
case null:
break; // flexible
case 1:
flags |= OperationFlags.Text;
break;
case 4:
flags |= OperationFlags.StoredProcedure;
break;
case 512:
flags |= OperationFlags.TableDirect;
break;
default: // treat as flexible
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.UnexpectedCommandType, arg.Syntax.GetLocation()));
break;
}
}
break;
default:
if (!flags.HasAny(OperationFlags.NotAotSupported | OperationFlags.DoNotGenerate))
{
flags |= OperationFlags.DoNotGenerate;
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.UnexpectedArgument, arg.Syntax.GetLocation(), arg.Parameter?.Name));
}
break;
}
}
if (exitFirstFailure && flags.HasAny(OperationFlags.DoNotGenerate))
{
resultType = null;
return callLocation;
}
// additional flags
if (IsEnabled(ctx, op, Types.CacheCommandAttribute, out _))
{
flags |= OperationFlags.CacheCommand;
}
if (!string.IsNullOrWhiteSpace(sql) && flags.HasAny(OperationFlags.Text)
&& IsEnabled(ctx, op, Types.IncludeLocationAttribute, out _))
{
flags |= OperationFlags.IncludeLocation;
}
if (flags.HasAny(OperationFlags.Query) && buffered.HasValue)
{
flags |= buffered.GetValueOrDefault() ? OperationFlags.Buffered : OperationFlags.Unbuffered;
}
resultType = op.GetResultType(flags);
if (flags.HasAny(OperationFlags.Query) && IdentifyDbType(resultType, out _) is null)
{
flags |= OperationFlags.BindResultsByName;
}
if (flags.HasAny(OperationFlags.Query) || flags.HasAll(OperationFlags.Execute | OperationFlags.Scalar))
{
bool resultTuple = Inspection.InvolvesTupleType(resultType, out var withNames), bindByNameDefined = false;
// tuples are positional by default
if (resultTuple && !IsEnabled(ctx, op, Types.BindTupleByNameAttribute, out bindByNameDefined))
{
flags &= ~OperationFlags.BindResultsByName;
}
if (flags.HasAny(OperationFlags.DoNotGenerate))
{
// extra checks specific to Dapper vanilla
if (resultTuple && withNames && IsEnabled(ctx, op, Types.BindTupleByNameAttribute, out _))
{ // Dapper vanilla supports bind-by-position for tuples; warn if bind-by-name is enabled
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.DapperLegacyBindNameTupleResults, callLocation));
}
}
else
{
// extra checks specific to DapperAOT
if (InvolvesGenericTypeParameter(resultType))
{
flags |= OperationFlags.DoNotGenerate;
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.GenericTypeParameter, callLocation, resultType!.ToDisplayString()));
}
else if (resultTuple)
{
if (!bindByNameDefined)
{
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.DapperAotAddBindTupleByName, callLocation));
}
// but not implemented currently!
flags |= OperationFlags.DoNotGenerate;
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.DapperAotTupleResults, callLocation));
}
else if (!IsPublicOrAssemblyLocal(resultType, ctx, out var failing))
{
flags |= OperationFlags.DoNotGenerate;
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.NonPublicType, callLocation, failing!.ToDisplayString(), NameAccessibility(failing)));
}
}
}
if (flags.HasAny(OperationFlags.Query) && (IsEnabled(ctx, op, Types.StrictTypesAttribute, out _)))
{
flags |= OperationFlags.StrictTypes;
}
if (exitFirstFailure && flags.HasAny(OperationFlags.DoNotGenerate))
{
resultType = null;
return callLocation;
}
// additional parameter checks
if (flags.HasAny(OperationFlags.HasParameters))
{
var argLocation = argExpression?.Syntax.GetLocation() ?? callLocation;
var paramType = argExpression?.Type;
bool paramTuple = Inspection.InvolvesTupleType(paramType, out _);
if (flags.HasAny(OperationFlags.DoNotGenerate))
{
// extra checks specific to Dapper vanilla
if (reportDiagnostic is not null)
{
if (paramTuple)
{
reportDiagnostic.Invoke(Diagnostic.Create(Diagnostics.DapperLegacyTupleParameter, argLocation));
}
}
}
else
{
// extra checks specific to DapperAOT
if (paramTuple)
{
if (IsEnabled(ctx, op, Types.BindTupleByNameAttribute, out var defined))
{
flags |= OperationFlags.BindTupleParameterByName;
}
if (!defined)
{
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.DapperAotAddBindTupleByName, argLocation));
}
// but not implemented currently!
flags |= OperationFlags.DoNotGenerate;
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.DapperAotTupleParameter, argLocation));
}
else if (InvolvesGenericTypeParameter(paramType))
{
flags |= OperationFlags.DoNotGenerate;
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.GenericTypeParameter, argLocation, paramType!.ToDisplayString()));
}
else if (IsMissingOrObjectOrDynamic(paramType) || IsDynamicParameters(paramType, out _))
{
flags |= OperationFlags.DoNotGenerate;
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.UntypedParameter, argLocation));
}
else if (!IsPublicOrAssemblyLocal(paramType, ctx, out var failing))
{
flags |= OperationFlags.DoNotGenerate;
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.NonPublicType, argLocation, failing!.ToDisplayString(), NameAccessibility(failing)));
}
}
}
return callLocation;
}
enum ParameterMode
{
None, All, Defer, Filter
}
internal static AdditionalCommandState? SharedGetAdditionalCommandState(ISymbol target, MemberMap? parameters, Action<Diagnostic>? reportDiagnostic)
{
var methodAttribs = target.GetAttributes();
if (methodAttribs.IsDefaultOrEmpty && parameters is not { Members.IsDefaultOrEmpty: false }) return null;
int cmdPropsCount = 0;
int rowCountHint = 0;
ElementMember? rowCountHintMember = null;
var location = target.Locations.FirstOrDefault();
if (parameters is not null)
{
foreach (var member in parameters.Members)
{
if (member.IsRowCountHint)
{
if (rowCountHintMember is not null)
{
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.RowCountHintDuplicated, member.GetLocation(Types.RowCountHintAttribute),
additionalLocations: rowCountHintMember.GetValueOrDefault().AsAdditionalLocations(Types.RowCountHintAttribute),
messageArgs: [member.CodeName]));
}
rowCountHintMember = member;
if (reportDiagnostic is not null)
{
if (member.IsMapped)
{
foreach (var attrib in member.Member.GetAttributes())
{
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)
{
if (IsDapperAttribute(attrib))
{
switch (attrib.AttributeClass!.Name)
{
case Types.RowCountHintAttribute:
if (attrib.ConstructorArguments.Length == 1 && attrib.ConstructorArguments[0].Value is int i
&& i > 0)
{
if (rowCountHintMember is null)
{
rowCountHint = i;
}
else
{
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.RowCountHintRedundant,
attrib.ApplicationSyntaxReference?.GetSyntax().GetLocation() ?? location,
rowCountHintMember.GetValueOrDefault().CodeName));
}
}
else
{
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.RowCountHintInvalidValue,
attrib.ApplicationSyntaxReference?.GetSyntax().GetLocation() ?? location,
location));
}
break;
case Types.CommandPropertyAttribute:
cmdPropsCount++;
break;
case Types.BatchSizeAttribute:
if (attrib.ConstructorArguments.Length == 1 && attrib.ConstructorArguments[0].Value is int batchTmp)
{
batchSize = batchTmp;
}
break;
case Types.QueryColumnsAttribute:
queryColumns = ParseQueryColumns(attrib, reportDiagnostic, location);
break;
}
}
}
ImmutableArray<CommandProperty> cmdProps;
if (cmdPropsCount != 0)
{
var builder = ImmutableArray.CreateBuilder<CommandProperty>(cmdPropsCount);
foreach (var attrib in methodAttribs)
{
if (IsDapperAttribute(attrib) && attrib.AttributeClass!.Name == Types.CommandPropertyAttribute
&& attrib.AttributeClass.Arity == 1
&& attrib.AttributeClass.TypeArguments[0] is INamedTypeSymbol cmdType
&& attrib.ConstructorArguments.Length == 2
&& attrib.ConstructorArguments[0].Value is string name
&& attrib.ConstructorArguments[1].Value is object value)
{
builder.Add(new(cmdType, name, value, location));
}
}
cmdProps = builder.ToImmutable();
}
else
{
cmdProps = ImmutableArray<CommandProperty>.Empty;
}
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)
{
if (parameters is null) return;
var usingVanillaDapperMode = flags.HasAny(OperationFlags.DoNotGenerate); // using vanilla Dapper mode
if (usingVanillaDapperMode)
{
if (parameters.Members.Any(s => s.IsCancellation) || IsCancellationToken(parameters.ElementType))
{
onDiagnostic(Diagnostic.Create(Diagnostics.CancellationNotSupported, parameters.Location));
}
}
var isFirstCancellation = true;
foreach (var member in parameters.Members)
{
ValidateCancellationTokenParameter(member);
ValidateDbStringParameter(member);
}
void ValidateDbStringParameter(ElementMember member)
{
if (usingVanillaDapperMode)
{
// reporting ONLY in Dapper AOT
return;
}
if (member.DapperSpecialType == DapperSpecialType.DbString)
{
onDiagnostic(Diagnostic.Create(Diagnostics.MoveFromDbString, member.GetLocation()));
}
}
void ValidateCancellationTokenParameter(ElementMember member)
{
if (!usingVanillaDapperMode && member.IsCancellation)
{
if (isFirstCancellation) isFirstCancellation = false;
else onDiagnostic(Diagnostic.Create(Diagnostics.CancellationDuplicated, member.GetLocation()));
}
}
}
static void ValidateMembers(MemberMap memberMap, Action<Diagnostic> onDiagnostic)
{
if (memberMap.Members.Length == 0)
{
return;
}
var normalizedPropertyNames = new Dictionary<string, List<Location>>();
var normalizedFieldNames = new Dictionary<string, List<Location>>();
foreach (var member in memberMap!.Members)
{
ValidateMember(member, onDiagnostic);
// build collection of duplicate normalized memberNames with memberLocations
Location? memberLoc;
if ((memberLoc = member.GetLocation()) is not null)
{
var normalizedName = StringHashing.Normalize(member.CodeName);
switch (member.SymbolKind)
{
case SymbolKind.Property:
if (!normalizedPropertyNames.ContainsKey(normalizedName)) normalizedPropertyNames[normalizedName] = new();
normalizedPropertyNames[normalizedName].Add(memberLoc);
break;
case SymbolKind.Field:
if (!normalizedFieldNames.ContainsKey(normalizedName)) normalizedFieldNames[normalizedName] = new();
normalizedFieldNames[normalizedName].Add(memberLoc);
break;
}
}
}
ReportNormalizedMembersNamesCollisions(normalizedFieldNames, Diagnostics.AmbiguousFields);
ReportNormalizedMembersNamesCollisions(normalizedPropertyNames, Diagnostics.AmbiguousProperties);
void ReportNormalizedMembersNamesCollisions(Dictionary<string, List<Location>> nameMemberLocations, DiagnosticDescriptor diagnosticDescriptor)
{
if (nameMemberLocations.Count == 0) return;
foreach (var entry in nameMemberLocations)
{
if (entry.Value?.Count <= 1)
{
continue;
}
onDiagnostic?.Invoke(Diagnostic.Create(diagnosticDescriptor,
location: entry.Value.First(),
additionalLocations: entry.Value.Skip(1),
messageArgs: entry.Key));
}
}
}
static void ValidateMember(ElementMember member, Action<Diagnostic>? reportDiagnostic)
{
ValidateColumnAttribute();
void ValidateColumnAttribute()
{
if (member.HasDbValueAttribute && member.ColumnAttributeData.IsCorrectUsage)
{
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.ParameterNameOverrideConflict,
location: member.GetLocation(Types.DbValueAttribute),
additionalLocations: member.AsAdditionalLocations(Types.ColumnAttribute, allowNonDapperLocations: true),
messageArgs: member.DbName));
}
if (member.ColumnAttributeData.ColumnAttribute == ColumnAttributeData.ColumnAttributeState.Specified
&& member.ColumnAttributeData.UseColumnAttribute == ColumnAttributeData.UseColumnAttributeState.NotSpecified)
{
reportDiagnostic?.Invoke(Diagnostic.Create(Diagnostics.UseColumnAttributeNotSpecified,
location: member.GetLocation(Types.ColumnAttribute, allowNonDapperLocations: true)));
}
}
}
internal static ImmutableArray<ElementMember>? SharedGetParametersToInclude(MemberMap? map, ref OperationFlags flags, string? sql, Action<Diagnostic>? reportDiagnostic, out SqlParseOutputFlags parseFlags)
{
SortedDictionary<string, ElementMember>? byDbName = null;
var filter = ImmutableHashSet<string>.Empty;
ParameterMode mode;
if (flags.HasAny(OperationFlags.StoredProcedure | OperationFlags.TableDirect))
{
parseFlags = flags.HasAny(OperationFlags.StoredProcedure) ? SqlParseOutputFlags.MaybeQuery : SqlParseOutputFlags.Query;
mode = ParameterMode.All;
}
else
{
parseFlags = SqlParseOutputFlags.MaybeQuery;
// if command-type or command is not known statically: defer decision
if (!flags.HasAny(OperationFlags.Text) || string.IsNullOrWhiteSpace(sql))
{
mode = ParameterMode.Defer;