7
7
using System . Threading . Tasks ;
8
8
using NHibernate . AdoNet . Util ;
9
9
using NHibernate . Driver ;
10
- using NHibernate . Engine ;
11
10
using NHibernate . Exceptions ;
12
11
13
12
namespace NHibernate . AdoNet
@@ -38,20 +37,14 @@ public DbBatchBatcher(ConnectionManager connectionManager, IInterceptor intercep
38
37
39
38
public override int BatchSize
40
39
{
41
- get { return _batchSize ; }
42
- set { _batchSize = value ; }
40
+ get => _batchSize ;
41
+ set => _batchSize = value ;
43
42
}
44
43
45
- protected override int CountOfStatementsInCurrentBatch
46
- {
47
- get { return _currentBatch . BatchCommands . Count ; }
48
- }
44
+ protected override int CountOfStatementsInCurrentBatch => _currentBatch . BatchCommands . Count ;
49
45
50
- public override void AddToBatch ( IExpectation expectation )
46
+ private void LogBatchCommand ( DbCommand batchUpdate )
51
47
{
52
- _totalExpectedRowsAffected += expectation . ExpectedRowCount ;
53
- var batchUpdate = CurrentCommand ;
54
- Driver . AdjustCommand ( batchUpdate ) ;
55
48
string lineWithParameters = null ;
56
49
var sqlStatementLogger = Factory . Settings . SqlStatementLogger ;
57
50
if ( sqlStatementLogger . IsDebugEnabled || Log . IsDebugEnabled ( ) )
@@ -60,21 +53,15 @@ public override void AddToBatch(IExpectation expectation)
60
53
var formatStyle = sqlStatementLogger . DetermineActualStyle ( FormatStyle . Basic ) ;
61
54
lineWithParameters = formatStyle . Formatter . Format ( lineWithParameters ) ;
62
55
_currentBatchCommandsLog . Append ( "command " )
63
- . Append ( _currentBatch . BatchCommands . Count )
64
- . Append ( ":" )
65
- . AppendLine ( lineWithParameters ) ;
56
+ . Append ( _currentBatch . BatchCommands . Count )
57
+ . Append ( ':' )
58
+ . AppendLine ( lineWithParameters ) ;
66
59
}
60
+
67
61
if ( Log . IsDebugEnabled ( ) )
68
62
{
69
63
Log . Debug ( "Adding to batch:{0}" , lineWithParameters ) ;
70
64
}
71
-
72
- AddCommandToBatch ( batchUpdate ) ;
73
-
74
- if ( CountOfStatementsInCurrentBatch >= _batchSize )
75
- {
76
- ExecuteBatchWithTiming ( batchUpdate ) ;
77
- }
78
65
}
79
66
80
67
private void AddCommandToBatch ( DbCommand batchUpdate )
@@ -87,48 +74,49 @@ private void AddCommandToBatch(DbCommand batchUpdate)
87
74
{
88
75
dbBatchCommand . Parameters . Add ( ( ( ICloneable ) param ) . Clone ( ) ) ;
89
76
}
77
+
90
78
_currentBatch . BatchCommands . Add ( dbBatchCommand ) ;
91
79
}
92
80
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
+
93
95
public override Task AddToBatchAsync ( IExpectation expectation , CancellationToken cancellationToken )
94
96
{
95
97
if ( cancellationToken . IsCancellationRequested )
96
98
{
97
- return Task . FromCanceled < object > ( cancellationToken ) ;
99
+ return Task . FromCanceled ( cancellationToken ) ;
98
100
}
101
+
99
102
try
100
103
{
101
104
_totalExpectedRowsAffected += expectation . ExpectedRowCount ;
102
105
var batchUpdate = CurrentCommand ;
103
106
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 ) ;
121
108
AddCommandToBatch ( batchUpdate ) ;
122
109
123
110
if ( CountOfStatementsInCurrentBatch >= _batchSize )
124
111
{
125
112
return ExecuteBatchWithTimingAsync ( batchUpdate , cancellationToken ) ;
126
113
}
114
+
127
115
return Task . CompletedTask ;
128
116
}
129
117
catch ( Exception ex )
130
118
{
131
- return Task . FromException < object > ( ex ) ;
119
+ return Task . FromException ( ex ) ;
132
120
}
133
121
}
134
122
0 commit comments