mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 14:04:44 +08:00
Update mongodb
This commit is contained in:
parent
332808ce60
commit
739961e618
@ -14,22 +14,20 @@ namespace MongoDbTest
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Expression<Func<Order, bool>> exp = it=>it.Id==1||it.Name=="a";
|
||||
var json=MongoNestedTranslator.Translate(exp,null);
|
||||
|
||||
Expression<Func<Order, bool>> exp = it => it.Id == 1 || it.Name == "a";
|
||||
var json = MongoNestedTranslator.Translate(exp, null).ToString();
|
||||
var Order = new Order() { Id = 1 };
|
||||
Expression<Func<Order, bool>> exp2 = it => it.Id == Order.Id || it.Name == "a";
|
||||
var json2 = MongoNestedTranslator.Translate(exp2, null);
|
||||
|
||||
var json2 = MongoNestedTranslator.Translate(exp2, null).ToString();
|
||||
|
||||
Expression<Func<Order, bool>> exp3 = it=> it.IsValidate==true;
|
||||
var json23 = MongoNestedTranslator.Translate(exp3, null);
|
||||
Expression<Func<Order, bool>> exp3 = it => it.IsValidate == true;
|
||||
var json3 = MongoNestedTranslator.Translate(exp3, null).ToString();
|
||||
|
||||
Expression<Func<Order, bool>> exp4 = it => it.IsValidate != true;
|
||||
var json24 = MongoNestedTranslator.Translate(exp4, null);
|
||||
var json24 = MongoNestedTranslator.Translate(exp4, null).ToString();
|
||||
|
||||
Expression<Func<Order, bool>> exp5 = it =>1==it.Id;
|
||||
var json5 = MongoNestedTranslator.Translate(exp5, null);
|
||||
var json5 = MongoNestedTranslator.Translate(exp5, null).ToString();
|
||||
}
|
||||
}
|
||||
public class Order
|
||||
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar.MongoDbCore.ExpToSql.Context
|
||||
{
|
||||
public class ExpressionVisitorContext
|
||||
{
|
||||
public Type ExpType { get; set; }
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar.MongoDbCore
|
||||
namespace SqlSugar.MongoDbCore.ExpToSql.Context
|
||||
{
|
||||
public class MongoNestedTranslatorContext
|
||||
{
|
@ -1,4 +1,5 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||
using SqlSugar.MongoDbCore.ExpToSql.VisitorItems;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -7,26 +8,31 @@ using System.Text;
|
||||
|
||||
namespace SqlSugar.MongoDbCore
|
||||
{
|
||||
public class ExpressionVisitor
|
||||
public class ExpressionVisitor
|
||||
{
|
||||
private MongoNestedTranslatorContext context;
|
||||
|
||||
public ExpressionVisitorContext visitorContext;
|
||||
public ExpressionVisitor(MongoNestedTranslatorContext context, ExpressionVisitorContext expressionVisitorContext)
|
||||
{
|
||||
this.context = context;
|
||||
this.visitorContext = expressionVisitorContext;
|
||||
}
|
||||
public ExpressionVisitor(MongoNestedTranslatorContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public JToken Visit(Expression expr)
|
||||
public JToken Visit(Expression expr)
|
||||
{
|
||||
expr = MongoDbExpTools.RemoveConvert(expr);
|
||||
switch (expr)
|
||||
{
|
||||
case BinaryExpression binary:
|
||||
return new BinaryExpressionTranslator(context).Extract(binary);
|
||||
return new BinaryExpressionTranslator(context, visitorContext).Extract(binary);
|
||||
case MemberExpression member:
|
||||
return new FieldPathExtractor(context).Extract(member);
|
||||
return new FieldPathExtractor(context, visitorContext).Extract(member);
|
||||
case ConstantExpression constant:
|
||||
return new ValueExtractor(context).Extract(constant);
|
||||
return new ValueExtractor(context, visitorContext).Extract(constant);
|
||||
case UnaryExpression unary:
|
||||
return this.Visit(unary);
|
||||
case LambdaExpression lambda:
|
||||
|
@ -56,6 +56,23 @@ namespace SqlSugar.MongoDbCore
|
||||
// 默认的ToString
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
public static bool IsField(Expression expr)
|
||||
{
|
||||
// 如果是字段或属性访问(例如 x.Name)
|
||||
if (expr is MemberExpression)
|
||||
return true;
|
||||
|
||||
// 如果是类型转换(例如 (object)x.Name),递归判断内部表达式
|
||||
if (expr is UnaryExpression unaryExpr &&
|
||||
(unaryExpr.NodeType == ExpressionType.Convert || unaryExpr.NodeType == ExpressionType.ConvertChecked))
|
||||
{
|
||||
return IsField(unaryExpr.Operand);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static Expression RemoveConvert(Expression item)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq.Expressions;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||
|
||||
namespace SqlSugar.MongoDbCore
|
||||
{
|
||||
|
@ -1,4 +1,6 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using MongoDB.Driver;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
@ -10,7 +12,7 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
||||
{
|
||||
MongoNestedTranslatorContext _context;
|
||||
|
||||
public BinaryExpressionTranslator(MongoNestedTranslatorContext context)
|
||||
public BinaryExpressionTranslator(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
@ -53,9 +55,20 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
||||
|
||||
private JToken FieldComparisonExpression(BinaryExpression expr)
|
||||
{
|
||||
string field = new FieldPathExtractor(_context).Extract(expr.Left);
|
||||
JToken value = new ExpressionVisitor(_context).Visit(expr.Right);
|
||||
|
||||
var left = new ExpressionVisitor(_context, new ExpressionVisitorContext());
|
||||
var right = new ExpressionVisitor(_context, new ExpressionVisitorContext());
|
||||
JToken field = left.Visit(expr.Left);
|
||||
JToken value = right.Visit(expr.Right);
|
||||
var leftIsMember = false;
|
||||
var rightIsMember = false;
|
||||
if (left?.visitorContext?.ExpType == typeof(MemberExpression))
|
||||
{
|
||||
leftIsMember = true;
|
||||
}
|
||||
if (right?.visitorContext?.ExpType == typeof(MemberExpression))
|
||||
{
|
||||
rightIsMember = true;
|
||||
}
|
||||
string op = expr.NodeType switch
|
||||
{
|
||||
ExpressionType.Equal => value.Type == JTokenType.Null ? "$eq" : null,
|
||||
@ -67,12 +80,14 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
||||
_ => throw new NotSupportedException($"Unsupported binary op: {expr.NodeType}")
|
||||
};
|
||||
|
||||
if (op == null)
|
||||
return new JObject { [field] = value };
|
||||
if (op == null&&leftIsMember&&rightIsMember==false)
|
||||
return new JObject { [field.ToString()] = value };
|
||||
else if (op == null && rightIsMember && leftIsMember == false)
|
||||
return new JObject { [value.ToString()] = field };
|
||||
|
||||
return new JObject
|
||||
{
|
||||
[field] = new JObject { [op] = value }
|
||||
[field.ToString()] = new JObject { [op] = value }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
@ -9,10 +10,11 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
||||
public class FieldPathExtractor
|
||||
{
|
||||
MongoNestedTranslatorContext _context;
|
||||
|
||||
public FieldPathExtractor(MongoNestedTranslatorContext context)
|
||||
ExpressionVisitorContext _visitorContext;
|
||||
public FieldPathExtractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||
{
|
||||
_context = context;
|
||||
_visitorContext = visitorContext;
|
||||
}
|
||||
|
||||
public string Extract(Expression expr)
|
||||
@ -30,6 +32,10 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
||||
var value = ExpressionTool.GetMemberValue(oldMember.Member, oldExp);
|
||||
return MongoDbExpTools.CustomToString(value);
|
||||
}
|
||||
if (_visitorContext != null)
|
||||
{
|
||||
_visitorContext.ExpType = typeof(MemberExpression);
|
||||
}
|
||||
return string.Join(".", parts);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
@ -9,7 +10,7 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
||||
public class ValueExtractor
|
||||
{
|
||||
MongoNestedTranslatorContext _context;
|
||||
public ValueExtractor(MongoNestedTranslatorContext context)
|
||||
public ValueExtractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user