mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update mongodb
This commit is contained in:
parent
13376f101a
commit
699aacae41
@ -84,6 +84,20 @@ namespace MongoDbTest
|
||||
Age = true ? it.Age : it.Age
|
||||
}).ToList();
|
||||
if (list8.First().Age != 11) Cases.ThrowUnitError();
|
||||
|
||||
var list9 = db.Queryable<Student>().Select(it => new Student()
|
||||
{
|
||||
Age = it.Age>0 ? it.Age: 1,
|
||||
}).ToList();
|
||||
if (list9.First().Age != 11) Cases.ThrowUnitError();
|
||||
|
||||
var list10 = db.Queryable<Student>().Select(it => new
|
||||
{
|
||||
Age = it.Age > 0 ? it.Age : 1,
|
||||
Age2 = it.Age < 0 ? it.Age : 1,
|
||||
}).ToList();
|
||||
if (list10.First().Age != 11) Cases.ThrowUnitError();
|
||||
if (list10.First().Age2 != 1) Cases.ThrowUnitError();
|
||||
}
|
||||
[SqlSugar.SugarTable("UnitStudent1231sds3z1")]
|
||||
public class Student : MongoDbBase
|
||||
|
@ -18,7 +18,7 @@ namespace MongoDbTest
|
||||
//初始化数据
|
||||
InitializeStudentData(db);
|
||||
|
||||
//null类型测试
|
||||
//类型测试
|
||||
ValidateStudentData(db);
|
||||
|
||||
//函数
|
||||
@ -65,6 +65,9 @@ namespace MongoDbTest
|
||||
var list = db.Queryable<Student>().ToList();
|
||||
if (list.First() is { } first && (first.BoolNull != null || first.SchoolIdNull != null)) Cases.ThrowUnitError();
|
||||
if (list.Last() is { } last && (last.BoolNull != true || last.SchoolIdNull != 4)) Cases.ThrowUnitError();
|
||||
|
||||
var list3 = db.Queryable<Student>().Where(it=>it.SchoolId>2).ToList();
|
||||
if(list3.Any(s=>s.SchoolId<=2)) Cases.ThrowUnitError();
|
||||
}
|
||||
|
||||
private static void InitializeStudentData(SqlSugar.SqlSugarClient db)
|
||||
|
@ -7,5 +7,6 @@ namespace SqlSugar.MongoDb
|
||||
public class ExpressionVisitorContext
|
||||
{
|
||||
public Type ExpType { get; set; }
|
||||
public bool IsText { get; internal set; }
|
||||
}
|
||||
}
|
||||
|
@ -9,5 +9,6 @@ namespace SqlSugar.MongoDb
|
||||
public ResolveExpressType resolveType { get; internal set; }
|
||||
public SqlSugarProvider context { get; internal set; }
|
||||
public QueryBuilder queryBuilder { get; internal set; }
|
||||
public bool IsTest { get; internal set; }
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,8 @@ namespace SqlSugar.MongoDb
|
||||
return new MemberInitExpressionTractor(context, visitorContext).Extract(newMemberExp);
|
||||
case NewExpression newNewExpression:
|
||||
return new NewExpressionTractor(context, visitorContext).Extract(newNewExpression);
|
||||
case ConditionalExpression conditionalExpression:
|
||||
return new ConditionalExpressionTractor(context, visitorContext).Extract(conditionalExpression);
|
||||
default:
|
||||
throw new NotSupportedException($"Unsupported expression: {expr.NodeType}");
|
||||
}
|
||||
|
@ -9,6 +9,10 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
public static class MongoNestedTranslator
|
||||
{
|
||||
public static BsonValue TranslateNoFieldName(Expression expr, MongoNestedTranslatorContext context,ExpressionVisitorContext visContext=null)
|
||||
{
|
||||
return new ExpressionVisitor(context) { visitorContext= visContext }.Visit(expr);
|
||||
}
|
||||
public static BsonDocument Translate(Expression expr, MongoNestedTranslatorContext context)
|
||||
{
|
||||
var result = new ExpressionVisitor(context).Visit(expr);
|
||||
|
@ -44,13 +44,24 @@ namespace SqlSugar.MongoDb
|
||||
return GetOtherResult(op, leftValue, rightValue);
|
||||
}
|
||||
|
||||
private static BsonDocument GetOtherResult(string op, string leftValue, BsonValue rightValue)
|
||||
private BsonDocument GetOtherResult(string op, string leftValue, BsonValue rightValue)
|
||||
{
|
||||
if (_visitorContext?.IsText == true)
|
||||
{
|
||||
// 三元条件格式: { "$gt": ["$Age", 0] }
|
||||
return new BsonDocument
|
||||
{
|
||||
{ op, new BsonArray { "$" + leftValue, rightValue } }
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new BsonDocument
|
||||
{
|
||||
{ leftValue, new BsonDocument { { op, rightValue } } }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static BsonDocument GetEqResult(string leftValue, BsonValue rightValue)
|
||||
{
|
||||
|
@ -0,0 +1,56 @@
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar.MongoDb
|
||||
{
|
||||
public class ConditionalExpressionTractor
|
||||
{
|
||||
private MongoNestedTranslatorContext context;
|
||||
private ExpressionVisitorContext visitorContext;
|
||||
|
||||
public ConditionalExpressionTractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||
{
|
||||
this.context = context;
|
||||
this.visitorContext = visitorContext;
|
||||
}
|
||||
|
||||
internal BsonValue Extract(Expression expr)
|
||||
{
|
||||
var exp = expr as ConditionalExpression;
|
||||
if (exp == null)
|
||||
{
|
||||
throw new ArgumentException("表达式不是ConditionalExpression", nameof(expr));
|
||||
}
|
||||
if (!ExpressionTool.GetParameters(exp.Test).Any())
|
||||
{
|
||||
var isOk = (bool)ExpressionTool.DynamicInvoke(exp.Test);
|
||||
var tranExp = isOk ? exp.IfTrue : exp.IfFalse;
|
||||
return MongoNestedTranslator.Translate(tranExp, context);
|
||||
}
|
||||
else
|
||||
{
|
||||
var testValue = MongoNestedTranslator.TranslateNoFieldName(exp.Test, context, new ExpressionVisitorContext() { IsText=true });
|
||||
var ifTrueValue = MongoNestedTranslator.TranslateNoFieldName(exp.IfTrue, context);
|
||||
var ifFalseValue = MongoNestedTranslator.TranslateNoFieldName(exp.IfFalse, context);
|
||||
if (exp.IfTrue is MemberExpression member && member.Expression is ParameterExpression)
|
||||
{
|
||||
ifTrueValue = $"${ifTrueValue}";
|
||||
}
|
||||
if (exp.IfFalse is MemberExpression member2 && member2.Expression is ParameterExpression)
|
||||
{
|
||||
ifFalseValue = $"${ifFalseValue}";
|
||||
}
|
||||
// MongoDB的$cond操作符
|
||||
var condDoc = new BsonDocument
|
||||
{
|
||||
{ "$cond", new BsonArray { testValue,ifTrueValue, ifFalseValue } }
|
||||
};
|
||||
return condDoc;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -14,10 +14,11 @@ namespace SqlSugar.MongoDb
|
||||
public partial class BinaryExpressionTranslator
|
||||
{
|
||||
MongoNestedTranslatorContext _context;
|
||||
|
||||
ExpressionVisitorContext _visitorContext;
|
||||
public BinaryExpressionTranslator(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||
{
|
||||
_context = context;
|
||||
_visitorContext = visitorContext;
|
||||
}
|
||||
|
||||
public BsonDocument Extract(BinaryExpression expr)
|
||||
|
@ -56,6 +56,10 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
projectionDocument[fieldName] = json.ToString();
|
||||
}
|
||||
else if (memberAssignment.Expression is ConditionalExpression)
|
||||
{
|
||||
projectionDocument[fieldName] = json;
|
||||
}
|
||||
else
|
||||
{
|
||||
projectionDocument[fieldName] = "$" + json.ToString();
|
||||
|
Loading…
Reference in New Issue
Block a user