Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cancellation Token to CountAsync #290

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Dommel/Count.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Data;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Dapper;

Expand All @@ -19,7 +20,7 @@ public static long Count<TEntity>(this IDbConnection connection, IDbTransaction?
{
var sql = BuildCountAllSql(GetSqlBuilder(connection), typeof(TEntity));
LogQuery<TEntity>(sql);
return connection.ExecuteScalar<long>(sql, transaction);
return connection.ExecuteScalar<long>(sql: sql, transaction: transaction);
}

/// <summary>
Expand All @@ -28,12 +29,13 @@ public static long Count<TEntity>(this IDbConnection connection, IDbTransaction?
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="connection">The connection to the database. This can either be open or closed.</param>
/// <param name="transaction">Optional transaction for the command.</param>
/// <param name="cancellationToken">Optional cancellation token for the command.</param>
/// <returns>The number of entities matching the specified predicate.</returns>
public static Task<long> CountAsync<TEntity>(this IDbConnection connection, IDbTransaction? transaction = null)
public static Task<long> CountAsync<TEntity>(this IDbConnection connection, IDbTransaction? transaction = null, CancellationToken cancellationToken = default)
{
var sql = BuildCountAllSql(GetSqlBuilder(connection), typeof(TEntity));
LogQuery<TEntity>(sql);
return connection.ExecuteScalarAsync<long>(sql, transaction);
return connection.ExecuteScalarAsync<long>(new CommandDefinition(commandText: sql, transaction: transaction, cancellationToken: cancellationToken));
}

/// <summary>
Expand All @@ -58,12 +60,13 @@ public static long Count<TEntity>(this IDbConnection connection, Expression<Func
/// <param name="connection">The connection to the database. This can either be open or closed.</param>
/// <param name="predicate">A predicate to filter the results.</param>
/// <param name="transaction">Optional transaction for the command.</param>
/// <param name="cancellationToken">Optional cancellation token for the command.</param>
/// <returns>The number of entities matching the specified predicate.</returns>
public static Task<long> CountAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction? transaction = null)
public static Task<long> CountAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction? transaction = null, CancellationToken cancellationToken = default)
{
var sql = BuildCountSql(GetSqlBuilder(connection), predicate, out var parameters);
LogQuery<TEntity>(sql);
return connection.ExecuteScalarAsync<long>(sql, parameters, transaction);
return connection.ExecuteScalarAsync<long>(new CommandDefinition(commandText: sql, parameters: parameters, transaction: transaction, cancellationToken: cancellationToken));
}

internal static string BuildCountAllSql(ISqlBuilder sqlBuilder, Type type)
Expand Down