44using System . Globalization ;
55using System . IO ;
66using System . Linq ;
7+ using System . Text ;
78using System . Xml . Linq ;
89#if NET
910using 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
0 commit comments