From 365d7f313d358bd867fac0a57e781f2b29039412 Mon Sep 17 00:00:00 2001 From: busterwood Date: Thu, 17 Mar 2016 10:11:33 +0000 Subject: [PATCH] Add ExecuteNonQuery extensions --- Mapper/CommandExtension.cs | 8 ++++---- Mapper/ConnectionExtension.cs | 24 ++++++++++++++++++++++++ Mapper/Grouping.cs | 4 ++-- Mapper/HashLookup.cs | 19 +++++++++++++++++-- Mapper/Mapper.nuspec | 2 +- Mapper/Properties/AssemblyInfo.cs | 2 +- 6 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Mapper/CommandExtension.cs b/Mapper/CommandExtension.cs index 6db133b..dff6f0a 100644 --- a/Mapper/CommandExtension.cs +++ b/Mapper/CommandExtension.cs @@ -21,7 +21,7 @@ public static T ReadSingle(this IDbCommand cmd) { Contract.Requires(cmd != null); Contract.Ensures(Contract.Result() != null); - using (var reader = cmd.ExecuteReader()) + using (var reader = cmd.ExecuteReader(CommandBehavior.SingleRow)) { return reader.Single(); } @@ -33,7 +33,7 @@ public static async Task ReadSingleAsync(this SqlCommand cmd) { Contract.Requires(cmd != null); Contract.Ensures(Contract.Result() != null); - using (var reader = await cmd.ExecuteReaderAsync()) + using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SingleRow)) { return await reader.SingleAsync(); } @@ -44,7 +44,7 @@ public static async Task ReadSingleAsync(this SqlCommand cmd) public static T ReadSingleOrDefault(this IDbCommand cmd) { Contract.Requires(cmd != null); - using (var reader = cmd.ExecuteReader()) + using (var reader = cmd.ExecuteReader(CommandBehavior.SingleRow)) { return reader.SingleOrDefault(); } @@ -55,7 +55,7 @@ public static T ReadSingleOrDefault(this IDbCommand cmd) public static async Task ReadSingleOrDefaultAsync(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(); } diff --git a/Mapper/ConnectionExtension.cs b/Mapper/ConnectionExtension.cs index 243c326..bc3e943 100644 --- a/Mapper/ConnectionExtension.cs +++ b/Mapper/ConnectionExtension.cs @@ -13,6 +13,30 @@ namespace Mapper /// 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 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(this IDbConnection cnn, string sql, object parameters = null) { Contract.Requires(cnn != null); diff --git a/Mapper/Grouping.cs b/Mapper/Grouping.cs index 672b269..9412f7a 100644 --- a/Mapper/Grouping.cs +++ b/Mapper/Grouping.cs @@ -9,7 +9,7 @@ namespace Mapper { - internal class Grouping : IGrouping, IList + internal class Grouping : IGrouping, IList, IReadOnlyCollection { internal TKey _key; internal int _hashCode; @@ -50,7 +50,7 @@ IEnumerator IEnumerable.GetEnumerator() /// The key of the . public TKey Key => _key; - int ICollection.Count => _count; + public int Count => _count; bool ICollection.IsReadOnly => true; diff --git a/Mapper/HashLookup.cs b/Mapper/HashLookup.cs index dd61fb3..7584571 100644 --- a/Mapper/HashLookup.cs +++ b/Mapper/HashLookup.cs @@ -15,7 +15,7 @@ namespace Mapper /// public class HashLookup : ILookup { - private static readonly TElement[] Empty = new TElement[0]; + private static readonly List Empty = new List(); private readonly IEqualityComparer _comparer; private Grouping[] _groupings; private Grouping _lastGrouping; @@ -63,7 +63,7 @@ public void AddRange(IEnumerable items, Func keyFunc) /// The sequence of values indexed by the specified key. /// The key of the desired sequence of values. /// Returns an empty sequence if the key is not present - public IEnumerable this[TKey key] + IEnumerable ILookup.this[TKey key] { get { @@ -74,6 +74,21 @@ public IEnumerable this[TKey key] } } + /// Gets the sequence of values indexed by a specified key. + /// The sequence of values indexed by the specified key. + /// The key of the desired sequence of values. + /// Returns an empty sequence if the key is not present + public IReadOnlyCollection this[TKey key] + { + get + { + Contract.Ensures(Contract.Result>() != null); + int hashCode = InternalGetHashCode(key); + Grouping grouping = FindGrouping(key, hashCode); + return grouping ?? (IReadOnlyCollection)Empty; + } + } + /// /// Determines whether a specified key exists in the . /// diff --git a/Mapper/Mapper.nuspec b/Mapper/Mapper.nuspec index 9e1d6f2..1c08412 100644 --- a/Mapper/Mapper.nuspec +++ b/Mapper/Mapper.nuspec @@ -2,7 +2,7 @@ Mapper - 1.0.1.3 + 1.0.1.5 BusterWood A convention-based object cloner, object-object mapper (like AutoMapper), IDataReader to object mapper, object to IDbDataParameter mapper, etc. https://github.com/busterwood/mapper diff --git a/Mapper/Properties/AssemblyInfo.cs b/Mapper/Properties/AssemblyInfo.cs index bb50550..ccaf7df 100644 --- a/Mapper/Properties/AssemblyInfo.cs +++ b/Mapper/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file