Skip to content

Commit 91f7c6e

Browse files
committed
Improve UDT debug formatting
1 parent 2c30c4a commit 91f7c6e

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

src/Dibix/Diagnostics/TSqlDebugStatementFormatter.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Globalization;
55
using System.IO;
66
using System.Linq;
7+
using System.Text;
78
using System.Xml.Linq;
89
#if NET
910
using Microsoft.Data.SqlClient.Server;
@@ -66,8 +67,10 @@ string FormatParameterDeclaration(string declaration, int maxDeclarationLength,
6667
string debugStatements = String.Join(Environment.NewLine, parameterDeclarations.Select(x => FormatParameterDeclaration(x.Declaration, maxDeclarationLength, x.Parameter)));
6768
if (udtInitializers.Length > 0)
6869
{
69-
debugStatements = $@"{debugStatements}
70-
{udtInitializers}";
70+
debugStatements = $"""
71+
{debugStatements}
72+
{udtInitializers}
73+
""";
7174
}
7275

7376
return debugStatements;
@@ -83,6 +86,24 @@ string[] CollectValues(SqlDataRecord record)
8386
return strValues;
8487
}
8588

89+
string FormatRow(string[] values, IList<int> maxLengths)
90+
{
91+
StringBuilder sb = new StringBuilder();
92+
for (int i = 0; i < values.Length; i++)
93+
{
94+
string value = values[i];
95+
sb.Append(value);
96+
97+
if (i + 1 < values.Length)
98+
{
99+
sb.Append(',');
100+
sb.Append(new string(' ', maxLengths[i]));
101+
}
102+
}
103+
104+
return $"({sb})";
105+
}
106+
86107
string GenerateUdtInitializer(ParameterDescriptor parameter)
87108
{
88109
if (parameter.Value is not StructuredType structuredType)
@@ -95,8 +116,10 @@ string GenerateUdtInitializer(ParameterDescriptor parameter)
95116

96117
IList<int> maxLengths = metadata.Select((x, i) => Math.Max(rows.Max(y => y[i].Length), x.Name.Length + 2)).ToArray();
97118
string padding = new string(' ', parameter.Name.Length);
98-
string initializer = $@"INSERT INTO @{parameter.Name} ({String.Join(", ", metadata.Select((x, i) => $"[{x.Name}]".PadRight(maxLengths[i])))})
99-
{padding} VALUES {String.Join($"{Environment.NewLine}{padding} , ", rows.Select(x => $"({String.Join(", ", x.Select((y, i) => y.PadRight(maxLengths[i])))})"))}";
119+
string initializer = $"""
120+
INSERT INTO @{parameter.Name} ({String.Join(", ", metadata.Select((x, i) => $"[{x.Name}]".PadRight(maxLengths[i])))})
121+
{padding} VALUES {String.Join($"{Environment.NewLine}{padding} , ", rows.Select(x => FormatRow(x, maxLengths)))}
122+
""";
100123
return initializer;
101124
}
102125

tests/Dibix.Http.Server.Tests/HttpActionInvokerTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ 2 II
180180
DECLARE @b [x]
181181
DECLARE @c NVARCHAR(5) = N'value'
182182
INSERT INTO @b ([intValue], [stringValue])
183-
VALUES (1 , N'I' )
184-
, (2 , N'II' )
183+
VALUES (1, N'I')
184+
, (2, N'II')
185185
186186
EXEC x @a = @a
187187
, @b = @b
@@ -194,8 +194,8 @@ DECLARE @a VARBINARY(MAX) = 0x01
194194
DECLARE @b [x]
195195
DECLARE @c NVARCHAR(5) = N'value'
196196
INSERT INTO @b ([intValue], [stringValue])
197-
VALUES (1 , N'I' )
198-
, (2 , N'II' )
197+
VALUES (1, N'I')
198+
, (2, N'II')
199199
200200
EXEC x @a = @a
201201
, @b = @b
@@ -240,8 +240,8 @@ 2 II
240240
DECLARE @b [x]
241241
DECLARE @c NVARCHAR(5) = N'value'
242242
INSERT INTO @b ([intValue], [stringValue])
243-
VALUES (1 , N'I' )
244-
, (2 , N'II' )", ex.TSqlDebugStatement);
243+
VALUES (1, N'I')
244+
, (2, N'II')", ex.TSqlDebugStatement);
245245
Assert.AreEqual(@"Dibix.DatabaseAccessException: Oops
246246
CommandType: Text
247247
CommandText: <Inline>
@@ -250,8 +250,8 @@ DECLARE @a VARBINARY(MAX) = 0x01
250250
DECLARE @b [x]
251251
DECLARE @c NVARCHAR(5) = N'value'
252252
INSERT INTO @b ([intValue], [stringValue])
253-
VALUES (1 , N'I' )
254-
, (2 , N'II' )", GetExceptionTextWithoutCallStack(ex));
253+
VALUES (1, N'I')
254+
, (2, N'II')", GetExceptionTextWithoutCallStack(ex));
255255
}
256256
}
257257
private static void Invoke_DML_WithSqlException_WrappedExceptionIsThrown_Target(IDatabaseAccessorFactory databaseAccessorFactory) => throw CreateException(errorInfoNumber: default, errorMessage: "Oops", CommandType.Text, commandText: "x", (InputParameterVisitor visitParameter) =>

0 commit comments

Comments
 (0)