Skip to content

Commit

Permalink
Add transaction parameter to Count methods
Browse files Browse the repository at this point in the history
  • Loading branch information
henkmollema committed Mar 24, 2018
1 parent cae06f4 commit adfdeb9
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Dommel/DommelMapper.Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ namespace Dommel
{
public static partial class DommelMapper
{
private static readonly ConcurrentDictionary<Type, string> _getCountCache = new ConcurrentDictionary<Type, string>();
private static readonly ConcurrentDictionary<Type, string> _countQueryCache = new ConcurrentDictionary<Type, string>();

/// <summary>
/// Returns the number of entities matching the specified predicate.
/// </summary>
/// <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="predicate">A predicate to filter the results.</param>
/// <param name="transaction">Optional transaction for the command.</param>
/// <returns>The number of entities matching the specified predicate.</returns>
public static long Count<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate)
public static long Count<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction transaction = null)
{
var sql = BuildCountSql(predicate, out var parameters);
LogQuery<TEntity>(sql);
return connection.ExecuteScalar<long>(sql, parameters);
return connection.ExecuteScalar<long>(sql, parameters, transaction);
}

/// <summary>
Expand All @@ -31,22 +32,23 @@ public static long Count<TEntity>(this IDbConnection connection, Expression<Func
/// <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="predicate">A predicate to filter the results.</param>
/// <param name="transaction">Optional transaction 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)
public static Task<long> CountAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction transaction = null)
{
var sql = BuildCountSql(predicate, out var parameters);
LogQuery<TEntity>(sql);
return connection.ExecuteScalarAsync<long>(sql, parameters);
return connection.ExecuteScalarAsync<long>(sql, parameters, transaction);
}

private static string BuildCountSql<TEntity>(Expression<Func<TEntity, bool>> predicate, out DynamicParameters parameters)
{
var type = typeof(TEntity);
if (!_getCountCache.TryGetValue(type, out var sql))
if (!_countQueryCache.TryGetValue(type, out var sql))
{
var tableName = Resolvers.Table(type);
sql = $"select count(*) from {tableName}";
_getCountCache.TryAdd(type, sql);
_countQueryCache.TryAdd(type, sql);
}

sql += new SqlExpression<TEntity>()
Expand Down

0 comments on commit adfdeb9

Please sign in to comment.