Update db.Storageable

This commit is contained in:
skx 2021-01-29 15:28:20 +08:00
parent 0d95bfef6e
commit 317af187ac
4 changed files with 40 additions and 20 deletions

View File

@ -64,28 +64,30 @@ namespace OrmTest
}
list2.First().Name = null;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
db.Insertable(new UinitBlukTable() { Id = 2, Name = "b", Create = DateTime.Now }).ExecuteCommand();
var x=Db.Storageable(list2)
.SplitInsert(it => !string.IsNullOrEmpty(it.Item.Name))
.SplitUpdate(it =>it.Database.Any(y=>y.Id==it.Item.Id))
.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==2)
.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")
.WhereColumns(it=>it.Id)
.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);
Console.Write(item.StorageMessage+" ");
}
db.DbMaintenance.TruncateTable<UinitBlukTable>();
}
public class UinitBlukTable
{
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; }
public string Name { get; set; }
[SqlSugar.SugarColumn(IsNullable =true)]

View File

@ -15,7 +15,7 @@ namespace SqlSugar
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;
Expression<Func<T, object>> whereExpression;
public Storageable(List<T> datas, SqlSugarProvider context)
{
this.Context = context;
@ -64,6 +64,15 @@ namespace SqlSugar
{
if (this.allDatas.Count == 0)
return new StorageableResult<T>();
var pkInfos = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsPrimarykey);
if (whereExpression==null&&!pkInfos.Any())
{
Check.Exception(true, "Need primary key or WhereColumn");
}
if (whereExpression == null && pkInfos.Any())
{
}
var messageList = allDatas.Select(it => new StorageableMessage<T>()
{
Item = it.Item,
@ -99,10 +108,10 @@ namespace SqlSugar
IgnoreList = ignore,
TotalList = messageList
};
if (this.columns != null)
if (this.whereExpression != null)
{
result.AsUpdateable.WhereColumns(columns);
result.AsDeleteable.WhereColumns(columns);
result.AsUpdateable.WhereColumns(whereExpression);
result.AsDeleteable.WhereColumns(whereExpression);
}
result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
return result;
@ -120,9 +129,13 @@ namespace SqlSugar
it.DbColumnName.Equals(y, StringComparison.CurrentCultureIgnoreCase) ||
it.PropertyName.Equals(y, StringComparison.CurrentCultureIgnoreCase))
).ToList();
if (whereColumns.Count == 0)
{
whereColumns = dbColumns.Where(it => it.IsPrimarykey).ToList();
}
if (whereColumns.Count > 0)
{
this.Context.Utilities.PageEach(allDatas, 300, itemList =>
this.Context.Utilities.PageEach(allDatas, 200, itemList =>
{
List<IConditionalModel> conditList = new List<IConditionalModel>();
SetConditList(itemList, whereColumns, conditList);
@ -130,27 +143,31 @@ namespace SqlSugar
this.dbDataList.AddRange(addItem);
});
}
this.whereExpression = columns;
return this;
}
}
private static void SetConditList(List<StorageableInfo<T>> itemList, List<EntityColumnInfo> whereColumns, List<IConditionalModel> conditList)
{
var condition = new ConditionalCollections()
{
ConditionalList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()
};
;
foreach (var dataItem in itemList)
{
var condition = new ConditionalCollections()
{
ConditionalList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()
};
conditList.Add(condition);
int i = 0;
foreach (var item in whereColumns)
{
conditList.Add(condition);
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel()
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(i==0?WhereType.Or :WhereType.And, new ConditionalModel()
{
FieldName = item.DbColumnName,
ConditionalType = ConditionalType.Equal,
FieldValue = item.PropertyInfo.GetValue(dataItem, null) + ""
FieldValue = item.PropertyInfo.GetValue(dataItem.Item, null) + ""
}));
++i;
}
}
}

View File

@ -742,8 +742,9 @@ namespace SqlSugar
public IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new()
{
this.InitMappingInfo<T>();
var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
var result= new Storageable<T>(dataList,this);
result.Builder = this._SqlBuilder;
result.Builder = sqlBuilder;
return result;
}
#endregion

View File

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