Commit b6689c9e authored by mgravell's avatar mgravell

Easier syntax for C# 3.9

parent cf8b5460
...@@ -347,6 +347,28 @@ public bool Equals(Identity other) ...@@ -347,6 +347,28 @@ public bool Equals(Identity other)
} }
} }
#if CSHARP30
/// <summary>
/// Execute parameterized SQL
/// </summary>
/// <returns>Number of rows affected</returns>
public static int Execute(this IDbConnection cnn, string sql, object param)
{
return Execute(cnn, sql, param, null, null, null);
}
/// <summary>
/// Executes a query, returning the data typed as per T
/// </summary>
/// <remarks>the dynamic param may seem a bit odd, but this works around a major usability issue in vs, if it is Object vs completion gets annoying. Eg type new <space> get new object</remarks>
/// <returns>A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is
/// created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
/// </returns>
public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param)
{
return Query<T>(cnn, sql, param, null, true, null, null);
}
#endif
/// <summary> /// <summary>
/// Execute parameterized SQL /// Execute parameterized SQL
/// </summary> /// </summary>
...@@ -406,7 +428,13 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn ...@@ -406,7 +428,13 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn
} }
#endif #endif
// the dynamic param may seem a bit odd, but this works around a major usability issue in vs, if it is Object vs completion gets annoying. Eg type new <space> get new object /// <summary>
/// Executes a query, returning the data typed as per T
/// </summary>
/// <remarks>the dynamic param may seem a bit odd, but this works around a major usability issue in vs, if it is Object vs completion gets annoying. Eg type new <space> get new object</remarks>
/// <returns>A sequence of data of the supplied type; if a basic type (int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is
/// created per row, and a direct column-name===member-name mapping is assumed (case insensitive).
/// </returns>
public static IEnumerable<T> Query<T>( public static IEnumerable<T> Query<T>(
#if CSHARP30 #if CSHARP30
this IDbConnection cnn, string sql, object param, IDbTransaction transaction, bool buffered, int? commandTimeout, CommandType? commandType this IDbConnection cnn, string sql, object param, IDbTransaction transaction, bool buffered, int? commandTimeout, CommandType? commandType
......
...@@ -11,14 +11,12 @@ public class Tests ...@@ -11,14 +11,12 @@ public class Tests
public void TestBasicStringUsage() public void TestBasicStringUsage()
{ {
var arr = connection.Query<string>("select 'abc' as [Value] union all select @txt", new {txt = "def"}, null, false, null, null). var arr = connection.Query<string>("select 'abc' as [Value] union all select @txt", new {txt = "def"}).ToArray();
ToArray();
arr.IsSequenceEqualTo(new[] { "abc", "def" }); arr.IsSequenceEqualTo(new[] { "abc", "def" });
} }
public void TestClassWithStringUsage() public void TestClassWithStringUsage()
{ {
var arr = connection.Query<BasicType>("select 'abc' as [Value] union all select @txt", new { txt = "def" }, null, false, null, null). var arr = connection.Query<BasicType>("select 'abc' as [Value] union all select @txt", new { txt = "def" }).ToArray();
ToArray();
arr.Select(x => x.Value).IsSequenceEqualTo(new[] { "abc", "def" }); arr.Select(x => x.Value).IsSequenceEqualTo(new[] { "abc", "def" });
} }
class BasicType class BasicType
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment