|
| 1 | +using Dapper; |
| 2 | +using System; |
| 3 | +using System.Data; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace AA.Dapper.Dommel |
| 8 | +{ |
| 9 | + public static partial class DommelMapper |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Determines whether there's any entity of type <typeparamref name="TEntity"/> in the database. |
| 13 | + /// </summary> |
| 14 | + /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| 15 | + /// <param name="connection">The connection to the database. This can either be open or closed.</param> |
| 16 | + /// <param name="transaction">Optional transaction for the command.</param> |
| 17 | + /// <returns><c>true</c> if there's at least one entity in the database; otherwise, <c>false</c>.</returns> |
| 18 | + public static bool Any<TEntity>(this IDbConnection connection, IDbTransaction? transaction = null) |
| 19 | + { |
| 20 | + var sql = BuildAnyAllSql(GetSqlBuilder(connection), typeof(TEntity)); |
| 21 | + LogQuery<TEntity>(sql); |
| 22 | + return connection.ExecuteScalar<bool>(sql, transaction); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Determines whether there's any entity of type <typeparamref name="TEntity"/> in the database. |
| 27 | + /// </summary> |
| 28 | + /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| 29 | + /// <param name="connection">The connection to the database. This can either be open or closed.</param> |
| 30 | + /// <param name="transaction">Optional transaction for the command.</param> |
| 31 | + /// <returns><c>true</c> if there's at least one entity in the database; otherwise, <c>false</c>.</returns> |
| 32 | + public static Task<bool> AnyAsync<TEntity>(this IDbConnection connection, IDbTransaction? transaction = null) |
| 33 | + { |
| 34 | + var sql = BuildAnyAllSql(GetSqlBuilder(connection), typeof(TEntity)); |
| 35 | + LogQuery<TEntity>(sql); |
| 36 | + return connection.ExecuteScalarAsync<bool>(sql, transaction); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Determines whether there's any entity of type <typeparamref name="TEntity"/> matching the specified predicate in the database. |
| 41 | + /// </summary> |
| 42 | + /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| 43 | + /// <param name="connection">The connection to the database. This can either be open or closed.</param> |
| 44 | + /// <param name="predicate">A predicate to filter the results.</param> |
| 45 | + /// <param name="transaction">Optional transaction for the command.</param> |
| 46 | + /// <returns><c>true</c> if there's at least one entity in the database that matches the specified predicate; otherwise, <c>false</c>.</returns> |
| 47 | + public static bool Any<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction? transaction = null) |
| 48 | + { |
| 49 | + var sql = BuildAnySql(GetSqlBuilder(connection), predicate, out var parameters); |
| 50 | + LogQuery<TEntity>(sql); |
| 51 | + return connection.ExecuteScalar<bool>(sql, parameters, transaction); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Determines whether there's any entity of type <typeparamref name="TEntity"/> matching the specified predicate in the database. |
| 56 | + /// </summary> |
| 57 | + /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| 58 | + /// <param name="connection">The connection to the database. This can either be open or closed.</param> |
| 59 | + /// <param name="predicate">A predicate to filter the results.</param> |
| 60 | + /// <param name="transaction">Optional transaction for the command.</param> |
| 61 | + /// <returns><c>true</c> if there's at least one entity in the database that matches the specified predicate; otherwise, <c>false</c>.</returns> |
| 62 | + public static Task<bool> AnyAsync<TEntity>(this IDbConnection connection, Expression<Func<TEntity, bool>> predicate, IDbTransaction? transaction = null) |
| 63 | + { |
| 64 | + var sql = BuildAnySql(GetSqlBuilder(connection), predicate, out var parameters); |
| 65 | + LogQuery<TEntity>(sql); |
| 66 | + return connection.ExecuteScalarAsync<bool>(sql, parameters, transaction); |
| 67 | + } |
| 68 | + |
| 69 | + private static string BuildAnyPredicate(ISqlBuilder sqlBuilder, Type type) |
| 70 | + { |
| 71 | + var cacheKey = new QueryCacheKey(QueryCacheType.Any, sqlBuilder, type); |
| 72 | + if (!QueryCache.TryGetValue(cacheKey, out var sql)) |
| 73 | + { |
| 74 | + var tableName = Resolvers.Table(type, sqlBuilder); |
| 75 | + sql = $"select 1 from {tableName}"; |
| 76 | + QueryCache.TryAdd(cacheKey, sql); |
| 77 | + } |
| 78 | + |
| 79 | + return sql; |
| 80 | + } |
| 81 | + |
| 82 | + internal static string BuildAnyAllSql(ISqlBuilder sqlBuilder, Type type) |
| 83 | + { |
| 84 | + var sql = $"{BuildAnyPredicate(sqlBuilder, type)} {sqlBuilder.LimitClause(1)}"; |
| 85 | + return sql; |
| 86 | + } |
| 87 | + |
| 88 | + internal static string BuildAnySql<TEntity>(ISqlBuilder sqlBuilder, Expression<Func<TEntity, bool>> predicate, out DynamicParameters parameters) |
| 89 | + { |
| 90 | + var sql = BuildAnyPredicate(sqlBuilder, typeof(TEntity)); |
| 91 | + sql += CreateSqlExpression<TEntity>(sqlBuilder) |
| 92 | + .Where(predicate) |
| 93 | + .ToSql(out parameters); |
| 94 | + sql += $" {sqlBuilder.LimitClause(1)}"; |
| 95 | + return sql; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments