Skip to content

Commit

Permalink
Moved the creation of DbBatchCommands to the driver
Browse files Browse the repository at this point in the history
  • Loading branch information
gliljas committed Dec 20, 2024
1 parent c31b36c commit 32e30d2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
11 changes: 2 additions & 9 deletions src/NHibernate/AdoNet/DbBatchBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,8 @@ private void LogBatchCommand(DbCommand batchUpdate)

private void AddCommandToBatch(DbCommand batchUpdate)
{
var dbBatchCommand = _currentBatch.CreateBatchCommand();
dbBatchCommand.CommandText = batchUpdate.CommandText;
dbBatchCommand.CommandType = batchUpdate.CommandType;

foreach (var param in batchUpdate.Parameters)
{
dbBatchCommand.Parameters.Add(((ICloneable) param).Clone());
}

var dbBatchCommand = Driver.CreateDbBatchCommandFromDbCommand(_currentBatch, batchUpdate);

_currentBatch.BatchCommands.Add(dbBatchCommand);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DbCommand CreateCommand()
#if NET6_0_OR_GREATER
public DbBatch CreateBatch() => dbProviderFactory.CreateBatch();

public bool CanCreateBatch => dbProviderFactory.CanCreateBatch && dbProviderFactory.CreateCommand() is ICloneable;
public bool CanCreateBatch => dbProviderFactory.CanCreateBatch && dbProviderFactory.CreateCommand().CreateParameter() is ICloneable;
#endif
}
}
19 changes: 19 additions & 0 deletions src/NHibernate/Driver/DriverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,25 @@ public virtual DbBatch CreateBatch()

public virtual bool CanCreateBatch => false;

/// <summary>
/// Override to use a custom mechanism to create a <see cref="DbBatchCommand"/> from a <see cref="DbCommand"/>.
/// The default implementation relies on the parameters implementing (and properly supporting) <see cref="System.ICloneable"/>
/// </summary>
/// <param name="dbBatch"></param>
/// <param name="dbCommand"></param>
/// <returns></returns>
public virtual DbBatchCommand CreateDbBatchCommandFromDbCommand(DbBatch dbBatch, DbCommand dbCommand)
{
var dbBatchCommand = dbBatch.CreateBatchCommand();
dbBatchCommand.CommandText = dbCommand.CommandText;
dbBatchCommand.CommandType = dbCommand.CommandType;

foreach (var param in dbCommand.Parameters)
{
dbBatchCommand.Parameters.Add(((ICloneable) param).Clone());
}
return dbBatchCommand;
}

#endif

Expand Down
35 changes: 35 additions & 0 deletions src/NHibernate/Driver/IDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,46 @@ public interface IDriver
DateTime MinDate { get; }

#if NET6_0_OR_GREATER
/// <summary>
/// Create a <see cref="DbBatch"/>
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
DbBatch CreateBatch() => throw new NotImplementedException();

/// <summary>
/// Can this driver create <see cref="DbBatch"/>es?
/// </summary>
bool CanCreateBatch => false;

/// <summary>
/// Make any adjustments to each <see cref="DbBatch"/> object before it is added to the batcher.
/// </summary>
/// <param name="dbBatch">The batch.</param>
/// <remarks>
/// This method should be executed before adding each single batch to the batcher.
/// If you have to adjust parameters values/type (when the command is fullfilled) this is a good place to do it.
/// </remarks>
void AdjustBatch(DbBatch dbBatch) => throw new NotImplementedException();

/// <summary>
/// Prepare the <paramref name="dbBatch" /> by calling <see cref="DbBatch.Prepare()" />.
/// May be a no-op if the driver does not support preparing commands, or for any other reason.
/// </summary>
/// <param name="dbBatch">The batch.</param>
void PrepareBatch(DbBatch dbBatch) => throw new NotImplementedException();

/// <summary>
/// Creates (clones) a <see cref="DbBatchCommand"/> from a <see cref="DbCommand"/>,
/// copying its <see cref="DbCommand.CommandText"/>, <see cref="DbCommand.CommandType"/>
/// and all its parameters.
/// The returned <see cref="DbBatchCommand"/> will not be added to <paramref name="dbBatch"/>
/// </summary>
/// <param name="dbBatch"></param>
/// <param name="dbCommand"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
DbBatchCommand CreateDbBatchCommandFromDbCommand(DbBatch dbBatch, DbCommand dbCommand) => throw new NotImplementedException();
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ReflectionDriveConnectionCommandProvider(System.Type connectionType, Syst
_canCreateBatch = new Lazy<bool>(() => {
using (var connection = CreateConnection())
{
return connection.CanCreateBatch && connection.CreateCommand() is ICloneable;
return connection.CanCreateBatch && connection.CreateCommand().CreateParameter() is ICloneable;
}
});
#endif
Expand Down

0 comments on commit 32e30d2

Please sign in to comment.