Skip to content

[WIP] - NH-2088 - Preliminary work required to generate async code #623

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/NHibernate/AdoNet/AbstractBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public virtual DbDataReader ExecuteReader(DbCommand cmd)

if (!_factory.ConnectionProvider.Driver.SupportsMultipleOpenReaders)
{
reader = new NHybridDataReader(reader);
reader = NHybridDataReader.Create(reader);
}

_readersToClose.Add(reader);
Expand Down
17 changes: 17 additions & 0 deletions src/NHibernate/AdoNet/ResultSetWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;

namespace NHibernate.AdoNet
{
Expand Down Expand Up @@ -47,6 +49,21 @@ public override bool Read()
return rs.Read();
}

public override Task<bool> ReadAsync(CancellationToken cancellationToken)
{
return rs.ReadAsync(cancellationToken);
}

public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
{
return rs.NextResultAsync(cancellationToken);
}

public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
{
return rs.IsDBNullAsync(ordinal, cancellationToken);
}

public override int Depth
{
get { return rs.Depth; }
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Collection/AbstractPersistentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ protected virtual ICollection GetOrphans(ICollection oldElements, ICollection cu
var currentIds = new HashSet<TypedValue>();
foreach (object current in currentElements)
{
if (current != null && ForeignKeys.IsNotTransient(entityName, current, null, session))
if (current != null && ForeignKeys.IsNotTransientSlow(entityName, current, session))
{
object currentId = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, current, session);
currentIds.Add(new TypedValue(idType, currentId));
Expand All @@ -736,7 +736,7 @@ protected virtual ICollection GetOrphans(ICollection oldElements, ICollection cu

public void IdentityRemove(IList list, object obj, string entityName, ISessionImplementor session)
{
if (obj != null && ForeignKeys.IsNotTransient(entityName, obj, null, session))
if (obj != null && ForeignKeys.IsNotTransientSlow(entityName, obj, session))
{
IType idType = session.Factory.GetEntityPersister(entityName).IdentifierType;

Expand Down
40 changes: 36 additions & 4 deletions src/NHibernate/Criterion/QueryOver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,30 @@ private IFutureValue<U> FutureValue<U>()
return criteria.FutureValue<U>();
}

/// <summary>
/// Get an executable instance of <c>IQueryOver&lt;TRoot&gt;</c>,
/// to actually run the query.</summary>
public IQueryOver<TRoot,TRoot> GetExecutableQueryOver(ISession session)
private IAsyncEnumerable<TRoot> FutureAsync()
{
return criteria.FutureAsync<TRoot>();
}

private IAsyncEnumerable<U> FutureAsync<U>()
{
return criteria.FutureAsync<U>();
}

private IFutureValueAsync<TRoot> FutureValueAsync()
{
return criteria.FutureValueAsync<TRoot>();
}

private IFutureValueAsync<U> FutureValueAsync<U>()
{
return criteria.FutureValueAsync<U>();
}

/// <summary>
/// Get an executable instance of <c>IQueryOver&lt;TRoot&gt;</c>,
/// to actually run the query.</summary>
public IQueryOver<TRoot,TRoot> GetExecutableQueryOver(ISession session)
{
impl.Session = session.GetSessionImplementation();
return new QueryOver<TRoot,TRoot>(impl);
Expand Down Expand Up @@ -246,6 +266,18 @@ IFutureValue<TRoot> IQueryOver<TRoot>.FutureValue()
IFutureValue<U> IQueryOver<TRoot>.FutureValue<U>()
{ return FutureValue<U>(); }

IAsyncEnumerable<TRoot> IQueryOver<TRoot>.FutureAsync()
{ return FutureAsync(); }

IAsyncEnumerable<U> IQueryOver<TRoot>.FutureAsync<U>()
{ return FutureAsync<U>(); }

IFutureValueAsync<TRoot> IQueryOver<TRoot>.FutureValueAsync()
{ return FutureValueAsync(); }

IFutureValueAsync<U> IQueryOver<TRoot>.FutureValueAsync<U>()
{ return FutureValueAsync<U>(); }

IQueryOver<TRoot,TRoot> IQueryOver<TRoot>.Clone()
{ return Clone(); }

Expand Down
32 changes: 28 additions & 4 deletions src/NHibernate/Driver/BasicResultSetsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
using System.Threading;
using System.Threading.Tasks;

namespace NHibernate.Driver
{
Expand Down Expand Up @@ -53,7 +55,7 @@ public virtual DbDataReader GetReader(int? commandTimeout)
}
log.Info(command.CommandText);
BindParameters(command);
return new BatcherDataReaderWrapper(batcher, command);
return BatcherDataReaderWrapper.Create(batcher, command);
}

protected virtual void BindParameters(DbCommand command)
Expand Down Expand Up @@ -84,9 +86,9 @@ public class BatcherDataReaderWrapper: DbDataReader
{
private readonly IBatcher batcher;
private readonly DbCommand command;
private readonly DbDataReader reader;
private DbDataReader reader;

public BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
protected BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
{
if (batcher == null)
{
Expand All @@ -98,7 +100,14 @@ public BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
}
this.batcher = batcher;
this.command = command;
reader = batcher.ExecuteReader(command);
}

public static BatcherDataReaderWrapper Create(IBatcher batcher, DbCommand command)
{
return new BatcherDataReaderWrapper(batcher, command)
{
reader = batcher.ExecuteReader(command)
};
}

public override string GetName(int i)
Expand Down Expand Up @@ -266,6 +275,21 @@ public override bool Read()
return reader.Read();
}

public override Task<bool> ReadAsync(CancellationToken cancellationToken)
{
return reader.ReadAsync(cancellationToken);
}

public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
{
return reader.IsDBNullAsync(ordinal, cancellationToken);
}

public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
{
return reader.NextResultAsync(cancellationToken);
}

public override int Depth
{
get { return reader.Depth; }
Expand Down
57 changes: 42 additions & 15 deletions src/NHibernate/Driver/NDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Data;
using System.Data.Common;
using NHibernate.Util;
using System.Threading;
using System.Threading.Tasks;

namespace NHibernate.Driver
{
Expand Down Expand Up @@ -32,6 +34,8 @@ public class NDataReader : DbDataReader
private char[] cachedCharArray;
private int cachedColIndex = -1;

protected NDataReader() { }

/// <summary>
/// Creates a NDataReader from a <see cref="DbDataReader" />
/// </summary>
Expand All @@ -43,8 +47,9 @@ public class NDataReader : DbDataReader
/// pick up the <see cref="DbDataReader"/> midstream so that the underlying <see cref="DbDataReader"/> can be closed
/// so a new one can be opened.
/// </remarks>
public NDataReader(DbDataReader reader, bool isMidstream)
public static NDataReader Create(DbDataReader reader, bool isMidstream)
{
var dataReader = new NDataReader();
var resultList = new List<NResult>(2);

try
Expand All @@ -53,19 +58,19 @@ public NDataReader(DbDataReader reader, bool isMidstream)
// positioned on the first row (index=0)
if (isMidstream)
{
currentRowIndex = 0;
dataReader.currentRowIndex = 0;
}

// there will be atleast one result
resultList.Add(new NResult(reader, isMidstream));
resultList.Add(NResult.Create(reader, isMidstream));

while (reader.NextResult())
{
// the second, third, nth result is not processed midstream
resultList.Add(new NResult(reader, false));
resultList.Add(NResult.Create(reader, false));
}

results = resultList.ToArray();
dataReader.results = resultList.ToArray();
}
catch (Exception e)
{
Expand All @@ -75,6 +80,7 @@ public NDataReader(DbDataReader reader, bool isMidstream)
{
reader.Close();
}
return dataReader;
}

/// <summary>
Expand Down Expand Up @@ -157,6 +163,21 @@ public override bool Read()
return true;
}

public override Task<bool> ReadAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Read());
}

public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
{
return Task.FromResult(NextResult());
}

public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
{
return Task.FromResult(IsDBNull(ordinal));
}

/// <summary></summary>
public override int Depth
{
Expand Down Expand Up @@ -463,10 +484,10 @@ public override short GetInt16(int i)
private class NResult
{
// [row][column]
private readonly object[][] records;
private object[][] records;
private int colCount = 0;

private readonly DataTable schemaTable;
private DataTable schemaTable;

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

private NResult() { }

/// <summary>
/// Initializes a new instance of the NResult class.
/// </summary>
Expand All @@ -483,9 +506,12 @@ private class NResult
/// <see langword="true" /> if the <see cref="DbDataReader"/> is already positioned on the record
/// to start reading from.
/// </param>
internal NResult(DbDataReader reader, bool isMidstream)
internal static NResult Create(DbDataReader reader, bool isMidstream)
{
schemaTable = reader.GetSchemaTable();
var result = new NResult
{
schemaTable = reader.GetSchemaTable()
};

List<object[]> recordsList = new List<object[]>();
int rowIndex = 0;
Expand All @@ -499,13 +525,13 @@ internal NResult(DbDataReader reader, bool isMidstream)
for (int i = 0; i < reader.FieldCount; i++)
{
string fieldName = reader.GetName(i);
fieldNameToIndex[fieldName] = i;
fieldIndexToName.Add(fieldName);
fieldTypes.Add(reader.GetFieldType(i));
fieldDataTypeNames.Add(reader.GetDataTypeName(i));
result.fieldNameToIndex[fieldName] = i;
result.fieldIndexToName.Add(fieldName);
result.fieldTypes.Add(reader.GetFieldType(i));
result.fieldDataTypeNames.Add(reader.GetDataTypeName(i));
}

colCount = reader.FieldCount;
result.colCount = reader.FieldCount;
}

rowIndex++;
Expand All @@ -519,7 +545,8 @@ internal NResult(DbDataReader reader, bool isMidstream)
isMidstream = false;
}

records = recordsList.ToArray();
result.records = recordsList.ToArray();
return result;
}

/// <summary>
Expand Down
Loading