Skip to content

Fix GenericBatchingBatcher to call IInterceptor.OnPrepareStatement #2285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/NHibernate.Test/Ado/GenericBatchingBatcherFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using NHibernate.AdoNet;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Linq;
using NHibernate.SqlCommand;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

Expand Down Expand Up @@ -102,6 +104,63 @@ public void MassivePerformanceTest(bool batched)
}
}

[Test]
public void InterceptorOnPrepareStatementTest()
{
var interceptor = new DatabaseInterceptor();
using (var sqlLog = new SqlLogSpy())
using (var s = Sfi.WithOptions().Interceptor(interceptor).OpenSession())
using (var tx = s.BeginTransaction())
{
s.SetBatchSize(5);
for (var i = 0; i < 20; i++)
{
s.Save(new VerySimple { Id = 1 + i, Name = $"Fabio{i}", Weight = 1.45 + i });
}

tx.Commit();

// Called for each insert statement and once for generating AbstractBatcher.CurrentCommand
Assert.That(interceptor.TotalCalls, Is.EqualTo(21));
var log = sqlLog.GetWholeLog();
Assert.That(FindAllOccurrences(log, "/* TEST */"), Is.EqualTo(20));
}

interceptor = new DatabaseInterceptor();
using (var sqlLog = new SqlLogSpy())
using (var s = Sfi.WithOptions().Interceptor(interceptor).OpenSession())
using (var tx = s.BeginTransaction())
{
var future = s.Query<VerySimple>().ToFuture();
s.Query<VerySimple>().Where(o => o.Weight > 0).ToFuture();

using (var enumerator = future.GetEnumerable().GetEnumerator())
{
while (enumerator.MoveNext()) { }
}

tx.Commit();

var totalCalls = Sfi.ConnectionProvider.Driver.SupportsMultipleQueries ? 1 : 2;
Assert.That(interceptor.TotalCalls, Is.EqualTo(totalCalls));
var log = sqlLog.GetWholeLog();
Assert.That(FindAllOccurrences(log, "/* TEST */"), Is.EqualTo(totalCalls));
}

Cleanup();
}

private class DatabaseInterceptor : EmptyInterceptor
{
public int TotalCalls { get; private set; }

public override SqlString OnPrepareStatement(SqlString sql)
{
TotalCalls++;
return sql.Append("/* TEST */");
}
}

private void BatchInsert(int totalRecords)
{
Sfi.Statistics.Clear();
Expand Down
60 changes: 59 additions & 1 deletion src/NHibernate.Test/Async/Ado/GenericBatchingBatcherFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
using NHibernate.AdoNet;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Linq;
using NHibernate.SqlCommand;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;
using NHibernate.Linq;

namespace NHibernate.Test.Ado
{
Expand Down Expand Up @@ -115,6 +116,63 @@ public async Task MassivePerformanceTestAsync(bool batched)
}
}

[Test]
public async Task InterceptorOnPrepareStatementTestAsync()
{
var interceptor = new DatabaseInterceptor();
using (var sqlLog = new SqlLogSpy())
using (var s = Sfi.WithOptions().Interceptor(interceptor).OpenSession())
using (var tx = s.BeginTransaction())
{
s.SetBatchSize(5);
for (var i = 0; i < 20; i++)
{
await (s.SaveAsync(new VerySimple { Id = 1 + i, Name = $"Fabio{i}", Weight = 1.45 + i }));
}

await (tx.CommitAsync());

// Called for each insert statement and once for generating AbstractBatcher.CurrentCommand
Assert.That(interceptor.TotalCalls, Is.EqualTo(21));
var log = sqlLog.GetWholeLog();
Assert.That(FindAllOccurrences(log, "/* TEST */"), Is.EqualTo(20));
}

interceptor = new DatabaseInterceptor();
using (var sqlLog = new SqlLogSpy())
using (var s = Sfi.WithOptions().Interceptor(interceptor).OpenSession())
using (var tx = s.BeginTransaction())
{
var future = s.Query<VerySimple>().ToFuture();
s.Query<VerySimple>().Where(o => o.Weight > 0).ToFuture();

using (var enumerator = (await (future.GetEnumerableAsync())).GetEnumerator())
{
while (enumerator.MoveNext()) { }
}

await (tx.CommitAsync());

var totalCalls = Sfi.ConnectionProvider.Driver.SupportsMultipleQueries ? 1 : 2;
Assert.That(interceptor.TotalCalls, Is.EqualTo(totalCalls));
var log = sqlLog.GetWholeLog();
Assert.That(FindAllOccurrences(log, "/* TEST */"), Is.EqualTo(totalCalls));
}

await (CleanupAsync());
}

private class DatabaseInterceptor : EmptyInterceptor
{
public int TotalCalls { get; private set; }

public override SqlString OnPrepareStatement(SqlString sql)
{
TotalCalls++;
return sql.Append("/* TEST */");
}
}

private async Task BatchInsertAsync(int totalRecords, CancellationToken cancellationToken = default(CancellationToken))
{
Sfi.Statistics.Clear();
Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/AdoNet/GenericBatchingBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void Append(DbParameterCollection parameters)
_commandType = _batcher.CurrentCommand.CommandType;
}

_sql.Add(_batcher.CurrentCommandSql.Copy());
_sql.Add(_batcher.GetSQL(_batcher.CurrentCommandSql.Copy()));
_sqlTypes.AddRange(_batcher.CurrentCommandParameterTypes);

foreach (DbParameter parameter in parameters)
Expand All @@ -207,6 +207,7 @@ public int ExecuteNonQuery()
{
return 0;
}

var batcherCommand = _batcher.Driver.GenerateCommand(
_commandType,
_sql.ToSqlString(),
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Async/AdoNet/GenericBatchingBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{
return 0;
}

var batcherCommand = _batcher.Driver.GenerateCommand(
_commandType,
_sql.ToSqlString(),
Expand Down