diff --git a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/CalculationOperationExpression.cs b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/CalculationOperationExpression.cs new file mode 100644 index 000000000..33355b402 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/CalculationOperationExpression.cs @@ -0,0 +1,42 @@ +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace SqlSugar.MongoDb +{ + public partial class BinaryExpressionTranslator + { + private BsonDocument GetCalculationOperation(BsonValue field, ExpressionType nodeType, BsonValue value, bool leftIsMember, bool rightIsMember) + { + string operation = GetCalculationType(nodeType); + if (IsStringAdd(value, operation)) + return StringAddCalculation(field, value, leftIsMember, rightIsMember, out operation); + else + return GetCommonCalculation(field, value, operation); + } + private static BsonDocument GetCommonCalculation(BsonValue field, BsonValue value, string operation) + { + return new BsonDocument + { + { field.ToString(), new BsonDocument { { operation, value } } } + }; + } + + private static BsonDocument StringAddCalculation(BsonValue field, BsonValue value, bool leftIsMember, bool rightIsMember, out string operation) + { + operation = "$concat"; + return new BsonDocument + { + { operation, new BsonArray { UtilMethods.GetBsonValue(leftIsMember, field), UtilMethods.GetBsonValue(rightIsMember, value) } } + }; + ; + } + private static bool IsStringAdd(BsonValue value, string operation) + { + return operation == "$add" && value.BsonType == BsonType.String; + } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/FieldComparisonExpression.cs b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/ComparisonOperationExpression.cs similarity index 62% rename from Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/FieldComparisonExpression.cs rename to Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/ComparisonOperationExpression.cs index e210f5047..b29def48f 100644 --- a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/FieldComparisonExpression.cs +++ b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/ComparisonOperationExpression.cs @@ -8,15 +8,7 @@ using System.Text; namespace SqlSugar.MongoDb { public partial class BinaryExpressionTranslator - { - private BsonDocument FieldComparisonOrCalculationExpression(BinaryExpression expr) - { - OutParameters(expr, out var field, out var value, out var leftIsMember, out var rightIsMember, out var op); - return op == null - ? GetCalculationOperation(field, expr.NodeType, value, leftIsMember, rightIsMember) - : GetComparisonOperation(expr, field, value, leftIsMember, rightIsMember, op); - } - + { private BsonDocument GetComparisonOperation(BinaryExpression expr, BsonValue field, BsonValue value, bool leftIsMember, bool rightIsMember, string op) { string leftValue = ""; @@ -76,31 +68,5 @@ namespace SqlSugar.MongoDb } } - private BsonDocument GetCalculationOperation(BsonValue field, ExpressionType nodeType, BsonValue value, bool leftIsMember, bool rightIsMember) - { - string operation = nodeType switch - { - ExpressionType.Add => "$add", - ExpressionType.Subtract => "$subtract", - ExpressionType.Multiply => "$multiply", - ExpressionType.Divide => "$divide", - ExpressionType.Modulo => "$mod", - _ => throw new NotSupportedException($"Unsupported calculation operation: {nodeType}") - }; - if (operation == "$add" && value.BsonType == BsonType.String) - { - operation = "$concat"; - return new BsonDocument - { - { operation, new BsonArray { UtilMethods.GetBsonValue(leftIsMember, field), UtilMethods.GetBsonValue(rightIsMember, value) } } - }; - ; - } - return new BsonDocument - { - { field.ToString(), new BsonDocument { { operation, value } } } - }; - } - } } diff --git a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/FieldExpressionCase.cs b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/FieldExpressionCase.cs new file mode 100644 index 000000000..1330bb767 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/FieldExpressionCase.cs @@ -0,0 +1,20 @@ +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; + +namespace SqlSugar.MongoDb +{ + public partial class BinaryExpressionTranslator + { + private BsonDocument FieldComparisonOrCalculationExpression(BinaryExpression expr) + { + OutParameters(expr, out var field, out var value, out var leftIsMember, out var rightIsMember, out var op); + return op == null + ? GetCalculationOperation(field, expr.NodeType, value, leftIsMember, rightIsMember) + : GetComparisonOperation(expr, field, value, leftIsMember, rightIsMember, op); + } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/Helper.cs b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/Helper.cs index c21a30f7a..c931a5ac6 100644 --- a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/Helper.cs +++ b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/ExpToSql/VisitorItems/BinaryExpressionTranslator/Helper.cs @@ -29,6 +29,19 @@ namespace SqlSugar.MongoDb ExpressionType.LessThanOrEqual => "$lte", _ => null }; - } + } + + private static string GetCalculationType(ExpressionType nodeType) + { + return nodeType switch + { + ExpressionType.Add => "$add", + ExpressionType.Subtract => "$subtract", + ExpressionType.Multiply => "$multiply", + ExpressionType.Divide => "$divide", + ExpressionType.Modulo => "$mod", + _ => throw new NotSupportedException($"Unsupported calculation operation: {nodeType}") + }; + } } }