forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericBatchingBatcher.cs
113 lines (102 loc) · 3.41 KB
/
GenericBatchingBatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using NHibernate.AdoNet.Util;
using NHibernate.Exceptions;
using NHibernate.SqlCommand;
namespace NHibernate.AdoNet
{
using System.Threading.Tasks;
using System.Threading;
public partial class GenericBatchingBatcher : AbstractBatcher
{
public override async Task AddToBatchAsync(IExpectation expectation, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var batchCommand = CurrentCommand;
if (_maxNumberOfParameters.HasValue &&
_currentBatch.CountOfParameters + batchCommand.Parameters.Count > _maxNumberOfParameters)
{
await (ExecuteBatchWithTimingAsync(batchCommand, cancellationToken)).ConfigureAwait(false);
}
_totalExpectedRowsAffected += expectation.ExpectedRowCount;
Driver.AdjustCommand(batchCommand);
LogBatchCommand(batchCommand);
_currentBatch.Append(batchCommand.Parameters);
if (_currentBatch.CountOfCommands >= BatchSize)
{
await (ExecuteBatchWithTimingAsync(batchCommand, cancellationToken)).ConfigureAwait(false);
}
}
protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (_currentBatch.CountOfCommands == 0)
{
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, 0, ps);
return;
}
try
{
Log.Debug("Executing batch");
await (CheckReadersAsync(cancellationToken)).ConfigureAwait(false);
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
Factory.Settings.SqlStatementLogger.LogBatchCommand(_currentBatchCommandsLog.ToString());
}
int rowsAffected;
try
{
rowsAffected = await (_currentBatch.ExecuteNonQueryAsync(cancellationToken)).ConfigureAwait(false);
}
catch (DbException e)
{
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
}
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
}
finally
{
ClearCurrentBatch();
}
}
private partial class BatchingCommandSet
{
public async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (CountOfCommands == 0)
{
return 0;
}
using (var batcherCommand = _batcher.Driver.GenerateCommand(
_commandType,
_sql.ToSqlString(),
_sqlTypes.ToArray()))
{
for (var i = 0; i < _parameters.Count; i++)
{
var parameter = _parameters[i];
var cmdParam = batcherCommand.Parameters[i];
cmdParam.Value = parameter.Value;
cmdParam.Direction = parameter.Direction;
cmdParam.Precision = parameter.Precision;
cmdParam.Scale = parameter.Scale;
cmdParam.Size = parameter.Size;
}
await (_batcher.PrepareAsync(batcherCommand, cancellationToken)).ConfigureAwait(false);
return await (batcherCommand.ExecuteNonQueryAsync(cancellationToken)).ConfigureAwait(false);
}
}
}
}
}