|
1 | 1 | namespace VerifyTests.EntityFramework; |
2 | 2 |
|
3 | | -public class LogEntry |
| 3 | +public class LogEntry( |
| 4 | + string type, |
| 5 | + DbCommand command, |
| 6 | + CommandEndEventData data, |
| 7 | + Exception? exception = null) |
4 | 8 | { |
5 | | - public string Type { get; } |
6 | | - public DateTimeOffset StartTime { get; } |
7 | | - internal bool IsSqlServer { get; } |
8 | | - public TimeSpan Duration { get; } |
9 | | - public bool HasTransaction { get; } |
10 | | - public Exception? Exception { get; } |
11 | | - public IDictionary<string, object> Parameters { get; } |
12 | | - public string Text { get; } |
13 | | - |
14 | | - public LogEntry( |
15 | | - string type, |
16 | | - DbCommand command, |
17 | | - CommandEndEventData data, |
18 | | - Exception? exception = null) |
19 | | - { |
20 | | - Type = type; |
21 | | - StartTime = data.StartTime; |
22 | | - IsSqlServer = command.GetType().Name == "SqlCommand"; |
23 | | - Duration = data.Duration; |
24 | | - HasTransaction = command.Transaction != null; |
25 | | - Exception = exception; |
26 | | - |
27 | | - var parameters = command.Parameters.ToDictionary(); |
28 | | - var text = command.CommandText.Trim(); |
29 | | - NormalizeDescriptiveParameterNames(ref parameters, ref text); |
30 | | - Parameters = parameters; |
31 | | - Text = text; |
32 | | - } |
33 | | - |
34 | | - // When using descriptive parameter names, the generator produces: |
35 | | - // first occurrence: @Id (no suffix) |
36 | | - // subsequent: @Id1, @Id2, etc. |
37 | | - // This normalizes to 0-based numbering for duplicates: |
38 | | - // @Id → @Id0 (when @Id1 also exists) |
39 | | - // while leaving singletons without a suffix. |
40 | | - static void NormalizeDescriptiveParameterNames( |
41 | | - ref Dictionary<string, object> parameters, |
42 | | - ref string text) |
43 | | - { |
44 | | - // Dictionary keys are formatted as "@Name (Type)" |
45 | | - // Find parameter names (the @Name part) that need renaming |
46 | | - List<(string oldKey, string newKey, string oldParamName, string newParamName)>? renames = null; |
47 | | - |
48 | | - foreach (var key in parameters.Keys) |
49 | | - { |
50 | | - var spaceIndex = key.IndexOf(' '); |
51 | | - if (spaceIndex < 0) |
52 | | - { |
53 | | - continue; |
54 | | - } |
55 | | - |
56 | | - var paramName = key[..spaceIndex]; |
57 | | - var suffix = key[spaceIndex..]; |
58 | | - var paramName1 = paramName + "1"; |
59 | | - |
60 | | - // Check if paramName + "1" exists as a parameter name |
61 | | - var hasDuplicate = false; |
62 | | - foreach (var otherKey in parameters.Keys) |
63 | | - { |
64 | | - if (otherKey.StartsWith(paramName1) && |
65 | | - otherKey.Length > paramName1.Length && |
66 | | - otherKey[paramName1.Length] == ' ') |
67 | | - { |
68 | | - hasDuplicate = true; |
69 | | - break; |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - if (!hasDuplicate) |
74 | | - { |
75 | | - continue; |
76 | | - } |
77 | | - |
78 | | - renames ??= []; |
79 | | - var newParamName = paramName + "0"; |
80 | | - renames.Add((key, newParamName + suffix, paramName, newParamName)); |
81 | | - } |
82 | | - |
83 | | - if (renames is null) |
84 | | - { |
85 | | - return; |
86 | | - } |
87 | | - |
88 | | - foreach (var (oldKey, newKey, oldParamName, newParamName) in renames) |
89 | | - { |
90 | | - var value = parameters[oldKey]; |
91 | | - parameters.Remove(oldKey); |
92 | | - parameters[newKey] = value; |
93 | | - text = Regex.Replace(text, Regex.Escape(oldParamName) + @"(?!\w)", newParamName); |
94 | | - } |
95 | | - } |
| 9 | + public string Type { get; } = type; |
| 10 | + public DateTimeOffset StartTime { get; } = data.StartTime; |
| 11 | + internal bool IsSqlServer { get; } = command.GetType().Name == "SqlCommand"; |
| 12 | + public TimeSpan Duration { get; } = data.Duration; |
| 13 | + public bool HasTransaction { get; } = command.Transaction != null; |
| 14 | + public Exception? Exception { get; } = exception; |
| 15 | + public IDictionary<string, object> Parameters { get; } = command.Parameters.ToDictionary(); |
| 16 | + public string Text { get; } = command.CommandText.Trim(); |
96 | 17 | } |
0 commit comments