mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-08 10:24:55 +08:00
Update core
This commit is contained in:
@@ -1206,7 +1206,7 @@ namespace SqlSugar
|
||||
}
|
||||
private void Async()
|
||||
{
|
||||
if (this.Context.Root != null & this.Context.Root.AsyncId == null)
|
||||
if (this.Context.Root != null && this.Context.Root.AsyncId == null)
|
||||
{
|
||||
this.Context.Root.AsyncId = Guid.NewGuid(); ;
|
||||
}
|
||||
|
||||
@@ -121,6 +121,10 @@ namespace SqlSugar
|
||||
entityInfo.DbTableName = tableName;
|
||||
entityInfo.Columns.ForEach(it => { it.DbTableName = tableName; });
|
||||
var isAny = this.Context.DbMaintenance.IsAnyTable(tableName);
|
||||
if (isAny&&entityInfo.IsDisabledUpdateAll)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (isAny)
|
||||
ExistLogic(entityInfo);
|
||||
else
|
||||
@@ -147,7 +151,7 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual void ExistLogic(EntityInfo entityInfo)
|
||||
{
|
||||
if (entityInfo.Columns.HasValue())
|
||||
if (entityInfo.Columns.HasValue()&&entityInfo.IsDisabledUpdateAll==false)
|
||||
{
|
||||
//Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Multiple primary keys do not support modifications");
|
||||
|
||||
@@ -184,10 +188,13 @@ namespace SqlSugar
|
||||
this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
||||
isChange = true;
|
||||
}
|
||||
foreach (var item in dropColumns)
|
||||
if (entityInfo.IsDisabledDelete==false)
|
||||
{
|
||||
this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName);
|
||||
isChange = true;
|
||||
foreach (var item in dropColumns)
|
||||
{
|
||||
this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName);
|
||||
isChange = true;
|
||||
}
|
||||
}
|
||||
foreach (var item in alterColumns)
|
||||
{
|
||||
|
||||
@@ -28,6 +28,8 @@ namespace SqlSugar
|
||||
var sugarTable = (SugarTable)sugarAttributeInfo;
|
||||
result.DbTableName = sugarTable.TableName;
|
||||
result.TableDescription = sugarTable.TableDescription;
|
||||
result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll;
|
||||
result.IsDisabledDelete = sugarTable.IsDisabledDelete;
|
||||
}
|
||||
if (this.Context.Context.CurrentConnectionConfig.ConfigureExternalServices != null && this.Context.CurrentConnectionConfig.ConfigureExternalServices.EntityNameService != null) {
|
||||
if (result.DbTableName == null)
|
||||
|
||||
@@ -667,6 +667,20 @@ namespace SqlSugar
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IInsertable<T> CallEntityMethod(Expression<Action<T>> method)
|
||||
{
|
||||
if (this.InsertObjs.HasValue())
|
||||
{
|
||||
var expression = (LambdaExpression.Lambda(method).Body as LambdaExpression).Body;
|
||||
Check.Exception(!(expression is MethodCallExpression), method.ToString() + " is not method");
|
||||
var callExpresion = expression as MethodCallExpression;
|
||||
UtilMethods.DataInoveByExpresson(this.InsertObjs,callExpresion);
|
||||
this.InsertBuilder.DbColumnInfoList = new List<DbColumnInfo>();
|
||||
Init();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
@@ -72,8 +72,13 @@ namespace SqlSugar
|
||||
}
|
||||
return resul;
|
||||
}
|
||||
[Obsolete("use ExecuteCommand")]
|
||||
public object ExecuteReturnPrimaryKey()
|
||||
{
|
||||
return ExecuteCommand();
|
||||
}
|
||||
|
||||
public object ExecuteReturnPrimaryKey()
|
||||
public object ExecuteCommand()
|
||||
{
|
||||
var isNoTrean = this.Context.Ado.Transaction == null;
|
||||
try
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace SqlSugar
|
||||
public ISqlBuilder SqlBuilder { get; set; }
|
||||
public MappingTableList OldMappingTableList { get; set; }
|
||||
public MappingTableList QueryableMappingTableList { get; set; }
|
||||
public Action<T> MapperAction { get; set; }
|
||||
public List<Action<T>> MapperAction { get; set; }
|
||||
public Action<T, MapperCache<T>> MapperActionWithCache { get; set; }
|
||||
public List<Action<List<T>>> Mappers { get; set; }
|
||||
public bool IsCache { get; set; }
|
||||
@@ -105,9 +105,97 @@ namespace SqlSugar
|
||||
|
||||
public virtual ISugarQueryable<T> Mapper(Action<T> mapperAction)
|
||||
{
|
||||
this.MapperAction = mapperAction;
|
||||
this.MapperAction=UtilMethods.IsNullReturnNew(this.MapperAction);
|
||||
this.MapperAction.Add(mapperAction);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Mapper<AType, BType, MappingType>(Expression<Func<MappingType, ManyToMany>> expression)
|
||||
{
|
||||
var args = ((expression as LambdaExpression).Body as MethodCallExpression).Arguments;
|
||||
|
||||
Type aType = typeof(AType);
|
||||
Type bType = typeof(BType);
|
||||
Type bListType = typeof(List<BType>);
|
||||
|
||||
this.Context.InitMappingInfo(aType);
|
||||
this.Context.InitMappingInfo(bType);
|
||||
this.Context.InitMappingInfo(typeof(MappingType));
|
||||
|
||||
//Mapping
|
||||
var mappingEntity = this.Context.EntityMaintenance.GetEntityInfo(typeof(MappingType));
|
||||
string m_aPropertyName = (args[0] as MemberExpression).Member.Name;
|
||||
string m_bPropertyName= (args[1] as MemberExpression).Member.Name;
|
||||
var m_aDbField = mappingEntity.Columns.First(it => it.PropertyName == m_aPropertyName).DbColumnName;
|
||||
var m_bDbField = mappingEntity.Columns.First(it => it.PropertyName == m_bPropertyName).DbColumnName;
|
||||
|
||||
//A
|
||||
var aEntity = this.Context.EntityMaintenance.GetEntityInfo(aType);
|
||||
var aPropertyName = aEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true)?.PropertyName;
|
||||
Check.Exception(aPropertyName == null, aEntity.EntityName + " no primary key");
|
||||
|
||||
//B
|
||||
var bEntity = this.Context.EntityMaintenance.GetEntityInfo(bType);
|
||||
var bProperty = bEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true)?.PropertyName;
|
||||
Check.Exception(bProperty == null, bEntity.EntityName + " no primary key");
|
||||
var bDbFiled = bEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true).DbColumnName;
|
||||
this.Mapper((it,cache) =>
|
||||
{
|
||||
var list= cache.Get<Dictionary<object, List<BType>>>(oldList=> {
|
||||
|
||||
|
||||
//query mapping by a
|
||||
var cons = new List<IConditionalModel>() {
|
||||
new ConditionalModel(){
|
||||
ConditionalType=ConditionalType.In,
|
||||
FieldName= m_aDbField,
|
||||
FieldValue=string.Join(",",oldList.Select(z=>UtilMethods.GetPropertyValue(z,aPropertyName)).Distinct())
|
||||
}
|
||||
};
|
||||
var mappingList = this.Context.Queryable<MappingType>().Where(cons).ToList();
|
||||
var bids = mappingList.Select(z => UtilMethods.GetPropertyValue(z, m_bPropertyName)).Distinct().ToList();
|
||||
|
||||
//queryable b by mapping
|
||||
cons = new List<IConditionalModel>() {
|
||||
new ConditionalModel(){
|
||||
ConditionalType=ConditionalType.In,
|
||||
FieldName= bDbFiled,
|
||||
FieldValue=string.Join(",",mappingList.Select(z=>UtilMethods.GetPropertyValue(z,m_bPropertyName)).Distinct())
|
||||
}
|
||||
};
|
||||
var bList = this.Context.Queryable<BType>().Where(cons).ToList();
|
||||
|
||||
//get result
|
||||
Dictionary<object, List<BType>> result = new Dictionary<object, List<BType>>();
|
||||
var group = mappingList.GroupBy(z => UtilMethods.GetPropertyValue(z, m_aPropertyName));
|
||||
foreach (var item in group)
|
||||
{
|
||||
var currentBids = item.Select(z => UtilMethods.GetPropertyValue(z, m_bPropertyName)).ToList();
|
||||
result.Add(item.Key, bList.Where(z => currentBids.Contains(UtilMethods.GetPropertyValue(z, bProperty))).ToList());
|
||||
}
|
||||
return result;
|
||||
|
||||
}, expression.ToString());
|
||||
foreach (var item in aEntity.Columns)
|
||||
{
|
||||
var aid = UtilMethods.GetPropertyValue(it, aPropertyName);
|
||||
if (list.ContainsKey(aid))
|
||||
{
|
||||
if (item.PropertyInfo.PropertyType == bType)
|
||||
{
|
||||
var b=UtilMethods.ChangeType<BType>(list[aid].FirstOrDefault());
|
||||
item.PropertyInfo.SetValue(it, b);
|
||||
}
|
||||
else if (item.PropertyInfo.PropertyType == bListType)
|
||||
{
|
||||
var bList = UtilMethods.ChangeType<List<BType>>(list[aid]);
|
||||
item.PropertyInfo.SetValue(it, bList);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual ISugarQueryable<T> Mapper(Action<T, MapperCache<T>> mapperAction)
|
||||
{
|
||||
this.MapperActionWithCache = mapperAction;
|
||||
@@ -120,7 +208,7 @@ namespace SqlSugar
|
||||
{
|
||||
CallContext.MapperExpression.Value = new List<MapperExpression>();
|
||||
}
|
||||
CallContext.MapperExpression.Value.Add(new MapperExpression() { SqlBuilder = SqlBuilder, QueryBuilder = this.QueryBuilder, Type = MapperExpressionType.oneToOne, FillExpression = mapperObject, MappingField1Expression = mainField, MappingField2Expression=childField, Context = this.Context });
|
||||
CallContext.MapperExpression.Value.Add(new MapperExpression() { SqlBuilder = SqlBuilder, QueryBuilder = this.QueryBuilder, Type = MapperExpressionType.oneToOne, FillExpression = mapperObject, MappingField1Expression = mainField, MappingField2Expression = childField, Context = this.Context });
|
||||
return _Mapper<TObject>(mapperObject, mainField, childField);
|
||||
}
|
||||
public ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, List<TObject>>> mapperObject, Expression<Func<T, object>> mainField, Expression<Func<T, object>> childField)
|
||||
@@ -134,7 +222,7 @@ namespace SqlSugar
|
||||
{
|
||||
CallContext.MapperExpression.Value = new List<MapperExpression>();
|
||||
}
|
||||
CallContext.MapperExpression.Value.Add(new MapperExpression() { SqlBuilder = SqlBuilder, QueryBuilder = this.QueryBuilder, Type = MapperExpressionType.oneToN, FillExpression = mapperObject, MappingField1Expression = mapperField,Context = this.Context });
|
||||
CallContext.MapperExpression.Value.Add(new MapperExpression() { SqlBuilder = SqlBuilder, QueryBuilder = this.QueryBuilder, Type = MapperExpressionType.oneToN, FillExpression = mapperObject, MappingField1Expression = mapperField, Context = this.Context });
|
||||
return _Mapper<TObject>(mapperObject, mapperField);
|
||||
}
|
||||
public virtual ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, TObject>> mapperObject, Expression<Func<T, object>> mapperField)
|
||||
@@ -143,7 +231,7 @@ namespace SqlSugar
|
||||
{
|
||||
CallContext.MapperExpression.Value = new List<MapperExpression>();
|
||||
}
|
||||
CallContext.MapperExpression.Value.Add(new MapperExpression() { SqlBuilder= SqlBuilder, QueryBuilder = this.QueryBuilder, Type=MapperExpressionType.oneToOne, FillExpression=mapperObject, MappingField1Expression= mapperField,Context=this.Context });
|
||||
CallContext.MapperExpression.Value.Add(new MapperExpression() { SqlBuilder = SqlBuilder, QueryBuilder = this.QueryBuilder, Type = MapperExpressionType.oneToOne, FillExpression = mapperObject, MappingField1Expression = mapperField, Context = this.Context });
|
||||
return _Mapper<TObject>(mapperObject, mapperField);
|
||||
}
|
||||
|
||||
@@ -197,7 +285,7 @@ namespace SqlSugar
|
||||
{
|
||||
return WhereClass(new List<ClassType>() { whereClass }, ignoreDefaultValue);
|
||||
}
|
||||
public ISugarQueryable<T> WhereClassByPrimaryKey(List<T> list)
|
||||
public ISugarQueryable<T> WhereClassByPrimaryKey(List<T> list)
|
||||
{
|
||||
_WhereClassByPrimaryKey(list);
|
||||
return this;
|
||||
@@ -213,7 +301,7 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
/// <param name="whereClassTypes"></param>
|
||||
/// <returns></returns>
|
||||
public ISugarQueryable<T> _WhereClassByPrimaryKey(List<T> whereClassTypes)
|
||||
public ISugarQueryable<T> _WhereClassByPrimaryKey(List<T> whereClassTypes)
|
||||
{
|
||||
|
||||
if (whereClassTypes.HasValue())
|
||||
@@ -580,7 +668,7 @@ namespace SqlSugar
|
||||
}
|
||||
else if (result.Count >= 2)
|
||||
{
|
||||
Check.Exception(true,ErrorMessage.GetThrowMessage(".Single() result must not exceed one . You can use.First()","使用single查询结果集不能大于1,适合主键查询,如果大于1你可以使用Queryable.First"));
|
||||
Check.Exception(true, ErrorMessage.GetThrowMessage(".Single() result must not exceed one . You can use.First()", "使用single查询结果集不能大于1,适合主键查询,如果大于1你可以使用Queryable.First"));
|
||||
return default(T);
|
||||
}
|
||||
else
|
||||
@@ -680,8 +768,8 @@ namespace SqlSugar
|
||||
Check.Exception(this.QueryBuilder.SelectValue.IsNullOrEmpty(), "MergeTable need to use Queryable.Select Method .");
|
||||
Check.Exception(this.QueryBuilder.Skip > 0 || this.QueryBuilder.Take > 0 || this.QueryBuilder.OrderByValue.HasValue(), "MergeTable Queryable cannot Take Skip OrderBy PageToList ");
|
||||
var sqlobj = this.ToSql();
|
||||
var index = QueryBuilder.WhereIndex+1;
|
||||
var result= this.Context.Queryable<T>().AS(SqlBuilder.GetPackTable(sqlobj.Key, "MergeTable")).AddParameters(sqlobj.Value).Select("*").With(SqlWith.Null);
|
||||
var index = QueryBuilder.WhereIndex + 1;
|
||||
var result = this.Context.Queryable<T>().AS(SqlBuilder.GetPackTable(sqlobj.Key, "MergeTable")).AddParameters(sqlobj.Value).Select("*").With(SqlWith.Null);
|
||||
result.QueryBuilder.WhereIndex = index;
|
||||
result.QueryBuilder.LambdaExpressions.ParameterIndex = QueryBuilder.LambdaExpressions.ParameterIndex++;
|
||||
result.QueryBuilder.LambdaExpressions.Index = QueryBuilder.LambdaExpressions.Index++;
|
||||
@@ -778,7 +866,7 @@ namespace SqlSugar
|
||||
var cacheService = this.Context.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
|
||||
var result = CacheSchemeMain.GetOrCreate<string>(cacheService, this.QueryBuilder, () =>
|
||||
{
|
||||
return this.Context.Utilities.SerializeObject(this.ToList(),typeof(T));
|
||||
return this.Context.Utilities.SerializeObject(this.ToList(), typeof(T));
|
||||
}, CacheTime, this.Context);
|
||||
return result;
|
||||
}
|
||||
@@ -795,6 +883,56 @@ namespace SqlSugar
|
||||
{
|
||||
return this.Context.Utilities.SerializeObject(this.ToPageList(pageIndex, pageSize, ref totalNumber), typeof(T));
|
||||
}
|
||||
public List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||
{
|
||||
List<T> result = new List<T>() { };
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey).Count() == 0, "No Primary key");
|
||||
var parentIdName =UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name;
|
||||
var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName);
|
||||
var parentPropertyName= ParentInfo.DbColumnName;
|
||||
var current = this.Context.Queryable<T>().InSingle(primaryKeyValue);
|
||||
if (current != null)
|
||||
{
|
||||
result.Add(current);
|
||||
object parentId = ParentInfo.PropertyInfo.GetValue(current,null);
|
||||
int i = 0;
|
||||
while (parentId!=null&&this.Context.Queryable<T>().In(parentId).Any())
|
||||
{
|
||||
Check.Exception(i > 100, ErrorMessage.GetThrowMessage("Dead cycle", "出现死循环或超出循环上限(100),检查最顶层的ParentId是否是null或者0"));
|
||||
var parent = this.Context.Queryable<T>().InSingle(parentId);
|
||||
result.Add(parent);
|
||||
parentId= ParentInfo.PropertyInfo.GetValue(parent, null);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public async Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||
{
|
||||
List<T> result = new List<T>() { };
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
Check.Exception(entity.Columns.Where(it => it.IsPrimarykey).Count() == 0, "No Primary key");
|
||||
var parentIdName = UtilConvert.ToMemberExpression((parentIdExpression as LambdaExpression).Body).Member.Name;
|
||||
var ParentInfo = entity.Columns.First(it => it.PropertyName == parentIdName);
|
||||
var parentPropertyName = ParentInfo.DbColumnName;
|
||||
var current =await this.Context.Queryable<T>().InSingleAsync(primaryKeyValue);
|
||||
if (current != null)
|
||||
{
|
||||
result.Add(current);
|
||||
object parentId = ParentInfo.PropertyInfo.GetValue(current, null);
|
||||
int i = 0;
|
||||
while (parentId != null &&await this.Context.Queryable<T>().In(parentId).AnyAsync())
|
||||
{
|
||||
Check.Exception(i > 100, ErrorMessage.GetThrowMessage("Dead cycle", "出现死循环或超出循环上限(100),检查最顶层的ParentId是否是null或者0"));
|
||||
var parent =await this.Context.Queryable<T>().InSingleAsync(parentId);
|
||||
result.Add(parent);
|
||||
parentId = ParentInfo.PropertyInfo.GetValue(parent, null);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<T> ToTree(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue)
|
||||
{
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
@@ -1558,7 +1696,10 @@ namespace SqlSugar
|
||||
{
|
||||
if (typeof(TResult) == typeof(T))
|
||||
{
|
||||
this.MapperAction((T)(item as object));
|
||||
foreach (var mapper in this.MapperAction)
|
||||
{
|
||||
mapper((T)(item as object));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -42,6 +42,13 @@ namespace SqlSugar
|
||||
whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Update, conditions, message));
|
||||
return this;
|
||||
}
|
||||
|
||||
public IStorageable<T> Saveable(string inserMessage = null,string updateMessage=null)
|
||||
{
|
||||
return this
|
||||
.SplitUpdate(it => it.Any(),updateMessage)
|
||||
.SplitInsert(it => true, inserMessage);
|
||||
}
|
||||
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));
|
||||
@@ -63,7 +70,18 @@ namespace SqlSugar
|
||||
public StorageableResult<T> ToStorage()
|
||||
{
|
||||
if (this.allDatas.Count == 0)
|
||||
return new StorageableResult<T>();
|
||||
return new StorageableResult<T>() {
|
||||
AsDeleteable = this.Context.Deleteable<T>().Where(it => false),
|
||||
AsInsertable = this.Context.Insertable(new List<T>()),
|
||||
AsUpdateable = this.Context.Updateable(new List<T>()),
|
||||
InsertList = new List<StorageableMessage<T>>(),
|
||||
UpdateList = new List<StorageableMessage<T>>(),
|
||||
DeleteList = new List<StorageableMessage<T>>(),
|
||||
ErrorList = new List<StorageableMessage<T>>(),
|
||||
IgnoreList = new List<StorageableMessage<T>>(),
|
||||
OtherList=new List<StorageableMessage<T>>(),
|
||||
TotalList=new List<StorageableMessage<T>>()
|
||||
};
|
||||
var pkInfos = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsPrimarykey);
|
||||
if (whereExpression==null&&!pkInfos.Any())
|
||||
{
|
||||
@@ -76,10 +94,12 @@ namespace SqlSugar
|
||||
dbDataList.AddRange(addItems);
|
||||
});
|
||||
}
|
||||
var pkProperties = GetPkProperties(pkInfos);
|
||||
var messageList = allDatas.Select(it => new StorageableMessage<T>()
|
||||
{
|
||||
Item = it.Item,
|
||||
Database = dbDataList
|
||||
Database = dbDataList,
|
||||
PkFields= pkProperties
|
||||
}).ToList();
|
||||
foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
|
||||
{
|
||||
@@ -120,6 +140,18 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
|
||||
private string[] GetPkProperties(IEnumerable<EntityColumnInfo> pkInfos)
|
||||
{
|
||||
if (whereExpression == null)
|
||||
{
|
||||
return pkInfos.Select(it => it.PropertyName).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
return wherecolumnList.Select(it => it.PropertyName).ToArray();
|
||||
}
|
||||
}
|
||||
List<EntityColumnInfo> wherecolumnList;
|
||||
public IStorageable<T> WhereColumns(Expression<Func<T, object>> columns)
|
||||
{
|
||||
if (columns == null)
|
||||
@@ -132,6 +164,7 @@ namespace SqlSugar
|
||||
it.DbColumnName.Equals(y, StringComparison.CurrentCultureIgnoreCase) ||
|
||||
it.PropertyName.Equals(y, StringComparison.CurrentCultureIgnoreCase))
|
||||
).ToList();
|
||||
wherecolumnList = whereColumns;
|
||||
if (whereColumns.Count == 0)
|
||||
{
|
||||
whereColumns = dbColumns.Where(it => it.IsPrimarykey).ToList();
|
||||
|
||||
@@ -173,6 +173,20 @@ namespace SqlSugar
|
||||
}
|
||||
|
||||
#region Update by object
|
||||
public IUpdateable<T> CallEntityMethod(Expression<Action<T>> method)
|
||||
{
|
||||
ThrowUpdateByExpression();
|
||||
if (this.UpdateObjs.HasValue())
|
||||
{
|
||||
var expression = (LambdaExpression.Lambda(method).Body as LambdaExpression).Body;
|
||||
Check.Exception(!(expression is MethodCallExpression), method.ToString() + " is not method");
|
||||
var callExpresion = expression as MethodCallExpression;
|
||||
UtilMethods.DataInoveByExpresson(this.UpdateObjs, callExpresion);
|
||||
this.UpdateBuilder.DbColumnInfoList = new List<DbColumnInfo>();
|
||||
Init();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public IUpdateable<T> WhereColumns(Expression<Func<T, object>> columns)
|
||||
{
|
||||
@@ -302,7 +316,7 @@ namespace SqlSugar
|
||||
|
||||
public IUpdateable<T> Where(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
ThrowUpdateByObject();
|
||||
Check.Exception(UpdateObjectNotWhere()&&UpdateObjs.Length > 1, ErrorMessage.GetThrowMessage("update List no support where","集合更新不支持Where请使用WhereColumns"));
|
||||
var expResult = UpdateBuilder.GetExpressionValue(expression, ResolveExpressType.WhereSingle);
|
||||
var whereString = expResult.GetResultString();
|
||||
if (expression.ToString().Contains("Subqueryable()"))
|
||||
@@ -314,7 +328,7 @@ namespace SqlSugar
|
||||
}
|
||||
public IUpdateable<T> Where(string whereSql, object parameters = null)
|
||||
{
|
||||
ThrowUpdateByObject();
|
||||
Check.Exception(UpdateObjectNotWhere() && UpdateObjs.Length > 1, ErrorMessage.GetThrowMessage("update List no support where", "集合更新不支持Where请使用WhereColumns"));
|
||||
if (whereSql.HasValue())
|
||||
{
|
||||
UpdateBuilder.WhereValues.Add(whereSql);
|
||||
@@ -327,16 +341,22 @@ namespace SqlSugar
|
||||
}
|
||||
public IUpdateable<T> Where(string fieldName, string conditionalType, object fieldValue)
|
||||
{
|
||||
ThrowUpdateByObject();
|
||||
Check.Exception(UpdateObjectNotWhere() && UpdateObjs.Length > 1, ErrorMessage.GetThrowMessage("update List no support where", "集合更新不支持Where请使用WhereColumns"));
|
||||
var whereSql = this.SqlBuilder.GetWhere(fieldName, conditionalType, 0);
|
||||
this.Where(whereSql);
|
||||
string parameterName = this.SqlBuilder.SqlParameterKeyWord + fieldName + "0";
|
||||
this.UpdateBuilder.Parameters.Add(new SugarParameter(parameterName, fieldValue));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
|
||||
private bool UpdateObjectNotWhere()
|
||||
{
|
||||
return this.Context.CurrentConnectionConfig.DbType != DbType.MySql && this.Context.CurrentConnectionConfig.DbType != DbType.SqlServer;
|
||||
}
|
||||
|
||||
private void AppendSets()
|
||||
{
|
||||
if (SetColumnsIndex > 0)
|
||||
|
||||
@@ -15,5 +15,7 @@ namespace SqlSugar
|
||||
public string TableDescription { get; set; }
|
||||
public Type Type { get; set; }
|
||||
public List<EntityColumnInfo> Columns { get; set; }
|
||||
public bool IsDisabledDelete { get; set; }
|
||||
public bool IsDisabledUpdateAll { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class ManyToMany
|
||||
{
|
||||
public static ManyToMany Config<AField,BField>(AField aField,BField bField)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,21 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public Result Get<Result>(Func<List<T>, Result> action,string cachekey)
|
||||
{
|
||||
GetIndex++;
|
||||
string key = "Get" + typeof(Result) + action.GetHashCode() + action.Method.Name+ cachekey;
|
||||
if (caches.ContainsKey(key))
|
||||
{
|
||||
return (Result)caches[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = action(_list);
|
||||
caches.Add(key, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, double?> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
|
||||
@@ -11,6 +11,9 @@ namespace SqlSugar
|
||||
private SugarTable() { }
|
||||
public string TableName { get; set; }
|
||||
public string TableDescription { get; set; }
|
||||
public bool IsDisabledDelete { get; set; }
|
||||
public bool IsDisabledUpdateAll { get; set; }
|
||||
|
||||
public SugarTable(string tableName) {
|
||||
this.TableName = tableName;
|
||||
}
|
||||
@@ -19,6 +22,13 @@ namespace SqlSugar
|
||||
this.TableName = tableName;
|
||||
this.TableDescription = tableDescription;
|
||||
}
|
||||
|
||||
public SugarTable(string tableName, string tableDescription,bool isDisabledDelete)
|
||||
{
|
||||
this.TableName = tableName;
|
||||
this.TableDescription = tableDescription;
|
||||
this.IsDisabledDelete = isDisabledDelete;
|
||||
}
|
||||
}
|
||||
[AttributeUsage(AttributeTargets.Property , Inherited = true)]
|
||||
public class SugarColumn : Attribute
|
||||
|
||||
@@ -20,6 +20,7 @@ namespace SqlSugar
|
||||
ISugarQueryable<T> With(string withString);
|
||||
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter = false);
|
||||
ISugarQueryable<T> Mapper(Action<T> mapperAction);
|
||||
ISugarQueryable<T> Mapper<AType, BType, MappingType>(Expression<Func<MappingType, ManyToMany>> expression);
|
||||
ISugarQueryable<T> Mapper(Action<T, MapperCache<T>> mapperAction);
|
||||
ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, TObject>> mapperObject, Expression<Func<T, object>> mainField, Expression<Func<T, object>> childField);
|
||||
ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, List<TObject>>> mapperObject, Expression<Func<T, object>> mainField, Expression<Func<T, object>> childField);
|
||||
@@ -139,7 +140,8 @@ namespace SqlSugar
|
||||
string ToJsonPage(int pageIndex, int pageSize, ref int totalNumber);
|
||||
Task<string> ToJsonPageAsync(int pageIndex, int pageSize, RefAsync<int> totalNumber);
|
||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||
|
||||
List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
|
||||
Task<List<T>> ToParentListAsync(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue);
|
||||
List<T> ToTree(Expression<Func<T,IEnumerable<object>>> childListExpression, Expression<Func<T,object>> parentIdExpression,object rootValue);
|
||||
Task<List<T>> ToTreeAsync(Expression<Func<T, IEnumerable<object>>> childListExpression, Expression<Func<T, object>> parentIdExpression, object rootValue);
|
||||
DataTable ToDataTable();
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace SqlSugar
|
||||
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> Saveable(string inserMessage = null, string updateMessage = 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);
|
||||
@@ -22,6 +23,7 @@ namespace SqlSugar
|
||||
{
|
||||
public T Item { get; set; }
|
||||
internal List<T> Database { get; set; }
|
||||
internal string[] PkFields { get; set; }
|
||||
public bool Any(Func<T,bool> expression)
|
||||
{
|
||||
return Database.Any(expression);
|
||||
@@ -30,6 +32,19 @@ namespace SqlSugar
|
||||
{
|
||||
return !Database.Any(expression);
|
||||
}
|
||||
public bool Any()
|
||||
{
|
||||
var list = Database.Where(it=>true);
|
||||
foreach (var pk in PkFields)
|
||||
{
|
||||
list = list.Where(it => it.GetType().GetProperty(pk).GetValue(it, null).ObjToString() == Item.GetType().GetProperty(pk).GetValue(Item, null).ObjToString());
|
||||
}
|
||||
return list.Any();
|
||||
}
|
||||
public bool NotAny()
|
||||
{
|
||||
return !Any();
|
||||
}
|
||||
}
|
||||
|
||||
public class StorageableMessage<T> : StorageableInfo<T> where T : class, new()
|
||||
|
||||
@@ -7,6 +7,8 @@ namespace SqlSugar
|
||||
{
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> items);
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
|
||||
[Obsolete("use ExecuteCommand")]
|
||||
object ExecuteReturnPrimaryKey();
|
||||
object ExecuteCommand();
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,7 @@ namespace SqlSugar
|
||||
IUpdateable<T> EnableDiffLogEvent(object businessData = null);
|
||||
IUpdateable<T> ReSetValue(Expression<Func<T, bool>> setValueExpression);
|
||||
IUpdateable<T> RemoveDataCache();
|
||||
IUpdateable<T> CallEntityMethod(Expression<Action<T>> method);
|
||||
KeyValuePair<string,List<SugarParameter>> ToSql();
|
||||
void AddQueue();
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ namespace SqlSugar
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, object>> subForeignKey);
|
||||
ISubInsertable<T> AddSubList(Expression<Func<T, SubInsertTree>> tree);
|
||||
|
||||
IInsertable<T> CallEntityMethod(Expression<Action<T>> method);
|
||||
|
||||
IInsertable<T> EnableDiffLogEvent(object businessData = null);
|
||||
IInsertable<T> RemoveDataCache();
|
||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||
|
||||
@@ -30,6 +30,8 @@ namespace SqlSugar
|
||||
var IsBulkLoad = false;
|
||||
if (Entitys == null || Entitys.Length <= 0)
|
||||
return IsBulkLoad;
|
||||
if (Entitys.First() == null && Entitys.Length ==1)
|
||||
return IsBulkLoad;
|
||||
DataTable dt = new DataTable();
|
||||
Type type = typeof(T);
|
||||
var entity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
|
||||
@@ -242,7 +242,7 @@ namespace SqlSugar
|
||||
{
|
||||
get
|
||||
{
|
||||
return "";
|
||||
return " NOT NULL ";
|
||||
}
|
||||
}
|
||||
protected override string CreateTablePirmaryKey
|
||||
|
||||
@@ -17,6 +17,10 @@ namespace SqlSugar
|
||||
{
|
||||
return base.ExecuteCommand();
|
||||
}
|
||||
else if (base.UpdateObjs.Count() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
base.ExecuteCommand();
|
||||
|
||||
@@ -15,5 +15,40 @@ namespace SqlSugar
|
||||
return "SELECT {0}{"+UtilConstants.ReplaceKey+"} FROM {1}{2}{3}{4}";
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToSqlString()
|
||||
{
|
||||
string oldOrderBy = this.OrderByValue;
|
||||
string externalOrderBy = oldOrderBy;
|
||||
var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
|
||||
AppendFilter();
|
||||
sql = new StringBuilder();
|
||||
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY GetDate() ";
|
||||
if (this.PartitionByValue.HasValue())
|
||||
{
|
||||
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
||||
}
|
||||
var isFirst = (Skip == 0 || Skip == null) && Take == 1;
|
||||
var isRowNumber = (Skip != null || Take != null) && !isFirst;
|
||||
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
||||
string groupByValue = GetGroupByString + HavingInfos;
|
||||
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
|
||||
if (isIgnoreOrderBy) { orderByValue = null; }
|
||||
sql.AppendFormat(SqlTemplate, isFirst ? (" TOP 1 " + GetSelectValue) : GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue);
|
||||
sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
|
||||
if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
|
||||
var result = isFirst ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip);
|
||||
if (ExternalPageIndex > 0)
|
||||
{
|
||||
if (externalOrderBy.IsNullOrEmpty())
|
||||
{
|
||||
externalOrderBy = " ORDER BY GetDate() ";
|
||||
}
|
||||
result = string.Format("SELECT *,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result);
|
||||
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
|
||||
}
|
||||
this.OrderByValue = oldOrderBy;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace SqlSugar
|
||||
{
|
||||
public override void ExistLogic(EntityInfo entityInfo)
|
||||
{
|
||||
if (entityInfo.Columns.HasValue())
|
||||
if (entityInfo.Columns.HasValue()&&entityInfo.IsDisabledUpdateAll==false)
|
||||
{
|
||||
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||
|
||||
|
||||
@@ -579,7 +579,7 @@ namespace SqlSugar
|
||||
public SqlSugarProvider GetConnection(dynamic configId)
|
||||
{
|
||||
InitTenant();
|
||||
var db = this._AllClients.FirstOrDefault(it => it.ConnectionConfig.ConfigId == configId);
|
||||
var db = this._AllClients.FirstOrDefault(it =>Convert.ToString(it.ConnectionConfig.ConfigId) ==Convert.ToString(configId));
|
||||
if (db == null)
|
||||
{
|
||||
Check.Exception(true, "ConfigId was not found {0}", configId);
|
||||
|
||||
@@ -15,10 +15,19 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Join(",", array.Where(c => c != null).Select(it => (it + "").ToSqlValue()));
|
||||
return string.Join(",", array.Where(c => c != null).Select(it => it.ToSqlValue()));
|
||||
}
|
||||
}
|
||||
|
||||
public static string ToSqlValue(this object value)
|
||||
{
|
||||
if (value!=null&& UtilConstants.NumericalTypes.Contains(value.GetType()))
|
||||
return value.ToString();
|
||||
|
||||
var str = value + "";
|
||||
return str.ToSqlValue();
|
||||
}
|
||||
|
||||
public static string ToSqlValue(this string value)
|
||||
{
|
||||
return string.Format("'{0}'", value.ToSqlFilter());
|
||||
|
||||
@@ -43,5 +43,18 @@ namespace SqlSugar
|
||||
internal static Type DicArraySO = typeof(Dictionary<string, object>);
|
||||
|
||||
public static Type SugarType = typeof(SqlSugarProvider);
|
||||
|
||||
|
||||
internal static Type[] NumericalTypes = new Type[]
|
||||
{
|
||||
typeof(int),
|
||||
typeof(uint),
|
||||
typeof(byte),
|
||||
typeof(sbyte),
|
||||
typeof(long),
|
||||
typeof(ulong),
|
||||
typeof(short),
|
||||
typeof(ushort),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
namespace SqlSugar
|
||||
{
|
||||
@@ -116,5 +117,17 @@ namespace SqlSugar
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
|
||||
internal static MemberExpression ToMemberExpression(Expression parentIdExpression)
|
||||
{
|
||||
if (parentIdExpression is UnaryExpression)
|
||||
{
|
||||
return (parentIdExpression as UnaryExpression).Operand as MemberExpression;
|
||||
}
|
||||
else
|
||||
{
|
||||
return parentIdExpression as MemberExpression;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Security.Cryptography;
|
||||
@@ -243,7 +244,10 @@ namespace SqlSugar
|
||||
{
|
||||
return Convert.ToInt64(string.Join("", bytes).PadRight(20, '0'));
|
||||
}
|
||||
|
||||
public static object GetPropertyValue<T>(T t, string PropertyName)
|
||||
{
|
||||
return t.GetType().GetProperty(PropertyName).GetValue(t, null);
|
||||
}
|
||||
internal static string GetMD5(string myString)
|
||||
{
|
||||
MD5 md5 = new MD5CryptoServiceProvider();
|
||||
@@ -320,5 +324,26 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
|
||||
public static void DataInoveByExpresson<Type>(Type[] datas, MethodCallExpression callExpresion)
|
||||
{
|
||||
var methodInfo = callExpresion.Method;
|
||||
foreach (var item in datas)
|
||||
{
|
||||
if (callExpresion.Arguments.Count == 0)
|
||||
{
|
||||
methodInfo.Invoke(item, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
List<object> methodParameters = new List<object>();
|
||||
foreach (var callItem in callExpresion.Arguments)
|
||||
{
|
||||
var parameter = callItem.GetType().GetProperties().First(it => it.Name == "Value").GetValue(callItem, null);
|
||||
methodParameters.Add(parameter);
|
||||
}
|
||||
methodInfo.Invoke(item, methodParameters.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user