Skip to content

Commit 9ca2f33

Browse files
committed
Reduce code duplication
1 parent afabc24 commit 9ca2f33

File tree

1 file changed

+28
-40
lines changed

1 file changed

+28
-40
lines changed

src/NHibernate/AdoNet/DbBatchBatcher.cs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Threading.Tasks;
88
using NHibernate.AdoNet.Util;
99
using NHibernate.Driver;
10-
using NHibernate.Engine;
1110
using NHibernate.Exceptions;
1211

1312
namespace NHibernate.AdoNet
@@ -38,20 +37,14 @@ public DbBatchBatcher(ConnectionManager connectionManager, IInterceptor intercep
3837

3938
public override int BatchSize
4039
{
41-
get { return _batchSize; }
42-
set { _batchSize = value; }
40+
get => _batchSize;
41+
set => _batchSize = value;
4342
}
4443

45-
protected override int CountOfStatementsInCurrentBatch
46-
{
47-
get { return _currentBatch.BatchCommands.Count; }
48-
}
44+
protected override int CountOfStatementsInCurrentBatch => _currentBatch.BatchCommands.Count;
4945

50-
public override void AddToBatch(IExpectation expectation)
46+
private void LogBatchCommand(DbCommand batchUpdate)
5147
{
52-
_totalExpectedRowsAffected += expectation.ExpectedRowCount;
53-
var batchUpdate = CurrentCommand;
54-
Driver.AdjustCommand(batchUpdate);
5548
string lineWithParameters = null;
5649
var sqlStatementLogger = Factory.Settings.SqlStatementLogger;
5750
if (sqlStatementLogger.IsDebugEnabled || Log.IsDebugEnabled())
@@ -60,21 +53,15 @@ public override void AddToBatch(IExpectation expectation)
6053
var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic);
6154
lineWithParameters = formatStyle.Formatter.Format(lineWithParameters);
6255
_currentBatchCommandsLog.Append("command ")
63-
.Append(_currentBatch.BatchCommands.Count)
64-
.Append(":")
65-
.AppendLine(lineWithParameters);
56+
.Append(_currentBatch.BatchCommands.Count)
57+
.Append(':')
58+
.AppendLine(lineWithParameters);
6659
}
60+
6761
if (Log.IsDebugEnabled())
6862
{
6963
Log.Debug("Adding to batch:{0}", lineWithParameters);
7064
}
71-
72-
AddCommandToBatch(batchUpdate);
73-
74-
if (CountOfStatementsInCurrentBatch >= _batchSize)
75-
{
76-
ExecuteBatchWithTiming(batchUpdate);
77-
}
7865
}
7966

8067
private void AddCommandToBatch(DbCommand batchUpdate)
@@ -87,48 +74,49 @@ private void AddCommandToBatch(DbCommand batchUpdate)
8774
{
8875
dbBatchCommand.Parameters.Add(((ICloneable) param).Clone());
8976
}
77+
9078
_currentBatch.BatchCommands.Add(dbBatchCommand);
9179
}
9280

81+
public override void AddToBatch(IExpectation expectation)
82+
{
83+
_totalExpectedRowsAffected += expectation.ExpectedRowCount;
84+
var batchUpdate = CurrentCommand;
85+
Driver.AdjustCommand(batchUpdate);
86+
LogBatchCommand(batchUpdate);
87+
AddCommandToBatch(batchUpdate);
88+
89+
if (CountOfStatementsInCurrentBatch >= _batchSize)
90+
{
91+
ExecuteBatchWithTiming(batchUpdate);
92+
}
93+
}
94+
9395
public override Task AddToBatchAsync(IExpectation expectation, CancellationToken cancellationToken)
9496
{
9597
if (cancellationToken.IsCancellationRequested)
9698
{
97-
return Task.FromCanceled<object>(cancellationToken);
99+
return Task.FromCanceled(cancellationToken);
98100
}
101+
99102
try
100103
{
101104
_totalExpectedRowsAffected += expectation.ExpectedRowCount;
102105
var batchUpdate = CurrentCommand;
103106
Driver.AdjustCommand(batchUpdate);
104-
string lineWithParameters = null;
105-
var sqlStatementLogger = Factory.Settings.SqlStatementLogger;
106-
if (sqlStatementLogger.IsDebugEnabled || Log.IsDebugEnabled())
107-
{
108-
lineWithParameters = sqlStatementLogger.GetCommandLineWithParameters(batchUpdate);
109-
var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic);
110-
lineWithParameters = formatStyle.Formatter.Format(lineWithParameters);
111-
_currentBatchCommandsLog.Append("command ")
112-
.Append(_currentBatch.BatchCommands.Count)
113-
.Append(":")
114-
.AppendLine(lineWithParameters);
115-
}
116-
if (Log.IsDebugEnabled())
117-
{
118-
Log.Debug("Adding to batch:{0}", lineWithParameters);
119-
}
120-
107+
LogBatchCommand(batchUpdate);
121108
AddCommandToBatch(batchUpdate);
122109

123110
if (CountOfStatementsInCurrentBatch >= _batchSize)
124111
{
125112
return ExecuteBatchWithTimingAsync(batchUpdate, cancellationToken);
126113
}
114+
127115
return Task.CompletedTask;
128116
}
129117
catch (Exception ex)
130118
{
131-
return Task.FromException<object>(ex);
119+
return Task.FromException(ex);
132120
}
133121
}
134122

0 commit comments

Comments
 (0)