Skip to content

Commit

Permalink
DbDataAdapter async methods #8 - introducing RecordSet.FromReader and…
Browse files Browse the repository at this point in the history
… RecordSet.FromReaderAsync
  • Loading branch information
VitaliyMF committed Aug 24, 2016
1 parent 11123da commit 08958af
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/NReco.Data.Tests/RecordSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void RecordSetReader() {
Assert.Equal(100, cnt);

// read RS from RecordSetReader
var testRSCopy = new RecordSet( new RecordSetReader(testRS) );
var testRSCopy = RecordSet.FromReader( new RecordSetReader(testRS) );
Assert.Equal( testRS.Count, testRSCopy.Count );
Assert.Equal( testRS.Columns.Count, testRSCopy.Columns.Count );

Expand Down
8 changes: 8 additions & 0 deletions src/NReco.Data/Internal/DataReaderAsyncExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ internal static Task<bool> ReadAsync(this IDataReader rdr, CancellationToken can
}
}

internal static Task<int> GetValuesAsync(this IDataReader rdr, object[] values, CancellationToken cancel) {
if (rdr is DbDataReader) {
return ((DbDataReader)rdr).GetValuesAsync(values, cancel);
} else {
return Task.FromResult(rdr.GetValues(values));
}
}

}


Expand Down
83 changes: 52 additions & 31 deletions src/NReco.Data/RecordSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Data;

Expand Down Expand Up @@ -59,15 +60,6 @@ public int Count {
public RecordSet(Column[] columns) : this(columns, 1) {
}

/// <summary>
/// Initializes a new instance of <see cref="RecordSet"/> with schema and data from specified <see cref="IDataReader"/>.
/// </summary>
/// <param name="rdr">An <see cref="IDataReader"/> that provides a result set.</param>
public RecordSet(IDataReader rdr) {
Rows = new List<Row>();
LoadInternal(rdr,true);
}

/// <summary>
/// Initializes a new instance of <see cref="RecordSet"/> with specified list of <see cref="RecordSet.Column"/> and capacity.
/// </summary>
Expand Down Expand Up @@ -212,19 +204,10 @@ IEnumerator IEnumerable.GetEnumerator() {
/// <param name="rdr">An <see cref="IDataReader"/> that provides a result set.</param>
/// <returns>Number of loaded rows.</returns>
public int Load(IDataReader rdr) {
return LoadInternal(rdr, false);
}

int LoadInternal(IDataReader rdr, bool initColumns) {
int processed = 0;
while (rdr.Read()) {
Tuple<int,int>[] rdrToRsColIdx = null;
if (initColumns && columns==null) {
var rs = DataHelper.GetRecordSetByReader(rdr);
columns = rs.Columns;
PrimaryKey = rs.PrimaryKey;
}
if (!initColumns && rdrToRsColIdx==null) {
if (rdrToRsColIdx==null) {
var rdrToRsColIdxList = new List<Tuple<int, int>>();
for (int i=0; i<rdr.FieldCount; i++) {
var colName = rdr.GetName(i);
Expand All @@ -235,23 +218,61 @@ int LoadInternal(IDataReader rdr, bool initColumns) {
}

processed++;
if (initColumns) {
// just copy values
var rowValues = new object[rdr.FieldCount];
rdr.GetValues(rowValues);
this.Add(rowValues).AcceptChanges();
} else {
var r = this.Add();
for (int i=0; i<rdrToRsColIdx.Length; i++) {
var t = rdrToRsColIdx[i];
r[ t.Item2 ] = rdr.GetValue(t.Item1);
}
r.AcceptChanges();

var r = this.Add();
for (int i=0; i<rdrToRsColIdx.Length; i++) {
var t = rdrToRsColIdx[i];
r[ t.Item2 ] = rdr.GetValue(t.Item1);
}
r.AcceptChanges();

}
return processed;
}

/// <summary>
/// Creates a <see cref="RecordSet"/> from a data source using the supplied <see cref="IDataReader"/>.
/// </summary>
/// <param name="rdr">An <see cref="IDataReader"/> that provides a result set.</param>
/// <returns><see cref="RecordSet"/> with schema inferred by reader and populated with incoming data.</returns>
public static RecordSet FromReader(IDataReader rdr) {
RecordSet rs = null;
while (rdr.Read()) {
if (rs==null) {
rs = DataHelper.GetRecordSetByReader(rdr);
}
// just copy values
var rowValues = new object[rdr.FieldCount];
rdr.GetValues(rowValues);
rs.Add(rowValues).AcceptChanges();
}
return rs;
}

/// <summary>
/// Asynchronously creates a <see cref="RecordSet"/> from a data source using the supplied <see cref="IDataReader"/>.
/// </summary>
public static Task<RecordSet> FromReaderAsync(IDataReader rdr) {
return FromReaderAsync(rdr, CancellationToken.None);
}

/// <summary>
/// Asynchronously creates a <see cref="RecordSet"/> from a data source using the supplied <see cref="IDataReader"/>.
/// </summary>
public static async Task<RecordSet> FromReaderAsync(IDataReader rdr, CancellationToken cancel) {
RecordSet rs = null;
while (await rdr.ReadAsync(cancel).ConfigureAwait(false)) {
if (rs==null) {
rs = DataHelper.GetRecordSetByReader(rdr);
}
// just copy values
var rowValues = new object[rdr.FieldCount];
await rdr.GetValuesAsync(rowValues, cancel).ConfigureAwait(false);
rs.Add(rowValues).AcceptChanges();
}
return rs;
}

/// <summary>
/// Represents the schema of a column in a <see cref="RecordSet"/>.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/NReco.Data/project.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "1.0.0-alpha4",
"version": "1.0.0-alpha5",
"title": "NReco.Data",
"description": "Lightweight db-independent DAL for generating SQL commands by abstract queries, schema-less CRUD operations, POCO mapping.",
"authors": [ "Vitalii Fedorchenko" ],
"copyright": "Copyright (c) 2016 Vitalii Fedorchenko",
"packOptions": {
"owners": [ "Vitalii Fedorchenko" ],
"projectUrl": "http://www.nrecosite.com/dalc_net.aspx",
"tags": [ "DAL", "ado.net", "data", "relex", "sql", "query", "recordset", "generator", "batch", "database", "micro-orm", "data-mapper", "poco", "schema-less", "netstandard", "netcore", "net40", "net45" ],
"tags": [ "DAL", "ado.net", "data", "relex", "sql", "query", "recordset", "generator", "batch", "database", "micro-orm", "data-mapper", "poco", "schema-less", "netstandard", "netcore", "net45" ],
"licenseUrl": "https://raw.githubusercontent.com/nreco/data/master/LICENSE",
"iconUrl": "http://www.nrecosite.com/img/nreco-logo-200.png"
},
Expand Down

0 comments on commit 08958af

Please sign in to comment.