@@ -43,6 +43,7 @@ public sealed class FbBatchCommand : IFbPreparedCommand, IDescriptorFiller, IDis
4343 private FbTransaction _transaction;
4444 private FbBatchParameterCollection _batchParameters;
4545 private StatementBase _statement;
46+ private BatchBase _batch;
4647 //private FbDataReader _activeReader;
4748 private IReadOnlyList < string > _namedParameters;
4849 private string _commandText;
@@ -291,6 +292,7 @@ public void Dispose()
291292 _connection = null ;
292293 _transaction = null ;
293294 _batchParameters = null ;
295+ _batch = null ;
294296 _statement = null ;
295297 //_activeReader = null;
296298 _namedParameters = null ;
@@ -319,6 +321,7 @@ public async ValueTask DisposeAsync()
319321 _connection = null ;
320322 _transaction = null ;
321323 _batchParameters = null ;
324+ _batch = null ;
322325 _statement = null ;
323326 //_activeReader = null;
324327 _namedParameters = null ;
@@ -549,6 +552,23 @@ public Task<string> GetCommandExplainedPlanAsync(CancellationToken cancellationT
549552 return _statement . GetExecutionExplainedPlanAsync ( cancellationToken ) . AsTask ( ) ;
550553 }
551554
555+ public int GetCurrentBatchSize ( )
556+ {
557+ if ( _batch == null )
558+ {
559+ throw new InvalidOperationException ( "Batch must be prepared." ) ;
560+ }
561+ return _batch . ComputeBatchSize ( _batchParameters . Count , this ) ;
562+ }
563+ public async Task < int > GetCurrentBatchSizeAsync ( CancellationToken cancellationToken = default )
564+ {
565+ if ( _batch == null )
566+ {
567+ throw new InvalidOperationException ( "Batch must be prepared." ) ;
568+ }
569+ return await _batch . ComputeBatchSizeAsync ( _batchParameters . Count , this , cancellationToken ) . ConfigureAwait ( false ) ;
570+ }
571+
552572 #endregion
553573
554574 #region Internal Methods
@@ -855,6 +875,8 @@ internal void Release()
855875 _connection . InnerConnection . RemovePreparedCommand ( this ) ;
856876 }
857877
878+ _batch = null ;
879+
858880 if ( _statement != null )
859881 {
860882 _statement . Dispose2 ( ) ;
@@ -873,6 +895,8 @@ internal async Task ReleaseAsync(CancellationToken cancellationToken = default)
873895 _connection . InnerConnection . RemovePreparedCommand ( this ) ;
874896 }
875897
898+ _batch = null ;
899+
876900 if ( _statement != null )
877901 {
878902 await _statement . Dispose2Async ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -1131,6 +1155,7 @@ private void Prepare(bool returnsSet)
11311155 if ( _statement == null )
11321156 {
11331157 _statement = innerConn . Database . CreateStatement ( _transaction . Transaction ) ;
1158+ _batch = _statement . CreateBatch ( ) ;
11341159 }
11351160
11361161 // Prepare the statement if needed
@@ -1150,6 +1175,7 @@ private void Prepare(bool returnsSet)
11501175 }
11511176 catch
11521177 {
1178+ _batch = null ;
11531179 // Release the statement and rethrow the exception
11541180 _statement . Release ( ) ;
11551181 _statement = null ;
@@ -1195,6 +1221,7 @@ private async Task PrepareAsync(bool returnsSet, CancellationToken cancellationT
11951221 if ( _statement == null )
11961222 {
11971223 _statement = innerConn . Database . CreateStatement ( _transaction . Transaction ) ;
1224+ _batch = _statement . CreateBatch ( ) ;
11981225 }
11991226
12001227 // Prepare the statement if needed
@@ -1214,6 +1241,7 @@ private async Task PrepareAsync(bool returnsSet, CancellationToken cancellationT
12141241 }
12151242 catch
12161243 {
1244+ _batch = null ;
12171245 // Release the statement and rethrow the exception
12181246 await _statement . ReleaseAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
12191247 _statement = null ;
@@ -1249,17 +1277,16 @@ private FbBatchNonQueryResult ExecuteCommand(bool returnsSet)
12491277 throw FbException . Create ( "Must declare command parameters." ) ;
12501278 }
12511279
1252- var batch = _statement . CreateBatch ( ) ;
12531280 try
12541281 {
1255- batch . MultiError = MultiError ;
1256- batch . BatchBufferSize = BatchBufferSize ;
1282+ _batch . MultiError = MultiError ;
1283+ _batch . BatchBufferSize = BatchBufferSize ;
12571284 // Execute
1258- return new FbBatchNonQueryResult ( batch . Execute ( _batchParameters . Count , this ) ) ;
1285+ return new FbBatchNonQueryResult ( _batch . Execute ( _batchParameters . Count , this ) ) ;
12591286 }
12601287 finally
12611288 {
1262- batch . Dispose2 ( ) ;
1289+ _batch . Release ( ) ;
12631290 }
12641291 }
12651292 private async Task < FbBatchNonQueryResult > ExecuteCommandAsync ( bool returnsSet , CancellationToken cancellationToken = default )
@@ -1280,17 +1307,16 @@ private async Task<FbBatchNonQueryResult> ExecuteCommandAsync(bool returnsSet, C
12801307 throw FbException . Create ( "Must declare command parameters." ) ;
12811308 }
12821309
1283- var batch = _statement . CreateBatch ( ) ;
12841310 try
12851311 {
1286- batch . MultiError = MultiError ;
1287- batch . BatchBufferSize = BatchBufferSize ;
1312+ _batch . MultiError = MultiError ;
1313+ _batch . BatchBufferSize = BatchBufferSize ;
12881314 // Execute
1289- return new FbBatchNonQueryResult ( await batch . ExecuteAsync ( _batchParameters . Count , this , cancellationToken ) . ConfigureAwait ( false ) ) ;
1315+ return new FbBatchNonQueryResult ( await _batch . ExecuteAsync ( _batchParameters . Count , this , cancellationToken ) . ConfigureAwait ( false ) ) ;
12901316 }
12911317 finally
12921318 {
1293- await batch . Dispose2Async ( cancellationToken ) . ConfigureAwait ( false ) ;
1319+ await _batch . ReleaseAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
12941320 }
12951321 }
12961322
0 commit comments