forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqlClientBatchingBatcher.cs
61 lines (53 loc) · 1.74 KB
/
SqlClientBatchingBatcher.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
using System.Data;
using System.Text;
namespace NHibernate.AdoNet
{
/// <summary>
/// Summary description for SqlClientBatchingBatcher.
/// </summary>
internal class SqlClientBatchingBatcher : BatcherImpl
{
private int batchSize;
private int totalExpectedRowsAffected;
private SqlClientSqlCommandSet currentBatch;
private StringBuilder currentBatchCommandsLog = new StringBuilder();
public SqlClientBatchingBatcher(ConnectionManager connectionManager)
: base(connectionManager)
{
batchSize = Factory.BatchSize;
currentBatch = new SqlClientSqlCommandSet();
}
public override int BatchSize
{
get { return batchSize; }
set { batchSize = value; }
}
public override void AddToBatch(IExpectation expectation)
{
totalExpectedRowsAffected += expectation.ExpectedRowCount;
log.Debug("Adding to batch:");
IDbCommand batchUpdate = CurrentCommand;
string commandLoggedText = GetCommandLogString(batchUpdate);
currentBatchCommandsLog.Append("Batch command: ").
AppendLine(commandLoggedText);
currentBatch.Append((System.Data.SqlClient.SqlCommand) batchUpdate);
if (currentBatch.CountOfCommands >= batchSize)
{
DoExecuteBatch(batchUpdate);
}
}
protected override void DoExecuteBatch(IDbCommand ps)
{
log.Debug("Executing batch");
CheckReaders();
Prepare(currentBatch.BatchCommand);
logSql.Debug(currentBatchCommandsLog.ToString());
currentBatchCommandsLog = new StringBuilder();
int rowsAffected = currentBatch.ExecuteNonQuery();
Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
currentBatch.Dispose();
totalExpectedRowsAffected = 0;
currentBatch = new SqlClientSqlCommandSet();
}
}
}