Skip to content

Commit ac92dea

Browse files
committed
Implementation for GetCurrentBatchSize.
1 parent 6acff36 commit ac92dea

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version16/GdsBatch.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,25 @@ public override async ValueTask<ExecuteResultItem[]> ExecuteAsync(int count, IDe
143143
}
144144
}
145145

146-
public override void Dispose2()
146+
public override int ComputeBatchSize(int count, IDescriptorFiller descriptorFiller)
147+
{
148+
var parametersData = GetParametersData(count, descriptorFiller);
149+
return parametersData.Sum(x => x.Length);
150+
}
151+
public override async ValueTask<int> ComputeBatchSizeAsync(int count, IDescriptorFiller descriptorFiller, CancellationToken cancellationToken = default)
152+
{
153+
var parametersData = await GetParametersDataAsync(count, descriptorFiller, cancellationToken).ConfigureAwait(false);
154+
return parametersData.Sum(x => x.Length);
155+
}
156+
157+
public override void Release()
147158
{
148159
Database.Xdr.Write(IscCodes.op_batch_rls);
149160
Database.Xdr.Write(_statement.Handle);
150161
Database.AppendDeferredPacket(ProcessReleaseResponse);
151162
}
152163

153-
public override async ValueTask Dispose2Async(CancellationToken cancellationToken = default)
164+
public override async ValueTask ReleaseAsync(CancellationToken cancellationToken = default)
154165
{
155166
await Database.Xdr.WriteAsync(IscCodes.op_batch_rls, cancellationToken).ConfigureAwait(false);
156167
await Database.Xdr.WriteAsync(_statement.Handle, cancellationToken).ConfigureAwait(false);

src/FirebirdSql.Data.FirebirdClient/Common/BatchBase.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ public class ExecuteResultItem
3535
public abstract ExecuteResultItem[] Execute(int count, IDescriptorFiller descriptorFiller);
3636
public abstract ValueTask<ExecuteResultItem[]> ExecuteAsync(int count, IDescriptorFiller descriptorFiller, CancellationToken cancellationToken = default);
3737

38-
public virtual void Dispose2()
39-
{ }
40-
public virtual ValueTask Dispose2Async(CancellationToken cancellationToken = default)
41-
{
42-
return ValueTask2.CompletedTask;
43-
}
38+
public abstract int ComputeBatchSize(int count, IDescriptorFiller descriptorFiller);
39+
public abstract ValueTask<int> ComputeBatchSizeAsync(int count, IDescriptorFiller descriptorFiller, CancellationToken cancellationToken = default);
40+
41+
public abstract void Release();
42+
public abstract ValueTask ReleaseAsync(CancellationToken cancellationToken = default);
4443
}

src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbBatchCommand.cs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)