Browse Source

Add Queryable.WhereClassByPrimaryKey

pull/2/head
skx 4 years ago
parent
commit
19d2a91fbc
  1. 39
      Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs
  2. 64
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  3. 5
      Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs
  4. 5
      Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

39
Src/Asp.Net/SqlServerTest/UnitTest/UInsert.cs

@ -84,7 +84,46 @@ namespace OrmTest
Console.Write(item.StorageMessage+" ");
}
db.DbMaintenance.TruncateTable<UinitBlukTable>();
IDemo2();
}
private static void IDemo1()
{
var db = Db;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
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();
db.DbMaintenance.TruncateTable<UinitBlukTable>();
}
private static void IDemo2()
{
var db = Db;
db.DbMaintenance.TruncateTable<UinitBlukTable>();
List<UinitBlukTable> list2 = new List<UinitBlukTable>();
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();
db.DbMaintenance.TruncateTable<UinitBlukTable>();
}
public class UinitBlukTable
{
[SqlSugar.SugarColumn(IsPrimaryKey =true)]

64
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -197,6 +197,70 @@ namespace SqlSugar
{
return WhereClass(new List<ClassType>() { whereClass }, ignoreDefaultValue);
}
public ISugarQueryable<T> WhereClassByPrimaryKey(List<T> list)
{
_WhereClassByPrimaryKey(list);
return this;
}
public ISugarQueryable<T> WhereClassByPrimaryKey(T data)
{
_WhereClassByPrimaryKey(new List<T>() { data });
return this;
}
/// <summary>
/// if a property that is primary key is a condition
/// </summary>
/// <param name="whereClassTypes"></param>
/// <returns></returns>
public ISugarQueryable<T> _WhereClassByPrimaryKey(List<T> whereClassTypes)
{
if (whereClassTypes.HasValue())
{
var columns = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsIgnore == false && it.IsPrimarykey == true).ToList();
Check.Exception(columns == null || columns.Count == 0, "{0} no primary key, Can not use WhereClassByPrimaryKey ", typeof(T).Name);
Check.Exception(this.QueryBuilder.IsSingle() == false, "No support join query");
List<IConditionalModel> whereModels = new List<IConditionalModel>();
foreach (var item in whereClassTypes)
{
var cons = new ConditionalCollections();
foreach (var column in columns)
{
WhereType WhereType = WhereType.And;
var value = column.PropertyInfo.GetValue(item, null);
if (cons.ConditionalList == null)
{
cons.ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>();
if (QueryBuilder.WhereInfos.IsNullOrEmpty() && whereModels.IsNullOrEmpty())
{
}
else
{
WhereType = WhereType.Or;
}
}
cons.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(WhereType, new ConditionalModel()
{
ConditionalType = ConditionalType.Equal,
FieldName = this.QueryBuilder.Builder.GetTranslationColumnName(column.DbColumnName),
FieldValue = value.ObjToString()
}));
}
if (cons.HasValue())
{
whereModels.Add(cons);
}
}
this.Where(whereModels);
}
else
{
this.Where(" 1=2 ");
}
return this;
}
/// <summary>
/// if a property that is not empty is a condition
/// </summary>

5
Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/Storageable.cs

@ -71,7 +71,10 @@ namespace SqlSugar
}
if (whereExpression == null && pkInfos.Any())
{
this.Context.Utilities.PageEach(allDatas, 300, item => {
var addItems=this.Context.Queryable<T>().WhereClassByPrimaryKey(item.Select(it => it.Item).ToList()).ToList();
dbDataList.AddRange(addItems);
});
}
var messageList = allDatas.Select(it => new StorageableMessage<T>()
{

5
Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

@ -41,8 +41,9 @@ namespace SqlSugar
/// </summary>
/// <param name="whereClassTypes"></param>
/// <returns></returns>
ISugarQueryable<T> WhereClass<ClassType>(List<ClassType> whereClassList,bool ignoreDefaultValue = false) where ClassType : class, new();
ISugarQueryable<T> WhereClass<ClassType>(List<ClassType> whereClassList,bool ignoreDefaultValue = false) where ClassType : class, new();
ISugarQueryable<T> WhereClassByPrimaryKey(List<T> list);
ISugarQueryable<T> WhereClassByPrimaryKey(T data) ;
ISugarQueryable<T> Where(Expression<Func<T, bool>> expression);
ISugarQueryable<T> Where(string whereString, object parameters = null);
ISugarQueryable<T> Where(List<IConditionalModel> conditionalModels);

Loading…
Cancel
Save