@@ -23,7 +23,7 @@ public BulkInserterHelper(AdoAdapter adapter, IEnumerable<IDictionary<string, ob
2323 _columns = columns ;
2424 }
2525
26- public virtual void InsertRowsWithoutFetchBack ( string insertSql )
26+ public virtual void InsertRowsWithoutFetchBack ( string insertSql , Func < IDictionary < string , object > , Exception , bool > onError )
2727 {
2828 var connection = Adapter . CreateConnection ( ) ;
2929 using ( connection . MaybeDisposable ( ) )
@@ -35,13 +35,13 @@ public virtual void InsertRowsWithoutFetchBack(string insertSql)
3535 insertCommand . Prepare ( ) ;
3636 foreach ( var row in Data )
3737 {
38- InsertRow ( row , insertCommand ) ;
38+ InsertRow ( row , insertCommand , onError ) ;
3939 }
4040 }
4141 }
4242 }
4343
44- public virtual IEnumerable < IDictionary < string , object > > InsertRowsWithSeparateStatements ( string insertSql , string selectSql )
44+ public virtual IEnumerable < IDictionary < string , object > > InsertRowsWithSeparateStatements ( string insertSql , string selectSql , Func < IDictionary < string , object > , Exception , bool > onError )
4545 {
4646 var connection = Adapter . CreateConnection ( ) ;
4747 using ( connection . MaybeDisposable ( ) )
@@ -52,12 +52,12 @@ public virtual IEnumerable<IDictionary<string, object>> InsertRowsWithSeparateSt
5252 selectCommand . CommandText = selectSql ;
5353 connection . OpenIfClosed ( ) ;
5454 TryPrepare ( insertCommand , selectCommand ) ;
55- return Data . Select ( row => InsertRow ( row , insertCommand , selectCommand ) ) . ToList ( ) ;
55+ return Data . Select ( row => InsertRow ( row , insertCommand , selectCommand , onError ) ) . Where ( r => r != null ) . ToList ( ) ;
5656 }
5757 }
5858 }
5959
60- public virtual IEnumerable < IDictionary < string , object > > InsertRowsWithCompoundStatement ( string insertSql , string selectSql )
60+ public virtual IEnumerable < IDictionary < string , object > > InsertRowsWithCompoundStatement ( string insertSql , string selectSql , Func < IDictionary < string , object > , Exception , bool > onError )
6161 {
6262 insertSql += "; " + selectSql ;
6363
@@ -68,12 +68,12 @@ public virtual IEnumerable<IDictionary<string, object>> InsertRowsWithCompoundSt
6868 {
6969 connection . OpenIfClosed ( ) ;
7070 TryPrepare ( command ) ;
71- return Data . Select ( row => InsertRowAndSelect ( row , command ) ) . ToList ( ) ;
71+ return Data . Select ( row => InsertRowAndSelect ( row , command , onError ) ) . Where ( r => r != null ) . ToList ( ) ;
7272 }
7373 }
7474 }
7575
76- protected IDictionary < string , object > InsertRowAndSelect ( IDictionary < string , object > row , IDbCommand command )
76+ protected IDictionary < string , object > InsertRowAndSelect ( IDictionary < string , object > row , IDbCommand command , Func < IDictionary < string , object > , Exception , bool > onError )
7777 {
7878 var values = new object [ command . Parameters . Count ] ;
7979 foreach ( var kvp in row )
@@ -86,11 +86,19 @@ protected IDictionary<string, object> InsertRowAndSelect(IDictionary<string, obj
8686 }
8787
8888 CommandHelper . SetParameterValues ( command , values ) ;
89- var insertedRow = TryExecuteSingletonQuery ( command ) ;
90- return insertedRow ;
89+ try
90+ {
91+ var insertedRow = TryExecuteSingletonQuery ( command ) ;
92+ return insertedRow ;
93+ }
94+ catch ( Exception ex )
95+ {
96+ if ( onError ( row , ex ) ) return null ;
97+ throw ;
98+ }
9199 }
92100
93- protected int InsertRow ( IDictionary < string , object > row , IDbCommand command )
101+ protected int InsertRow ( IDictionary < string , object > row , IDbCommand command , Func < IDictionary < string , object > , Exception , bool > onError )
94102 {
95103 var values = new object [ command . Parameters . Count ] ;
96104 foreach ( var kvp in row )
@@ -103,10 +111,18 @@ protected int InsertRow(IDictionary<string, object> row, IDbCommand command)
103111 }
104112
105113 CommandHelper . SetParameterValues ( command , values ) ;
106- return TryExecute ( command ) ;
114+ try
115+ {
116+ return TryExecute ( command ) ;
117+ }
118+ catch ( Exception ex )
119+ {
120+ if ( onError ( row , ex ) ) return 0 ;
121+ throw ;
122+ }
107123 }
108124
109- protected IDictionary < string , object > InsertRow ( IDictionary < string , object > row , IDbCommand insertCommand , IDbCommand selectCommand )
125+ protected IDictionary < string , object > InsertRow ( IDictionary < string , object > row , IDbCommand insertCommand , IDbCommand selectCommand , Func < IDictionary < string , object > , Exception , bool > onError )
110126 {
111127 var values = new object [ insertCommand . Parameters . Count ] ;
112128 foreach ( var kvp in row )
@@ -119,8 +135,16 @@ protected IDictionary<string, object> InsertRow(IDictionary<string, object> row,
119135 }
120136
121137 CommandHelper . SetParameterValues ( insertCommand , values ) ;
122- if ( TryExecute ( insertCommand ) == 1 )
123- return TryExecuteSingletonQuery ( selectCommand ) ;
138+ try
139+ {
140+ if ( TryExecute ( insertCommand ) == 1 )
141+ return TryExecuteSingletonQuery ( selectCommand ) ;
142+ }
143+ catch ( Exception ex )
144+ {
145+ if ( onError ( row , ex ) ) return null ;
146+ throw ;
147+ }
124148 return null ;
125149 }
126150
0 commit comments