From 7ccee2cc9e7799de4de103cab7fffbc8f3fd084b Mon Sep 17 00:00:00 2001 From: skx <610262374@qq.com> Date: Thu, 4 Feb 2021 16:26:36 +0800 Subject: [PATCH] Storageable BUG --- .../SqlBuilderProvider/SqlBuilderProvider.cs | 4 + .../SqlSugar/Interface/ISqlSugarClient.cs | 1 + Src/Asp.Net/SqlSugar/SqlSugarClient.cs | 4 + Src/Asp.Net/SqliteTest/SqliteTest.csproj | 1 + Src/Asp.Net/SqliteTest/UnitTest/Main.cs | 7 +- Src/Asp.Net/SqliteTest/UnitTest/UInsert.cs | 198 ++++++++++++++++++ 6 files changed, 212 insertions(+), 3 deletions(-) create mode 100644 Src/Asp.Net/SqliteTest/UnitTest/UInsert.cs diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs index e2ee48b7c..36b82e54b 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs @@ -143,6 +143,10 @@ namespace SqlSugar { parameterName = parameterName.Replace("]", "_"); } + if (parameterName.Contains(this.SqlTranslationLeft)) + { + parameterName = parameterName.Replace(this.SqlTranslationLeft, "_"); + } switch (item.ConditionalType) { case ConditionalType.Equal: diff --git a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs index 0282d7d2b..08b959f95 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs @@ -117,6 +117,7 @@ namespace SqlSugar #region Saveable IStorageable Storageable(List dataList) where T : class, new(); + IStorageable Storageable(T data) 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/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index f21fb1915..3b8025d62 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -343,6 +343,10 @@ namespace SqlSugar { return this.Context.Storageable(dataList); } + public IStorageable Storageable(T data) where T : class, new() + { + return this.Context.Storageable(new List { data}); + } public ISaveable Saveable(List saveObjects) where T : class, new() { return this.Context.Saveable(saveObjects); diff --git a/Src/Asp.Net/SqliteTest/SqliteTest.csproj b/Src/Asp.Net/SqliteTest/SqliteTest.csproj index 6f98bf223..7676819a3 100644 --- a/Src/Asp.Net/SqliteTest/SqliteTest.csproj +++ b/Src/Asp.Net/SqliteTest/SqliteTest.csproj @@ -81,6 +81,7 @@ + diff --git a/Src/Asp.Net/SqliteTest/UnitTest/Main.cs b/Src/Asp.Net/SqliteTest/UnitTest/Main.cs index 4201c64eb..ea60816ab 100644 --- a/Src/Asp.Net/SqliteTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqliteTest/UnitTest/Main.cs @@ -31,15 +31,16 @@ namespace OrmTest } public static void Init() { + Insert(); CodeFirst(); Updateable(); Json(); Ado(); Queryable(); QueryableAsync(); - Thread(); - Thread2(); - Thread3(); + //Thread(); + //Thread2(); + //Thread3(); } } } diff --git a/Src/Asp.Net/SqliteTest/UnitTest/UInsert.cs b/Src/Asp.Net/SqliteTest/UnitTest/UInsert.cs new file mode 100644 index 000000000..647231c84 --- /dev/null +++ b/Src/Asp.Net/SqliteTest/UnitTest/UInsert.cs @@ -0,0 +1,198 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public partial class NewUnitTest + { + public static void Insert() + { + var db = Db; + + db.CodeFirst.InitTables(); + var list = db.Queryable().ToList(); + db.DbMaintenance.TruncateTable(); + + List list2 = new List(); + for (int i = 1; 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(); + db.Insertable(new UinitBlukTable() { Id = 2, Name = "b", Create = DateTime.Now }).ExecuteCommand(); + var x=Db.Storageable(list2) + .SplitInsert(it => it.NotAny(y=>y.Id==it.Item.Id)) + .SplitUpdate(it => it.Any(y => y.Id == it.Item.Id)) + .SplitDelete(it=>it.Item.Id>10) + .SplitIgnore(it=>it.Item.Id==1) + .SplitError(it => it.Item.Id == 3,"id不能等于3") + .SplitError(it => it.Item.Id == 4, "id不能等于4") + .SplitError(it => it.Item.Id == 5, "id不能等于5") + .SplitError(it => it.Item.Name==null, "name不能等于") + .WhereColumns(it=> new { it.Id }) + .ToStorage(); + x.AsDeleteable.ExecuteCommand(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + foreach (var item in x.ErrorList) + { + Console.Write(item.StorageMessage+" "); + } + db.DbMaintenance.TruncateTable(); + IDemo1(); + IDemo2(); + IDemo3(); + IDemo4(); + } + private static void IDemo4() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 3, Name = "a", Create = DateTime.Now.AddYears(-2) }); + list2.Add(new UinitBlukTable() { Id = 4, Name ="", Create = DateTime.Now.AddYears(-2) }); + + db.Insertable(list2.First()).ExecuteCommand();//插入第一条 + + var x = Db.Storageable(list2) + .SplitError(it => string.IsNullOrEmpty(it.Item.Name), "名称不能为空") + .SplitError(it => it.Item.Create it.Any(y=>y.Id==it.Item.Id))//存在更新 + .SplitInsert(it => true)//剩余的插入 + .ToStorage(); + Console.WriteLine(" 插入 {0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4},总共{5}" , + x.InsertList.Count, + x.UpdateList.Count, + x.ErrorList.Count, + x.IgnoreList.Count, + x.DeleteList.Count, + x.TotalList.Count + ); + foreach (var item in x.ErrorList) + { + Console.WriteLine("id等于"+item.Item.Id+" : "+item.StorageMessage); + } + + int i=x.AsInsertable.ExecuteCommand(); + Console.WriteLine(1 + "条成功插入"); + i=x.AsUpdateable.ExecuteCommand(); + Console.WriteLine(1 + "条成功更新"); + + db.DbMaintenance.TruncateTable(); + } + private static void IDemo3() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 3, Name = "a", Create = DateTime.Now }); + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Item.Id == 1) + .SplitInsert(it => it.Item.Id == 2) + .SplitDelete(it => it.Item.Id == 3).ToStorage(); + + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + x.AsDeleteable.ExecuteCommand(); + + if (x.InsertList.Count != 1) + { + throw new Exception("Unit Insert"); + } + if (x.UpdateList.Count != 1) + { + throw new Exception("Unit Insert"); + } + if (x.DeleteList.Count != 1) + { + throw new Exception("Unit Insert"); + } + db.DbMaintenance.TruncateTable(); + } + + private static void IDemo1() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 0, Name = "a", Create = DateTime.Now }); + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Item.Id > 0) + .SplitInsert(it => it.Item.Id == 0).ToStorage(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + + if (x.InsertList.Count > 1) + { + throw new Exception("Unit Insert"); + } + if (x.UpdateList.Count !=2) + { + throw new Exception("Unit Insert"); + } + db.DbMaintenance.TruncateTable(); + } + private static void IDemo2() + { + var db = Db; + db.DbMaintenance.TruncateTable(); + List list2 = new List(); + list2.Add(new UinitBlukTable() { Id = 1, Name = "a1", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 2, Name = "a2", Create = DateTime.Now }); + list2.Add(new UinitBlukTable() { Id = 3, Name = "a3", Create = DateTime.Now }); + db.Insertable(list2[1]).ExecuteCommand(); + + var x = Db.Storageable(list2) + .SplitUpdate(it => it.Any(y=>y.Id==it.Item.Id)) + .SplitInsert(it => it.NotAny(y => y.Id == it.Item.Id)).ToStorage(); + x.AsInsertable.ExecuteCommand(); + x.AsUpdateable.ExecuteCommand(); + + + if (x.InsertList.Count!=2) + { + throw new Exception("Unit Insert"); + } + if (x.UpdateList.Count != 1) + { + throw new Exception("Unit Insert"); + } + db.DbMaintenance.TruncateTable(); + } + + public class UinitBlukTable + { + [SqlSugar.SugarColumn(IsPrimaryKey =true)] + public int Id { get; set; } + public string Name { get; set; } + [SqlSugar.SugarColumn(IsNullable =true)] + public DateTime? Create { get; set; } + } + [SqlSugar.SugarTable("UinitBlukTable")] + public class UinitBlukTable2 + { + public string Name { get; set; } + public int Id { get; set; } + } + + } +}