@@ -14,6 +14,7 @@ class BulkInserterHelper
1414 protected readonly IEnumerable < IDictionary < string , object > > Data ;
1515 private readonly Table _table ;
1616 private readonly List < Column > _columns ;
17+ private Action < IDictionary < string , object > , IDbCommand > _parameterSetter ;
1718
1819 public BulkInserterHelper ( AdoAdapter adapter , IEnumerable < IDictionary < string , object > > data , Table table , List < Column > columns )
1920 {
@@ -74,17 +75,9 @@ public virtual IEnumerable<IDictionary<string, object>> InsertRowsWithCompoundSt
7475
7576 protected IDictionary < string , object > InsertRowAndSelect ( IDictionary < string , object > row , IDbCommand command , Func < IDictionary < string , object > , Exception , bool > onError )
7677 {
77- var values = new object [ command . Parameters . Count ] ;
78- foreach ( var kvp in row )
79- {
80- int index = _columns . IndexOf ( _table . FindColumn ( kvp . Key ) ) ;
81- if ( index > - 1 )
82- {
83- values [ index ] = kvp . Value ;
84- }
85- }
78+ if ( _parameterSetter == null ) _parameterSetter = BuildParameterSettingAction ( row ) ;
79+ _parameterSetter ( row , command ) ;
8680
87- CommandHelper . SetParameterValues ( command , values ) ;
8881 try
8982 {
9083 var insertedRow = TryExecuteSingletonQuery ( command ) ;
@@ -99,17 +92,9 @@ protected IDictionary<string, object> InsertRowAndSelect(IDictionary<string, obj
9992
10093 protected int InsertRow ( IDictionary < string , object > row , IDbCommand command , Func < IDictionary < string , object > , Exception , bool > onError )
10194 {
102- var values = new object [ command . Parameters . Count ] ;
103- foreach ( var kvp in row )
104- {
105- int index = _columns . IndexOf ( _table . FindColumn ( kvp . Key ) ) ;
106- if ( index > - 1 )
107- {
108- values [ index ] = kvp . Value ;
109- }
110- }
95+ if ( _parameterSetter == null ) _parameterSetter = BuildParameterSettingAction ( row ) ;
96+ _parameterSetter ( row , command ) ;
11197
112- CommandHelper . SetParameterValues ( command , values ) ;
11398 try
11499 {
115100 return TryExecute ( command ) ;
@@ -123,17 +108,9 @@ protected int InsertRow(IDictionary<string, object> row, IDbCommand command, Fun
123108
124109 protected IDictionary < string , object > InsertRow ( IDictionary < string , object > row , IDbCommand insertCommand , IDbCommand selectCommand , Func < IDictionary < string , object > , Exception , bool > onError )
125110 {
126- var values = new object [ insertCommand . Parameters . Count ] ;
127- foreach ( var kvp in row )
128- {
129- int index = _columns . IndexOf ( _table . FindColumn ( kvp . Key ) ) ;
130- if ( index > - 1 )
131- {
132- values [ index ] = kvp . Value ;
133- }
134- }
111+ if ( _parameterSetter == null ) _parameterSetter = BuildParameterSettingAction ( row ) ;
112+ _parameterSetter ( row , insertCommand ) ;
135113
136- CommandHelper . SetParameterValues ( insertCommand , values ) ;
137114 try
138115 {
139116 if ( TryExecute ( insertCommand ) == 1 )
@@ -195,5 +172,27 @@ private static void TryPrepare(params IDbCommand[] commands)
195172 }
196173 }
197174 }
175+
176+ private Action < IDictionary < string , object > , IDbCommand > BuildParameterSettingAction ( IDictionary < string , object > sample )
177+ {
178+ var actions =
179+ _columns . Select < Column , Action < IDictionary < string , object > , IDbCommand > > ( ( c , i ) => ( row , cmd ) => cmd . SetParameterValue ( i , null ) ) . ToArray ( ) ;
180+
181+ foreach ( var key in sample . Keys )
182+ {
183+ int index = _columns . IndexOf ( _table . FindColumn ( key ) ) ;
184+ if ( index >= 0 )
185+ actions [ index ] = BuildIndividualFunction ( key , index ) ;
186+
187+ ++ index ;
188+ }
189+
190+ return actions . Aggregate ( ( working , next ) => working + next ) ?? ( ( row , cmd ) => { } ) ;
191+ }
192+
193+ private Action < IDictionary < string , object > , IDbCommand > BuildIndividualFunction ( string key , int index )
194+ {
195+ return ( dict , command ) => command . SetParameterValue ( index , dict [ key ] ) ;
196+ }
198197 }
199198}
0 commit comments