1- using System . Text ;
1+ using System . Globalization ;
2+ using System . Text ;
23using Npgsql ;
34
45namespace NpgsqlRest ;
@@ -7,33 +8,27 @@ public class RoutineSourceParameterFormatter : IRoutineSourceParameterFormatter
78{
89 public bool IsFormattable { get ; } = false ;
910
10- public string AppendCommandParameter ( NpgsqlRestParameter parameter , int index , int count )
11+ public string AppendCommandParameter ( NpgsqlRestParameter parameter , int index )
1112 {
12- var suffix = parameter . TypeDescriptor . IsCastToText ( ) ? $ "::{ parameter . TypeDescriptor . OriginalType } " : "" ;
13+ var suffix = parameter . TypeDescriptor . IsCastToText ( ) ?
14+ string . Concat ( Consts . DoubleColon , parameter . TypeDescriptor . OriginalType ) :
15+ string . Empty ;
16+
1317 if ( index == 0 )
1418 {
15- if ( count == 1 )
16- {
17- return parameter . ActualName is null ?
18- string . Concat ( "$1" , suffix , ")" ) :
19- string . Concat ( parameter . ActualName , "=>$1" , suffix , ")" ) ;
20- }
21- return parameter . ActualName is null ?
22- string . Concat ( "$1" , suffix ) :
23- string . Concat ( parameter . ActualName , "=>$1" , suffix ) ;
24- }
25- if ( index == count - 1 )
26- {
27- return parameter . ActualName is null ?
28- string . Concat ( "," , "$" , ( index + 1 ) . ToString ( ) , suffix , ")" ) :
29- string . Concat ( "," , parameter . ActualName , "=>$" , ( index + 1 ) . ToString ( ) , suffix , ")" ) ;
19+ return parameter . ActualName is null ?
20+ string . Concat ( Consts . FirstParam , suffix ) :
21+ string . Concat ( parameter . ActualName , Consts . FirstNamedParam , suffix ) ;
3022 }
23+
24+ var indexStr = ( index + 1 ) . ToString ( CultureInfo . InvariantCulture ) ;
25+
3126 return parameter . ActualName is null ?
32- string . Concat ( "," , "$" , ( index + 1 ) . ToString ( ) , suffix ) :
33- string . Concat ( "," , parameter . ActualName , "=>$" , ( index + 1 ) . ToString ( ) , suffix ) ;
27+ string . Concat ( Consts . Comma , Consts . Dollar , indexStr , suffix ) :
28+ string . Concat ( Consts . Comma , parameter . ActualName , Consts . NamedParam , indexStr , suffix ) ;
3429 }
3530
36- public string ? AppendEmpty ( ) => ")" ;
31+ public string ? AppendEmpty ( ) => Consts . CloseParenthesisStr ;
3732}
3833
3934public class RoutineSourceCustomTypesParameterFormatter : IRoutineSourceParameterFormatter
@@ -42,36 +37,64 @@ public class RoutineSourceCustomTypesParameterFormatter : IRoutineSourceParamete
4237
4338 public string FormatCommand ( Routine routine , NpgsqlParameterCollection parameters )
4439 {
45- var sb = new StringBuilder ( routine . Expression ) ;
40+ var sb = new StringBuilder ( routine . Expression , routine . Expression . Length + parameters . Count * 20 ) ;
4641 var count = parameters . Count ;
42+
43+ var culture = CultureInfo . InvariantCulture ;
44+
4745 for ( var i = 0 ; i < count ; i ++ )
4846 {
49- NpgsqlRestParameter parameter = ( NpgsqlRestParameter ) parameters [ i ] ;
50- var suffix = parameter . TypeDescriptor . IsCastToText ( ) ? $ "::{ parameter . TypeDescriptor . OriginalType } " : "" ;
47+ var parameter = ( NpgsqlRestParameter ) parameters [ i ] ;
48+ var typeDescriptor = parameter . TypeDescriptor ;
49+
50+ var suffix = typeDescriptor . IsCastToText ( ) ?
51+ string . Concat ( Consts . DoubleColon , typeDescriptor . OriginalType ) :
52+ string . Empty ;
53+
5154 if ( i > 0 )
5255 {
5356 sb . Append ( Consts . Comma ) ;
5457 }
55- if ( parameter . TypeDescriptor . CustomType is null )
58+
59+ var indexStr = ( i + 1 ) . ToString ( culture ) ;
60+
61+ if ( typeDescriptor . CustomType is null )
5662 {
57- sb . Append ( parameter . ActualName is null ?
58- string . Concat ( "$" , ( i + 1 ) . ToString ( ) , suffix ) :
59- string . Concat ( parameter . ActualName , "=>$" , ( i + 1 ) . ToString ( ) ) ) ;
63+ if ( parameter . ActualName is null )
64+ {
65+ sb . Append ( Consts . Dollar )
66+ . Append ( indexStr )
67+ . Append ( suffix ) ;
68+ }
69+ else
70+ {
71+ sb . Append ( parameter . ActualName )
72+ . Append ( Consts . NamedParam )
73+ . Append ( indexStr ) ;
74+ }
6075 }
6176 else
6277 {
63- if ( parameter . TypeDescriptor . CustomTypePosition == 1 )
78+ if ( typeDescriptor . CustomTypePosition == 1 )
6479 {
65- sb . Append ( parameter . TypeDescriptor . OriginalParameterName ) ;
66- sb . Append ( "=>row(" ) ;
80+ sb . Append ( typeDescriptor . OriginalParameterName )
81+ . Append ( Consts . OpenRow ) ;
6782 }
68- sb . Append ( string . Concat ( "$" , ( i + 1 ) . ToString ( ) , suffix ) ) ;
69- if ( i == count - 1 || parameter . TypeDescriptor . CustomTypePosition != ( ( NpgsqlRestParameter ) parameters [ i + 1 ] ) . TypeDescriptor . CustomTypePosition - 1 )
83+
84+ sb . Append ( Consts . Dollar )
85+ . Append ( indexStr )
86+ . Append ( suffix ) ;
87+
88+ if ( i == count - 1 ||
89+ typeDescriptor . CustomTypePosition !=
90+ ( ( NpgsqlRestParameter ) parameters [ i + 1 ] ) . TypeDescriptor . CustomTypePosition - 1 )
7091 {
71- sb . Append ( string . Concat ( ")::" , parameter . TypeDescriptor . CustomType ) ) ;
92+ sb . Append ( Consts . CloseRow )
93+ . Append ( typeDescriptor . CustomType ) ;
7294 }
7395 }
7496 }
97+
7598 sb . Append ( Consts . CloseParenthesis ) ;
7699 return sb . ToString ( ) ;
77100 }
0 commit comments