@@ -43,6 +43,7 @@ public sealed class FbBatchCommand : IFbPreparedCommand, IDescriptorFiller, IDis
43
43
private FbTransaction _transaction;
44
44
private FbBatchParameterCollection _batchParameters;
45
45
private StatementBase _statement;
46
+ private BatchBase _batch;
46
47
//private FbDataReader _activeReader;
47
48
private IReadOnlyList < string > _namedParameters;
48
49
private string _commandText;
@@ -291,6 +292,7 @@ public void Dispose()
291
292
_connection = null ;
292
293
_transaction = null ;
293
294
_batchParameters = null ;
295
+ _batch = null ;
294
296
_statement = null ;
295
297
//_activeReader = null;
296
298
_namedParameters = null ;
@@ -319,6 +321,7 @@ public async ValueTask DisposeAsync()
319
321
_connection = null ;
320
322
_transaction = null ;
321
323
_batchParameters = null ;
324
+ _batch = null ;
322
325
_statement = null ;
323
326
//_activeReader = null;
324
327
_namedParameters = null ;
@@ -549,6 +552,23 @@ public Task<string> GetCommandExplainedPlanAsync(CancellationToken cancellationT
549
552
return _statement . GetExecutionExplainedPlanAsync ( cancellationToken ) . AsTask ( ) ;
550
553
}
551
554
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
+
552
572
#endregion
553
573
554
574
#region Internal Methods
@@ -855,6 +875,8 @@ internal void Release()
855
875
_connection . InnerConnection . RemovePreparedCommand ( this ) ;
856
876
}
857
877
878
+ _batch = null ;
879
+
858
880
if ( _statement != null )
859
881
{
860
882
_statement . Dispose2 ( ) ;
@@ -873,6 +895,8 @@ internal async Task ReleaseAsync(CancellationToken cancellationToken = default)
873
895
_connection . InnerConnection . RemovePreparedCommand ( this ) ;
874
896
}
875
897
898
+ _batch = null ;
899
+
876
900
if ( _statement != null )
877
901
{
878
902
await _statement . Dispose2Async ( cancellationToken ) . ConfigureAwait ( false ) ;
@@ -1131,6 +1155,7 @@ private void Prepare(bool returnsSet)
1131
1155
if ( _statement == null )
1132
1156
{
1133
1157
_statement = innerConn . Database . CreateStatement ( _transaction . Transaction ) ;
1158
+ _batch = _statement . CreateBatch ( ) ;
1134
1159
}
1135
1160
1136
1161
// Prepare the statement if needed
@@ -1150,6 +1175,7 @@ private void Prepare(bool returnsSet)
1150
1175
}
1151
1176
catch
1152
1177
{
1178
+ _batch = null ;
1153
1179
// Release the statement and rethrow the exception
1154
1180
_statement . Release ( ) ;
1155
1181
_statement = null ;
@@ -1195,6 +1221,7 @@ private async Task PrepareAsync(bool returnsSet, CancellationToken cancellationT
1195
1221
if ( _statement == null )
1196
1222
{
1197
1223
_statement = innerConn . Database . CreateStatement ( _transaction . Transaction ) ;
1224
+ _batch = _statement . CreateBatch ( ) ;
1198
1225
}
1199
1226
1200
1227
// Prepare the statement if needed
@@ -1214,6 +1241,7 @@ private async Task PrepareAsync(bool returnsSet, CancellationToken cancellationT
1214
1241
}
1215
1242
catch
1216
1243
{
1244
+ _batch = null ;
1217
1245
// Release the statement and rethrow the exception
1218
1246
await _statement . ReleaseAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
1219
1247
_statement = null ;
@@ -1249,17 +1277,16 @@ private FbBatchNonQueryResult ExecuteCommand(bool returnsSet)
1249
1277
throw FbException . Create ( "Must declare command parameters." ) ;
1250
1278
}
1251
1279
1252
- var batch = _statement . CreateBatch ( ) ;
1253
1280
try
1254
1281
{
1255
- batch . MultiError = MultiError ;
1256
- batch . BatchBufferSize = BatchBufferSize ;
1282
+ _batch . MultiError = MultiError ;
1283
+ _batch . BatchBufferSize = BatchBufferSize ;
1257
1284
// Execute
1258
- return new FbBatchNonQueryResult ( batch . Execute ( _batchParameters . Count , this ) ) ;
1285
+ return new FbBatchNonQueryResult ( _batch . Execute ( _batchParameters . Count , this ) ) ;
1259
1286
}
1260
1287
finally
1261
1288
{
1262
- batch . Dispose2 ( ) ;
1289
+ _batch . Release ( ) ;
1263
1290
}
1264
1291
}
1265
1292
private async Task < FbBatchNonQueryResult > ExecuteCommandAsync ( bool returnsSet , CancellationToken cancellationToken = default )
@@ -1280,17 +1307,16 @@ private async Task<FbBatchNonQueryResult> ExecuteCommandAsync(bool returnsSet, C
1280
1307
throw FbException . Create ( "Must declare command parameters." ) ;
1281
1308
}
1282
1309
1283
- var batch = _statement . CreateBatch ( ) ;
1284
1310
try
1285
1311
{
1286
- batch . MultiError = MultiError ;
1287
- batch . BatchBufferSize = BatchBufferSize ;
1312
+ _batch . MultiError = MultiError ;
1313
+ _batch . BatchBufferSize = BatchBufferSize ;
1288
1314
// 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 ) ) ;
1290
1316
}
1291
1317
finally
1292
1318
{
1293
- await batch . Dispose2Async ( cancellationToken ) . ConfigureAwait ( false ) ;
1319
+ await _batch . ReleaseAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
1294
1320
}
1295
1321
}
1296
1322
0 commit comments