Skip to content

Commit 32e30d2

Browse files
committed
Moved the creation of DbBatchCommands to the driver
1 parent c31b36c commit 32e30d2

5 files changed

+58
-11
lines changed

src/NHibernate/AdoNet/DbBatchBatcher.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,8 @@ private void LogBatchCommand(DbCommand batchUpdate)
6666

6767
private void AddCommandToBatch(DbCommand batchUpdate)
6868
{
69-
var dbBatchCommand = _currentBatch.CreateBatchCommand();
70-
dbBatchCommand.CommandText = batchUpdate.CommandText;
71-
dbBatchCommand.CommandType = batchUpdate.CommandType;
72-
73-
foreach (var param in batchUpdate.Parameters)
74-
{
75-
dbBatchCommand.Parameters.Add(((ICloneable) param).Clone());
76-
}
77-
69+
var dbBatchCommand = Driver.CreateDbBatchCommandFromDbCommand(_currentBatch, batchUpdate);
70+
7871
_currentBatch.BatchCommands.Add(dbBatchCommand);
7972
}
8073

src/NHibernate/Driver/DbProviderFactoryDriveConnectionCommandProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public DbCommand CreateCommand()
2828
#if NET6_0_OR_GREATER
2929
public DbBatch CreateBatch() => dbProviderFactory.CreateBatch();
3030

31-
public bool CanCreateBatch => dbProviderFactory.CanCreateBatch && dbProviderFactory.CreateCommand() is ICloneable;
31+
public bool CanCreateBatch => dbProviderFactory.CanCreateBatch && dbProviderFactory.CreateCommand().CreateParameter() is ICloneable;
3232
#endif
3333
}
3434
}

src/NHibernate/Driver/DriverBase.cs

+19
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,25 @@ public virtual DbBatch CreateBatch()
371371

372372
public virtual bool CanCreateBatch => false;
373373

374+
/// <summary>
375+
/// Override to use a custom mechanism to create a <see cref="DbBatchCommand"/> from a <see cref="DbCommand"/>.
376+
/// The default implementation relies on the parameters implementing (and properly supporting) <see cref="System.ICloneable"/>
377+
/// </summary>
378+
/// <param name="dbBatch"></param>
379+
/// <param name="dbCommand"></param>
380+
/// <returns></returns>
381+
public virtual DbBatchCommand CreateDbBatchCommandFromDbCommand(DbBatch dbBatch, DbCommand dbCommand)
382+
{
383+
var dbBatchCommand = dbBatch.CreateBatchCommand();
384+
dbBatchCommand.CommandText = dbCommand.CommandText;
385+
dbBatchCommand.CommandType = dbCommand.CommandType;
386+
387+
foreach (var param in dbCommand.Parameters)
388+
{
389+
dbBatchCommand.Parameters.Add(((ICloneable) param).Clone());
390+
}
391+
return dbBatchCommand;
392+
}
374393

375394
#endif
376395

src/NHibernate/Driver/IDriver.cs

+35
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,46 @@ public interface IDriver
165165
DateTime MinDate { get; }
166166

167167
#if NET6_0_OR_GREATER
168+
/// <summary>
169+
/// Create a <see cref="DbBatch"/>
170+
/// </summary>
171+
/// <returns></returns>
172+
/// <exception cref="NotImplementedException"></exception>
168173
DbBatch CreateBatch() => throw new NotImplementedException();
174+
175+
/// <summary>
176+
/// Can this driver create <see cref="DbBatch"/>es?
177+
/// </summary>
169178
bool CanCreateBatch => false;
170179

180+
/// <summary>
181+
/// Make any adjustments to each <see cref="DbBatch"/> object before it is added to the batcher.
182+
/// </summary>
183+
/// <param name="dbBatch">The batch.</param>
184+
/// <remarks>
185+
/// This method should be executed before adding each single batch to the batcher.
186+
/// If you have to adjust parameters values/type (when the command is fullfilled) this is a good place to do it.
187+
/// </remarks>
171188
void AdjustBatch(DbBatch dbBatch) => throw new NotImplementedException();
189+
190+
/// <summary>
191+
/// Prepare the <paramref name="dbBatch" /> by calling <see cref="DbBatch.Prepare()" />.
192+
/// May be a no-op if the driver does not support preparing commands, or for any other reason.
193+
/// </summary>
194+
/// <param name="dbBatch">The batch.</param>
172195
void PrepareBatch(DbBatch dbBatch) => throw new NotImplementedException();
196+
197+
/// <summary>
198+
/// Creates (clones) a <see cref="DbBatchCommand"/> from a <see cref="DbCommand"/>,
199+
/// copying its <see cref="DbCommand.CommandText"/>, <see cref="DbCommand.CommandType"/>
200+
/// and all its parameters.
201+
/// The returned <see cref="DbBatchCommand"/> will not be added to <paramref name="dbBatch"/>
202+
/// </summary>
203+
/// <param name="dbBatch"></param>
204+
/// <param name="dbCommand"></param>
205+
/// <returns></returns>
206+
/// <exception cref="NotImplementedException"></exception>
207+
DbBatchCommand CreateDbBatchCommandFromDbCommand(DbBatch dbBatch, DbCommand dbCommand) => throw new NotImplementedException();
173208
#endif
174209
}
175210
}

src/NHibernate/Driver/ReflectionDriveConnectionCommandProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ReflectionDriveConnectionCommandProvider(System.Type connectionType, Syst
2525
_canCreateBatch = new Lazy<bool>(() => {
2626
using (var connection = CreateConnection())
2727
{
28-
return connection.CanCreateBatch && connection.CreateCommand() is ICloneable;
28+
return connection.CanCreateBatch && connection.CreateCommand().CreateParameter() is ICloneable;
2929
}
3030
});
3131
#endif

0 commit comments

Comments
 (0)