mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 02:29:39 +08:00
Update mongodb
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -9,14 +9,6 @@ namespace SqlSugar.MongoDb
|
|||||||
{
|
{
|
||||||
public partial class BinaryExpressionTranslator
|
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)
|
private BsonDocument GetComparisonOperation(BinaryExpression expr, BsonValue field, BsonValue value, bool leftIsMember, bool rightIsMember, string op)
|
||||||
{
|
{
|
||||||
string leftValue = "";
|
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 } } }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -30,5 +30,18 @@ namespace SqlSugar.MongoDb
|
|||||||
_ => null
|
_ => 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}")
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user