@ -9,12 +9,19 @@ namespace SqlSugar
{
internal class OneToManyNavgateExpressionN
{
private SqlSugarProvider context ;
#region Constructor
public SqlSugarProvider context ;
public string shorName ;
public EntityInfo entityInfo ;
public List < ExpressionItems > items ;
public string whereSql ;
public MethodCallExpressionResolve methodCallExpressionResolve ;
public OneToManyNavgateExpressionN ( SqlSugarProvider context , MethodCallExpressionResolve methodCallExpressionResolve )
{
this . context = context ;
this . methodCallExpressionResolve = methodCallExpressionResolve ;
}
#endregion
internal bool IsNavgate ( Expression expression )
{
@ -29,32 +36,16 @@ namespace SqlSugar
var memberExp = exp as MethodCallExpression ;
if ( memberExp . Method . Name . IsIn ( "Any" , "Count" ) & & memberExp . Arguments . Count > 0 & & memberExp . Arguments [ 0 ] is MemberExpression )
{
result = ValidateNav ( result , memberExp . Arguments [ 0 ] as MemberExpression , memberExp . Arguments [ 0 ] ) ;
result = ValiteOneManyCall ( result , memberExp , memberExp . Arguments [ 0 ] as MemberExpression , memberExp . Arguments [ 0 ] ) ;
if ( memberExp . Arguments . Count > 1 )
{
whereSql = GetWhereSql ( memberExp ) ;
}
}
}
return result ;
}
private string GetWhereSql ( MethodCallExpression memberExp )
{
return null ;
}
private bool ValidateNav ( bool result , MemberExpression memberExp , Expression childExpression )
{
if ( childExpression ! = null & & childExpression is MemberExpression )
{
result = ValidateIsJoinMember ( result , memberExp , childExpression ) ;
}
return result ;
}
List < ExpressionItems > items ;
private bool ValidateIsJoinMember ( bool result , MemberExpression memberExp , Expression childExpression )
private bool ValiteOneManyCall ( bool result , MethodCallExpression callExpression , MemberExpression memberExp , Expression childExpression )
{
if ( childExpression ! = null & & childExpression is MemberExpression )
{
@ -65,8 +56,14 @@ namespace SqlSugar
return false ;
}
items = new List < ExpressionItems > ( ) ;
items . Add ( new ExpressionItems ( ) { Type = 1 , Expression = memberExp , ParentEntityInfo = this . context . EntityMaintenance . GetEntityInfo ( oldChildExpression . Type ) } ) ;
items . Add ( new ExpressionItems ( ) { Type = 2 , Expression = oldChildExpression , ThisEntityInfo = this . context . EntityMaintenance . GetEntityInfo ( oldChildExpression . Type ) , ParentEntityInfo = this . context . EntityMaintenance . GetEntityInfo ( child2Expression . Type ) } ) ;
var childType = oldChildExpression . Type ;
if ( ! childType . FullName . IsCollectionsList ( ) )
{
return false ;
}
childType = childType . GetGenericArguments ( ) [ 0 ] ;
items . Add ( new ExpressionItems ( ) { Type = 3 , Expression = callExpression , ParentEntityInfo = this . context . EntityMaintenance . GetEntityInfo ( childType ) } ) ;
items . Add ( new ExpressionItems ( ) { Type = 2 , Expression = oldChildExpression , ThisEntityInfo = this . context . EntityMaintenance . GetEntityInfo ( childType ) , ParentEntityInfo = this . context . EntityMaintenance . GetEntityInfo ( child2Expression . Type ) } ) ;
if ( items . Any ( it = > it . Type = = 2 & & it . Nav = = null ) )
{
return false ;
@ -97,10 +94,79 @@ namespace SqlSugar
}
return result ;
}
public object GetSql ( )
{
MapperSql MapperSql = new MapperSql ( ) ;
var memberInfo = this . items . Where ( it = > it . Type = = 3 ) . First ( ) ;
var subInfos = this . items . Where ( it = > it . Type = = 2 ) . Reverse ( ) . ToList ( ) ;
var formInfo = subInfos . First ( ) ;
var joinInfos = subInfos . Skip ( 1 ) . ToList ( ) ;
var i = 0 ;
var masterShortName = formInfo . ThisEntityInfo . DbTableName + i ;
var queryable = this . context . Queryable < object > ( masterShortName ) . AS ( formInfo . ThisEntityInfo . DbTableName ) ;
i + + ;
var lastShortName = "" ;
var index = 0 ;
foreach ( var item in joinInfos )
{
var shortName = item . ThisEntityInfo . DbTableName + i ;
EntityColumnInfo pkColumn ;
EntityColumnInfo navColum ;
if ( index = = 0 )
{
pkColumn = item . ThisEntityInfo . Columns . FirstOrDefault ( it = > it . PropertyName = = item . Nav . Name ) ;
navColum = item . ParentEntityInfo . Columns . FirstOrDefault ( it = > it . IsPrimarykey ) ;
}
else
{
pkColumn = item . ThisEntityInfo . Columns . FirstOrDefault ( it = > it . IsPrimarykey ) ;
navColum = item . ParentEntityInfo . Columns . FirstOrDefault ( it = > it . PropertyName = = item . Nav . Name ) ;
}
Check . ExceptionEasy ( pkColumn = = null , $"{item.ThisEntityInfo.EntityName} need PrimayKey" , $"使用导航属性{item.ThisEntityInfo.EntityName} 缺少主键" ) ;
var on = $" {shortName}.{pkColumn.DbColumnName}={formInfo.ThisEntityInfo.DbTableName + (i - 1)}.{navColum.DbColumnName}" ;
queryable . AddJoinInfo ( item . ThisEntityInfo . DbTableName , shortName , on , JoinType . Inner ) ;
+ + i ;
index + + ;
lastShortName = shortName ;
formInfo = item ;
}
queryable . Select ( $" COUNT(1)" ) ;
var last = subInfos . First ( ) ;
var FirstPkColumn = last . ThisEntityInfo . Columns . FirstOrDefault ( it = > it . IsPrimarykey ) ;
Check . ExceptionEasy ( FirstPkColumn = = null , $"{ last.ThisEntityInfo.EntityName} need PrimayKey" , $"使用导航属性{ last.ThisEntityInfo.EntityName} 缺少主键" ) ;
var PkColumn = last . ParentEntityInfo . Columns . FirstOrDefault ( it = > it . PropertyName = = last . Nav . Name ) ;
Check . ExceptionEasy ( PkColumn = = null , $"{ last.ParentEntityInfo.EntityName} no found {last.Nav.Name}" , $"{ last.ParentEntityInfo.EntityName} 不存在 {last.Nav.Name}" ) ;
queryable . Where ( $" {this.shorName}.{PkColumn.DbColumnName} = {masterShortName}.{FirstPkColumn.DbColumnName} " ) ;
MapperSql . Sql = $"( {queryable.ToSql().Key} ) " ;
if ( ( memberInfo . Expression as MethodCallExpression ) . Method . Name = = "Any" )
{
MapperSql . Sql = $"( {MapperSql.Sql}>0 ) " ;
}
return MapperSql ;
}
#region Helper
private string GetWhereSql ( MethodCallExpression memberExp )
{
var whereExp = memberExp . Arguments [ 1 ] ;
var result = this . methodCallExpressionResolve . GetNewExpressionValue ( whereExp ) ;
return result ;
}
private static bool IsParameter ( Expression child2Expression )
{
return child2Expression . Type . IsClass ( ) & & child2Expression is ParameterExpression ;
}
private static Expression GetMemberExpression ( Expression child2Expression )
{
return ( child2Expression as MemberExpression ) . Expression ;
}
internal object GetSql ( )
private static bool IsClass ( Expression child2Expression )
{
throw new NotImplementedException ( ) ;
return child2Expression . Type . IsClass ( ) & & child2Expression is MemberExpression ;
}
#endregion
}
}