From 3a57437f0e6404df0c13270313fe851217d16db1 Mon Sep 17 00:00:00 2001 From: skx <610262374@qq.com> Date: Wed, 21 Oct 2020 15:26:14 +0800 Subject: [PATCH] SimpleClient add async method --- .../SqlSugar/Interface/ISimpleClient.cs | 28 +++++ Src/Asp.Net/SqlSugar/SimpleClient.cs | 115 +++++++++++++++++- 2 files changed, 141 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs b/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs index 975b6b628..612cfb9d6 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ISimpleClient.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading.Tasks; namespace SqlSugar { @@ -38,5 +39,32 @@ namespace SqlSugar bool Update(T updateObj); bool UpdateRange(List updateObjs); bool UpdateRange(T[] updateObjs); + + + + + Task CountAsync(Expression> whereExpression); + Task DeleteAsync(Expression> whereExpression); + Task DeleteAsync(T deleteObj); + Task DeleteByIdAsync(dynamic id); + Task DeleteByIdsAsync(dynamic[] ids); + Task GetByIdAsync(dynamic id); + Task> GetListAsync(); + Task> GetListAsync(Expression> whereExpression); + Task> GetPageListAsync(Expression> whereExpression, PageModel page); + Task> GetPageListAsync(Expression> whereExpression, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc); + Task> GetPageListAsync(List conditionalList, PageModel page); + Task> GetPageListAsync(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc); + Task GetSingleAsync(Expression> whereExpression); + Task InsertAsync(T insertObj); + Task InsertRangeAsync(List insertObjs); + Task InsertRangeAsync(T[] insertObjs); + Task InsertReturnIdentityAsync(T insertObj); + Task IsAnyAsync(Expression> whereExpression); + Task UpdateAsync(Expression> columns, Expression> whereExpression); + Task UpdateAsync(T updateObj); + Task UpdateRangeAsync(List updateObjs); + Task UpdateRangeAsync(T[] updateObjs); + } } \ No newline at end of file diff --git a/Src/Asp.Net/SqlSugar/SimpleClient.cs b/Src/Asp.Net/SqlSugar/SimpleClient.cs index 3b0a7a3a4..0e9e9086d 100644 --- a/Src/Asp.Net/SqlSugar/SimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/SimpleClient.cs @@ -3,12 +3,14 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; +using System.Threading.Tasks; namespace SqlSugar { public partial class SimpleClient : ISimpleClient where T : class, new() { + #region Interface protected ISqlSugarClient Context { get; set; } public ITenant AsTenant() @@ -37,7 +39,7 @@ namespace SqlSugar { return Context.Insertable(insertObj); } - public IInsertable AsInsertable(T[] insertObjs) + public IInsertable AsInsertable(T[] insertObjs) { return Context.Insertable(insertObjs); } @@ -60,8 +62,10 @@ namespace SqlSugar public IDeleteable AsDeleteable() { return Context.Deleteable(); - } + } + #endregion + #region Method public T GetById(dynamic id) { return Context.Queryable().InSingle(id); @@ -165,6 +169,113 @@ namespace SqlSugar { return this.Context.Deleteable().In(ids).ExecuteCommand() > 0; } + #endregion + + #region Async Method + public Task GetByIdAsync(dynamic id) + { + return Context.Queryable().InSingleAsync(id); + } + public Task> GetListAsync() + { + return Context.Queryable().ToListAsync(); + } + + public Task> GetListAsync(Expression> whereExpression) + { + return Context.Queryable().Where(whereExpression).ToListAsync(); + } + public Task GetSingleAsync(Expression> whereExpression) + { + return Context.Queryable().SingleAsync(whereExpression); + } + public Task> GetPageListAsync(Expression> whereExpression, PageModel page) + { + RefAsync count = 0; + var result = Context.Queryable().Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count); + page.PageCount = count; + return result; + } + public Task> GetPageListAsync(Expression> whereExpression, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) + { + RefAsync count = 0; + var result = Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count); + page.PageCount = count; + return result; + } + public Task> GetPageListAsync(List conditionalList, PageModel page) + { + RefAsync count = 0; + var result = Context.Queryable().Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count); + page.PageCount = count; + return result; + } + public Task> GetPageListAsync(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) + { + RefAsync count = 0; + var result = Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count); + page.PageCount = count; + return result; + } + public Task IsAnyAsync(Expression> whereExpression) + { + return Context.Queryable().Where(whereExpression).AnyAsync(); + } + public Task CountAsync(Expression> whereExpression) + { + + return Context.Queryable().Where(whereExpression).CountAsync(); + } + + public async Task InsertAsync(T insertObj) + { + return await this.Context.Insertable(insertObj).ExecuteCommandAsync() > 0; + } + public Task InsertReturnIdentityAsync(T insertObj) + { + return this.Context.Insertable(insertObj).ExecuteReturnIdentityAsync(); + } + public async Task InsertRangeAsync(T[] insertObjs) + { + return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0; + } + public async Task InsertRangeAsync(List insertObjs) + { + return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0; + } + public async Task UpdateAsync(T updateObj) + { + return await this.Context.Updateable(updateObj).ExecuteCommandAsync() > 0; + } + public async Task UpdateRangeAsync(T[] updateObjs) + { + return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0; + } + public async Task UpdateRangeAsync(List updateObjs) + { + return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0; + } + public async Task UpdateAsync(Expression> columns, Expression> whereExpression) + { + return await this.Context.Updateable().SetColumns(columns).Where(whereExpression).ExecuteCommandAsync() > 0; + } + public async Task DeleteAsync(T deleteObj) + { + return await this.Context.Deleteable().Where(deleteObj).ExecuteCommandAsync() > 0; + } + public async Task DeleteAsync(Expression> whereExpression) + { + return await this.Context.Deleteable().Where(whereExpression).ExecuteCommandAsync() > 0; + } + public async Task DeleteByIdAsync(dynamic id) + { + return await this.Context.Deleteable().In(id).ExecuteCommand() > 0; + } + public async Task DeleteByIdsAsync(dynamic[] ids) + { + return await this.Context.Deleteable().In(ids).ExecuteCommandAsync() > 0; + } + #endregion [Obsolete("Use AsSugarClient()")] public ISqlSugarClient FullClient { get { return this.Context; } }