Skip to content

Commit

Permalink
Add ExecuteNonQuery extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
busterwood committed Mar 18, 2016
1 parent f6884b7 commit 365d7f3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Mapper/CommandExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static T ReadSingle<T>(this IDbCommand cmd)
{
Contract.Requires(cmd != null);
Contract.Ensures(Contract.Result<T>() != null);
using (var reader = cmd.ExecuteReader())
using (var reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
return reader.Single<T>();
}
Expand All @@ -33,7 +33,7 @@ public static async Task<T> ReadSingleAsync<T>(this SqlCommand cmd)
{
Contract.Requires(cmd != null);
Contract.Ensures(Contract.Result<T>() != null);
using (var reader = await cmd.ExecuteReaderAsync())
using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SingleRow))
{
return await reader.SingleAsync<T>();
}
Expand All @@ -44,7 +44,7 @@ public static async Task<T> ReadSingleAsync<T>(this SqlCommand cmd)
public static T ReadSingleOrDefault<T>(this IDbCommand cmd)
{
Contract.Requires(cmd != null);
using (var reader = cmd.ExecuteReader())
using (var reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
return reader.SingleOrDefault<T>();
}
Expand All @@ -55,7 +55,7 @@ public static T ReadSingleOrDefault<T>(this IDbCommand cmd)
public static async Task<T> ReadSingleOrDefaultAsync<T>(this SqlCommand cmd)
{
Contract.Requires(cmd != null);
using (var reader = await cmd.ExecuteReaderAsync())
using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SingleRow))
{
return await reader.SingleOrDefaultAsync<T>();
}
Expand Down
24 changes: 24 additions & 0 deletions Mapper/ConnectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ namespace Mapper
/// </summary>
public static class ConnectionExtension
{
public static int ExecuteNonQuery(this IDbConnection cnn, string sql, object parameters = null)
{
Contract.Requires(cnn != null);
Contract.Requires(cnn.State == ConnectionState.Open);
Contract.Requires(!string.IsNullOrWhiteSpace(sql));
using (var cmd = cnn.CreateCommand())
{
SetupCommand(cmd, cnn, sql, parameters);
return cmd.ExecuteNonQuery();
}
}

public static Task<int> ExecuteNonQueryAsync(this SqlConnection cnn, string sql, object parameters = null)
{
Contract.Requires(cnn != null);
Contract.Requires(cnn.State == ConnectionState.Open);
Contract.Requires(!string.IsNullOrWhiteSpace(sql));
using (var cmd = cnn.CreateCommand())
{
SetupCommand(cmd, cnn, sql, parameters);
return cmd.ExecuteNonQueryAsync();
}
}

public static T QuerySingle<T>(this IDbConnection cnn, string sql, object parameters = null)
{
Contract.Requires(cnn != null);
Expand Down
4 changes: 2 additions & 2 deletions Mapper/Grouping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Mapper
{
internal class Grouping<TKey, TElement> : IGrouping<TKey, TElement>, IList<TElement>
internal class Grouping<TKey, TElement> : IGrouping<TKey, TElement>, IList<TElement>, IReadOnlyCollection<TElement>
{
internal TKey _key;
internal int _hashCode;
Expand Down Expand Up @@ -50,7 +50,7 @@ IEnumerator IEnumerable.GetEnumerator()
/// <returns>The key of the <see cref="T:System.Linq.IGrouping`2"/>.</returns>
public TKey Key => _key;

int ICollection<TElement>.Count => _count;
public int Count => _count;

bool ICollection<TElement>.IsReadOnly => true;

Expand Down
19 changes: 17 additions & 2 deletions Mapper/HashLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Mapper
/// </summary>
public class HashLookup<TKey, TElement> : ILookup<TKey, TElement>
{
private static readonly TElement[] Empty = new TElement[0];
private static readonly List<TElement> Empty = new List<TElement>();
private readonly IEqualityComparer<TKey> _comparer;
private Grouping<TKey, TElement>[] _groupings;
private Grouping<TKey, TElement> _lastGrouping;
Expand Down Expand Up @@ -63,7 +63,7 @@ public void AddRange(IEnumerable<TElement> items, Func<TElement, TKey> keyFunc)
/// <returns>The <see cref="T:System.Collections.Generic.IEnumerable`1"/> sequence of values indexed by the specified key.</returns>
/// <param name="key">The key of the desired sequence of values.</param>
/// <remarks>Returns an empty sequence if the key is not present</remarks>
public IEnumerable<TElement> this[TKey key]
IEnumerable<TElement> ILookup<TKey, TElement>.this[TKey key]
{
get
{
Expand All @@ -74,6 +74,21 @@ public IEnumerable<TElement> this[TKey key]
}
}

/// <summary>Gets the <see cref="T:System.Collections.Generic.IEnumerable`1"/> sequence of values indexed by a specified key.</summary>
/// <returns>The <see cref="T:System.Collections.Generic.IEnumerable`1"/> sequence of values indexed by the specified key.</returns>
/// <param name="key">The key of the desired sequence of values.</param>
/// <remarks>Returns an empty sequence if the key is not present</remarks>
public IReadOnlyCollection<TElement> this[TKey key]
{
get
{
Contract.Ensures(Contract.Result<IEnumerable<TElement>>() != null);
int hashCode = InternalGetHashCode(key);
Grouping<TKey, TElement> grouping = FindGrouping(key, hashCode);
return grouping ?? (IReadOnlyCollection<TElement>)Empty;
}
}

/// <summary>
/// Determines whether a specified key exists in the <see cref="HashLookup{TKey,TElement}"/>.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Mapper/Mapper.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Mapper</id>
<version>1.0.1.3</version>
<version>1.0.1.5</version>
<authors>BusterWood</authors>
<description>A convention-based object cloner, object-object mapper (like AutoMapper), IDataReader to object mapper, object to IDbDataParameter mapper, etc.</description>
<projectUrl>https://github.com/busterwood/mapper</projectUrl>
Expand Down
2 changes: 1 addition & 1 deletion Mapper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.1.3")]
[assembly: AssemblyFileVersion("1.0.1.5")]

[assembly: InternalsVisibleTo("Mapper.UnitTests")]

0 comments on commit 365d7f3

Please sign in to comment.