From 3baad5150e0cca3a0e20b74bafb4b814decddf3e Mon Sep 17 00:00:00 2001 From: skx <610262374@qq.com> Date: Thu, 28 Jan 2021 21:09:15 +0800 Subject: [PATCH] Add db.Storageable --- Src/Asp.Net/SqlServerTest/UnitTest/Main.cs | 2 +- Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs | 26 +++++ .../Abstract/SaveableProvider/IStorageable.cs | 106 ++++++++++++++++++ .../SugarProvider/SqlSugarProvider.cs | 10 +- .../UpdateProvider/UpdateableProvider.cs | 2 +- .../SqlSugar/Interface/ISqlSugarClient.cs | 1 + .../SqlSugar/Interface/IStorageable.cs | 64 +++++++++++ Src/Asp.Net/SqlSugar/SqlSugar.csproj | 2 + Src/Asp.Net/SqlSugar/SqlSugarClient.cs | 5 + 9 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/IStorageable.cs create mode 100644 Src/Asp.Net/SqlSugar/Interface/IStorageable.cs diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs index 9453499c2..086587078 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs @@ -31,9 +31,9 @@ namespace OrmTest } public static void Init() { + Insert(); Enum(); Tran(); - Insert(); Queue(); CodeFirst(); Updateable(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs index f0b20032f..8036697aa 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs @@ -11,6 +11,7 @@ namespace OrmTest public static void Insert() { var db = Db; + db.DbMaintenance.TruncateTable(); db.CodeFirst.InitTables(); db.Insertable(new List { @@ -50,9 +51,34 @@ namespace OrmTest { throw new Exception("Unit Insert"); } + List list2 = new List(); + for (int i = 0; i <= 20; i++) + { + UinitBlukTable data = new UinitBlukTable() + { + Create=DateTime.Now.AddDays(-1), + Id=i , + Name =i%3==0?"a":"b" + }; + list2.Add(data); + } + list2.First().Name = null; + db.DbMaintenance.TruncateTable(); + var x=Db.Storageable(list2) + .SplitInsert(it => !string.IsNullOrEmpty(it.Item.Name)) + .SplitUpdate(it => string.IsNullOrEmpty(it.Item.Name)) + .SplitDelete(it=>it.Item.Id>10) + .SplitIgnore(it=>it.Item.Id==2) + .SplitError(it => it.Item.Id == 3,"id不能等于3") + .ToStorage(); + x.AsDeleteable.ExecuteCommand(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + db.DbMaintenance.TruncateTable(); } public class UinitBlukTable { + [SqlSugar.SugarColumn(IsPrimaryKey =true)] public int Id { get; set; } public string Name { get; set; } [SqlSugar.SugarColumn(IsNullable =true)] diff --git a/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/IStorageable.cs b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/IStorageable.cs new file mode 100644 index 000000000..19d040c57 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/IStorageable.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class Storageable : IStorageable where T : class, new() + { + List> datas = new List>(); + + List, bool>, string>> whereFuncs = new List, bool>,string>>(); + SqlSugarProvider Context { get; set; } + Expression> columns; + public Storageable(List datas, SqlSugarProvider context) + { + this.Context = context; + if (datas == null) + datas = new List(); + this.datas = datas.Select(it => new StorageableInfo() + { + Item = it + }).ToList(); + } + + public IStorageable SplitInsert(Func, bool> conditions, string message = null) + { + whereFuncs.Add(new KeyValuePair, bool>,string>(StorageType.Insert, conditions, message)); + return this; + } + public IStorageable SplitDelete(Func, bool> conditions, string message = null) + { + whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Delete, conditions, message)); + return this; + } + public IStorageable SplitUpdate(Func, bool> conditions, string message = null) + { + whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Update, conditions, message)); + return this; + } + public IStorageable SplitError(Func, bool> conditions, string message = null) + { + whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Error, conditions, message)); + return this; + } + + public IStorageable SplitIgnore(Func, bool> conditions, string message = null) + { + whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Ignore, conditions, message)); + return this; + } + + public IStorageable SplitOther(Func, bool> conditions, string message = null) + { + whereFuncs.Add(new KeyValuePair, bool>, string>(StorageType.Other, conditions, message)); + return this; + } + + public StorageableResult ToStorage() + { + if (this.datas.Count == 0) + return new StorageableResult(); + foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key)) + { + List> whereList = datas.Where(it => it.StorageType == null).ToList(); + Func, bool> exp = item.value1; + var list = whereList.Where(exp).ToList(); + foreach (var it in list) + { + it.StorageType = item.key; + it.StorageMessage = item.value2; + } + } + var delete = datas.Where(it => it.StorageType == StorageType.Delete).ToList(); + var update = datas.Where(it => it.StorageType == StorageType.Update).ToList(); + var inset = datas.Where(it => it.StorageType == StorageType.Insert).ToList(); + var error = datas.Where(it => it.StorageType == StorageType.Error).ToList(); + var ignore = datas.Where(it => it.StorageType == StorageType.Ignore).ToList(); + var other = datas.Where(it => it.StorageType == StorageType.Other).ToList(); + StorageableResult result = new StorageableResult() + { + AsDeleteable = this.Context.Deleteable(delete.Select(it => it.Item).ToList()), + AsUpdateable = this.Context.Updateable(update.Select(it => it.Item).ToList()), + AsInsertable = this.Context.Insertable(inset.Select(it => it.Item).ToList()), + OtherList = other, + InsertList = inset, + DeleteList = delete, + UpdateList = update, + ErrorList = error, + IgnoreList = ignore, + TotalList = datas + }; + return result; + } + + + public IStorageable WhereColumns(Expression> columns) + { + this.columns = columns; + return this; + } + + } +} diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index de592afba..2de552455 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -674,7 +674,11 @@ namespace SqlSugar } public virtual IUpdateable Updateable(List UpdateObjs) where T : class, new() { - Check.ArgumentNullException(UpdateObjs, "Updateable.UpdateObjs can't be null"); + //Check.ArgumentNullException(UpdateObjs, "Updateable.UpdateObjs can't be null"); + if (UpdateObjs == null) + { + UpdateObjs = new List(); + } return Updateable(UpdateObjs.ToArray()); } public virtual IUpdateable Updateable(T UpdateObj) where T : class, new() @@ -735,6 +739,10 @@ namespace SqlSugar { return new SaveableProvider(this, saveObject); } + public IStorageable Storageable(List dataList) where T : class, new() + { + return new Storageable(dataList,this); + } #endregion #region DbFirst diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index d7d56a5c0..edecfb3c0 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -360,7 +360,7 @@ namespace SqlSugar this.UpdateBuilder.TableName = mappingInfo.DbTableName; } } - Check.Exception(UpdateObjs == null || UpdateObjs.Count() == 0, "UpdateObjs is null"); + //Check.Exception(UpdateObjs == null || UpdateObjs.Count() == 0, "UpdateObjs is null"); int i = 0; foreach (var item in UpdateObjs) { diff --git a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs index 4026507c4..0282d7d2b 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs @@ -116,6 +116,7 @@ namespace SqlSugar #endregion #region Saveable + IStorageable Storageable(List dataList) where T : class, new(); ISaveable Saveable(List saveObjects) where T : class, new(); ISaveable Saveable(T saveObject) where T : class, new(); #endregion diff --git a/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs new file mode 100644 index 000000000..456f26c19 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Interface/IStorageable.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; +namespace SqlSugar +{ + public interface IStorageable where T : class, new() + { + IStorageable WhereColumns(Expression> columns); + IStorageable SplitInsert(Func, bool> conditions, string message=null); + IStorageable SplitUpdate(Func, bool> conditions, string message = null); + IStorageable SplitError(Func, bool> conditions, string message = null); + IStorageable SplitIgnore(Func, bool> conditions, string message = null); + IStorageable SplitDelete(Func, bool> conditions, string message = null); + IStorageable SplitOther(Func, bool> conditions, string message = null); + StorageableResult ToStorage(); + } + + public class StorageableInfo where T : class, new() + { + public T Item { get; set; } + public List ExistData { get; set; } + public string StorageMessage { get; set; } + public StorageType? StorageType { get; set; } + } + + public enum StorageType + { + Insert=0, + Update=1, + Delete=2, + Error=3, + Other=4, + Ignore=5, + } + internal struct KeyValuePair + { + public TKey key; + public TValue value1; + public TValue2 value2; + public KeyValuePair(TKey key, TValue value1, TValue2 value2) + { + this.key = key; + this.value1 = value1; + this.value2 = value2; + } + } + + public class StorageableResult where T : class, new() + { + public List> TotalList { get; set; } + public List> InsertList { get; set; } + public List> UpdateList { get; set; } + public List> DeleteList { get; set; } + public List> ErrorList { get; set; } + public List> IgnoreList { get; set; } + public List> OtherList { get; set; } + public IInsertable AsInsertable { get; set; } + public IUpdateable AsUpdateable { get; set; } + public IDeleteable AsDeleteable { get; set; } + } +} diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index d9780a8fb..1327a5f5a 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -88,12 +88,14 @@ + + diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index 78426585e..f21fb1915 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -338,6 +338,11 @@ namespace SqlSugar #endregion #region Saveable + + public IStorageable Storageable(List dataList) where T : class, new() + { + return this.Context.Storageable(dataList); + } public ISaveable Saveable(List saveObjects) where T : class, new() { return this.Context.Saveable(saveObjects);