Skip to content

Commit

Permalink
Merge branch 'release/v0.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
verdie-g committed Jan 8, 2018
2 parents f1ea94f + fc20f1d commit 5facb19
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ Dictionary<TKey, List<TValue>> ToLookup<TKey, TValue>()
Task<Dictionary<TKey, List<TValue>>> ToLookupAsync<TKey, TValue>()
HashSet<T> ToSet<T>()
Task<HashSet<T>> ToSetAsync<T>()
List<T> Column<T>(string columnName = null)
Task<List<T>> ColumnAsync<T>(string columnName = null)
List<T> Column<T>()
List<T> Column<T>(string columnName)
Task<List<T>> ColumnAsync<T>()
Task<List<T>> ColumnAsync<T>(string columnName)
T First<T>()
Task<T> FirstAsync<T>()
T FirstOrDefault<T>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PropertyGroup Condition="'$(Platform)'=='AnyCPU'">
<LangVersion>7.1</LangVersion>
</PropertyGroup>

Expand Down
84 changes: 56 additions & 28 deletions StoredProcedureEFCore/DbDataReaderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace StoredProcedureEFCore
Expand Down Expand Up @@ -48,43 +47,51 @@ public static class DbDataReaderExtension
}

/// <summary>
/// Map the first or the specified column to a list
/// Map the first column to a list
/// </summary>
/// <typeparam name="T">Model</typeparam>
/// <param name="reader"></param>
/// <param name="columnName">Name of the column to read</param>
/// <returns></returns>
public static List<T> Column<T>(this DbDataReader reader, string columnName = null) where T : IComparable
public static List<T> Column<T>(this DbDataReader reader) where T : IComparable
{
int ord = columnName == null ? 0 : reader.GetOrdinal(columnName);
return Column<T>(reader, 0);
}

var res = new List<T>();
while (reader.Read())
{
T value = reader.IsDBNull(ord) ? default(T) : (T)reader.GetValue(ord);
res.Add(value);
}
return res;
/// <summary>
/// Map the specified column to a list
/// </summary>
/// <typeparam name="T">Model</typeparam>
/// <param name="reader"></param>
/// <param name="columnName">Name of the column to read. Use first column if null</param>
/// <returns></returns>
public static List<T> Column<T>(this DbDataReader reader, string columnName) where T : IComparable
{
int ordinal = columnName is null ? 0 : reader.GetOrdinal(columnName);
return Column<T>(reader, ordinal);
}

/// <summary>
/// Map the first or the specified column to a list
/// Map the first column to a list
/// </summary>
/// <typeparam name="T">Model</typeparam>
/// <param name="reader"></param>
/// <param name="columnName">Name of the column to read</param>
/// <returns></returns>
public static async Task<List<T>> ColumnAsync<T>(this DbDataReader reader, string columnName = null) where T : IComparable
public static Task<List<T>> ColumnAsync<T>(this DbDataReader reader) where T : IComparable
{
int ord = columnName == null ? 0 : reader.GetOrdinal(columnName);
return ColumnAsync<T>(reader, 0);
}

var res = new List<T>();
while (await reader.ReadAsync())
{
T value = await reader.IsDBNullAsync(ord) ? default(T) : (T)reader.GetValue(ord);
res.Add(value);
}
return res;
/// <summary>
/// Map the specified column to a list
/// </summary>
/// <typeparam name="T">Model</typeparam>
/// <param name="reader"></param>
/// <param name="columnName">Name of the column to read. Use first column if null</param>
/// <returns></returns>
public static Task<List<T>> ColumnAsync<T>(this DbDataReader reader, string columnName) where T : IComparable
{
int ordinal = columnName is null ? 0 : reader.GetOrdinal(columnName);
return ColumnAsync<T>(reader, ordinal);
}

/// <summary>
Expand Down Expand Up @@ -313,6 +320,28 @@ public static async Task<HashSet<T>> ToSetAsync<T>(this DbDataReader reader) whe
return await FirstAsync<T>(reader, true, true);
}

private static List<T> Column<T>(DbDataReader reader, int ordinal) where T : IComparable
{
var res = new List<T>();
while (reader.Read())
{
T value = reader.IsDBNull(ordinal) ? default(T) : (T)reader.GetValue(ordinal);
res.Add(value);
}
return res;
}

private static async Task<List<T>> ColumnAsync<T>(DbDataReader reader, int ordinal) where T : IComparable
{
var res = new List<T>();
while (await reader.ReadAsync())
{
T value = await reader.IsDBNullAsync(ordinal) ? default(T) : (T)reader.GetValue(ordinal);
res.Add(value);
}
return res;
}

private static T First<T>(DbDataReader reader, bool orDefault, bool throwIfNotSingle) where T : class, new()
{
if (reader.Read())
Expand All @@ -334,12 +363,12 @@ public static async Task<HashSet<T>> ToSetAsync<T>(this DbDataReader reader) whe

private static async Task<T> FirstAsync<T>(DbDataReader reader, bool orDefault, bool throwIfNotSingle) where T : class, new()
{
if (reader.Read())
if (await reader.ReadAsync())
{
PropertyInfo[] props = GetDataReaderColumns<T>(reader);
T row = await MapNextRowAsync<T>(reader, props);

if (throwIfNotSingle && reader.Read())
if (throwIfNotSingle && await reader.ReadAsync())
throw new InvalidOperationException("Sequence contains more than one element");

return row;
Expand Down Expand Up @@ -403,9 +432,8 @@ public static async Task<HashSet<T>> ToSetAsync<T>(this DbDataReader reader) whe
Type modelType = typeof(T);
for (int i = 0; i < reader.FieldCount; i++)
{
string name = reader.GetName(i);
string nameNoUnderscore = Regex.Replace(name, "[_-]", "");
res[i] = modelType.GetProperty(nameNoUnderscore, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
string name = reader.GetName(i).Replace("_", "");
res[i] = modelType.GetProperty(name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
}
return res;
}
Expand Down

0 comments on commit 5facb19

Please sign in to comment.