Skip to content

Commit

Permalink
优化仓储Any方法内部sql实现;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Mar 22, 2022
1 parent 53aea71 commit e3da943
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions SQLBuilder/Repositories/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,12 @@ public virtual async Task<int> UpdateAsync<T>(Expression<Func<T, bool>> predicat

#region Any
#region Sync
/// <summary>
/// 获取Any对应的sql语句
/// </summary>
/// <returns></returns>
public virtual string GetAnySql() => "SELECT CASE WHEN EXISTS ({0}) THEN 1 ELSE 0 END;";

/// <summary>
/// 是否存在任意一个满足查询条件的实体
/// </summary>
Expand All @@ -2529,7 +2535,10 @@ public virtual async Task<int> UpdateAsync<T>(Expression<Func<T, bool>> predicat
/// <returns>返回查询结果</returns>
public virtual bool Any<T>(Expression<Func<T, bool>> predicate) where T : class
{
return Count(predicate) > 0;
var builder = Sql.Select<T>(x => "1", DatabaseType, isEnableFormat: IsEnableFormat).Where(predicate);
var res = FindObject(string.Format(GetAnySql(), builder.Sql), builder.DynamicParameters);

return res.To<int>() == 1;
}
#endregion

Expand All @@ -2542,10 +2551,11 @@ public virtual bool Any<T>(Expression<Func<T, bool>> predicate) where T : class
/// <returns>返回查询结果</returns>
public virtual async Task<bool> AnyAsync<T>(Expression<Func<T, bool>> predicate) where T : class
{
return await CountAsync(predicate) > 0;
}

var builder = Sql.Select<T>(x => "1", DatabaseType, isEnableFormat: IsEnableFormat).Where(predicate);
var res = await FindObjectAsync(string.Format(GetAnySql(), builder.Sql), builder.DynamicParameters);

return res.To<int>() == 1;
}
#endregion
#endregion

Expand Down
8 changes: 8 additions & 0 deletions SQLBuilder/Repositories/OracleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public OracleRepository() { }
public OracleRepository(string connectionString) : base(connectionString) { }
#endregion

#region Any
/// <summary>
/// 获取Any对应的sql语句
/// </summary>
/// <returns></returns>
public override string GetAnySql() => "SELECT CASE WHEN EXISTS ({0}) THEN 1 ELSE 0 END FROM DUAL";
#endregion

#region Page
/// <summary>
/// 获取分页语句
Expand Down

0 comments on commit e3da943

Please sign in to comment.