mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-24 16:18:47 +08:00
Update db.Storageable
This commit is contained in:
parent
0d95bfef6e
commit
317af187ac
@ -64,28 +64,30 @@ namespace OrmTest
|
|||||||
}
|
}
|
||||||
list2.First().Name = null;
|
list2.First().Name = null;
|
||||||
db.DbMaintenance.TruncateTable<UinitBlukTable>();
|
db.DbMaintenance.TruncateTable<UinitBlukTable>();
|
||||||
|
db.Insertable(new UinitBlukTable() { Id = 2, Name = "b", Create = DateTime.Now }).ExecuteCommand();
|
||||||
var x=Db.Storageable(list2)
|
var x=Db.Storageable(list2)
|
||||||
.SplitInsert(it => !string.IsNullOrEmpty(it.Item.Name))
|
.SplitInsert(it => it.NotAny(y=>y.Id==it.Item.Id))
|
||||||
.SplitUpdate(it =>it.Database.Any(y=>y.Id==it.Item.Id))
|
.SplitUpdate(it => it.Any(y => y.Id == it.Item.Id))
|
||||||
.SplitDelete(it=>it.Item.Id>10)
|
.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 == 3,"id不能等于3")
|
||||||
.SplitError(it => it.Item.Id == 4, "id不能等于4")
|
.SplitError(it => it.Item.Id == 4, "id不能等于4")
|
||||||
.SplitError(it => it.Item.Id == 5, "id不能等于5")
|
.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();
|
.ToStorage();
|
||||||
x.AsDeleteable.ExecuteCommand();
|
x.AsDeleteable.ExecuteCommand();
|
||||||
x.AsInsertable.ExecuteCommand();
|
x.AsInsertable.ExecuteCommand();
|
||||||
x.AsUpdateable.ExecuteCommand();
|
x.AsUpdateable.ExecuteCommand();
|
||||||
foreach (var item in x.ErrorList)
|
foreach (var item in x.ErrorList)
|
||||||
{
|
{
|
||||||
Console.Write(item.StorageMessage);
|
Console.Write(item.StorageMessage+" ");
|
||||||
}
|
}
|
||||||
db.DbMaintenance.TruncateTable<UinitBlukTable>();
|
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)]
|
||||||
|
@ -15,7 +15,7 @@ namespace SqlSugar
|
|||||||
List<StorageableInfo<T>> allDatas = new List<StorageableInfo<T>>();
|
List<StorageableInfo<T>> allDatas = new List<StorageableInfo<T>>();
|
||||||
List<T> dbDataList = new List<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>>();
|
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)
|
public Storageable(List<T> datas, SqlSugarProvider context)
|
||||||
{
|
{
|
||||||
this.Context = context;
|
this.Context = context;
|
||||||
@ -64,6 +64,15 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
if (this.allDatas.Count == 0)
|
if (this.allDatas.Count == 0)
|
||||||
return new StorageableResult<T>();
|
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>()
|
var messageList = allDatas.Select(it => new StorageableMessage<T>()
|
||||||
{
|
{
|
||||||
Item = it.Item,
|
Item = it.Item,
|
||||||
@ -99,10 +108,10 @@ namespace SqlSugar
|
|||||||
IgnoreList = ignore,
|
IgnoreList = ignore,
|
||||||
TotalList = messageList
|
TotalList = messageList
|
||||||
};
|
};
|
||||||
if (this.columns != null)
|
if (this.whereExpression != null)
|
||||||
{
|
{
|
||||||
result.AsUpdateable.WhereColumns(columns);
|
result.AsUpdateable.WhereColumns(whereExpression);
|
||||||
result.AsDeleteable.WhereColumns(columns);
|
result.AsDeleteable.WhereColumns(whereExpression);
|
||||||
}
|
}
|
||||||
result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
|
result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
|
||||||
return result;
|
return result;
|
||||||
@ -120,9 +129,13 @@ namespace SqlSugar
|
|||||||
it.DbColumnName.Equals(y, StringComparison.CurrentCultureIgnoreCase) ||
|
it.DbColumnName.Equals(y, StringComparison.CurrentCultureIgnoreCase) ||
|
||||||
it.PropertyName.Equals(y, StringComparison.CurrentCultureIgnoreCase))
|
it.PropertyName.Equals(y, StringComparison.CurrentCultureIgnoreCase))
|
||||||
).ToList();
|
).ToList();
|
||||||
|
if (whereColumns.Count == 0)
|
||||||
|
{
|
||||||
|
whereColumns = dbColumns.Where(it => it.IsPrimarykey).ToList();
|
||||||
|
}
|
||||||
if (whereColumns.Count > 0)
|
if (whereColumns.Count > 0)
|
||||||
{
|
{
|
||||||
this.Context.Utilities.PageEach(allDatas, 300, itemList =>
|
this.Context.Utilities.PageEach(allDatas, 200, itemList =>
|
||||||
{
|
{
|
||||||
List<IConditionalModel> conditList = new List<IConditionalModel>();
|
List<IConditionalModel> conditList = new List<IConditionalModel>();
|
||||||
SetConditList(itemList, whereColumns, conditList);
|
SetConditList(itemList, whereColumns, conditList);
|
||||||
@ -130,27 +143,31 @@ namespace SqlSugar
|
|||||||
this.dbDataList.AddRange(addItem);
|
this.dbDataList.AddRange(addItem);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
this.whereExpression = columns;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SetConditList(List<StorageableInfo<T>> itemList, List<EntityColumnInfo> whereColumns, List<IConditionalModel> conditList)
|
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)
|
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)
|
foreach (var item in whereColumns)
|
||||||
{
|
{
|
||||||
conditList.Add(condition);
|
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(i==0?WhereType.Or :WhereType.And, new ConditionalModel()
|
||||||
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel()
|
|
||||||
{
|
{
|
||||||
FieldName = item.DbColumnName,
|
FieldName = item.DbColumnName,
|
||||||
ConditionalType = ConditionalType.Equal,
|
ConditionalType = ConditionalType.Equal,
|
||||||
FieldValue = item.PropertyInfo.GetValue(dataItem, null) + ""
|
FieldValue = item.PropertyInfo.GetValue(dataItem.Item, null) + ""
|
||||||
}));
|
}));
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -742,8 +742,9 @@ namespace SqlSugar
|
|||||||
public IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new()
|
public IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new()
|
||||||
{
|
{
|
||||||
this.InitMappingInfo<T>();
|
this.InitMappingInfo<T>();
|
||||||
|
var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
|
||||||
var result= new Storageable<T>(dataList,this);
|
var result= new Storageable<T>(dataList,this);
|
||||||
result.Builder = this._SqlBuilder;
|
result.Builder = sqlBuilder;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -21,7 +21,7 @@ namespace SqlSugar
|
|||||||
public class StorageableInfo<T> where T : class, new()
|
public class StorageableInfo<T> where T : class, new()
|
||||||
{
|
{
|
||||||
public T Item { get; set; }
|
public T Item { get; set; }
|
||||||
public List<T> Database { get; set; }
|
internal List<T> Database { get; set; }
|
||||||
public bool Any(Func<T,bool> expression)
|
public bool Any(Func<T,bool> expression)
|
||||||
{
|
{
|
||||||
return Database.Any(expression);
|
return Database.Any(expression);
|
||||||
|
Loading…
Reference in New Issue
Block a user