mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 10:08:19 +08:00
Update mongodb
This commit is contained in:
@@ -14,22 +14,20 @@ namespace MongoDbTest
|
|||||||
{
|
{
|
||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
Expression<Func<Order, bool>> exp = it=>it.Id==1||it.Name=="a";
|
Expression<Func<Order, bool>> exp = it => it.Id == 1 || it.Name == "a";
|
||||||
var json=MongoNestedTranslator.Translate(exp,null);
|
var json = MongoNestedTranslator.Translate(exp, null).ToString();
|
||||||
|
|
||||||
var Order = new Order() { Id = 1 };
|
var Order = new Order() { Id = 1 };
|
||||||
Expression<Func<Order, bool>> exp2 = it => it.Id == Order.Id || it.Name == "a";
|
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;
|
Expression<Func<Order, bool>> exp3 = it => it.IsValidate == true;
|
||||||
var json23 = MongoNestedTranslator.Translate(exp3, null);
|
var json3 = MongoNestedTranslator.Translate(exp3, null).ToString();
|
||||||
|
|
||||||
Expression<Func<Order, bool>> exp4 = it => it.IsValidate != true;
|
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;
|
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
|
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.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SqlSugar.MongoDbCore
|
namespace SqlSugar.MongoDbCore.ExpToSql.Context
|
||||||
{
|
{
|
||||||
public class MongoNestedTranslatorContext
|
public class MongoNestedTranslatorContext
|
||||||
{
|
{
|
@@ -1,4 +1,5 @@
|
|||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||||
using SqlSugar.MongoDbCore.ExpToSql.VisitorItems;
|
using SqlSugar.MongoDbCore.ExpToSql.VisitorItems;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -7,26 +8,31 @@ using System.Text;
|
|||||||
|
|
||||||
namespace SqlSugar.MongoDbCore
|
namespace SqlSugar.MongoDbCore
|
||||||
{
|
{
|
||||||
public class ExpressionVisitor
|
public class ExpressionVisitor
|
||||||
{
|
{
|
||||||
private MongoNestedTranslatorContext context;
|
private MongoNestedTranslatorContext context;
|
||||||
|
public ExpressionVisitorContext visitorContext;
|
||||||
|
public ExpressionVisitor(MongoNestedTranslatorContext context, ExpressionVisitorContext expressionVisitorContext)
|
||||||
|
{
|
||||||
|
this.context = context;
|
||||||
|
this.visitorContext = expressionVisitorContext;
|
||||||
|
}
|
||||||
public ExpressionVisitor(MongoNestedTranslatorContext context)
|
public ExpressionVisitor(MongoNestedTranslatorContext context)
|
||||||
{
|
{
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JToken Visit(Expression expr)
|
public JToken Visit(Expression expr)
|
||||||
{
|
{
|
||||||
expr = MongoDbExpTools.RemoveConvert(expr);
|
expr = MongoDbExpTools.RemoveConvert(expr);
|
||||||
switch (expr)
|
switch (expr)
|
||||||
{
|
{
|
||||||
case BinaryExpression binary:
|
case BinaryExpression binary:
|
||||||
return new BinaryExpressionTranslator(context).Extract(binary);
|
return new BinaryExpressionTranslator(context, visitorContext).Extract(binary);
|
||||||
case MemberExpression member:
|
case MemberExpression member:
|
||||||
return new FieldPathExtractor(context).Extract(member);
|
return new FieldPathExtractor(context, visitorContext).Extract(member);
|
||||||
case ConstantExpression constant:
|
case ConstantExpression constant:
|
||||||
return new ValueExtractor(context).Extract(constant);
|
return new ValueExtractor(context, visitorContext).Extract(constant);
|
||||||
case UnaryExpression unary:
|
case UnaryExpression unary:
|
||||||
return this.Visit(unary);
|
return this.Visit(unary);
|
||||||
case LambdaExpression lambda:
|
case LambdaExpression lambda:
|
||||||
|
@@ -56,6 +56,23 @@ namespace SqlSugar.MongoDbCore
|
|||||||
// 默认的ToString
|
// 默认的ToString
|
||||||
return value.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)
|
internal static Expression RemoveConvert(Expression item)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||||
|
|
||||||
namespace SqlSugar.MongoDbCore
|
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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
@@ -10,7 +12,7 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
|||||||
{
|
{
|
||||||
MongoNestedTranslatorContext _context;
|
MongoNestedTranslatorContext _context;
|
||||||
|
|
||||||
public BinaryExpressionTranslator(MongoNestedTranslatorContext context)
|
public BinaryExpressionTranslator(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
@@ -53,9 +55,20 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
|||||||
|
|
||||||
private JToken FieldComparisonExpression(BinaryExpression expr)
|
private JToken FieldComparisonExpression(BinaryExpression expr)
|
||||||
{
|
{
|
||||||
string field = new FieldPathExtractor(_context).Extract(expr.Left);
|
var left = new ExpressionVisitor(_context, new ExpressionVisitorContext());
|
||||||
JToken value = new ExpressionVisitor(_context).Visit(expr.Right);
|
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
|
string op = expr.NodeType switch
|
||||||
{
|
{
|
||||||
ExpressionType.Equal => value.Type == JTokenType.Null ? "$eq" : null,
|
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}")
|
_ => throw new NotSupportedException($"Unsupported binary op: {expr.NodeType}")
|
||||||
};
|
};
|
||||||
|
|
||||||
if (op == null)
|
if (op == null&&leftIsMember&&rightIsMember==false)
|
||||||
return new JObject { [field] = value };
|
return new JObject { [field.ToString()] = value };
|
||||||
|
else if (op == null && rightIsMember && leftIsMember == false)
|
||||||
|
return new JObject { [value.ToString()] = field };
|
||||||
|
|
||||||
return new JObject
|
return new JObject
|
||||||
{
|
{
|
||||||
[field] = new JObject { [op] = value }
|
[field.ToString()] = new JObject { [op] = value }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
@@ -9,10 +10,11 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
|||||||
public class FieldPathExtractor
|
public class FieldPathExtractor
|
||||||
{
|
{
|
||||||
MongoNestedTranslatorContext _context;
|
MongoNestedTranslatorContext _context;
|
||||||
|
ExpressionVisitorContext _visitorContext;
|
||||||
public FieldPathExtractor(MongoNestedTranslatorContext context)
|
public FieldPathExtractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
|
_visitorContext = visitorContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Extract(Expression expr)
|
public string Extract(Expression expr)
|
||||||
@@ -30,6 +32,10 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
|||||||
var value = ExpressionTool.GetMemberValue(oldMember.Member, oldExp);
|
var value = ExpressionTool.GetMemberValue(oldMember.Member, oldExp);
|
||||||
return MongoDbExpTools.CustomToString(value);
|
return MongoDbExpTools.CustomToString(value);
|
||||||
}
|
}
|
||||||
|
if (_visitorContext != null)
|
||||||
|
{
|
||||||
|
_visitorContext.ExpType = typeof(MemberExpression);
|
||||||
|
}
|
||||||
return string.Join(".", parts);
|
return string.Join(".", parts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using SqlSugar.MongoDbCore.ExpToSql.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
@@ -9,7 +10,7 @@ namespace SqlSugar.MongoDbCore.ExpToSql.VisitorItems
|
|||||||
public class ValueExtractor
|
public class ValueExtractor
|
||||||
{
|
{
|
||||||
MongoNestedTranslatorContext _context;
|
MongoNestedTranslatorContext _context;
|
||||||
public ValueExtractor(MongoNestedTranslatorContext context)
|
public ValueExtractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user