Synchronization code

This commit is contained in:
sunkaixuan 2023-10-05 19:09:38 +08:00
parent 1f2a75dd87
commit c90f35bf71
8 changed files with 75 additions and 5 deletions

View File

@ -11,7 +11,7 @@ namespace SqlSugar
internal object MethodInfos { get; set; } internal object MethodInfos { get; set; }
internal SqlSugarProvider Context { get; set; } internal SqlSugarProvider Context { get; set; }
public DeleteNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions = null) public DeleteNavMethodInfo IncludeByNameString(string navMemberName, DeleteNavOptions updateNavOptions = null)
{ {
var type = MethodInfos.GetType().GetGenericArguments()[0]; var type = MethodInfos.GetType().GetGenericArguments()[0];
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type); var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type);
@ -24,7 +24,7 @@ namespace SqlSugar
this.MethodInfos = obj; this.MethodInfos = obj;
return this; return this;
} }
public DeleteNavMethodInfo ThenIncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions = null) public DeleteNavMethodInfo ThenIncludeByNameString(string navMemberName, DeleteNavOptions updateNavOptions = null)
{ {
var type = MethodInfos.GetType().GetGenericArguments()[1]; var type = MethodInfos.GetType().GetGenericArguments()[1];
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type); var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type);

View File

@ -11,7 +11,7 @@ namespace SqlSugar
internal object MethodInfos { get; set; } internal object MethodInfos { get; set; }
internal SqlSugarProvider Context { get; set; } internal SqlSugarProvider Context { get; set; }
public InsertNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions = null) public InsertNavMethodInfo IncludeByNameString(string navMemberName, InsertNavRootOptions updateNavOptions = null)
{ {
var type = MethodInfos.GetType().GetGenericArguments()[0]; var type = MethodInfos.GetType().GetGenericArguments()[0];
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type); var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type);
@ -24,7 +24,7 @@ namespace SqlSugar
this.MethodInfos = obj; this.MethodInfos = obj;
return this; return this;
} }
public InsertNavMethodInfo ThenIncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions = null) public InsertNavMethodInfo ThenIncludeByNameString(string navMemberName, InsertNavRootOptions updateNavOptions = null)
{ {
var type = MethodInfos.GetType().GetGenericArguments()[1]; var type = MethodInfos.GetType().GetGenericArguments()[1];
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type); var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type);

View File

@ -1265,6 +1265,21 @@ namespace SqlSugar
#region Other #region Other
private bool IsSingleWithChildTableQuery()
{
return this.QueryBuilder.IsSingle() && this.QueryBuilder.TableShortName.HasValue();
}
private Expression<Func<T, bool>> ReplaceMasterTableParameters(Expression<Func<T, bool>> expression)
{
var parameterName = (expression as LambdaExpression)?.Parameters?.FirstOrDefault()?.Name;
if (parameterName != null && parameterName != this.QueryBuilder.TableShortName)
{
expression = ExpressionTool.ChangeLambdaExpression(expression, parameterName, this.QueryBuilder.TableShortName);
}
return expression;
}
private void orderPropertyNameByJoin(string orderPropertyName, OrderByType? orderByType) private void orderPropertyNameByJoin(string orderPropertyName, OrderByType? orderByType)
{ {
var shortName = orderPropertyName.Split('.').FirstOrDefault(); var shortName = orderPropertyName.Split('.').FirstOrDefault();

View File

@ -934,9 +934,14 @@ namespace SqlSugar
} }
public virtual ISugarQueryable<T> Where(Expression<Func<T, bool>> expression) public virtual ISugarQueryable<T> Where(Expression<Func<T, bool>> expression)
{ {
if (IsSingleWithChildTableQuery())
{
expression = ReplaceMasterTableParameters(expression);
}
this._Where(expression); this._Where(expression);
return this; return this;
} }
public virtual ISugarQueryable<T> Where(string whereString, object whereObj = null) public virtual ISugarQueryable<T> Where(string whereString, object whereObj = null)
{ {
if (whereString.HasValue()) if (whereString.HasValue())
@ -1008,6 +1013,10 @@ namespace SqlSugar
public virtual ISugarQueryable<T> WhereIF(bool isWhere, Expression<Func<T, bool>> expression) public virtual ISugarQueryable<T> WhereIF(bool isWhere, Expression<Func<T, bool>> expression)
{ {
if (!isWhere) return this; if (!isWhere) return this;
if (IsSingleWithChildTableQuery())
{
expression = ReplaceMasterTableParameters(expression);
}
_Where(expression); _Where(expression);
return this; return this;
} }

View File

@ -10,6 +10,27 @@ namespace SqlSugar
{ {
public class ExpressionTool public class ExpressionTool
{ {
public static Expression<Func<T, bool>> ChangeLambdaExpression<T>(Expression<Func<T,bool>> exp,string replaceParameterName, string newParameterName)
{
var parameter = Expression.Parameter(typeof(T), newParameterName);
// 替换Lambda表达式中指定参数名
var visitor = new ParameterReplacer(replaceParameterName, parameter);
var newBody = visitor.Visit(exp);
return (Expression<Func<T, bool>>)newBody;
}
public static Expression ChangeLambdaExpression(Expression exp, Type targetType, string replaceParameterName, string newParameterName)
{
var parameter = Expression.Parameter(targetType, newParameterName);
// 替换Lambda表达式中指定参数名
var visitor = new ParameterReplacer(replaceParameterName, parameter);
var newBody = visitor.Visit(exp);
return newBody;
}
public static List<string> GetNewArrayMembers(NewArrayExpression newArrayExpression) public static List<string> GetNewArrayMembers(NewArrayExpression newArrayExpression)
{ {
List<string> strings = new List<string>(); List<string> strings = new List<string>();

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
internal class ParameterReplacer : ExpressionVisitor
{
private readonly string _oldParameterName;
private readonly ParameterExpression _newParameter;
public ParameterReplacer(string oldParameterName, ParameterExpression newParameter)
{
_oldParameterName = oldParameterName;
_newParameter = newParameter;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return node.Name == _oldParameterName ? _newParameter : base.VisitParameter(node);
}
}
}

View File

@ -174,6 +174,7 @@
<Compile Include="ExpressionsToSql\Common\ListAnyParameter.cs" /> <Compile Include="ExpressionsToSql\Common\ListAnyParameter.cs" />
<Compile Include="ExpressionsToSql\Common\MapperSql.cs" /> <Compile Include="ExpressionsToSql\Common\MapperSql.cs" />
<Compile Include="ExpressionsToSql\Common\NewExpressionInfo.cs" /> <Compile Include="ExpressionsToSql\Common\NewExpressionInfo.cs" />
<Compile Include="ExpressionsToSql\Common\ParameterReplacer.cs" />
<Compile Include="ExpressionsToSql\ResolveItems\BaseResolve_Append.cs" /> <Compile Include="ExpressionsToSql\ResolveItems\BaseResolve_Append.cs" />
<Compile Include="ExpressionsToSql\ResolveItems\BaseResolve_Helper.cs" /> <Compile Include="ExpressionsToSql\ResolveItems\BaseResolve_Helper.cs" />
<Compile Include="ExpressionsToSql\ResolveItems\BaseResolve_Validate.cs" /> <Compile Include="ExpressionsToSql\ResolveItems\BaseResolve_Validate.cs" />

View File

@ -8,7 +8,7 @@ namespace SqlSugar.Extensions
///Common Extensions for external users ///Common Extensions for external users
/// </summary> /// </summary>
public static class UtilExtensions public static class UtilExtensions
{ {
public static int ObjToInt(this object thisValue) public static int ObjToInt(this object thisValue)
{ {
int reval = 0; int reval = 0;