mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-04 04:37:58 +08:00
Update .net core project
This commit is contained in:
parent
e9b992673a
commit
7e084cb4bc
@ -16,7 +16,8 @@ namespace SqlSugar
|
||||
public EntityInfo _ParentEntity { get; set; }
|
||||
public EntityColumnInfo _ParentPkColumn { get; set; }
|
||||
public SqlSugarProvider _Context { get; set; }
|
||||
|
||||
public NavigateType? _NavigateType { get; set; }
|
||||
public bool IsFirst { get; set; }
|
||||
|
||||
public InsertNavProvider<Root, Root> AsNav()
|
||||
{
|
||||
@ -30,8 +31,13 @@ namespace SqlSugar
|
||||
}
|
||||
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T, TChild>> expression) where TChild : class, new()
|
||||
{
|
||||
InitParentList();
|
||||
|
||||
var name = ExpressionTool.GetMemberName(expression);
|
||||
if (this._ParentEntity == null)
|
||||
{
|
||||
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
|
||||
this.IsFirst = true;
|
||||
}
|
||||
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||
if (nav.Navigat == null)
|
||||
{
|
||||
@ -39,37 +45,51 @@ namespace SqlSugar
|
||||
}
|
||||
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||
{
|
||||
InitParentList();
|
||||
InsertOneToOne<TChild>(name, nav);
|
||||
}
|
||||
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||
{
|
||||
_NavigateType = NavigateType.OneToMany;
|
||||
InitParentList();
|
||||
InsertOneToMany<TChild>(name, nav);
|
||||
}
|
||||
else
|
||||
{
|
||||
InitParentList();
|
||||
InsertManyToMany<TChild>(name, nav);
|
||||
}
|
||||
return GetResult<TChild>();
|
||||
}
|
||||
public InsertNavProvider<Root, TChild> ThenInclude<TChild>(Expression<Func<T,List<TChild>>> expression) where TChild : class, new()
|
||||
{
|
||||
InitParentList();
|
||||
var name = ExpressionTool.GetMemberName(expression);
|
||||
if (this._ParentEntity == null)
|
||||
{
|
||||
this._ParentEntity = this._Context.EntityMaintenance.GetEntityInfo<Root>();
|
||||
IsFirst = true;
|
||||
}
|
||||
var nav = this._ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
|
||||
;
|
||||
|
||||
if (nav.Navigat == null)
|
||||
{
|
||||
Check.ExceptionEasy($"{name} no navigate attribute", $"{this._ParentEntity.EntityName}的属性{name}没有导航属性");
|
||||
}
|
||||
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
|
||||
{
|
||||
InitParentList();
|
||||
InsertOneToOne<TChild>(name, nav);
|
||||
}
|
||||
else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
|
||||
{
|
||||
_NavigateType = NavigateType.OneToMany;
|
||||
InitParentList();
|
||||
InsertOneToMany<TChild>(name, nav);
|
||||
}
|
||||
else
|
||||
{
|
||||
InitParentList();
|
||||
InsertManyToMany<TChild>(name, nav);
|
||||
}
|
||||
return GetResult<TChild>();
|
||||
|
@ -26,7 +26,7 @@ namespace SqlSugar
|
||||
var pkColumn = this._Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it => it.IsPrimarykey);
|
||||
this._ParentPkColumn = pkColumn;
|
||||
}
|
||||
|
||||
IsFirst = false;
|
||||
}
|
||||
|
||||
private InsertNavProvider<Root, TChild> GetResult<TChild>() where TChild : class, new()
|
||||
@ -81,11 +81,26 @@ namespace SqlSugar
|
||||
children = children.Distinct().ToList();
|
||||
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
|
||||
var insertData = children = x.InsertList.Select(it => it.Item).ToList();
|
||||
if (_NavigateType == NavigateType.OneToMany&&IsFirst==false)
|
||||
{
|
||||
var updateData = x.UpdateList.Select(it => it.Item).ToList();
|
||||
ClearPk(updateData, pkColumn);
|
||||
insertData.AddRange(updateData);
|
||||
}
|
||||
Check.ExceptionEasy(pkColumn==null&&NavColumn==null,$"The entity is invalid",$"实体错误无法使用导航");
|
||||
InitData(pkColumn, insertData);
|
||||
this._ParentList = children.Cast<object>().ToList();
|
||||
}
|
||||
|
||||
private void ClearPk<TChild>(List<TChild> updateData, EntityColumnInfo pkColumn) where TChild : class, new()
|
||||
{
|
||||
foreach (var child in updateData)
|
||||
{
|
||||
var defaultValue =UtilMethods.DefaultForType(pkColumn.PropertyInfo.PropertyType);
|
||||
pkColumn.PropertyInfo.SetValue(child, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitData<TChild>(EntityColumnInfo pkColumn, List<TChild> insertData) where TChild : class, new()
|
||||
{
|
||||
if (pkColumn.IsIdentity || pkColumn.OracleSequenceName.HasValue())
|
||||
@ -128,7 +143,7 @@ namespace SqlSugar
|
||||
if (IsDefaultValue(pkColumn.PropertyInfo.GetValue(child)))
|
||||
{
|
||||
var name = pkColumn.EntityName + " " + pkColumn.DbColumnName;
|
||||
Check.ExceptionEasy($"The field {name} is not an autoassignment type and requires an assignment", $"字段{name}不是可自动赋值类型,需要赋值 , 可赋值类型有 自增、long、Guid、string");
|
||||
Check.ExceptionEasy($"The field {name} is not an autoassignment type and requires an assignment", $"字段{name}不是可自动赋值类型需要赋值(并且不能是已存在值) , 可赋值类型有 自增、long、Guid、string");
|
||||
}
|
||||
}
|
||||
this._Context.Insertable(insertData).ExecuteCommand();
|
||||
|
@ -382,7 +382,10 @@ namespace SqlSugar
|
||||
DependencyManagement.TryPostgreSQL();
|
||||
break;
|
||||
case DbType.OpenGauss:
|
||||
Check.ExceptionEasy("Use DbType.PostgreSQL , ConnectionString add No Reset On Close=true", "OpenGausso数据库可以使用DbType.PostgreSQL 并且连接字符串加上 No Reset On Close=true");
|
||||
Check.ExceptionEasy("Use DbType.PostgreSQL , ConnectionString add No Reset On Close=true", "OpenGausso数据库请使用DbType.PostgreSQL 并且连接字符串加上 No Reset On Close=true");
|
||||
break;
|
||||
case DbType.HG:
|
||||
Check.ExceptionEasy("Use DbType.PostgreSQL", "瀚高数据库请使用DbType.PostgreSQL");
|
||||
break;
|
||||
case DbType.Kdbndp:
|
||||
DependencyManagement.TryKdbndb();
|
||||
|
@ -62,6 +62,7 @@ namespace SqlSugar
|
||||
|
||||
public virtual int ExecuteCommandWithOptLock(bool IsVersionValidation=false)
|
||||
{
|
||||
Check.ExceptionEasy(this.UpdateBuilder.IsListUpdate==true, " OptLock can only be used on a single object, and the argument cannot be List", "乐观锁只能用于单个对象,参数不能是List,如果是一对多操作请更新主表统一用主表验证");
|
||||
var updateData = UpdateObjs.FirstOrDefault();
|
||||
if (updateData == null) return 0;
|
||||
var name=_ExecuteCommandWithOptLock(updateData);
|
||||
@ -92,6 +93,7 @@ namespace SqlSugar
|
||||
|
||||
public virtual async Task<int> ExecuteCommandWithOptLockAsync(bool IsVersionValidation = false)
|
||||
{
|
||||
Check.ExceptionEasy(this.UpdateBuilder.IsListUpdate == true, " OptLock can only be used on a single object, and the argument cannot be List", "乐观锁只能用于单个对象,参数不能是List,如果是一对多操作请更新主表统一用主表验证");
|
||||
var updateData = UpdateObjs.FirstOrDefault();
|
||||
if (updateData == null) return 0;
|
||||
var name=_ExecuteCommandWithOptLock(updateData);
|
||||
|
@ -19,6 +19,7 @@ namespace SqlSugar
|
||||
Access,
|
||||
OpenGauss,
|
||||
QuestDB,
|
||||
HG,
|
||||
Custom =900
|
||||
}
|
||||
}
|
||||
|
@ -430,5 +430,50 @@ namespace SqlSugar
|
||||
{
|
||||
return item is UnaryExpression && item.NodeType == ExpressionType.Convert;
|
||||
}
|
||||
|
||||
internal static List<NewExpressionInfo> GetNewexpressionInfos(Expression item,ExpressionContext context)
|
||||
{
|
||||
List<NewExpressionInfo> result = new List<NewExpressionInfo>();
|
||||
foreach (MemberBinding binding in ((MemberInitExpression)item).Bindings)
|
||||
{
|
||||
if (binding.BindingType != MemberBindingType.Assignment)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
MemberAssignment memberAssignment = (MemberAssignment)binding;
|
||||
NewExpressionInfo additem = new NewExpressionInfo();
|
||||
if ((memberAssignment.Expression is MemberExpression))
|
||||
{
|
||||
additem.LeftNameName = memberAssignment.Member.Name;
|
||||
var member = (memberAssignment.Expression as MemberExpression).Expression;
|
||||
additem.ShortName = member + "";
|
||||
additem.RightName = (memberAssignment.Expression as MemberExpression).Member.Name;
|
||||
additem.RightDbName = context.GetDbColumnName(member.Type.Name, additem.RightName);
|
||||
result.Add(additem);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
internal static List<NewExpressionInfo> GetNewDynamicexpressionInfos(Expression item, ExpressionContext context)
|
||||
{
|
||||
List<NewExpressionInfo> result = new List<NewExpressionInfo>();
|
||||
foreach (var binding in ((NewExpression)item).Arguments)
|
||||
{
|
||||
NewExpressionInfo additem = new NewExpressionInfo();
|
||||
if (binding is MemberExpression)
|
||||
{
|
||||
var member = (MemberExpression)binding;
|
||||
//var memberAssignment = binding;
|
||||
//NewExpressionInfo additem = new NewExpressionInfo();
|
||||
additem.RightName = member.Member.Name;
|
||||
additem.ShortName = member.Expression + "";
|
||||
additem.RightName = member.Member.Name;
|
||||
additem.RightDbName = context.GetDbColumnName(member.Type.Name, additem.RightName);
|
||||
//additem.Value = "";
|
||||
result.Add(additem);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class NewExpressionInfo
|
||||
{
|
||||
public string LeftNameName { get; set; }
|
||||
public string RightName { get; set; }
|
||||
public string RightDbName { get; set; }
|
||||
public string ShortName { get; set; }
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ namespace SqlSugar
|
||||
{
|
||||
public class BaseResolve
|
||||
{
|
||||
#region Property
|
||||
protected Expression Expression { get; set; }
|
||||
protected Expression ExactExpression { get; set; }
|
||||
public ExpressionContext Context { get; set; }
|
||||
@ -111,6 +112,9 @@ namespace SqlSugar
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Append
|
||||
protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue)
|
||||
{
|
||||
|
||||
@ -150,7 +154,7 @@ namespace SqlSugar
|
||||
this.Context.Result.Append(sql);
|
||||
}
|
||||
}
|
||||
else if(parameter.CurrentExpression is MethodCallExpression || parameter.CurrentExpression is ConditionalExpression || parameter.CurrentExpression.NodeType == ExpressionType.Coalesce)
|
||||
else if (parameter.CurrentExpression is MethodCallExpression || parameter.CurrentExpression is ConditionalExpression || parameter.CurrentExpression.NodeType == ExpressionType.Coalesce)
|
||||
{
|
||||
var appendValue = value;
|
||||
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
|
||||
@ -171,7 +175,7 @@ namespace SqlSugar
|
||||
if (value.ObjToString() != "NULL" && !parameter.ValueIsNull)
|
||||
{
|
||||
EntityColumnInfo columnInfo = GetColumnInfo(oppoSiteExpression);
|
||||
if (columnInfo != null && columnInfo.SqlParameterDbType != null&& columnInfo.SqlParameterDbType is System.Data.DbType)
|
||||
if (columnInfo != null && columnInfo.SqlParameterDbType != null && columnInfo.SqlParameterDbType is System.Data.DbType)
|
||||
{
|
||||
this.Context.Parameters.Add(new SugarParameter(appendValue, value, (System.Data.DbType)columnInfo.SqlParameterDbType));
|
||||
}
|
||||
@ -259,19 +263,6 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private EntityColumnInfo GetColumnInfo(Expression oppoSiteExpression)
|
||||
{
|
||||
var oppsite = (oppoSiteExpression as MemberExpression);
|
||||
if (oppsite == null) return null;
|
||||
if (this.Context.SugarContext == null) return null;
|
||||
if (this.Context.SugarContext.Context == null) return null;
|
||||
if (oppsite.Expression == null) return null;
|
||||
var columnInfo = this.Context.SugarContext.Context.EntityMaintenance
|
||||
.GetEntityInfo(oppsite.Expression.Type).Columns.FirstOrDefault(it => it.PropertyName == oppsite.Member.Name);
|
||||
return columnInfo;
|
||||
}
|
||||
|
||||
protected void AppendOpreator(ExpressionParameter parameter, bool? isLeft)
|
||||
{
|
||||
if (isLeft == true)
|
||||
@ -292,7 +283,7 @@ namespace SqlSugar
|
||||
var lastCharIsSpace = this.Context.Result.LastCharIsSpace;
|
||||
if (isAppend)
|
||||
{
|
||||
this.Context.Result.Append(lastCharIsSpace?"NOT":" NOT");
|
||||
this.Context.Result.Append(lastCharIsSpace ? "NOT" : " NOT");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -312,33 +303,9 @@ namespace SqlSugar
|
||||
this.Context.Result.Replace(ExpressionConst.FormatSymbol, "-");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContext();
|
||||
newContext.MappingColumns = this.Context.MappingColumns;
|
||||
newContext.MappingTables = this.Context.MappingTables;
|
||||
newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
|
||||
newContext.IsSingle = this.Context.IsSingle;
|
||||
newContext.SqlFuncServices = this.Context.SqlFuncServices;
|
||||
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
if (newContext.SingleTableNameSubqueryShortName.HasValue())
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
var methodCallExpressionArgs = new MethodCallExpressionArgs()
|
||||
{
|
||||
IsMember = true,
|
||||
MemberName = newContext.Result.GetResultString()
|
||||
};
|
||||
return methodCallExpressionArgs;
|
||||
}
|
||||
#region New Expression
|
||||
|
||||
public string GetNewExpressionValue(Expression item)
|
||||
{
|
||||
@ -351,13 +318,13 @@ namespace SqlSugar
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
if (this.Context.SingleTableNameSubqueryShortName == "Subqueryable()")
|
||||
if (this.Context.SingleTableNameSubqueryShortName == "Subqueryable()")
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
return newContext.Result.GetResultString();
|
||||
}
|
||||
public string GetNewExpressionValue(Expression item,ResolveExpressType type)
|
||||
public string GetNewExpressionValue(Expression item, ResolveExpressType type)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContextWithMapping();
|
||||
newContext.SugarContext = this.Context.SugarContext;
|
||||
@ -370,7 +337,6 @@ namespace SqlSugar
|
||||
}
|
||||
return newContext.Result.GetResultString();
|
||||
}
|
||||
|
||||
protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
|
||||
{
|
||||
if (item is ConstantExpression)
|
||||
@ -490,58 +456,43 @@ namespace SqlSugar
|
||||
else if (item.Type.IsClass())
|
||||
{
|
||||
var mappingKeys = GetMappingColumns(parameter.CurrentExpression);
|
||||
var isSameType = mappingKeys.Keys.Count>0;
|
||||
CallContextThread<Dictionary<string,string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
var isSameType = mappingKeys.Keys.Count > 0;
|
||||
CallContextThread<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
CallContextAsync<Dictionary<string, string>>.SetData("Exp_Select_Mapping_Key", mappingKeys);
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
var shortName = parameter.CommonTempData;
|
||||
var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
|
||||
foreach (var property in listProperties)
|
||||
if (this.Context.IsJoin&& (item is MemberInitExpression|| item is NewExpression))
|
||||
{
|
||||
var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (hasIgnore)
|
||||
List<NewExpressionInfo> newExpressionInfos = new List<NewExpressionInfo>();
|
||||
if (item is MemberInitExpression)
|
||||
{
|
||||
continue;
|
||||
newExpressionInfos = ExpressionTool.GetNewexpressionInfos(item,this.Context);
|
||||
}
|
||||
if (property.PropertyType.IsClass())
|
||||
else
|
||||
{
|
||||
var comumnInfo=property.GetCustomAttribute<SugarColumn>();
|
||||
if (comumnInfo != null && comumnInfo.IsJson && isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else if(comumnInfo != null && comumnInfo.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
else if (comumnInfo!=null&&this.Context.SugarContext != null&&this.Context.SugarContext.Context != null)
|
||||
{
|
||||
var entityInfo=this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(item.Type);
|
||||
var entityColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == property.Name);
|
||||
if (entityColumn != null && entityColumn.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
newExpressionInfos = ExpressionTool.GetNewDynamicexpressionInfos(item, this.Context);
|
||||
}
|
||||
else if (isSameType)
|
||||
foreach (NewExpressionInfo newExpressionInfo in newExpressionInfos)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
//var property=item.Type.GetProperties().Where(it => it.Name == newExpressionInfo.l).First();
|
||||
//asName = GetAsName(item, newExpressionInfo.ShortName, property);
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(
|
||||
this.Context.SqlTranslationLeft+asName + "." + newExpressionInfo.RightDbName+this.Context.SqlTranslationRight,
|
||||
newExpressionInfo.ShortName+"."+newExpressionInfo.RightDbName
|
||||
));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsNameResolveAnObject(parameter, item, asName, isSameType);
|
||||
}
|
||||
}
|
||||
else if (item.Type == UtilConstants.BoolType && item is MethodCallExpression && IsNotCaseExpression(item))
|
||||
{
|
||||
this.Expression = item;
|
||||
this.Start();
|
||||
var sql= this.Context.DbMehtods.IIF(new MethodCallExpressionModel()
|
||||
var sql = this.Context.DbMehtods.IIF(new MethodCallExpressionModel()
|
||||
{
|
||||
Args=new List<MethodCallExpressionArgs>() {
|
||||
Args = new List<MethodCallExpressionArgs>() {
|
||||
new MethodCallExpressionArgs() {
|
||||
IsMember=true,
|
||||
MemberName=parameter.CommonTempData.ObjToString()
|
||||
@ -558,14 +509,14 @@ namespace SqlSugar
|
||||
});
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, sql));
|
||||
}
|
||||
else if (item.NodeType == ExpressionType.Not
|
||||
else if (item.NodeType == ExpressionType.Not
|
||||
&& (item as UnaryExpression).Operand is MethodCallExpression
|
||||
&& ((item as UnaryExpression).Operand as MethodCallExpression).Method.Name.IsIn("IsNullOrEmpty", "IsNullOrWhiteSpace"))
|
||||
{
|
||||
var asValue = packIfElse(GetNewExpressionValue(item)).ObjToString();
|
||||
var asValue = GetAsNamePackIfElse(GetNewExpressionValue(item)).ObjToString();
|
||||
parameter.Context.Result.Append(this.Context.GetAsString(asName, asValue));
|
||||
}
|
||||
else if (item is MethodCallExpression && (item as MethodCallExpression).Method.Name.IsIn("Count", "Any")&&!item.ToString().StartsWith("Subqueryable"))
|
||||
else if (item is MethodCallExpression && (item as MethodCallExpression).Method.Name.IsIn("Count", "Any") && !item.ToString().StartsWith("Subqueryable"))
|
||||
{
|
||||
if (this.Context.IsSingle && this.Context.SingleTableNameSubqueryShortName == null)
|
||||
{
|
||||
@ -584,120 +535,11 @@ namespace SqlSugar
|
||||
Check.ThrowNotSupportedException(item.GetType().Name);
|
||||
}
|
||||
}
|
||||
public object packIfElse(object methodValue)
|
||||
{
|
||||
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
|
||||
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
|
||||
new KeyValuePair<string, string>("Return","1"),
|
||||
new KeyValuePair<string, string>("End","0")
|
||||
});
|
||||
return methodValue;
|
||||
}
|
||||
private static bool IsNotCaseExpression(Expression item)
|
||||
{
|
||||
if ((item as MethodCallExpression).Method.Name == "IIF")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "IsNull")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "End"&&item.ToString().Contains("IF("))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name== "AggregateMax")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateMin")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateSum")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "ToBool")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "ToBoolean")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "Select"&& item.ToString().Contains("Subqueryable()"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, string> GetMappingColumns(Expression currentExpression)
|
||||
{
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
if (currentExpression == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
List<Type> types = new List<Type>();
|
||||
int i = 0;
|
||||
if (currentExpression is NewExpression)
|
||||
{
|
||||
i = (currentExpression as NewExpression).Arguments.Count;
|
||||
foreach (var item in (currentExpression as NewExpression).Arguments)
|
||||
{
|
||||
if (item.Type.IsClass())
|
||||
{
|
||||
types.Add(item.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (currentExpression is MemberInitExpression)
|
||||
{
|
||||
i = (currentExpression as MemberInitExpression).Bindings.Count;
|
||||
foreach (var item in (currentExpression as MemberInitExpression).Bindings)
|
||||
{
|
||||
MemberAssignment memberAssignment = (MemberAssignment)item;
|
||||
if (memberAssignment.Expression.Type.IsClass())
|
||||
{
|
||||
types.Add(memberAssignment.Expression.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (types.Count == i&&(types.Count==types.Distinct().Count()))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
var array = currentExpression.ToString().Split(',');
|
||||
foreach (var item in array)
|
||||
{
|
||||
var itemArray = item.Split('=').ToArray();
|
||||
var last = itemArray.Last().Trim().Split('.').First().TrimEnd(')').TrimEnd('}');
|
||||
var first = itemArray.First().Trim();
|
||||
if (first.Contains("{"))
|
||||
{
|
||||
first = first.Split('{').Last().Trim();
|
||||
}
|
||||
if (first.Contains("("))
|
||||
{
|
||||
first = first.Split('(').Last().Trim();
|
||||
}
|
||||
if (!result.ContainsKey(first))
|
||||
{
|
||||
result.Add(first, last);
|
||||
}
|
||||
else
|
||||
{
|
||||
//future
|
||||
}
|
||||
}
|
||||
return result; ;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
|
||||
protected void SetNavigateResult()
|
||||
{
|
||||
if (this.Context != null)
|
||||
@ -740,7 +582,7 @@ namespace SqlSugar
|
||||
{
|
||||
dbColumnName = mappingInfo.DbColumnName;
|
||||
}
|
||||
if (shortName != null && shortName.ObjToString().Contains(this.Context.SqlTranslationLeft)&&this.Context.IsSingle)
|
||||
if (shortName != null && shortName.ObjToString().Contains(this.Context.SqlTranslationLeft) && this.Context.IsSingle)
|
||||
{
|
||||
asName = this.Context.GetTranslationText(item.Type.Name + "." + propertyName);
|
||||
}
|
||||
@ -759,6 +601,144 @@ namespace SqlSugar
|
||||
|
||||
return asName;
|
||||
}
|
||||
private EntityColumnInfo GetColumnInfo(Expression oppoSiteExpression)
|
||||
{
|
||||
var oppsite = (oppoSiteExpression as MemberExpression);
|
||||
if (oppsite == null) return null;
|
||||
if (this.Context.SugarContext == null) return null;
|
||||
if (this.Context.SugarContext.Context == null) return null;
|
||||
if (oppsite.Expression == null) return null;
|
||||
var columnInfo = this.Context.SugarContext.Context.EntityMaintenance
|
||||
.GetEntityInfo(oppsite.Expression.Type).Columns.FirstOrDefault(it => it.PropertyName == oppsite.Member.Name);
|
||||
return columnInfo;
|
||||
}
|
||||
protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item)
|
||||
{
|
||||
var newContext = this.Context.GetCopyContext();
|
||||
newContext.MappingColumns = this.Context.MappingColumns;
|
||||
newContext.MappingTables = this.Context.MappingTables;
|
||||
newContext.IgnoreComumnList = this.Context.IgnoreComumnList;
|
||||
newContext.IsSingle = this.Context.IsSingle;
|
||||
newContext.SqlFuncServices = this.Context.SqlFuncServices;
|
||||
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
|
||||
this.Context.Index = newContext.Index;
|
||||
this.Context.ParameterIndex = newContext.ParameterIndex;
|
||||
if (newContext.Parameters.HasValue())
|
||||
{
|
||||
this.Context.Parameters.AddRange(newContext.Parameters);
|
||||
}
|
||||
if (newContext.SingleTableNameSubqueryShortName.HasValue())
|
||||
{
|
||||
this.Context.SingleTableNameSubqueryShortName = newContext.SingleTableNameSubqueryShortName;
|
||||
}
|
||||
var methodCallExpressionArgs = new MethodCallExpressionArgs()
|
||||
{
|
||||
IsMember = true,
|
||||
MemberName = newContext.Result.GetResultString()
|
||||
};
|
||||
return methodCallExpressionArgs;
|
||||
}
|
||||
private string GetAsNameResolveAnObject(ExpressionParameter parameter, Expression item, string asName, bool isSameType)
|
||||
{
|
||||
this.Start();
|
||||
var shortName = parameter.CommonTempData;
|
||||
var listProperties = item.Type.GetProperties().Cast<PropertyInfo>().ToList();
|
||||
foreach (var property in listProperties)
|
||||
{
|
||||
var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase));
|
||||
if (hasIgnore)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (property.PropertyType.IsClass())
|
||||
{
|
||||
var comumnInfo = property.GetCustomAttribute<SugarColumn>();
|
||||
if (comumnInfo != null && comumnInfo.IsJson && isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else if (comumnInfo != null && comumnInfo.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
else if (comumnInfo != null && this.Context.SugarContext != null && this.Context.SugarContext.Context != null)
|
||||
{
|
||||
var entityInfo = this.Context.SugarContext.Context.EntityMaintenance.GetEntityInfo(item.Type);
|
||||
var entityColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == property.Name);
|
||||
if (entityColumn != null && entityColumn.IsJson)
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isSameType)
|
||||
{
|
||||
asName = GetAsNameAndShortName(item, shortName, property);
|
||||
}
|
||||
else
|
||||
{
|
||||
asName = GetAsName(item, shortName, property);
|
||||
}
|
||||
}
|
||||
|
||||
return asName;
|
||||
}
|
||||
public object GetAsNamePackIfElse(object methodValue)
|
||||
{
|
||||
methodValue = this.Context.DbMehtods.CaseWhen(new List<KeyValuePair<string, string>>() {
|
||||
new KeyValuePair<string, string>("IF",methodValue.ObjToString()),
|
||||
new KeyValuePair<string, string>("Return","1"),
|
||||
new KeyValuePair<string, string>("End","0")
|
||||
});
|
||||
return methodValue;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Validate
|
||||
|
||||
private static bool IsNotCaseExpression(Expression item)
|
||||
{
|
||||
if ((item as MethodCallExpression).Method.Name == "IIF")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "IsNull")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "End" && item.ToString().Contains("IF("))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateMax")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateMin")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "AggregateSum")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "ToBool")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "ToBoolean")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((item as MethodCallExpression).Method.Name == "Select" && item.ToString().Contains("Subqueryable()"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
private static bool IsBoolValue(Expression item)
|
||||
{
|
||||
return item.Type == UtilConstants.BoolType &&
|
||||
@ -767,12 +747,10 @@ namespace SqlSugar
|
||||
(item as MemberExpression).Expression.Type == typeof(bool?) &&
|
||||
(item as MemberExpression).Member.Name == "Value";
|
||||
}
|
||||
|
||||
protected static bool IsConvert(Expression item)
|
||||
{
|
||||
return item is UnaryExpression && item.NodeType == ExpressionType.Convert;
|
||||
}
|
||||
|
||||
protected static bool IsNotMember(Expression item)
|
||||
{
|
||||
return item is UnaryExpression &&
|
||||
@ -791,11 +769,74 @@ namespace SqlSugar
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression != null &&
|
||||
((item as UnaryExpression).Operand as MemberExpression).Expression.NodeType == ExpressionType.MemberAccess;
|
||||
}
|
||||
|
||||
protected bool IsSubMethod(MethodCallExpression express)
|
||||
{
|
||||
return SubTools.SubItemsConst.Any(it => express.Object != null && express.Object.Type.Name.StartsWith("Subqueryable`"));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Dictionary
|
||||
private Dictionary<string, string> GetMappingColumns(Expression currentExpression)
|
||||
{
|
||||
Dictionary<string, string> result = new Dictionary<string, string>();
|
||||
if (currentExpression == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
List<Type> types = new List<Type>();
|
||||
int i = 0;
|
||||
if (currentExpression is NewExpression)
|
||||
{
|
||||
i = (currentExpression as NewExpression).Arguments.Count;
|
||||
foreach (var item in (currentExpression as NewExpression).Arguments)
|
||||
{
|
||||
if (item.Type.IsClass())
|
||||
{
|
||||
types.Add(item.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (currentExpression is MemberInitExpression)
|
||||
{
|
||||
i = (currentExpression as MemberInitExpression).Bindings.Count;
|
||||
foreach (var item in (currentExpression as MemberInitExpression).Bindings)
|
||||
{
|
||||
MemberAssignment memberAssignment = (MemberAssignment)item;
|
||||
if (memberAssignment.Expression.Type.IsClass())
|
||||
{
|
||||
types.Add(memberAssignment.Expression.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (types.Count == i && (types.Count == types.Distinct().Count()))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
var array = currentExpression.ToString().Split(',');
|
||||
foreach (var item in array)
|
||||
{
|
||||
var itemArray = item.Split('=').ToArray();
|
||||
var last = itemArray.Last().Trim().Split('.').First().TrimEnd(')').TrimEnd('}');
|
||||
var first = itemArray.First().Trim();
|
||||
if (first.Contains("{"))
|
||||
{
|
||||
first = first.Split('{').Last().Trim();
|
||||
}
|
||||
if (first.Contains("("))
|
||||
{
|
||||
first = first.Split('(').Last().Trim();
|
||||
}
|
||||
if (!result.ContainsKey(first))
|
||||
{
|
||||
result.Add(first, last);
|
||||
}
|
||||
else
|
||||
{
|
||||
//future
|
||||
}
|
||||
}
|
||||
return result; ;
|
||||
}
|
||||
protected static Dictionary<string, string> MethodMapping = new Dictionary<string, string>() {
|
||||
{ "ToString","ToString"},
|
||||
{ "ToInt32","ToInt32"},
|
||||
@ -819,8 +860,7 @@ namespace SqlSugar
|
||||
{ "Substring","Substring"},
|
||||
{ "DateAdd","DateAdd"}
|
||||
};
|
||||
|
||||
protected static Dictionary<string, DateType> MethodTimeMapping = new Dictionary<string, DateType>() {
|
||||
protected static Dictionary<string, DateType> MethodTimeMapping = new Dictionary<string, DateType>() {
|
||||
{ "AddYears",DateType.Year},
|
||||
{ "AddMonths",DateType.Month},
|
||||
{ "AddDays",DateType.Day},
|
||||
@ -828,6 +868,7 @@ namespace SqlSugar
|
||||
{ "AddMinutes",DateType.Minute},
|
||||
{ "AddSeconds",DateType.Second},
|
||||
{ "AddMilliseconds",DateType.Millisecond}
|
||||
};
|
||||
};
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -436,6 +436,11 @@ namespace SqlSugar
|
||||
{
|
||||
var key = typeName + "." + name;
|
||||
var info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
|
||||
if (info == null && typeName !=null && typeName.Contains("<>f__AnonymousType"))
|
||||
{
|
||||
key = item.Name + "." + name;
|
||||
info = readerValues.Select(it => it.Key).FirstOrDefault(it => it.ToLower() == key.ToLower());
|
||||
}
|
||||
var oldInfo = info;
|
||||
if (mappingKeys!=null&&mappingKeys.ContainsKey(item.Name))
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user