Skip to content

Commit

Permalink
Fix for #808
Browse files Browse the repository at this point in the history
Add dynamic overloads for:
- QueryFirstAsync
- QueryFirstOrDefaultAsync
- QuerySingleAsync
- QuerySingleOrDefaultAsync

This may change a lot in 2.x, but many people asking for them now so here they are. Unit tests also fixed for QuerySingleOrDefaultAsync.

Note: this builds on the netstandard2 branch because of the xUnit changes to go with VS 2017 15.3.
  • Loading branch information
NickCraver committed Aug 20, 2017
1 parent 9d9afea commit 1bd1b4f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
32 changes: 30 additions & 2 deletions Dapper.Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public async Task TestBasicStringUsageQueryFirstAsync()
str.IsEqualTo("abc");
}

[Fact]
public async Task TestBasicStringUsageQueryFirstAsyncDynamic()
{
var str = await connection.QueryFirstAsync("select 'abc' as [Value] union all select @txt", new { txt = "def" }).ConfigureAwait(false);
str.IsEqualTo("abc");
}

[Fact]
public async Task TestBasicStringUsageQueryFirstOrDefaultAsync()
{
Expand All @@ -37,16 +44,37 @@ public async Task TestBasicStringUsageQueryFirstOrDefaultAsync()
}

[Fact]
public async Task TestBasicStringUsageQuerySingleAsync()
public async Task TestBasicStringUsageQueryFirstOrDefaultAsyncDynamic()
{
var str = await connection.QueryFirstOrDefaultAsync("select null as [Value] union all select @txt", new { txt = "def" }).ConfigureAwait(false);
str.IsNull();
}

[Fact]
public async Task TestBasicStringUsageQuerySingleAsyncDynamic()
{
var str = await connection.QuerySingleAsync<string>(new CommandDefinition("select 'abc' as [Value]")).ConfigureAwait(false);
str.IsEqualTo("abc");
}

[Fact]
public async Task TestBasicStringUsageQuerySingleAsync()
{
var str = await connection.QuerySingleAsync("select 'abc' as [Value]").ConfigureAwait(false);
str.IsEqualTo("abc");
}

[Fact]
public async Task TestBasicStringUsageQuerySingleOrDefaultAsync()
{
var str = await connection.QuerySingleAsync<string>(new CommandDefinition("select null as [Value]")).ConfigureAwait(false);
var str = await connection.QuerySingleOrDefaultAsync<string>(new CommandDefinition("select null as [Value]")).ConfigureAwait(false);
str.IsNull();
}

[Fact]
public async Task TestBasicStringUsageQuerySingleOrDefaultAsyncDynamic()
{
var str = await connection.QuerySingleOrDefaultAsync("select null as [Value]").ConfigureAwait(false);
str.IsNull();
}

Expand Down
48 changes: 48 additions & 0 deletions Dapper/SqlMapper.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,54 @@ public static Task<T> QuerySingleAsync<T>(this IDbConnection cnn, string sql, ob
public static Task<T> QuerySingleOrDefaultAsync<T>(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
QueryRowAsync<T>(cnn, Row.SingleOrDefault, typeof(T), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));

/// <summary>
/// Execute a single-row query asynchronously using .NET 4.5 Task.
/// </summary>
/// <param name="cnn">The connection to query on.</param>
/// <param name="sql">The SQL to execute for the query.</param>
/// <param name="param">The parameters to pass, if any.</param>
/// <param name="transaction">The transaction to use, if any.</param>
/// <param name="commandTimeout">The command timeout (in seconds).</param>
/// <param name="commandType">The type of command to execute.</param>
public static Task<dynamic> QueryFirstAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
QueryRowAsync<dynamic>(cnn, Row.First, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));

/// <summary>
/// Execute a single-row query asynchronously using .NET 4.5 Task.
/// </summary>
/// <param name="cnn">The connection to query on.</param>
/// <param name="sql">The SQL to execute for the query.</param>
/// <param name="param">The parameters to pass, if any.</param>
/// <param name="transaction">The transaction to use, if any.</param>
/// <param name="commandTimeout">The command timeout (in seconds).</param>
/// <param name="commandType">The type of command to execute.</param>
public static Task<dynamic> QueryFirstOrDefaultAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
QueryRowAsync<dynamic>(cnn, Row.FirstOrDefault, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));

/// <summary>
/// Execute a single-row query asynchronously using .NET 4.5 Task.
/// </summary>
/// <param name="cnn">The connection to query on.</param>
/// <param name="sql">The SQL to execute for the query.</param>
/// <param name="param">The parameters to pass, if any.</param>
/// <param name="transaction">The transaction to use, if any.</param>
/// <param name="commandTimeout">The command timeout (in seconds).</param>
/// <param name="commandType">The type of command to execute.</param>
public static Task<dynamic> QuerySingleAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
QueryRowAsync<dynamic>(cnn, Row.Single, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));

/// <summary>
/// Execute a single-row query asynchronously using .NET 4.5 Task.
/// </summary>
/// <param name="cnn">The connection to query on.</param>
/// <param name="sql">The SQL to execute for the query.</param>
/// <param name="param">The parameters to pass, if any.</param>
/// <param name="transaction">The transaction to use, if any.</param>
/// <param name="commandTimeout">The command timeout (in seconds).</param>
/// <param name="commandType">The type of command to execute.</param>
public static Task<dynamic> QuerySingleOrDefaultAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
QueryRowAsync<dynamic>(cnn, Row.SingleOrDefault, typeof(DapperRow), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default(CancellationToken)));

/// <summary>
/// Execute a query asynchronously using .NET 4.5 Task.
/// </summary>
Expand Down

0 comments on commit 1bd1b4f

Please sign in to comment.