Skip to content

Commit b177721

Browse files
committed
Use interface default implementations
1 parent b96fe21 commit b177721

13 files changed

+71
-93
lines changed

src/NHibernate.Test/Ado/BatcherFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void BatchedoutputShouldBeFormatted()
168168
{
169169
FillDb();
170170
var log = sqlLog.GetWholeLog();
171-
Assert.IsTrue(log.Contains("INSERT \n INTO"));
171+
Assert.That(log, Does.Contain("INSERT \n INTO").IgnoreCase);
172172
}
173173

174174
Cleanup();
@@ -239,7 +239,7 @@ public void AbstractBatcherLog()
239239
foreach (var loggingEvent in sl.Appender.GetEvents())
240240
{
241241
string message = loggingEvent.RenderedMessage;
242-
if(message.ToLowerInvariant().Contains("insert"))
242+
if(message.Contains("insert"))
243243
{
244244
Assert.That(message, Does.Contain("batch").IgnoreCase);
245245
}

src/NHibernate/AdoNet/ConnectionManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,15 +524,15 @@ public void EnlistInTransaction(DbBatch batch)
524524
if (batch == null)
525525
throw new ArgumentNullException(nameof(batch));
526526

527-
if (_transaction is ITransactionWithBatchSupport transactionWithBatch)
527+
if (_transaction != null)
528528
{
529-
transactionWithBatch.Enlist(batch);
529+
_transaction.Enlist(batch);
530530
return;
531531
}
532532

533533
if (batch.Transaction != null)
534534
{
535-
_log.Warn("set a nonnull DbCommand.Transaction to null because the Session had no Transaction");
535+
_log.Warn("set a nonnull DbBatch.Transaction to null because the Session had no Transaction");
536536
batch.Transaction = null;
537537
}
538538
}

src/NHibernate/AdoNet/DbBatchBatcher.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if NET6_0_OR_GREATER
1+
#if NET6_0_OR_GREATER
22
using System;
33
using System.Data.Common;
44
using System.Linq;
@@ -26,7 +26,7 @@ public DbBatchBatcher(ConnectionManager connectionManager, IInterceptor intercep
2626
{
2727
_batchSize = Factory.Settings.AdoBatchSize;
2828
_defaultTimeout = Driver.GetCommandTimeout();
29-
29+
3030
_currentBatch = CreateConfiguredBatch();
3131
//we always create this, because we need to deal with a scenario in which
3232
//the user change the logging configuration at runtime. Trying to put this
@@ -152,7 +152,7 @@ protected override void DoExecuteBatch(DbCommand ps)
152152
{
153153
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
154154
}
155-
155+
156156
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
157157
}
158158
finally
@@ -168,8 +168,7 @@ protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToke
168168
{
169169
Log.Debug("Executing batch");
170170
await (CheckReadersAsync(cancellationToken)).ConfigureAwait(false);
171-
//await (PrepareAsync(_currentBatch, cancellationToken)).ConfigureAwait(false);
172-
Prepare(_currentBatch);
171+
await (PrepareAsync(_currentBatch, cancellationToken)).ConfigureAwait(false);
173172
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
174173
{
175174
Factory.Settings.SqlStatementLogger.LogBatchCommand(_currentBatchCommandsLog.ToString());
@@ -194,7 +193,7 @@ protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToke
194193

195194
private DbBatch CreateConfiguredBatch()
196195
{
197-
var result = ((IDriverWithBatchSupport) Driver).CreateBatch();
196+
var result = Driver.CreateBatch();
198197
if (_defaultTimeout > 0)
199198
{
200199
try
@@ -218,7 +217,7 @@ private void ClearCurrentBatch()
218217
_currentBatch.Dispose();
219218
_totalExpectedRowsAffected = 0;
220219
_currentBatch = CreateConfiguredBatch();
221-
220+
222221
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
223222
{
224223
_currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
@@ -285,7 +284,44 @@ protected void Prepare(DbBatch batch)
285284
}
286285

287286
_connectionManager.EnlistInTransaction(batch);
288-
(Driver as IDriverWithBatchSupport).PrepareBatch(batch);
287+
Driver.PrepareBatch(batch);
288+
}
289+
catch (InvalidOperationException ioe)
290+
{
291+
throw new ADOException("While preparing " + string.Join(Environment.NewLine, batch.BatchCommands.Select(x => x.CommandText)) + " an error occurred", ioe);
292+
}
293+
}
294+
295+
/// <summary>
296+
/// Prepares the <see cref="DbBatch"/> for execution in the database.
297+
/// </summary>
298+
/// <remarks>
299+
/// This takes care of hooking the <see cref="DbBatch"/> up to an <see cref="DbConnection"/>
300+
/// and <see cref="DbTransaction"/> if one exists. It will call <c>Prepare</c> if the Driver
301+
/// supports preparing batches.
302+
/// </remarks>
303+
protected async Task PrepareAsync(DbBatch batch, CancellationToken cancellationToken)
304+
{
305+
try
306+
{
307+
var sessionConnection = await _connectionManager.GetConnectionAsync(cancellationToken).ConfigureAwait(false);
308+
309+
if (batch.Connection != null)
310+
{
311+
// make sure the commands connection is the same as the Sessions connection
312+
// these can be different when the session is disconnected and then reconnected
313+
if (batch.Connection != sessionConnection)
314+
{
315+
batch.Connection = sessionConnection;
316+
}
317+
}
318+
else
319+
{
320+
batch.Connection = sessionConnection;
321+
}
322+
323+
_connectionManager.EnlistInTransaction(batch);
324+
Driver.PrepareBatch(batch);
289325
}
290326
catch (InvalidOperationException ioe)
291327
{

src/NHibernate/Driver/DbProviderFactoryDriveConnectionCommandProvider.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
namespace NHibernate.Driver
55
{
66
public class DbProviderFactoryDriveConnectionCommandProvider : IDriveConnectionCommandProvider
7-
#if NET6_0_OR_GREATER
8-
, IDriveConnectionCommandProviderWithBatchSupport
9-
#endif
107
{
118
private readonly DbProviderFactory dbProviderFactory;
129

@@ -29,10 +26,7 @@ public DbCommand CreateCommand()
2926
return dbProviderFactory.CreateCommand();
3027
}
3128
#if NET6_0_OR_GREATER
32-
public DbBatch CreateBatch()
33-
{
34-
return dbProviderFactory.CreateBatch();
35-
}
29+
public DbBatch CreateBatch() => dbProviderFactory.CreateBatch();
3630

3731
public bool CanCreateBatch => dbProviderFactory.CanCreateBatch && dbProviderFactory.CreateCommand() is ICloneable;
3832
#endif

src/NHibernate/Driver/DriverBase.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ namespace NHibernate.Driver
1515
/// Base class for the implementation of IDriver
1616
/// </summary>
1717
public abstract class DriverBase : IDriver, ISqlParameterFormatter
18-
#if NET6_0_OR_GREATER
19-
, IDriverWithBatchSupport
20-
#endif
2118
{
2219
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(DriverBase));
2320

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Data.Common;
23

34
namespace NHibernate.Driver
@@ -6,5 +7,9 @@ public interface IDriveConnectionCommandProvider
67
{
78
DbConnection CreateConnection();
89
DbCommand CreateCommand();
10+
#if NET6_0_OR_GREATER
11+
DbBatch CreateBatch() => throw new NotImplementedException();
12+
bool CanCreateBatch => false;
13+
#endif
914
}
10-
}
15+
}

src/NHibernate/Driver/IDriveConnectionCommandProviderWithBatchSupport.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/NHibernate/Driver/IDriver.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Data.Common;
5-
using System.Linq;
6-
using System.Windows.Input;
75
using NHibernate.Engine;
86
using NHibernate.SqlCommand;
97
using NHibernate.SqlTypes;
@@ -165,5 +163,13 @@ public interface IDriver
165163
/// The minimal date supplied as a <see cref="DateTime" /> supported by this driver.
166164
/// </summary>
167165
DateTime MinDate { get; }
166+
167+
#if NET6_0_OR_GREATER
168+
DbBatch CreateBatch() => throw new NotImplementedException();
169+
bool CanCreateBatch => false;
170+
171+
void AdjustBatch(DbBatch dbBatch) => throw new NotImplementedException();
172+
void PrepareBatch(DbBatch dbBatch) => throw new NotImplementedException();
173+
#endif
168174
}
169175
}

src/NHibernate/Driver/IDriverWithBatchSupport.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/NHibernate/Driver/ReflectionBasedDriver.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,9 @@ public override DbCommand CreateCommand()
7575
}
7676

7777
#if NET6_0_OR_GREATER
78-
public override DbBatch CreateBatch()
79-
{
80-
if (connectionCommandProvider is IDriveConnectionCommandProviderWithBatchSupport driveConnectionCommandProviderWithBatch)
81-
{
82-
return driveConnectionCommandProviderWithBatch.CreateBatch();
83-
}
84-
throw new NotSupportedException();
85-
}
78+
public override DbBatch CreateBatch() => connectionCommandProvider.CreateBatch();
8679

87-
public override bool CanCreateBatch
88-
{
89-
get
90-
{
91-
if (connectionCommandProvider is IDriveConnectionCommandProviderWithBatchSupport driveConnectionCommandProviderWithBatch)
92-
{
93-
return driveConnectionCommandProviderWithBatch.CanCreateBatch;
94-
}
95-
return false;
96-
}
97-
}
80+
public override bool CanCreateBatch => connectionCommandProvider.CanCreateBatch;
9881
#endif
9982
}
10083
}

src/NHibernate/Driver/ReflectionDriveConnectionCommandProvider.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
namespace NHibernate.Driver
66
{
77
public class ReflectionDriveConnectionCommandProvider : IDriveConnectionCommandProvider
8-
#if NET6_0_OR_GREATER
9-
, IDriveConnectionCommandProviderWithBatchSupport
10-
#endif
118
{
129
private readonly System.Type commandType;
1310
private readonly System.Type connectionType;
@@ -49,7 +46,6 @@ public DbCommand CreateCommand()
4946
#endregion
5047

5148
#if NET6_0_OR_GREATER
52-
5349
private Lazy<bool> _canCreateBatch;
5450

5551
public DbBatch CreateBatch()
@@ -63,8 +59,6 @@ public DbBatch CreateBatch()
6359
}
6460

6561
public bool CanCreateBatch => _canCreateBatch.Value;
66-
67-
6862
#endif
6963
}
7064
}

src/NHibernate/ITransaction.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,18 @@ public partial interface ITransaction : IDisposable
8181
"RegisterSynchronization(ITransactionCompletionSynchronization)': the TransactionExtensions extension " +
8282
"method will call it.")]
8383
void RegisterSynchronization(ISynchronization synchronization);
84-
}
8584

8685
#if NET6_0_OR_GREATER
87-
internal interface ITransactionWithBatchSupport : ITransaction
88-
{
8986
/// <summary>
9087
/// Enlist a <see cref="DbBatch"/> in the current Transaction.
9188
/// </summary>
9289
/// <param name="batch">The <see cref="DbBatch"/> to enlist.</param>
9390
/// <remarks>
9491
/// It is okay for this to be a no op implementation.
9592
/// </remarks>
96-
void Enlist(DbBatch batch);
97-
}
93+
void Enlist(DbBatch batch) => throw new NotImplementedException();
9894
#endif
95+
}
9996

10097
// 6.0 TODO: merge into ITransaction
10198
public static class TransactionExtensions

src/NHibernate/Transaction/AdoTransaction.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ namespace NHibernate.Transaction
1313
/// the <see cref="ITransaction" /> interface.
1414
/// </summary>
1515
public partial class AdoTransaction : ITransaction
16-
#if NET6_0_OR_GREATER
17-
, ITransactionWithBatchSupport
18-
#endif
1916
{
2017
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(AdoTransaction));
2118
private ISessionImplementor session;
@@ -112,7 +109,7 @@ public void Enlist(DbBatch batch)
112109
{
113110
if (batch.Transaction != null)
114111
{
115-
log.Warn("set a nonnull DbCommand.Transaction to null because the Session had no Transaction");
112+
log.Warn("set a nonnull DbBatch.Transaction to null because the Session had no Transaction");
116113
}
117114
}
118115

@@ -127,11 +124,11 @@ public void Enlist(DbBatch batch)
127124
// don't need to be confused by that - just a normal part of initialization...
128125
if (batch.Transaction != null && batch.Transaction != trans)
129126
{
130-
log.Warn("The DbCommand had a different Transaction than the Session. This can occur when " +
127+
log.Warn("The DbBatch had a different Transaction than the Session. This can occur when " +
131128
"Disconnecting and Reconnecting Sessions because the PreparedCommand Cache is Session specific.");
132129
}
133130
}
134-
log.Debug("Enlist Command");
131+
log.Debug("Enlist DbBatch");
135132

136133
// If you try to assign a disposed transaction to a command with MSSQL, it will leave the command's
137134
// transaction as null and not throw an error. With SQLite, for example, it will throw an exception

0 commit comments

Comments
 (0)