Skip to content

Commit 9232cbb

Browse files
committed
Moved statement terminator setting into dialect and done some minor refactoring.
1 parent 651de92 commit 9232cbb

File tree

5 files changed

+49
-62
lines changed

5 files changed

+49
-62
lines changed

src/NHibernate/AdoNet/GenericBatchingBatcher.cs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ public partial class GenericBatchingBatcher : AbstractBatcher
2525
private int _totalExpectedRowsAffected;
2626
private StringBuilder _currentBatchCommandsLog;
2727

28-
public GenericBatchingBatcher(ConnectionManager connectionManager, IInterceptor interceptor, char statementTerminator)
28+
public GenericBatchingBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
2929
: base(connectionManager, interceptor)
3030
{
3131
BatchSize = Factory.Settings.AdoBatchSize;
32-
StatementTerminator = statementTerminator;
33-
_currentBatch = new BatchingCommandSet(this);
32+
_currentBatch = new BatchingCommandSet(this, Factory.Dialect.StatementTerminator);
3433
_maxNumberOfParameters = Factory.Dialect.MaxNumberOfParameters;
3534

3635
// We always create this, because we need to deal with a scenario in which
@@ -40,44 +39,26 @@ public GenericBatchingBatcher(ConnectionManager connectionManager, IInterceptor
4039
_currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
4140
}
4241

43-
public char StatementTerminator { get; }
44-
4542
public sealed override int BatchSize { get; set; }
4643

4744
protected override int CountOfStatementsInCurrentBatch => _currentBatch.CountOfCommands;
4845

4946
public override void AddToBatch(IExpectation expectation)
5047
{
51-
var batchUpdate = CurrentCommand;
48+
var batchCommand = CurrentCommand;
5249
if (_maxNumberOfParameters.HasValue &&
53-
_currentBatch.CountOfParameters + CurrentCommand.Parameters.Count > _maxNumberOfParameters)
50+
_currentBatch.CountOfParameters + batchCommand.Parameters.Count > _maxNumberOfParameters)
5451
{
55-
ExecuteBatchWithTiming(batchUpdate);
52+
ExecuteBatchWithTiming(batchCommand);
5653
}
5754
_totalExpectedRowsAffected += expectation.ExpectedRowCount;
58-
Driver.AdjustCommand(batchUpdate);
59-
string lineWithParameters = null;
60-
var sqlStatementLogger = Factory.Settings.SqlStatementLogger;
61-
if (sqlStatementLogger.IsDebugEnabled || Log.IsDebugEnabled())
62-
{
63-
lineWithParameters = sqlStatementLogger.GetCommandLineWithParameters(batchUpdate);
64-
var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic);
65-
lineWithParameters = formatStyle.Formatter.Format(lineWithParameters);
66-
_currentBatchCommandsLog.Append("command ")
67-
.Append(_currentBatch.CountOfCommands)
68-
.Append(":")
69-
.AppendLine(lineWithParameters);
70-
}
71-
if (Log.IsDebugEnabled())
72-
{
73-
Log.Debug("Adding to batch:{0}", lineWithParameters);
74-
}
75-
76-
_currentBatch.Append(CurrentCommand.Parameters);
55+
Driver.AdjustCommand(batchCommand);
56+
LogBatchCommand(batchCommand);
57+
_currentBatch.Append(batchCommand.Parameters);
7758

7859
if (_currentBatch.CountOfCommands >= BatchSize)
7960
{
80-
ExecuteBatchWithTiming(batchUpdate);
61+
ExecuteBatchWithTiming(batchCommand);
8162
}
8263
}
8364

@@ -115,6 +96,26 @@ protected override void DoExecuteBatch(DbCommand ps)
11596
}
11697
}
11798

99+
private void LogBatchCommand(DbCommand batchCommand)
100+
{
101+
string lineWithParameters = null;
102+
var sqlStatementLogger = Factory.Settings.SqlStatementLogger;
103+
if (sqlStatementLogger.IsDebugEnabled || Log.IsDebugEnabled())
104+
{
105+
lineWithParameters = sqlStatementLogger.GetCommandLineWithParameters(batchCommand);
106+
var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic);
107+
lineWithParameters = formatStyle.Formatter.Format(lineWithParameters);
108+
_currentBatchCommandsLog.Append("command ")
109+
.Append(_currentBatch.CountOfCommands)
110+
.Append(":")
111+
.AppendLine(lineWithParameters);
112+
}
113+
if (Log.IsDebugEnabled())
114+
{
115+
Log.Debug("Adding to batch:{0}", lineWithParameters);
116+
}
117+
}
118+
118119
private void ClearCurrentBatch()
119120
{
120121
_currentBatch.Clear();
@@ -140,6 +141,7 @@ protected override void Dispose(bool isDisposing)
140141

141142
private partial class BatchingCommandSet
142143
{
144+
private readonly string _statementTerminator;
143145
private readonly GenericBatchingBatcher _batcher;
144146
private readonly SqlStringBuilder _sql = new SqlStringBuilder();
145147
private readonly List<SqlTypes.SqlType> _sqlTypes = new List<SqlTypes.SqlType>();
@@ -159,9 +161,10 @@ private class BatchParameter
159161
public object Value { get; set; }
160162
}
161163

162-
public BatchingCommandSet(GenericBatchingBatcher batcher)
164+
public BatchingCommandSet(GenericBatchingBatcher batcher, char statementTerminator)
163165
{
164166
_batcher = batcher;
167+
_statementTerminator = statementTerminator.ToString();
165168
}
166169

167170
public int CountOfCommands { get; private set; }
@@ -172,7 +175,7 @@ public void Append(DbParameterCollection parameters)
172175
{
173176
if (CountOfCommands > 0)
174177
{
175-
_sql.Add(_batcher.StatementTerminator.ToString());
178+
_sql.Add(_statementTerminator);
176179
}
177180
else
178181
{

src/NHibernate/AdoNet/GenericBatchingBatcherFactory.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@ namespace NHibernate.AdoNet
44
{
55
public class GenericBatchingBatcherFactory: IBatcherFactory
66
{
7-
private char _statementTerminator = ';';
8-
97
public virtual IBatcher CreateBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
108
{
11-
return new GenericBatchingBatcher(connectionManager, interceptor, _statementTerminator);
12-
}
13-
14-
public void SetStatementTerminator(char value)
15-
{
16-
_statementTerminator = value;
9+
return new GenericBatchingBatcher(connectionManager, interceptor);
1710
}
1811
}
1912
}

src/NHibernate/Async/AdoNet/GenericBatchingBatcher.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,20 @@ public partial class GenericBatchingBatcher : AbstractBatcher
2727
public override async Task AddToBatchAsync(IExpectation expectation, CancellationToken cancellationToken)
2828
{
2929
cancellationToken.ThrowIfCancellationRequested();
30-
var batchUpdate = CurrentCommand;
30+
var batchCommand = CurrentCommand;
3131
if (_maxNumberOfParameters.HasValue &&
32-
_currentBatch.CountOfParameters + CurrentCommand.Parameters.Count > _maxNumberOfParameters)
32+
_currentBatch.CountOfParameters + batchCommand.Parameters.Count > _maxNumberOfParameters)
3333
{
34-
await (ExecuteBatchWithTimingAsync(batchUpdate, cancellationToken)).ConfigureAwait(false);
34+
await (ExecuteBatchWithTimingAsync(batchCommand, cancellationToken)).ConfigureAwait(false);
3535
}
3636
_totalExpectedRowsAffected += expectation.ExpectedRowCount;
37-
Driver.AdjustCommand(batchUpdate);
38-
string lineWithParameters = null;
39-
var sqlStatementLogger = Factory.Settings.SqlStatementLogger;
40-
if (sqlStatementLogger.IsDebugEnabled || Log.IsDebugEnabled())
41-
{
42-
lineWithParameters = sqlStatementLogger.GetCommandLineWithParameters(batchUpdate);
43-
var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic);
44-
lineWithParameters = formatStyle.Formatter.Format(lineWithParameters);
45-
_currentBatchCommandsLog.Append("command ")
46-
.Append(_currentBatch.CountOfCommands)
47-
.Append(":")
48-
.AppendLine(lineWithParameters);
49-
}
50-
if (Log.IsDebugEnabled())
51-
{
52-
Log.Debug("Adding to batch:{0}", lineWithParameters);
53-
}
54-
55-
_currentBatch.Append(CurrentCommand.Parameters);
37+
Driver.AdjustCommand(batchCommand);
38+
LogBatchCommand(batchCommand);
39+
_currentBatch.Append(batchCommand.Parameters);
5640

5741
if (_currentBatch.CountOfCommands >= BatchSize)
5842
{
59-
await (ExecuteBatchWithTimingAsync(batchUpdate, cancellationToken)).ConfigureAwait(false);
43+
await (ExecuteBatchWithTimingAsync(batchCommand, cancellationToken)).ConfigureAwait(false);
6044
}
6145
}
6246

src/NHibernate/Dialect/Dialect.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,11 @@ public virtual string LowercaseFunction
24512451
/// </summary>
24522452
public virtual int? MaxNumberOfParameters => null;
24532453

2454+
/// <summary>
2455+
/// The character used to terminate a SQL statement.
2456+
/// </summary>
2457+
public virtual char StatementTerminator => ';';
2458+
24542459
/// <summary>
24552460
/// The syntax used to add a column to a table. Note this is deprecated
24562461
/// </summary>

src/NHibernate/Driver/BasicResultSetsCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ public partial class BasicResultSetsCommand: IResultSetsCommand
1616
{
1717
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(BasicResultSetsCommand));
1818
private SqlString sqlString = SqlString.Empty;
19+
private readonly string _statementTerminator;
1920

2021
public BasicResultSetsCommand(ISessionImplementor session)
2122
{
2223
Commands = new List<ISqlCommand>();
2324
Session = session;
25+
_statementTerminator = session.Factory.Dialect.StatementTerminator.ToString();
2426
}
2527

2628
protected List<ISqlCommand> Commands { get; private set; }
@@ -30,7 +32,7 @@ public BasicResultSetsCommand(ISessionImplementor session)
3032
public virtual void Append(ISqlCommand command)
3133
{
3234
Commands.Add(command);
33-
sqlString = sqlString.Append(command.Query).Append(";").Append(Environment.NewLine);
35+
sqlString = sqlString.Append(command.Query).Append(_statementTerminator).Append(Environment.NewLine);
3436
}
3537

3638
public bool HasQueries

0 commit comments

Comments
 (0)