Update db.Storageable

This commit is contained in:
skx 2021-01-29 01:17:51 +08:00
parent 4c798e151c
commit 5a9478ee0d
6 changed files with 206 additions and 122 deletions

View File

@ -65,8 +65,8 @@ namespace OrmTest
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))
.SplitInsert(it => !string.IsNullOrEmpty(it.Item.Name))
.SplitUpdate(it =>it.Database.Any(y=>y.Id==it.Item.Id))
.SplitDelete(it=>it.Item.Id>10)
.SplitIgnore(it=>it.Item.Id==2)
.SplitError(it => it.Item.Id == 3,"id不能等于3")
@ -74,9 +74,13 @@ namespace OrmTest
.SplitError(it => it.Item.Id == 5, "id不能等于5")
.WhereColumns(it=>it.Id)
.ToStorage();
x.AsDeleteable.ExecuteCommand();
x.AsInsertable.ExecuteCommand();
x.AsDeleteable.ExecuteCommand();
x.AsInsertable.ExecuteCommand();
x.AsUpdateable.ExecuteCommand();
foreach (var item in x.ErrorList)
{
Console.Write(item.StorageMessage);
}
db.DbMaintenance.TruncateTable<UinitBlukTable>();
}
public class UinitBlukTable

View File

@ -1,115 +0,0 @@
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>();
var messageList = datas.Select(it => new StorageableMessage<T>() {
Item=it.Item
}).ToList();
foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
{
List<StorageableMessage<T>> whereList = messageList.Where(it => it.StorageType == null).ToList();
Func<StorageableMessage<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 = messageList.Where(it => it.StorageType == StorageType.Delete).ToList();
var update = messageList.Where(it => it.StorageType == StorageType.Update).ToList();
var inset = messageList.Where(it => it.StorageType == StorageType.Insert).ToList();
var error = messageList.Where(it => it.StorageType == StorageType.Error).ToList();
var ignore = messageList.Where(it => it.StorageType == StorageType.Ignore||it.StorageType==null).ToList();
var other = messageList.Where(it => it.StorageType == StorageType.Other).ToList();
StorageableResult<T> result = new StorageableResult<T>()
{
AsDeleteable = this.Context.Deleteable<T>(),
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 = messageList
};
if (this.columns != null)
{
result.AsUpdateable.WhereColumns(columns);
result.AsDeleteable.WhereColumns(columns);
}
result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
return result;
}
public IStorageable<T> WhereColumns(Expression<Func<T, object>> columns)
{
this.columns = columns;
return this;
}
}
}

View File

@ -0,0 +1,184 @@
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()
{
SqlSugarProvider Context { get; set; }
internal ISqlBuilder Builder;
List<SugarParameter> Parameters;
List<StorageableInfo<T>> allDatas = new List<StorageableInfo<T>>();
List<T> dbDataList = new List<T>();
List<KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>> whereFuncs = new List<KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>>();
Expression<Func<T, object>> columns;
public Storageable(List<T> datas, SqlSugarProvider context)
{
this.Context = context;
if (datas == null)
datas = new List<T>();
this.allDatas = 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.allDatas.Count == 0)
return new StorageableResult<T>();
var messageList = allDatas.Select(it => new StorageableMessage<T>()
{
Item = it.Item,
Database = dbDataList
}).ToList();
foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
{
List<StorageableMessage<T>> whereList = messageList.Where(it => it.StorageType == null).ToList();
Func<StorageableMessage<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 = messageList.Where(it => it.StorageType == StorageType.Delete).ToList();
var update = messageList.Where(it => it.StorageType == StorageType.Update).ToList();
var inset = messageList.Where(it => it.StorageType == StorageType.Insert).ToList();
var error = messageList.Where(it => it.StorageType == StorageType.Error).ToList();
var ignore = messageList.Where(it => it.StorageType == StorageType.Ignore || it.StorageType == null).ToList();
var other = messageList.Where(it => it.StorageType == StorageType.Other).ToList();
StorageableResult<T> result = new StorageableResult<T>()
{
AsDeleteable = this.Context.Deleteable<T>(),
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 = messageList
};
if (this.columns != null)
{
result.AsUpdateable.WhereColumns(columns);
result.AsDeleteable.WhereColumns(columns);
}
result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
return result;
}
public IStorageable<T> WhereColumns(Expression<Func<T, object>> columns)
{
if (columns == null)
return this;
else
{
List<string> list = GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => Builder.GetNoTranslationColumnName(it)).ToList();
var dbColumns = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsIgnore == false);
var whereColumns = dbColumns.Where(it => list.Any(y =>
it.DbColumnName.Equals(y, StringComparison.CurrentCultureIgnoreCase) ||
it.PropertyName.Equals(y, StringComparison.CurrentCultureIgnoreCase))
).ToList();
if (whereColumns.Count > 0)
{
this.Context.Utilities.PageEach(allDatas, 300, itemList =>
{
List<IConditionalModel> conditList = new List<IConditionalModel>();
var condition = new ConditionalCollections()
{
ConditionalList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()
};
foreach (var dataItem in itemList)
{
foreach (var item in whereColumns)
{
conditList.Add(condition);
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel()
{
FieldName = item.DbColumnName,
ConditionalType = ConditionalType.Equal,
FieldValue = item.PropertyInfo.GetValue(dataItem,null)+""
}));
}
var addItem = this.Context.Queryable<T>().Where(conditList).ToList();
this.dbDataList.AddRange(addItem);
}
});
}
return this;
}
}
public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
{
ILambdaExpressions resolveExpress = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig); ;
if (this.Context.CurrentConnectionConfig.MoreSettings != null)
{
resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
}
else
{
resolveExpress.PgSqlIsAutoToLower = true;
}
resolveExpress.MappingColumns = Context.MappingColumns;
resolveExpress.MappingTables = Context.MappingTables;
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
resolveExpress.InitMappingInfo = Context.InitMappingInfo;
resolveExpress.RefreshMapping = () =>
{
resolveExpress.MappingColumns = Context.MappingColumns;
resolveExpress.MappingTables = Context.MappingTables;
resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
};
resolveExpress.Resolve(expression, resolveType);
if (this.Parameters == null)
this.Parameters = new List<SugarParameter>();
this.Parameters.AddRange(resolveExpress.Parameters);
var result = resolveExpress.Result;
return result;
}
}
}

View File

@ -741,7 +741,10 @@ namespace SqlSugar
}
public IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new()
{
return new Storageable<T>(dataList,this);
this.InitMappingInfo<T>();
var result= new Storageable<T>(dataList,this);
result.Builder = this._SqlBuilder;
return result;
}
#endregion

View File

@ -21,7 +21,15 @@ namespace SqlSugar
public class StorageableInfo<T> where T : class, new()
{
public T Item { get; set; }
public List<T> ExistData { get; set; }
public List<T> Database { get; set; }
public bool Any(Func<T,bool> expression)
{
return Database.Any(expression);
}
public bool NotAny(Func<T, bool> expression)
{
return !Database.Any(expression);
}
}
public class StorageableMessage<T> : StorageableInfo<T> where T : class, new()

View File

@ -88,7 +88,7 @@
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
<Compile Include="Abstract\SaveableProvider\IStorageable.cs" />
<Compile Include="Abstract\SaveableProvider\Storageable.cs" />
<Compile Include="Entities\StackTraceInfo.cs" />
<Compile Include="Entities\SubInsertTree.cs" />
<Compile Include="ExpressionsToSql\Common\MapperExpression.cs" />