Skip to content

Commit 802f4bd

Browse files
David EllingsworthDavid Ellingsworth
David Ellingsworth
authored and
David Ellingsworth
committed
GH-3530: Add a mechanism to Drivers to execute a command and return a DbDataReader.
1 parent 6c6d8a9 commit 802f4bd

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

src/NHibernate/AdoNet/AbstractBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private DbDataReader DoExecuteReader(DbCommand cmd)
252252
{
253253
try
254254
{
255-
var reader = cmd.ExecuteReader();
255+
var reader = Driver.ExecuteReader(cmd);
256256
if (reader == null)
257257
{
258258
// MySql may return null instead of an exception, by example when the query is canceled by another thread.

src/NHibernate/Async/AdoNet/AbstractBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private async Task<DbDataReader> DoExecuteReaderAsync(DbCommand cmd, Cancellatio
167167
cancellationToken.ThrowIfCancellationRequested();
168168
try
169169
{
170-
var reader = await (cmd.ExecuteReaderAsync(cancellationToken)).ConfigureAwait(false);
170+
var reader = await (Driver.ExecuteReaderAsync(cmd, cancellationToken)).ConfigureAwait(false);
171171
if (reader == null)
172172
{
173173
// MySql may return null instead of an exception, by example when the query is canceled by another thread.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Data;
14+
using System.Data.Common;
15+
using System.Linq;
16+
using NHibernate.Engine;
17+
using NHibernate.SqlCommand;
18+
using NHibernate.SqlTypes;
19+
using NHibernate.Util;
20+
using Environment = NHibernate.Cfg.Environment;
21+
22+
namespace NHibernate.Driver
23+
{
24+
using System.Threading.Tasks;
25+
using System.Threading;
26+
public abstract partial class DriverBase : IDriver, ISqlParameterFormatter
27+
{
28+
29+
#if NETFX
30+
#else
31+
32+
#endif
33+
34+
/// <inheritdoc />
35+
public virtual Task<DbDataReader> ExecuteReaderAsync(DbCommand command, CancellationToken cancellationToken)
36+
{
37+
if (cancellationToken.IsCancellationRequested)
38+
{
39+
return Task.FromCanceled<DbDataReader>(cancellationToken);
40+
}
41+
return command.ExecuteReaderAsync(cancellationToken);
42+
}
43+
}
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Data;
12+
using System.Data.Common;
13+
using NHibernate.AdoNet;
14+
using NHibernate.SqlTypes;
15+
using NHibernate.Util;
16+
17+
namespace NHibernate.Driver
18+
{
19+
using System.Threading.Tasks;
20+
using System.Threading;
21+
public static partial class DriverExtensions
22+
{
23+
24+
// 6.0 TODO: merge into IDriver
25+
/// <summary>
26+
/// Executes the command and returns a <see cref="DbDataReader"/>.
27+
/// </summary>
28+
/// <param name="driver">The driver.</param>
29+
/// <param name="command">The command to execute.</param>
30+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
31+
/// <returns>A DbDataReader</returns>
32+
public static Task<DbDataReader> ExecuteReaderAsync(this IDriver driver, DbCommand command, CancellationToken cancellationToken)
33+
{
34+
if (cancellationToken.IsCancellationRequested)
35+
{
36+
return Task.FromCanceled<DbDataReader>(cancellationToken);
37+
}
38+
try
39+
{
40+
return driver is DriverBase driverBase ? driverBase.ExecuteReaderAsync(command, cancellationToken) : command.ExecuteReaderAsync(cancellationToken);
41+
}
42+
catch (System.Exception ex)
43+
{
44+
return Task.FromException<DbDataReader>(ex);
45+
}
46+
}
47+
48+
}
49+
}

src/NHibernate/Driver/DriverBase.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace NHibernate.Driver
1414
/// <summary>
1515
/// Base class for the implementation of IDriver
1616
/// </summary>
17-
public abstract class DriverBase : IDriver, ISqlParameterFormatter
17+
public abstract partial class DriverBase : IDriver, ISqlParameterFormatter
1818
{
1919
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(DriverBase));
2020

@@ -361,5 +361,11 @@ public DbParameter GenerateOutputParameter(DbCommand command)
361361
/// Get the timeout in seconds for ADO.NET queries.
362362
/// </summary>
363363
public virtual int CommandTimeout => commandTimeout;
364+
365+
/// <inheritdoc />
366+
public virtual DbDataReader ExecuteReader(DbCommand command)
367+
{
368+
return command.ExecuteReader();
369+
}
364370
}
365371
}

src/NHibernate/Driver/DriverExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace NHibernate.Driver
88
{
9-
public static class DriverExtensions
9+
public static partial class DriverExtensions
1010
{
1111
internal static void AdjustParameterForValue(this IDriver driver, DbParameter parameter, SqlType sqlType, object value)
1212
{
@@ -60,5 +60,18 @@ public static DbCommand UnwrapDbCommand(this IDriver driver, DbCommand command)
6060
{
6161
return driver is DriverBase driverBase ? driverBase.UnwrapDbCommand(command) : command;
6262
}
63+
64+
// 6.0 TODO: merge into IDriver
65+
/// <summary>
66+
/// Executes the command and returns a <see cref="DbDataReader"/>.
67+
/// </summary>
68+
/// <param name="driver">The driver.</param>
69+
/// <param name="command">The command to execute.</param>
70+
/// <returns>A DbDataReader</returns>
71+
public static DbDataReader ExecuteReader(this IDriver driver, DbCommand command)
72+
{
73+
return driver is DriverBase driverBase ? driverBase.ExecuteReader(command) : command.ExecuteReader();
74+
}
75+
6376
}
6477
}

0 commit comments

Comments
 (0)