From 18e1be83bede4a760da7f3cb2b06b87d5e2c7bd0 Mon Sep 17 00:00:00 2001
From: sunkaixuan <610262374@qq.com>
Date: Mon, 2 May 2022 14:04:58 +0800
Subject: [PATCH] Update exp to sql
---
.../Common/ExpressionItems.cs | 34 ++++++
.../OneToManyNavgateExpressionN.cs | 106 ++++++++++++++++++
.../OneToOneNavgateExpressionN.cs | 36 +-----
Src/Asp.Net/SqlSugar/SqlSugar.csproj | 2 +
4 files changed, 143 insertions(+), 35 deletions(-)
create mode 100644 Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionItems.cs
create mode 100644 Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/OneToManyNavgateExpressionN.cs
diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionItems.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionItems.cs
new file mode 100644
index 000000000..0ded2f454
--- /dev/null
+++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/ExpressionItems.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SqlSugar
+{
+ internal class ExpressionItems
+ {
+ ///
+ /// 0 memeber, 2 method ,3 class
+ ///
+ public int Type { get; set; }
+ public EntityInfo ParentEntityInfo { get; set; }
+ public EntityInfo ThisEntityInfo { get; set; }
+ public Expression Expression { get; set; }
+ public Navigate Nav
+ {
+ get
+ {
+ if (Expression is MemberExpression)
+ {
+ var name = (Expression as MemberExpression).Member.Name;
+ var navColumn = ParentEntityInfo.Columns.FirstOrDefault(it => it.PropertyName == name);
+ return navColumn == null ? null : navColumn.Navigat;
+ }
+ return null;
+ }
+ }
+
+ }
+}
diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/OneToManyNavgateExpressionN.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/OneToManyNavgateExpressionN.cs
new file mode 100644
index 000000000..93cea3de9
--- /dev/null
+++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/OneToManyNavgateExpressionN.cs
@@ -0,0 +1,106 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SqlSugar
+{
+ internal class OneToManyNavgateExpressionN
+ {
+ private SqlSugarProvider context;
+ public OneToManyNavgateExpressionN(SqlSugarProvider context, MethodCallExpressionResolve methodCallExpressionResolve)
+ {
+ this.context = context;
+
+ }
+
+ internal bool IsNavgate(Expression expression)
+ {
+ var result = false;
+ var exp = expression;
+ if (exp is UnaryExpression)
+ {
+ exp = (exp as UnaryExpression).Operand;
+ }
+ if (exp is MethodCallExpression)
+ {
+ 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]);
+ if (memberExp.Arguments.Count > 1)
+ {
+ }
+ }
+ }
+ 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 items;
+ private bool ValidateIsJoinMember(bool result, MemberExpression memberExp, Expression childExpression)
+ {
+ if (childExpression != null && childExpression is MemberExpression)
+ {
+ var oldChildExpression = childExpression;
+ var child2Expression = (childExpression as MemberExpression).Expression;
+ if (child2Expression == null || (child2Expression is ConstantExpression))
+ {
+ return false;
+ }
+ items = new List();
+ 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) });
+ if (items.Any(it => it.Type == 2 && it.Nav == null))
+ {
+ return false;
+ }
+ while (child2Expression != null)
+ {
+ if (IsClass(child2Expression))
+ {
+ items.Add(new ExpressionItems() { Type = 2, Expression = child2Expression, ThisEntityInfo = this.context.EntityMaintenance.GetEntityInfo(child2Expression.Type), ParentEntityInfo = this.context.EntityMaintenance.GetEntityInfo(GetMemberExpression(child2Expression).Type) });
+ child2Expression = GetMemberExpression(child2Expression);
+
+ }
+ else if (IsParameter(child2Expression))
+ {
+ shorName = child2Expression.ToString();
+ entityInfo = this.context.EntityMaintenance.GetEntityInfo(child2Expression.Type);
+ break;
+ }
+ else
+ {
+ break;
+ }
+ }
+ if (!items.Any(it => it.Type == 2 && it.Nav == null))
+ {
+ return true;
+ }
+ }
+ return result;
+ }
+
+ internal object GetSql()
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/OneToOneNavgateExpressionN.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/OneToOneNavgateExpressionN.cs
index 7840a2a99..46e6a306e 100644
--- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/OneToOneNavgateExpressionN.cs
+++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/OneToOneNavgateExpressionN.cs
@@ -12,7 +12,7 @@ namespace SqlSugar
#region Constructor
public string shorName { get; set; }
public EntityInfo entityInfo;
-
+ public List items;
public SqlSugarProvider context;
public OneToOneNavgateExpressionN(SqlSugarProvider context)
{
@@ -75,7 +75,6 @@ namespace SqlSugar
#endregion
#region All one to one
- List items;
private bool ValidateIsJoinMember(bool result, MemberExpression memberExp, Expression childExpression)
{
if (childExpression != null && childExpression is MemberExpression)
@@ -121,13 +120,6 @@ namespace SqlSugar
}
#endregion
- #region One to one Last Method
- private bool ValidateIsJoinAny(bool result, MemberExpression memberExp, Expression childExpression)
- {
- return false;
- }
- #endregion
-
#region Helper
private static bool IsParameter(Expression child2Expression)
@@ -146,31 +138,5 @@ namespace SqlSugar
}
#endregion
- #region Entities
- internal class ExpressionItems
- {
- ///
- /// 0 memeber, 2 method ,3 class
- ///
- public int Type { get; set; }
- public EntityInfo ParentEntityInfo { get; set; }
- public EntityInfo ThisEntityInfo { get; set; }
- public Expression Expression { get; set; }
- public Navigate Nav
- {
- get
- {
- if (Expression is MemberExpression)
- {
- var name=(Expression as MemberExpression).Member.Name;
- var navColumn = ParentEntityInfo.Columns.FirstOrDefault(it => it.PropertyName == name);
- return navColumn==null?null:navColumn.Navigat;
- }
- return null;
- }
- }
-
- }
- #endregion
}
}
diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj
index 188b9fd30..5632f75e4 100644
--- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj
+++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj
@@ -107,7 +107,9 @@
+
+