Skip to content

Commit d699a35

Browse files
authored
Fixes issue with SqlServer transport being unable to retrieve queue length (#4463)
1 parent cd053d7 commit d699a35

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/ServiceControl.Transports.SqlServer/SqlNameHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,15 @@ public static string Quote(string name)
1515

1616
return prefix + name.Replace(suffix, suffix + suffix) + suffix;
1717
}
18+
19+
public static string Unquote(string quotedString)
20+
{
21+
if (!quotedString.StartsWith(prefix) || !quotedString.EndsWith(suffix))
22+
{
23+
return quotedString;
24+
}
25+
return quotedString
26+
.Substring(prefix.Length, quotedString.Length - prefix.Length - suffix.Length).Replace(suffix + suffix, suffix);
27+
}
1828
}
1929
}

src/ServiceControl.Transports.SqlServer/SqlTable.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#nullable enable
22
namespace ServiceControl.Transports.SqlServer
33
{
4-
using System.Linq;
54

65
class SqlTable
76
{
87
SqlTable(string name, string schema, string? catalog)
98
{
9+
var unquotedSchema = SqlNameHelper.Unquote(schema);
10+
var unquotedName = SqlNameHelper.Unquote(name);
1011
var quotedName = SqlNameHelper.Quote(name);
1112
var quotedSchema = SqlNameHelper.Quote(schema);
1213
//HINT: The query approximates queue length value based on max and min
@@ -20,7 +21,7 @@ class SqlTable
2021
_fullTableName = $"{quotedSchema}.{quotedName}";
2122

2223
LengthQuery = $"""
23-
IF (EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{schema}' AND TABLE_NAME = '{name}'))
24+
IF (EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{unquotedSchema}' AND TABLE_NAME = '{unquotedName}'))
2425
SELECT isnull(cast(max([RowVersion]) - min([RowVersion]) + 1 AS int), 0) FROM {_fullTableName} WITH (nolock)
2526
ELSE
2627
SELECT -1;
@@ -32,7 +33,7 @@ SELECT isnull(cast(max([RowVersion]) - min([RowVersion]) + 1 AS int), 0) FROM {_
3233
_fullTableName = $"{quotedCatalog}.{quotedSchema}.{quotedName}";
3334

3435
LengthQuery = $"""
35-
IF (EXISTS (SELECT TABLE_NAME FROM {quotedCatalog}.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{schema}' AND TABLE_NAME = '{name}'))
36+
IF (EXISTS (SELECT TABLE_NAME FROM {quotedCatalog}.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{unquotedSchema}' AND TABLE_NAME = '{unquotedName}'))
3637
SELECT isnull(cast(max([RowVersion]) - min([RowVersion]) + 1 AS int), 0) FROM {_fullTableName} WITH (nolock)
3738
ELSE
3839
SELECT -1;
@@ -51,9 +52,9 @@ public static SqlTable Parse(string address, string defaultSchema)
5152
var parts = address.Split('@');
5253

5354
return new SqlTable(
54-
parts[0],
55-
parts.Length > 1 ? parts[1] : defaultSchema,
56-
parts.Length > 2 ? parts[2] : null
55+
name: parts[0],
56+
schema: parts.Length > 1 ? parts[1] : defaultSchema,
57+
catalog: parts.Length > 2 ? parts[2] : null
5758
);
5859
}
5960

0 commit comments

Comments
 (0)