Skip to content

Commit

Permalink
Add transaction parameter to Select and FirstOrDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
henkmollema committed Mar 24, 2018
1 parent 0af0cc5 commit cae06f4
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/Dommel/DommelMapper.Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static partial class DommelMapper
/// <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>
/// <param name="buffered">
/// A value indicating whether the result of the query should be executed directly,
/// or when the query is materialized (using <c>ToList()</c> for example).
Expand All @@ -23,11 +24,11 @@ public static partial class DommelMapper
/// A collection of entities of type <typeparamref name="TEntity"/> matching the specified
/// <paramref name="predicate"/>.
/// </returns>
public static IEnumerable<TEntity> Select<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, bool buffered = true)
public static IEnumerable<TEntity> Select<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction transaction = null, bool buffered = true)
{
var sql = BuildSelectSql(predicate, out var parameters);
LogQuery<TEntity>(sql);
return connection.Query<TEntity>(sql, parameters, buffered: buffered);
return connection.Query<TEntity>(sql, parameters, transaction, buffered);
}

/// <summary>
Expand All @@ -36,31 +37,16 @@ public static IEnumerable<TEntity> Select<TEntity>(this IDbConnection connection
/// <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>
/// A collection of entities of type <typeparamref name="TEntity"/> matching the specified
/// <paramref name="predicate"/>.
/// </returns>
public static Task<IEnumerable<TEntity>> SelectAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate)
public static Task<IEnumerable<TEntity>> SelectAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction transaction = null)
{
var sql = BuildSelectSql(predicate, out var parameters);
LogQuery<TEntity>(sql);
return connection.QueryAsync<TEntity>(sql, parameters);
}

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

sql += new SqlExpression<TEntity>()
.Where(predicate)
.ToSql(out parameters);
return sql;
return connection.QueryAsync<TEntity>(sql, parameters, transaction);
}

/// <summary>
Expand All @@ -69,15 +55,16 @@ private static string BuildSelectSql<TEntity>(Expression<Func<TEntity, bool>> pr
/// <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>
/// A instance of type <typeparamref name="TEntity"/> matching the specified
/// <paramref name="predicate"/>.
/// </returns>
public static TEntity FirstOrDefault<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate)
public static TEntity FirstOrDefault<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction transaction = null)
{
var sql = BuildSelectSql(predicate, out var parameters);
LogQuery<TEntity>(sql);
return connection.QueryFirstOrDefault<TEntity>(sql, parameters);
return connection.QueryFirstOrDefault<TEntity>(sql, parameters, transaction);
}

/// <summary>
Expand All @@ -86,15 +73,32 @@ public static TEntity FirstOrDefault<TEntity>(this IDbConnection connection, Exp
/// <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>
/// A instance of type <typeparamref name="TEntity"/> matching the specified
/// <paramref name="predicate"/>.
/// </returns>
public static Task<TEntity> FirstOrDefaultAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate)
public static Task<TEntity> FirstOrDefaultAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction transaction = null)
{
var sql = BuildSelectSql(predicate, out var parameters);
LogQuery<TEntity>(sql);
return connection.QueryFirstOrDefaultAsync<TEntity>(sql, parameters);
return connection.QueryFirstOrDefaultAsync<TEntity>(sql, parameters, transaction);
}

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

sql += new SqlExpression<TEntity>()
.Where(predicate)
.ToSql(out parameters);
return sql;
}
}
}

0 comments on commit cae06f4

Please sign in to comment.