SqlSugar/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs

386 lines
16 KiB
C#
Raw Normal View History

2017-08-24 15:18:53 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
2021-04-25 17:31:49 +08:00
using System.Reflection;
2017-08-24 15:18:53 +08:00
using System.Text;
2020-10-25 20:33:32 +08:00
using System.Threading.Tasks;
2017-08-24 15:18:53 +08:00
namespace SqlSugar
{
2019-05-17 22:26:30 +08:00
2021-04-25 17:31:49 +08:00
public partial class SimpleClient<T> : ISugarRepository,ISimpleClient<T> where T : class, new()
2017-08-24 15:18:53 +08:00
{
2020-10-25 20:33:32 +08:00
#region Interface
2021-04-25 17:31:49 +08:00
public ISqlSugarClient Context { get; set; }
2019-05-17 22:26:30 +08:00
2019-06-01 16:30:38 +08:00
public ITenant AsTenant()
{
return this.Context as ITenant;
}
2019-05-17 22:26:30 +08:00
public ISqlSugarClient AsSugarClient()
{
return this.Context;
}
2017-08-24 15:18:53 +08:00
2022-05-23 23:52:08 +08:00
public SimpleClient()
2017-08-24 15:18:53 +08:00
{
}
2019-05-17 22:26:30 +08:00
public SimpleClient(ISqlSugarClient context)
2017-08-24 15:18:53 +08:00
{
this.Context = context;
}
2021-01-08 12:27:23 +08:00
public SimpleClient<ChangeType> Change<ChangeType>() where ChangeType : class, new()
{
return this.Context.GetSimpleClient<ChangeType>();
}
2021-04-25 17:31:49 +08:00
public RepositoryType ChangeRepository<RepositoryType>() where RepositoryType : ISugarRepository
{
Type type = typeof(RepositoryType);
2022-10-21 12:34:18 +08:00
var isAnyParamter = type.GetConstructors().Any(z => z.GetParameters().Any());
object o = null;
if (isAnyParamter)
{
o=Activator.CreateInstance(type, new string[] { null });
}
else
{
o = Activator.CreateInstance(type);
}
2021-04-25 17:31:49 +08:00
var result= (RepositoryType)o;
if (result.Context == null)
{
result.Context = this.Context;
}
return result;
}
2019-05-17 22:26:30 +08:00
public ISugarQueryable<T> AsQueryable()
2017-08-24 15:18:53 +08:00
{
2019-05-17 22:26:30 +08:00
return Context.Queryable<T>();
2017-08-24 15:18:53 +08:00
}
2019-05-17 22:26:30 +08:00
public IInsertable<T> AsInsertable(T insertObj)
2019-04-10 21:00:32 +08:00
{
2019-05-17 22:26:30 +08:00
return Context.Insertable<T>(insertObj);
2019-04-10 21:00:32 +08:00
}
2020-10-25 20:33:32 +08:00
public IInsertable<T> AsInsertable(T[] insertObjs)
2017-08-24 15:18:53 +08:00
{
2019-05-17 22:26:30 +08:00
return Context.Insertable<T>(insertObjs);
2017-08-24 15:18:53 +08:00
}
2019-05-17 22:26:30 +08:00
public IInsertable<T> AsInsertable(List<T> insertObjs)
2018-05-05 18:27:54 +08:00
{
2019-05-17 22:26:30 +08:00
return Context.Insertable<T>(insertObjs);
2018-05-05 18:27:54 +08:00
}
2019-05-17 22:26:30 +08:00
public IUpdateable<T> AsUpdateable(T updateObj)
{
return Context.Updateable<T>(updateObj);
}
public IUpdateable<T> AsUpdateable(T[] updateObjs)
{
return Context.Updateable<T>(updateObjs);
}
public IUpdateable<T> AsUpdateable(List<T> updateObjs)
{
return Context.Updateable<T>(updateObjs);
}
2021-11-13 19:26:06 +08:00
public IUpdateable<T> AsUpdateable()
{
return Context.Updateable<T>();
}
2019-05-17 22:26:30 +08:00
public IDeleteable<T> AsDeleteable()
{
return Context.Deleteable<T>();
2020-10-25 20:33:32 +08:00
}
#endregion
2019-05-17 22:26:30 +08:00
2020-10-25 20:33:32 +08:00
#region Method
public virtual T GetById(dynamic id)
2019-05-17 22:26:30 +08:00
{
return Context.Queryable<T>().InSingle(id);
}
2020-10-25 20:33:32 +08:00
public virtual List<T> GetList()
2019-05-17 22:26:30 +08:00
{
return Context.Queryable<T>().ToList();
}
2020-10-25 20:33:32 +08:00
public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression)
2017-08-24 15:18:53 +08:00
{
return Context.Queryable<T>().Where(whereExpression).ToList();
}
2020-10-25 20:33:32 +08:00
public virtual T GetSingle(Expression<Func<T, bool>> whereExpression)
2019-05-17 22:26:30 +08:00
{
return Context.Queryable<T>().Single(whereExpression);
}
2022-04-07 11:01:20 +08:00
public virtual T GetFirst(Expression<Func<T, bool>> whereExpression)
2021-10-12 19:43:57 +08:00
{
return Context.Queryable<T>().First(whereExpression);
}
2020-10-25 20:33:32 +08:00
public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page)
2018-01-23 11:24:18 +08:00
{
int count = 0;
2018-05-05 18:27:54 +08:00
var result = Context.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
2021-08-01 17:17:09 +08:00
page.TotalCount = count;
2018-05-05 18:27:54 +08:00
return result;
}
2020-10-25 20:33:32 +08:00
public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
2018-05-05 18:27:54 +08:00
{
int count = 0;
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
2021-08-01 17:17:09 +08:00
page.TotalCount = count;
2018-01-23 11:24:18 +08:00
return result;
}
2020-10-25 20:33:32 +08:00
public virtual List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page)
2018-02-11 17:26:42 +08:00
{
int count = 0;
var result = Context.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
2021-08-01 17:17:09 +08:00
page.TotalCount = count;
2018-02-11 17:26:42 +08:00
return result;
}
2020-10-25 20:33:32 +08:00
public virtual List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
2018-05-05 18:27:54 +08:00
{
int count = 0;
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
2021-08-01 17:17:09 +08:00
page.TotalCount = count;
2018-05-05 18:27:54 +08:00
return result;
}
2020-10-25 20:33:32 +08:00
public virtual bool IsAny(Expression<Func<T, bool>> whereExpression)
2018-02-11 17:26:42 +08:00
{
return Context.Queryable<T>().Where(whereExpression).Any();
}
2020-10-25 20:33:32 +08:00
public virtual int Count(Expression<Func<T, bool>> whereExpression)
2019-05-17 22:26:30 +08:00
{
return Context.Queryable<T>().Where(whereExpression).Count();
}
2020-10-25 20:33:32 +08:00
public virtual bool Insert(T insertObj)
2017-08-24 15:18:53 +08:00
{
return this.Context.Insertable(insertObj).ExecuteCommand() > 0;
}
2022-06-14 19:01:35 +08:00
public virtual bool InsertOrUpdate(T data)
{
return this.Context.Storageable(data).ExecuteCommand() > 0;
}
public virtual bool InsertOrUpdate(List<T> datas)
{
return this.Context.Storageable(datas).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual int InsertReturnIdentity(T insertObj)
2017-08-24 15:18:53 +08:00
{
return this.Context.Insertable(insertObj).ExecuteReturnIdentity();
}
2021-01-17 02:50:01 +08:00
public virtual long InsertReturnBigIdentity(T insertObj)
{
return this.Context.Insertable(insertObj).ExecuteReturnBigIdentity();
}
2021-08-01 17:17:09 +08:00
public virtual long InsertReturnSnowflakeId(T insertObj)
{
return this.Context.Insertable(insertObj).ExecuteReturnSnowflakeId();
}
public virtual List<long> InsertReturnSnowflakeId(List<T> insertObjs)
{
return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdList();
}
public virtual Task<long> InsertReturnSnowflakeIdAsync(T insertObj)
{
return this.Context.Insertable(insertObj).ExecuteReturnSnowflakeIdAsync();
}
public virtual Task<List<long>> InsertReturnSnowflakeIdAsync(List<T> insertObjs)
{
return this.Context.Insertable(insertObjs).ExecuteReturnSnowflakeIdListAsync();
}
2022-09-10 21:19:05 +08:00
public virtual T InsertReturnEntity(T insertObj)
{
return this.Context.Insertable(insertObj).ExecuteReturnEntity();
}
2020-10-25 20:33:32 +08:00
public virtual bool InsertRange(T[] insertObjs)
2017-08-24 15:18:53 +08:00
{
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool InsertRange(List<T> insertObjs)
2017-08-24 15:18:53 +08:00
{
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool Update(T updateObj)
2017-08-24 15:18:53 +08:00
{
return this.Context.Updateable(updateObj).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool UpdateRange(T[] updateObjs)
2018-05-05 18:27:54 +08:00
{
return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool UpdateRange(List<T> updateObjs)
2018-12-03 00:35:00 +08:00
{
return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
2017-08-24 15:18:53 +08:00
{
2019-05-17 22:26:30 +08:00
return this.Context.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommand() > 0;
2017-08-24 15:18:53 +08:00
}
2022-09-29 20:50:12 +08:00
public virtual bool UpdateSetColumnsTrue(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
{
return this.Context.Updateable<T>().SetColumns(columns,true).Where(whereExpression).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool Delete(T deleteObj)
2017-08-24 15:18:53 +08:00
{
return this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
}
2022-04-30 19:03:10 +08:00
public virtual bool Delete(List<T> deleteObjs)
{
return this.Context.Deleteable<T>().Where(deleteObjs).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool Delete(Expression<Func<T, bool>> whereExpression)
2017-08-24 15:18:53 +08:00
{
return this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool DeleteById(dynamic id)
2017-08-24 15:18:53 +08:00
{
return this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual bool DeleteByIds(dynamic[] ids)
2017-08-24 15:18:53 +08:00
{
return this.Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
}
2020-10-25 20:33:32 +08:00
#endregion
#region Async Method
public virtual Task<T> GetByIdAsync(dynamic id)
{
return Context.Queryable<T>().InSingleAsync(id);
}
public virtual Task<List<T>> GetListAsync()
{
return Context.Queryable<T>().ToListAsync();
}
public virtual Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression)
{
return Context.Queryable<T>().Where(whereExpression).ToListAsync();
}
public virtual Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression)
{
return Context.Queryable<T>().SingleAsync(whereExpression);
}
2022-04-07 11:01:20 +08:00
public virtual Task<T> GetFirstAsync(Expression<Func<T, bool>> whereExpression)
2021-10-12 19:43:57 +08:00
{
return Context.Queryable<T>().FirstAsync(whereExpression);
}
2021-01-08 12:27:23 +08:00
public virtual async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page)
2020-10-25 20:33:32 +08:00
{
RefAsync<int> count = 0;
2021-01-08 12:27:23 +08:00
var result =await Context.Queryable<T>().Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
2021-08-01 17:17:09 +08:00
page.TotalCount = count;
2020-10-25 20:33:32 +08:00
return result;
}
2021-01-08 12:27:23 +08:00
public virtual async Task<List<T>> GetPageListAsync(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
2020-10-25 20:33:32 +08:00
{
RefAsync<int> count = 0;
2021-01-08 12:27:23 +08:00
var result =await Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageListAsync(page.PageIndex, page.PageSize, count);
2021-08-01 17:17:09 +08:00
page.TotalCount = count;
2020-10-25 20:33:32 +08:00
return result;
}
2021-01-08 12:27:23 +08:00
public virtual async Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page)
2020-10-25 20:33:32 +08:00
{
RefAsync<int> count = 0;
2021-01-08 12:27:23 +08:00
var result =await Context.Queryable<T>().Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
2021-08-01 17:17:09 +08:00
page.TotalCount = count;
2020-10-25 20:33:32 +08:00
return result;
}
2021-01-08 12:27:23 +08:00
public virtual async Task<List<T>> GetPageListAsync(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
2020-10-25 20:33:32 +08:00
{
RefAsync<int> count = 0;
2021-01-08 12:27:23 +08:00
var result =await Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageListAsync(page.PageIndex, page.PageSize, count);
2021-08-01 17:17:09 +08:00
page.TotalCount = count;
2020-10-25 20:33:32 +08:00
return result;
}
public virtual Task<bool> IsAnyAsync(Expression<Func<T, bool>> whereExpression)
{
return Context.Queryable<T>().Where(whereExpression).AnyAsync();
}
public virtual Task<int> CountAsync(Expression<Func<T, bool>> whereExpression)
{
return Context.Queryable<T>().Where(whereExpression).CountAsync();
}
2022-06-14 19:01:35 +08:00
public virtual async Task<bool> InsertOrUpdateAsync(T data)
{
return await this.Context.Storageable(data).ExecuteCommandAsync() > 0;
}
public virtual async Task<bool> InsertOrUpdateAsync(List<T> datas)
{
return await this.Context.Storageable(datas).ExecuteCommandAsync() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual async Task<bool> InsertAsync(T insertObj)
{
return await this.Context.Insertable(insertObj).ExecuteCommandAsync() > 0;
}
public virtual Task<int> InsertReturnIdentityAsync(T insertObj)
{
return this.Context.Insertable(insertObj).ExecuteReturnIdentityAsync();
}
2021-01-17 02:50:01 +08:00
public virtual Task<long> InsertReturnBigIdentityAsync(T insertObj)
{
return this.Context.Insertable(insertObj).ExecuteReturnBigIdentityAsync();
}
2022-09-10 21:19:05 +08:00
public virtual async Task<T> InsertReturnEntityAsync(T insertObj)
{
return await this.Context.Insertable(insertObj).ExecuteReturnEntityAsync();
}
2020-10-25 20:33:32 +08:00
public virtual async Task<bool> InsertRangeAsync(T[] insertObjs)
{
return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;
}
public virtual async Task<bool> InsertRangeAsync(List<T> insertObjs)
{
return await this.Context.Insertable(insertObjs).ExecuteCommandAsync() > 0;
}
public virtual async Task<bool> UpdateAsync(T updateObj)
{
return await this.Context.Updateable(updateObj).ExecuteCommandAsync() > 0;
}
public virtual async Task<bool> UpdateRangeAsync(T[] updateObjs)
{
return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0;
}
public virtual async Task<bool> UpdateRangeAsync(List<T> updateObjs)
{
return await this.Context.Updateable(updateObjs).ExecuteCommandAsync() > 0;
}
public virtual async Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
{
return await this.Context.Updateable<T>().SetColumns(columns).Where(whereExpression).ExecuteCommandAsync() > 0;
2022-09-29 20:50:12 +08:00
}
public virtual async Task<bool> UpdateSetColumnsTrueAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
{
return await this.Context.Updateable<T>().SetColumns(columns,true).Where(whereExpression).ExecuteCommandAsync() > 0;
2020-10-25 20:33:32 +08:00
}
public virtual async Task<bool> DeleteAsync(T deleteObj)
{
return await this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommandAsync() > 0;
}
2022-04-30 19:03:10 +08:00
public virtual async Task<bool> DeleteAsync(List<T> deleteObjs)
{
return await this.Context.Deleteable<T>().Where(deleteObjs).ExecuteCommandAsync() > 0;
}
2020-10-25 20:33:32 +08:00
public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
{
return await this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommandAsync() > 0;
}
public virtual async Task<bool> DeleteByIdAsync(dynamic id)
{
2020-12-29 23:56:46 +08:00
return await this.Context.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
2020-10-25 20:33:32 +08:00
}
public virtual async Task<bool> DeleteByIdsAsync(dynamic[] ids)
{
return await this.Context.Deleteable<T>().In(ids).ExecuteCommandAsync() > 0;
}
#endregion
2021-12-07 11:30:01 +08:00
2017-08-24 15:18:53 +08:00
}
}