From 88b35d338ce2eba22a0ed66c617e7b90c9c6b3f6 Mon Sep 17 00:00:00 2001 From: Bourne Shi Date: Sat, 19 Oct 2024 11:32:49 +0800 Subject: [PATCH] feat: recover code --- .../Linq/GeneratorExpressionTreeVisitor.cs | 94 +++++++++++++------ 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/src/AElf.EntityMapping.Elasticsearch/Linq/GeneratorExpressionTreeVisitor.cs b/src/AElf.EntityMapping.Elasticsearch/Linq/GeneratorExpressionTreeVisitor.cs index 37db570..68ad514 100644 --- a/src/AElf.EntityMapping.Elasticsearch/Linq/GeneratorExpressionTreeVisitor.cs +++ b/src/AElf.EntityMapping.Elasticsearch/Linq/GeneratorExpressionTreeVisitor.cs @@ -133,22 +133,22 @@ protected override Expression VisitMethodCall(MethodCallExpression expression) // private string GetFullNameKey(MemberExpression memberExpression) // { - // var key = _propertyNameInferrerParser.Parser(memberExpression.Member.Name); - // while (memberExpression.Expression != null) - // { - // memberExpression = memberExpression.Expression as MemberExpression; - // if (memberExpression == null) - // { - // break; - // } - // - // key = _propertyNameInferrerParser.Parser(memberExpression.Member.Name + "." + key); - // return key; - // } - // - // return key; + // var key = _propertyNameInferrerParser.Parser(memberExpression.Member.Name); + // while (memberExpression.Expression != null) + // { + // memberExpression = memberExpression.Expression as MemberExpression; + // if (memberExpression == null) + // { + // break; + // } + // + // key = _propertyNameInferrerParser.Parser(memberExpression.Member.Name + "." + key); + // return key; // } - + // + // return key; + // } + private string GetFullPropertyPath(Expression expression) { switch (expression) @@ -166,6 +166,7 @@ private string GetFullPropertyPath(Expression expression) var collectionPath = GetFullPropertyPath(methodCallExpression.Object); return collectionPath; // Returns the path of the collection directly, without adding an index } + break; } @@ -256,7 +257,7 @@ protected override Expression VisitSubQuery(SubQueryExpression expression) VisitBinarySetSubQuery((BinaryExpression)predicate.Right, from, fullPath, true); } } - + } break; @@ -299,14 +300,14 @@ private void HandleNestedContains(SubQueryExpression subQueryExpression, Express { if (subQueryExpression == null || expression == null) throw new ArgumentNullException("SubQueryExpression or expression cannot be null."); - + //Check if the number of items in the Terms query array within the Contains clause is too large. if (subQueryExpression.QueryModel.MainFromClause .FromExpression is ConstantExpression constantExpression) { CheckTermsArrayLength(constantExpression); } - + foreach (var resultOperator in subQueryExpression.QueryModel.ResultOperators) { switch (resultOperator) @@ -320,29 +321,48 @@ private void HandleNestedContains(SubQueryExpression subQueryExpression, Express Node query; query = GetDifferentTypesTermsQueryNode(); - + QueryMap[expression] = ParseQuery(query); QueryMap[expression].IsSubQuery = true; QueryMap[expression].SubQueryPath = subQueryPath; //from.ToLower(); QueryMap[expression].SubQueryFullPath = subQueryFullPath; } - - private static readonly HashSet SupportedTermsTypes = new HashSet - { - typeof(Guid), typeof(Guid?), - typeof(int), typeof(int?), - typeof(long), typeof(long?), - typeof(double), typeof(double?), - typeof(bool), typeof(bool?) - }; private Node GetDifferentTypesTermsQueryNode() { Node query; - if (SupportedTermsTypes.Contains(PropertyType)) + if (PropertyType == typeof(Guid)) { query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); } + else if (PropertyType == typeof(Guid?)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } + else if (PropertyType == typeof(int)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } + else if (PropertyType == typeof(int?)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } + else if (PropertyType == typeof(long)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } + else if (PropertyType == typeof(long?)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } + else if (PropertyType == typeof(double)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } + else if (PropertyType == typeof(double?)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } else if (PropertyType == typeof(DateTime)) { query = new TermsNode(PropertyName, @@ -353,6 +373,14 @@ private Node GetDifferentTypesTermsQueryNode() query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); } + else if (PropertyType == typeof(bool)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } + else if (PropertyType == typeof(bool?)) + { + query = new TermsNode(PropertyName, ((IEnumerable)Value).Select(x => x.ToString())); + } else if (PropertyType == typeof(string)) { query = new TermsNode(PropertyName, (IEnumerable)Value); @@ -365,6 +393,8 @@ private Node GetDifferentTypesTermsQueryNode() return query; } + + private void CheckTermsArrayLength(ConstantExpression constantExpression) { if (constantExpression.Value is System.Collections.IEnumerable objectList) @@ -372,7 +402,8 @@ private void CheckTermsArrayLength(ConstantExpression constantExpression) var count = objectList.Cast().Count(); if (count > _elasticsearchOptions.TermsArrayMaxLength) { - throw new Exception($"The array input for Terms query is too large, exceeding {_elasticsearchOptions.TermsArrayMaxLength} items."); + throw new Exception( + $"The array input for Terms query is too large, exceeding {_elasticsearchOptions.TermsArrayMaxLength} items."); } } } @@ -693,7 +724,8 @@ private object ConvertEnumValue(Type entityType, string propertyName, object val return (int)enumValue; } - protected void VisitBinarySetSubQuery(BinaryExpression expression, string path, string fullPath, bool parentIsSubQuery) + protected void VisitBinarySetSubQuery(BinaryExpression expression, string path, string fullPath, + bool parentIsSubQuery) { if (expression.Left is BinaryExpression && expression.Right is ConstantExpression) {