mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add db.Storageable
This commit is contained in:
parent
7de6ba71d9
commit
3baad5150e
@ -31,9 +31,9 @@ namespace OrmTest
|
|||||||
}
|
}
|
||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
|
Insert();
|
||||||
Enum();
|
Enum();
|
||||||
Tran();
|
Tran();
|
||||||
Insert();
|
|
||||||
Queue();
|
Queue();
|
||||||
CodeFirst();
|
CodeFirst();
|
||||||
Updateable();
|
Updateable();
|
||||||
|
@ -11,6 +11,7 @@ namespace OrmTest
|
|||||||
public static void Insert()
|
public static void Insert()
|
||||||
{
|
{
|
||||||
var db = Db;
|
var db = Db;
|
||||||
|
db.DbMaintenance.TruncateTable<UinitBlukTable>();
|
||||||
db.CodeFirst.InitTables<UinitBlukTable>();
|
db.CodeFirst.InitTables<UinitBlukTable>();
|
||||||
db.Insertable(new List<UinitBlukTable>
|
db.Insertable(new List<UinitBlukTable>
|
||||||
{
|
{
|
||||||
@ -50,9 +51,34 @@ namespace OrmTest
|
|||||||
{
|
{
|
||||||
throw new Exception("Unit Insert");
|
throw new Exception("Unit Insert");
|
||||||
}
|
}
|
||||||
|
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
|
||||||
|
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<UinitBlukTable>();
|
||||||
|
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<UinitBlukTable>();
|
||||||
}
|
}
|
||||||
public class UinitBlukTable
|
public class UinitBlukTable
|
||||||
{
|
{
|
||||||
|
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
[SqlSugar.SugarColumn(IsNullable =true)]
|
[SqlSugar.SugarColumn(IsNullable =true)]
|
||||||
|
106
Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/IStorageable.cs
Normal file
106
Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/IStorageable.cs
Normal file
@ -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<T> : IStorageable<T> where T : class, new()
|
||||||
|
{
|
||||||
|
List<StorageableInfo<T>> datas = new List<StorageableInfo<T>>();
|
||||||
|
|
||||||
|
List<KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>> whereFuncs = new List<KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>,string>>();
|
||||||
|
SqlSugarProvider Context { get; set; }
|
||||||
|
Expression<Func<T, object>> columns;
|
||||||
|
public Storageable(List<T> datas, SqlSugarProvider context)
|
||||||
|
{
|
||||||
|
this.Context = context;
|
||||||
|
if (datas == null)
|
||||||
|
datas = new List<T>();
|
||||||
|
this.datas = datas.Select(it => new StorageableInfo<T>()
|
||||||
|
{
|
||||||
|
Item = it
|
||||||
|
}).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IStorageable<T> SplitInsert(Func<StorageableInfo<T>, bool> conditions, string message = null)
|
||||||
|
{
|
||||||
|
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>,string>(StorageType.Insert, conditions, message));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public IStorageable<T> SplitDelete(Func<StorageableInfo<T>, bool> conditions, string message = null)
|
||||||
|
{
|
||||||
|
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Delete, conditions, message));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public IStorageable<T> SplitUpdate(Func<StorageableInfo<T>, bool> conditions, string message = null)
|
||||||
|
{
|
||||||
|
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Update, conditions, message));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
public IStorageable<T> SplitError(Func<StorageableInfo<T>, bool> conditions, string message = null)
|
||||||
|
{
|
||||||
|
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Error, conditions, message));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IStorageable<T> SplitIgnore(Func<StorageableInfo<T>, bool> conditions, string message = null)
|
||||||
|
{
|
||||||
|
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Ignore, conditions, message));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IStorageable<T> SplitOther(Func<StorageableInfo<T>, bool> conditions, string message = null)
|
||||||
|
{
|
||||||
|
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Other, conditions, message));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StorageableResult<T> ToStorage()
|
||||||
|
{
|
||||||
|
if (this.datas.Count == 0)
|
||||||
|
return new StorageableResult<T>();
|
||||||
|
foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
|
||||||
|
{
|
||||||
|
List<StorageableInfo<T>> whereList = datas.Where(it => it.StorageType == null).ToList();
|
||||||
|
Func<StorageableInfo<T>, 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<T> result = new StorageableResult<T>()
|
||||||
|
{
|
||||||
|
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<T> WhereColumns(Expression<Func<T, object>> columns)
|
||||||
|
{
|
||||||
|
this.columns = columns;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -674,7 +674,11 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
public virtual IUpdateable<T> Updateable<T>(List<T> UpdateObjs) where T : class, new()
|
public virtual IUpdateable<T> Updateable<T>(List<T> 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<T>();
|
||||||
|
}
|
||||||
return Updateable(UpdateObjs.ToArray());
|
return Updateable(UpdateObjs.ToArray());
|
||||||
}
|
}
|
||||||
public virtual IUpdateable<T> Updateable<T>(T UpdateObj) where T : class, new()
|
public virtual IUpdateable<T> Updateable<T>(T UpdateObj) where T : class, new()
|
||||||
@ -735,6 +739,10 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return new SaveableProvider<T>(this, saveObject);
|
return new SaveableProvider<T>(this, saveObject);
|
||||||
}
|
}
|
||||||
|
public IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new()
|
||||||
|
{
|
||||||
|
return new Storageable<T>(dataList,this);
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region DbFirst
|
#region DbFirst
|
||||||
|
@ -360,7 +360,7 @@ namespace SqlSugar
|
|||||||
this.UpdateBuilder.TableName = mappingInfo.DbTableName;
|
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;
|
int i = 0;
|
||||||
foreach (var item in UpdateObjs)
|
foreach (var item in UpdateObjs)
|
||||||
{
|
{
|
||||||
|
@ -116,6 +116,7 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Saveable
|
#region Saveable
|
||||||
|
IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new();
|
||||||
ISaveable<T> Saveable<T>(List<T> saveObjects) where T : class, new();
|
ISaveable<T> Saveable<T>(List<T> saveObjects) where T : class, new();
|
||||||
ISaveable<T> Saveable<T>(T saveObject) where T : class, new();
|
ISaveable<T> Saveable<T>(T saveObject) where T : class, new();
|
||||||
#endregion
|
#endregion
|
||||||
|
64
Src/Asp.Net/SqlSugar/Interface/IStorageable.cs
Normal file
64
Src/Asp.Net/SqlSugar/Interface/IStorageable.cs
Normal file
@ -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<T> where T : class, new()
|
||||||
|
{
|
||||||
|
IStorageable<T> WhereColumns(Expression<Func<T, object>> columns);
|
||||||
|
IStorageable<T> SplitInsert(Func<StorageableInfo<T>, bool> conditions, string message=null);
|
||||||
|
IStorageable<T> SplitUpdate(Func<StorageableInfo<T>, bool> conditions, string message = null);
|
||||||
|
IStorageable<T> SplitError(Func<StorageableInfo<T>, bool> conditions, string message = null);
|
||||||
|
IStorageable<T> SplitIgnore(Func<StorageableInfo<T>, bool> conditions, string message = null);
|
||||||
|
IStorageable<T> SplitDelete(Func<StorageableInfo<T>, bool> conditions, string message = null);
|
||||||
|
IStorageable<T> SplitOther(Func<StorageableInfo<T>, bool> conditions, string message = null);
|
||||||
|
StorageableResult<T> ToStorage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StorageableInfo<T> where T : class, new()
|
||||||
|
{
|
||||||
|
public T Item { get; set; }
|
||||||
|
public List<T> 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<TKey, TValue,TValue2>
|
||||||
|
{
|
||||||
|
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<T> where T : class, new()
|
||||||
|
{
|
||||||
|
public List<StorageableInfo<T>> TotalList { get; set; }
|
||||||
|
public List<StorageableInfo<T>> InsertList { get; set; }
|
||||||
|
public List<StorageableInfo<T>> UpdateList { get; set; }
|
||||||
|
public List<StorageableInfo<T>> DeleteList { get; set; }
|
||||||
|
public List<StorageableInfo<T>> ErrorList { get; set; }
|
||||||
|
public List<StorageableInfo<T>> IgnoreList { get; set; }
|
||||||
|
public List<StorageableInfo<T>> OtherList { get; set; }
|
||||||
|
public IInsertable<T> AsInsertable { get; set; }
|
||||||
|
public IUpdateable<T> AsUpdateable { get; set; }
|
||||||
|
public IDeleteable<T> AsDeleteable { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -88,12 +88,14 @@
|
|||||||
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
|
||||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||||
|
<Compile Include="Abstract\SaveableProvider\IStorageable.cs" />
|
||||||
<Compile Include="Entities\StackTraceInfo.cs" />
|
<Compile Include="Entities\StackTraceInfo.cs" />
|
||||||
<Compile Include="Entities\SubInsertTree.cs" />
|
<Compile Include="Entities\SubInsertTree.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Common\MapperExpression.cs" />
|
<Compile Include="ExpressionsToSql\Common\MapperExpression.cs" />
|
||||||
<Compile Include="ExpressionsToSql\ResolveItems\MapperExpressionResolve.cs" />
|
<Compile Include="ExpressionsToSql\ResolveItems\MapperExpressionResolve.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubInnerJoin.cs" />
|
<Compile Include="ExpressionsToSql\Subquery\Items\SubInnerJoin.cs" />
|
||||||
<Compile Include="ExpressionsToSql\Subquery\Items\SubLeftJoin.cs" />
|
<Compile Include="ExpressionsToSql\Subquery\Items\SubLeftJoin.cs" />
|
||||||
|
<Compile Include="Interface\IStorageable.cs" />
|
||||||
<Compile Include="OnlyNet\Compatible.cs" />
|
<Compile Include="OnlyNet\Compatible.cs" />
|
||||||
<Compile Include="OnlyNet\KdbndpInserttable.cs" />
|
<Compile Include="OnlyNet\KdbndpInserttable.cs" />
|
||||||
<Compile Include="Interface\ISubInsertable.cs" />
|
<Compile Include="Interface\ISubInsertable.cs" />
|
||||||
|
@ -338,6 +338,11 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Saveable
|
#region Saveable
|
||||||
|
|
||||||
|
public IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new()
|
||||||
|
{
|
||||||
|
return this.Context.Storageable(dataList);
|
||||||
|
}
|
||||||
public ISaveable<T> Saveable<T>(List<T> saveObjects) where T : class, new()
|
public ISaveable<T> Saveable<T>(List<T> saveObjects) where T : class, new()
|
||||||
{
|
{
|
||||||
return this.Context.Saveable<T>(saveObjects);
|
return this.Context.Saveable<T>(saveObjects);
|
||||||
|
Loading…
Reference in New Issue
Block a user