1
1
using System . Collections . Generic ;
2
+ using System . Linq ;
2
3
using System . Text . RegularExpressions ;
3
- using StackExchange . Profiling . Internal ;
4
4
5
5
namespace StackExchange . Profiling . SqlFormatters
6
6
{
@@ -9,7 +9,6 @@ namespace StackExchange.Profiling.SqlFormatters
9
9
/// </summary>
10
10
public class InlineFormatter : ISqlFormatter
11
11
{
12
- private static readonly Regex ParamPrefixes = new Regex ( "[@:?].+" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
13
12
private static bool includeTypeInfo ;
14
13
15
14
/// <summary>
@@ -35,22 +34,21 @@ public string FormatSql(string commandText, List<SqlTimingParameter> parameters)
35
34
return commandText ;
36
35
}
37
36
38
- var originalCommandText = commandText ;
39
-
37
+ var paramValuesByName = new Dictionary < string , string > ( parameters . Count ) ;
40
38
foreach ( var p in parameters )
41
39
{
42
- // If the parameter doesn't have a prefix (@,:,etc), append one
43
- var name = ParamPrefixes . IsMatch ( p . Name )
44
- ? p . Name
45
- : Regex . Match ( originalCommandText , "([@:?])" + Regex . Escape ( p . Name ) , RegexOptions . IgnoreCase ) . Value ;
46
- if ( name . HasValue ( ) )
47
- {
48
- var value = GetParameterValue ( p ) ;
49
- commandText = Regex . Replace ( commandText , "(" + Regex . Escape ( name ) + ")([^0-9A-z]|$)" , m => value + m . Groups [ 2 ] , RegexOptions . IgnoreCase ) ;
50
- }
40
+ var trimmedName = p . Name . TrimStart ( '@' , ':' , '?' ) . ToLower ( ) ;
41
+ paramValuesByName [ trimmedName ] = GetParameterValue ( p ) ;
51
42
}
52
43
53
- return commandText ;
44
+ var regexPattern = "[@:?](?:" + string . Join ( "|" , paramValuesByName . Keys . Select ( Regex . Escape ) ) + ")(?![0-9a-z])" ;
45
+
46
+ return Regex . Replace (
47
+ commandText ,
48
+ regexPattern ,
49
+ m => paramValuesByName [ m . Value . Substring ( 1 ) . ToLower ( ) ] ,
50
+ RegexOptions . IgnoreCase
51
+ ) ;
54
52
}
55
53
56
54
/// <summary>
0 commit comments