@@ -15,6 +15,7 @@ class CommandBuilder : ICommandBuilder
1515 private readonly ISchemaProvider _schemaProvider ;
1616 private readonly Dictionary < ParameterTemplate , object > _parameters = new Dictionary < ParameterTemplate , object > ( ) ;
1717 private readonly StringBuilder _text ;
18+ private readonly string _parameterSuffix ;
1819
1920 public string Joins { get ; set ; }
2021
@@ -24,23 +25,24 @@ public CommandBuilder(ISchemaProvider schemaProvider)
2425 _schemaProvider = schemaProvider ;
2526 }
2627
27- public CommandBuilder ( string text , ISchemaProvider schemaProvider )
28+ public CommandBuilder ( string text , ISchemaProvider schemaProvider , int bulkIndex )
2829 {
2930 _text = new StringBuilder ( text ) ;
3031 _schemaProvider = schemaProvider ;
32+ _parameterSuffix = ( bulkIndex >= 0 ) ? "_c" + bulkIndex : string . Empty ;
3133 }
3234
3335 public ParameterTemplate AddParameter ( object value , Column column )
3436 {
35- string name = _schemaProvider . NameParameter ( "p" + Interlocked . Increment ( ref _number ) ) ;
37+ string name = _schemaProvider . NameParameter ( "p" + Interlocked . Increment ( ref _number ) + _parameterSuffix ) ;
3638 var parameterTemplate = new ParameterTemplate ( name , column ) ;
3739 _parameters . Add ( parameterTemplate , value ) ;
3840 return parameterTemplate ;
3941 }
4042
4143 public ParameterTemplate AddParameter ( string name , DbType dbType , object value )
4244 {
43- name = _schemaProvider . NameParameter ( name ) ;
45+ name = _schemaProvider . NameParameter ( name + _parameterSuffix ) ;
4446 var parameterTemplate = new ParameterTemplate ( name , dbType , 0 ) ;
4547 _parameters . Add ( parameterTemplate , value ) ;
4648 return parameterTemplate ;
@@ -76,7 +78,7 @@ public IDbCommand GetCommand(IDbConnection connection)
7678 {
7779 var command = connection . CreateCommand ( ) ;
7880 command . CommandText = Text ;
79- SetParameters ( command ) ;
81+ SetParameters ( command , string . Empty ) ;
8082 return command ;
8183 }
8284
@@ -103,11 +105,17 @@ public CommandTemplate GetCommandTemplate(Table table)
103105 return new CommandTemplate ( _text . ToString ( ) , _parameters . Keys . ToArray ( ) , new Dictionary < string , int > ( index , HomogenizedEqualityComparer . DefaultInstance ) ) ;
104106 }
105107
106- private void SetParameters ( IDbCommand command )
108+ private void SetParameters ( IDbCommand command , string suffix )
107109 {
108- if ( _parameters . Any ( kvp => kvp . Value is IRange ) || _parameters . Any ( kvp => kvp . Value is IEnumerable && ! ( kvp . Value is string ) ) )
110+ SetParameters ( command , _parameters ) ;
111+ }
112+
113+ private static void SetParameters ( IDbCommand command , IEnumerable < KeyValuePair < ParameterTemplate , object > > parameters )
114+ {
115+ var parameterList = parameters . ToList ( ) ;
116+ if ( parameterList . Any ( kvp => kvp . Value is IRange ) || parameterList . Any ( kvp => kvp . Value is IEnumerable && ! ( kvp . Value is string ) ) )
109117 {
110- foreach ( var pair in _parameters )
118+ foreach ( var pair in parameterList )
111119 {
112120 foreach ( var parameter in CreateParameterComplex ( pair . Key , pair . Value , command ) )
113121 {
@@ -117,7 +125,7 @@ private void SetParameters(IDbCommand command)
117125 }
118126 else
119127 {
120- foreach ( var pair in _parameters )
128+ foreach ( var pair in parameterList )
121129 {
122130 command . Parameters . Add ( CreateSingleParameter ( pair . Value , command , pair . Key . Name , pair . Key . DbType ) ) ;
123131 }
@@ -205,5 +213,17 @@ private static IDbDataParameter CreateSingleParameter(object value, IDbCommand c
205213 parameter . Value = CommandHelper . FixObjectType ( value ) ;
206214 return parameter ;
207215 }
216+
217+ internal static IDbCommand CreateCommand ( ICommandBuilder [ ] commandBuilders , IDbConnection connection )
218+ {
219+ var command = connection . CreateCommand ( ) ;
220+ for ( int i = 0 ; i < commandBuilders . Length ; i ++ )
221+ {
222+ if ( ! string . IsNullOrWhiteSpace ( command . CommandText ) ) command . CommandText += "; " ;
223+ command . CommandText += commandBuilders [ i ] . Text ;
224+ SetParameters ( command , commandBuilders [ i ] . Parameters ) ;
225+ }
226+ return command ;
227+ }
208228 }
209229}
0 commit comments