Skip to content

Commit 8cea97c

Browse files
maca88fredericDelaporte
authored andcommitted
NH-3905 - Changes for having more async methods generated
1 parent 7ed9c60 commit 8cea97c

File tree

9 files changed

+166
-50
lines changed

9 files changed

+166
-50
lines changed

src/NHibernate/AdoNet/AbstractBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public virtual DbDataReader ExecuteReader(DbCommand cmd)
239239

240240
if (!_factory.ConnectionProvider.Driver.SupportsMultipleOpenReaders)
241241
{
242-
reader = new NHybridDataReader(reader);
242+
reader = NHybridDataReader.Create(reader);
243243
}
244244

245245
_readersToClose.Add(reader);

src/NHibernate/AdoNet/ResultSetWrapper.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections;
33
using System.Data;
44
using System.Data.Common;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57

68
namespace NHibernate.AdoNet
79
{
@@ -47,6 +49,21 @@ public override bool Read()
4749
return rs.Read();
4850
}
4951

52+
public override Task<bool> ReadAsync(CancellationToken cancellationToken)
53+
{
54+
return rs.ReadAsync(cancellationToken);
55+
}
56+
57+
public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
58+
{
59+
return rs.NextResultAsync(cancellationToken);
60+
}
61+
62+
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
63+
{
64+
return rs.IsDBNullAsync(ordinal, cancellationToken);
65+
}
66+
5067
public override int Depth
5168
{
5269
get { return rs.Depth; }

src/NHibernate/Driver/BasicResultSetsCommand.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Data;
55
using System.Data.Common;
66
using System.Linq;
7+
using System.Threading;
8+
using System.Threading.Tasks;
79
using NHibernate.Engine;
810
using NHibernate.SqlCommand;
911
using NHibernate.SqlTypes;
@@ -53,7 +55,7 @@ public virtual DbDataReader GetReader(int? commandTimeout)
5355
}
5456
log.Info(command.CommandText);
5557
BindParameters(command);
56-
return new BatcherDataReaderWrapper(batcher, command);
58+
return BatcherDataReaderWrapper.Create(batcher, command);
5759
}
5860

5961
protected virtual void BindParameters(DbCommand command)
@@ -84,9 +86,9 @@ public class BatcherDataReaderWrapper: DbDataReader
8486
{
8587
private readonly IBatcher batcher;
8688
private readonly DbCommand command;
87-
private readonly DbDataReader reader;
89+
private DbDataReader reader;
8890

89-
public BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
91+
protected BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
9092
{
9193
if (batcher == null)
9294
{
@@ -98,7 +100,14 @@ public BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
98100
}
99101
this.batcher = batcher;
100102
this.command = command;
101-
reader = batcher.ExecuteReader(command);
103+
}
104+
105+
public static BatcherDataReaderWrapper Create(IBatcher batcher, DbCommand command)
106+
{
107+
return new BatcherDataReaderWrapper(batcher, command)
108+
{
109+
reader = batcher.ExecuteReader(command)
110+
};
102111
}
103112

104113
public override string GetName(int i)
@@ -266,6 +275,21 @@ public override bool Read()
266275
return reader.Read();
267276
}
268277

278+
public override Task<bool> ReadAsync(CancellationToken cancellationToken)
279+
{
280+
return reader.ReadAsync(cancellationToken);
281+
}
282+
283+
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
284+
{
285+
return reader.IsDBNullAsync(ordinal, cancellationToken);
286+
}
287+
288+
public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
289+
{
290+
return reader.NextResultAsync(cancellationToken);
291+
}
292+
269293
public override int Depth
270294
{
271295
get { return reader.Depth; }
@@ -281,4 +305,4 @@ public override int RecordsAffected
281305
get { return reader.RecordsAffected; }
282306
}
283307
}
284-
}
308+
}

src/NHibernate/Driver/NDataReader.cs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Generic;
44
using System.Data;
55
using System.Data.Common;
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using NHibernate.Util;
79

810
namespace NHibernate.Driver
@@ -32,6 +34,8 @@ public class NDataReader : DbDataReader
3234
private char[] cachedCharArray;
3335
private int cachedColIndex = -1;
3436

37+
protected NDataReader() { }
38+
3539
/// <summary>
3640
/// Creates a NDataReader from a <see cref="DbDataReader" />
3741
/// </summary>
@@ -43,8 +47,9 @@ public class NDataReader : DbDataReader
4347
/// pick up the <see cref="DbDataReader"/> midstream so that the underlying <see cref="DbDataReader"/> can be closed
4448
/// so a new one can be opened.
4549
/// </remarks>
46-
public NDataReader(DbDataReader reader, bool isMidstream)
50+
public static NDataReader Create(DbDataReader reader, bool isMidstream)
4751
{
52+
var dataReader = new NDataReader();
4853
var resultList = new List<NResult>(2);
4954

5055
try
@@ -53,19 +58,19 @@ public NDataReader(DbDataReader reader, bool isMidstream)
5358
// positioned on the first row (index=0)
5459
if (isMidstream)
5560
{
56-
currentRowIndex = 0;
61+
dataReader.currentRowIndex = 0;
5762
}
5863

5964
// there will be atleast one result
60-
resultList.Add(new NResult(reader, isMidstream));
65+
resultList.Add(NResult.Create(reader, isMidstream));
6166

6267
while (reader.NextResult())
6368
{
6469
// the second, third, nth result is not processed midstream
65-
resultList.Add(new NResult(reader, false));
70+
resultList.Add(NResult.Create(reader, false));
6671
}
6772

68-
results = resultList.ToArray();
73+
dataReader.results = resultList.ToArray();
6974
}
7075
catch (Exception e)
7176
{
@@ -75,6 +80,7 @@ public NDataReader(DbDataReader reader, bool isMidstream)
7580
{
7681
reader.Close();
7782
}
83+
return dataReader;
7884
}
7985

8086
/// <summary>
@@ -157,6 +163,21 @@ public override bool Read()
157163
return true;
158164
}
159165

166+
public override Task<bool> ReadAsync(CancellationToken cancellationToken)
167+
{
168+
return Task.FromResult(Read());
169+
}
170+
171+
public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
172+
{
173+
return Task.FromResult(NextResult());
174+
}
175+
176+
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
177+
{
178+
return Task.FromResult(IsDBNull(ordinal));
179+
}
180+
160181
/// <summary></summary>
161182
public override int Depth
162183
{
@@ -463,10 +484,10 @@ public override short GetInt16(int i)
463484
private class NResult
464485
{
465486
// [row][column]
466-
private readonly object[][] records;
487+
private object[][] records;
467488
private int colCount = 0;
468489

469-
private readonly DataTable schemaTable;
490+
private DataTable schemaTable;
470491

471492
// key = field name
472493
// index = field index
@@ -475,6 +496,8 @@ private class NResult
475496
private readonly IList<System.Type> fieldTypes = new List<System.Type>();
476497
private readonly IList<string> fieldDataTypeNames = new List<string>();
477498

499+
private NResult() { }
500+
478501
/// <summary>
479502
/// Initializes a new instance of the NResult class.
480503
/// </summary>
@@ -483,9 +506,12 @@ private class NResult
483506
/// <see langword="true" /> if the <see cref="DbDataReader"/> is already positioned on the record
484507
/// to start reading from.
485508
/// </param>
486-
internal NResult(DbDataReader reader, bool isMidstream)
509+
internal static NResult Create(DbDataReader reader, bool isMidstream)
487510
{
488-
schemaTable = reader.GetSchemaTable();
511+
var result = new NResult
512+
{
513+
schemaTable = reader.GetSchemaTable()
514+
};
489515

490516
List<object[]> recordsList = new List<object[]>();
491517
int rowIndex = 0;
@@ -499,13 +525,13 @@ internal NResult(DbDataReader reader, bool isMidstream)
499525
for (int i = 0; i < reader.FieldCount; i++)
500526
{
501527
string fieldName = reader.GetName(i);
502-
fieldNameToIndex[fieldName] = i;
503-
fieldIndexToName.Add(fieldName);
504-
fieldTypes.Add(reader.GetFieldType(i));
505-
fieldDataTypeNames.Add(reader.GetDataTypeName(i));
528+
result.fieldNameToIndex[fieldName] = i;
529+
result.fieldIndexToName.Add(fieldName);
530+
result.fieldTypes.Add(reader.GetFieldType(i));
531+
result.fieldDataTypeNames.Add(reader.GetDataTypeName(i));
506532
}
507533

508-
colCount = reader.FieldCount;
534+
result.colCount = reader.FieldCount;
509535
}
510536

511537
rowIndex++;
@@ -519,7 +545,8 @@ internal NResult(DbDataReader reader, bool isMidstream)
519545
isMidstream = false;
520546
}
521547

522-
records = recordsList.ToArray();
548+
result.records = recordsList.ToArray();
549+
return result;
523550
}
524551

525552
/// <summary>
@@ -635,4 +662,4 @@ public int RowCount
635662
}
636663
}
637664
}
638-
}
665+
}

src/NHibernate/Driver/NHybridDataReader.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections;
33
using System.Data;
44
using System.Data.Common;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57

68
namespace NHibernate.Driver
79
{
@@ -30,29 +32,34 @@ public class NHybridDataReader : DbDataReader
3032

3133
public DbDataReader Target { get { return _reader; } }
3234

35+
protected NHybridDataReader() { }
36+
3337
/// <summary>
3438
/// Initializes a new instance of the <see cref="NHybridDataReader"/> class.
3539
/// </summary>
3640
/// <param name="reader">The underlying DbDataReader to use.</param>
37-
public NHybridDataReader(DbDataReader reader) : this(reader, false)
41+
public static NHybridDataReader Create(DbDataReader reader)
3842
{
43+
return Create(reader, false);
3944
}
4045

4146
/// <summary>
4247
/// Initializes a new instance of the NHybridDataReader class.
4348
/// </summary>
4449
/// <param name="reader">The underlying DbDataReader to use.</param>
4550
/// <param name="inMemory"><see langword="true" /> if the contents of the DbDataReader should be read into memory right away.</param>
46-
public NHybridDataReader(DbDataReader reader, bool inMemory)
51+
public static NHybridDataReader Create(DbDataReader reader, bool inMemory)
4752
{
53+
var dataReader = new NHybridDataReader();
4854
if (inMemory)
4955
{
50-
_reader = new NDataReader(reader, false);
56+
dataReader._reader = NDataReader.Create(reader, false);
5157
}
5258
else
5359
{
54-
_reader = reader;
60+
dataReader._reader = reader;
5561
}
62+
return dataReader;
5663
}
5764

5865
/// <summary>
@@ -70,7 +77,7 @@ public void ReadIntoMemory()
7077
{
7178
log.Debug("Moving DbDataReader into an NDataReader. It was converted in midstream " + _isMidstream.ToString());
7279
}
73-
_reader = new NDataReader(_reader, _isMidstream);
80+
_reader = NDataReader.Create(_reader, _isMidstream);
7481
}
7582
}
7683

@@ -121,6 +128,23 @@ public override bool Read()
121128
return _isMidstream;
122129
}
123130

131+
public override async Task<bool> ReadAsync(CancellationToken cancellationToken)
132+
{
133+
_isMidstream = await _reader.ReadAsync(cancellationToken).ConfigureAwait(false);
134+
return _isMidstream;
135+
}
136+
137+
public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
138+
{
139+
_isMidstream = false;
140+
return _reader.NextResultAsync(cancellationToken);
141+
}
142+
143+
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
144+
{
145+
return _reader.IsDBNullAsync(ordinal, cancellationToken);
146+
}
147+
124148
/// <summary></summary>
125149
public override int Depth
126150
{

src/NHibernate/ISession.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,10 @@ public interface ISession : IDisposable
525525
/// This operation cascades to associated instances if the association is mapped
526526
/// with <tt>cascade="merge"</tt>.<br/>
527527
/// The semantics of this method are defined by JSR-220.
528+
/// </summary>
528529
/// <param name="entityName">Name of the entity.</param>
529530
/// <param name="obj">a detached instance with state to be copied </param>
530531
/// <returns> an updated persistent instance </returns>
531-
/// </summary>
532-
/// <returns></returns>
533532
object Merge(string entityName, object obj);
534533

535534
/// <summary>
@@ -555,11 +554,10 @@ public interface ISession : IDisposable
555554
/// This operation cascades to associated instances if the association is mapped
556555
/// with <tt>cascade="merge"</tt>.<br/>
557556
/// The semantics of this method are defined by JSR-220.
557+
/// </summary>
558558
/// <param name="entityName">Name of the entity.</param>
559559
/// <param name="entity">a detached instance with state to be copied </param>
560560
/// <returns> an updated persistent instance </returns>
561-
/// </summary>
562-
/// <returns></returns>
563561
T Merge<T>(string entityName, T entity) where T : class;
564562

565563
/// <summary>

0 commit comments

Comments
 (0)