mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-22 20:13:41 +08:00
Update mongodb
This commit is contained in:
@@ -174,6 +174,18 @@ namespace MongoDbTest
|
||||
SchoolName2=z.Name
|
||||
}).ToList();
|
||||
if(list10.First().SchoolName!=list10.First().SchoolName2) Cases.ThrowUnitError();
|
||||
|
||||
var list11 = db.Queryable<Student>()
|
||||
.LeftJoin<School>((x, y) => y.Id== x.SchoolId )
|
||||
.LeftJoin<School>((x, y, z) => x.SchoolId == z.Id)
|
||||
.Where((x, y) => y.Name == "TestSchool")
|
||||
.Select((x, y, z) => new
|
||||
{
|
||||
StudentName = x.Name,
|
||||
SchoolName = y.Name,
|
||||
SchoolName2 = z.Name
|
||||
}).ToList();
|
||||
if (list11.First().SchoolName != list11.First().SchoolName2) Cases.ThrowUnitError();
|
||||
}
|
||||
[SqlSugar.SugarTable("UnitStudent123131")]
|
||||
public class Student : MongoDbBase
|
||||
|
@@ -3,13 +3,26 @@ using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.MongoDb
|
||||
{
|
||||
public class MongoDbQueryable<T> : QueryableProvider<T>
|
||||
{
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
return BuildJoinQueryInfo(joinExpression, joinType);
|
||||
}
|
||||
|
||||
public JoinQueryInfo BuildJoinQueryInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
public override ISugarQueryable<T> WhereClassByPrimaryKey(List<T> list)
|
||||
{
|
||||
var filterDoc = new BsonDocument();
|
||||
@@ -97,6 +110,12 @@ namespace SqlSugar.MongoDb
|
||||
}
|
||||
public class MongoDbQueryable<T, T2> : QueryableProvider<T, T2>
|
||||
{
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
public new ISugarQueryable<T, T2> With(string withString)
|
||||
{
|
||||
return this;
|
||||
@@ -104,42 +123,92 @@ namespace SqlSugar.MongoDb
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3> : QueryableProvider<T, T2, T3>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4> : QueryableProvider<T, T2, T3, T4>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4, T5> : QueryableProvider<T, T2, T3, T4, T5>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4, T5, T6> : QueryableProvider<T, T2, T3, T4, T5, T6>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4, T5, T6, T7> : QueryableProvider<T, T2, T3, T4, T5, T6, T7>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4, T5, T6, T7, T8> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
public class MongoDbQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>
|
||||
{
|
||||
|
||||
public override JoinQueryInfo GetJoinInfo(Expression joinExpression, JoinType joinType)
|
||||
{
|
||||
var queryBuilder = this.QueryBuilder;
|
||||
var context = this.Context;
|
||||
return UtilMethods.BuilderJoinInfo(joinExpression, joinType, queryBuilder, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,6 @@
|
||||
using MongoDB.Bson;
|
||||
using Dm.util;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using SqlSugar.MongoDb;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -202,17 +204,34 @@ namespace SqlSugar.MongoDb
|
||||
// from: 目标集合名(假设 JoinQueryInfo 有 TableName/ShortName 字段,需根据实际情况调整)
|
||||
// as: 关联后的别名(假设 JoinQueryInfo 有 ShortName 字段)
|
||||
string from = item.TableName ?? item.ShortName ?? "Unknown";
|
||||
string asName = item.ShortName ?? "y";
|
||||
|
||||
// $lookup
|
||||
var lookupDoc = new BsonDocument("$lookup", new BsonDocument
|
||||
string asName = item.ShortName;
|
||||
var asNamePrefix = $"{asName}.";
|
||||
var isValueKey = foreignField.StartsWith(asNamePrefix) && !localField.StartsWith(asNamePrefix);
|
||||
var isKeyValue = !foreignField.StartsWith(asNamePrefix) && localField.StartsWith(asNamePrefix);
|
||||
var isEasyJoin = isKeyValue || isValueKey;
|
||||
if (isKeyValue)
|
||||
{
|
||||
localField = localField.TrimStart(asNamePrefix.toCharArray());
|
||||
var oldLocalField = localField;
|
||||
localField = foreignField;
|
||||
foreignField = oldLocalField;
|
||||
}
|
||||
else if(isValueKey)
|
||||
{
|
||||
{ "from", from },
|
||||
{ "localField", localField },
|
||||
{ "foreignField", foreignField },
|
||||
{ "as", asName }
|
||||
});
|
||||
operations.Add(lookupDoc.ToJson(UtilMethods.GetJsonWriterSettings()));
|
||||
foreignField = foreignField.TrimStart(asNamePrefix.toCharArray());
|
||||
}
|
||||
if (isEasyJoin)
|
||||
{
|
||||
// $lookup
|
||||
var lookupDoc = new BsonDocument("$lookup", new BsonDocument
|
||||
{
|
||||
{ "from", from },
|
||||
{ "localField", localField },
|
||||
{ "foreignField", foreignField },
|
||||
{ "as", asName }
|
||||
});
|
||||
operations.Add(lookupDoc.ToJson(UtilMethods.GetJsonWriterSettings()));
|
||||
}
|
||||
|
||||
// $unwind
|
||||
BsonValue unwindDoc = null;
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using MongoDB.Bson;
|
||||
using Dm.util;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -17,7 +18,39 @@ using System.Text.RegularExpressions;
|
||||
namespace SqlSugar.MongoDb
|
||||
{
|
||||
public class UtilMethods
|
||||
{
|
||||
{
|
||||
internal static JoinQueryInfo BuilderJoinInfo(Expression joinExpression, JoinType joinType, QueryBuilder queryBuilder, SqlSugarProvider context)
|
||||
{
|
||||
queryBuilder.CheckExpressionNew(joinExpression, "Join");
|
||||
queryBuilder.JoinExpression = joinExpression;
|
||||
var express = LambdaExpression.Lambda(joinExpression).Body;
|
||||
var lastPareamter = (express as LambdaExpression).Parameters.Last();
|
||||
context.InitMappingInfo(lastPareamter.Type);
|
||||
var newJoins = queryBuilder.JoinQueryInfos.ToList();
|
||||
var oldJoins = queryBuilder.JoinQueryInfos;
|
||||
var result = new JoinQueryInfo()
|
||||
{
|
||||
JoinIndex = queryBuilder.JoinQueryInfos.Count,
|
||||
JoinType = joinType,
|
||||
JoinWhere = "1=1",
|
||||
ShortName = lastPareamter.Name,
|
||||
EntityType = lastPareamter.Type,
|
||||
TableName = null
|
||||
};
|
||||
newJoins.add(result);
|
||||
queryBuilder.JoinQueryInfos = newJoins;
|
||||
var expResult = queryBuilder.GetExpressionValue(joinExpression, ResolveExpressType.WhereMultiple);
|
||||
queryBuilder.JoinQueryInfos = oldJoins;
|
||||
result.JoinWhere = expResult.GetResultString();
|
||||
result.TableName = context.EntityMaintenance.GetTableName(lastPareamter.Type);
|
||||
|
||||
if (context.CurrentConnectionConfig?.MoreSettings?.PgSqlIsAutoToLower == false)
|
||||
{
|
||||
result.ShortName = queryBuilder.Builder.GetTranslationColumnName(result.ShortName);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static BsonValue ParseJsonObject(object json)
|
||||
{
|
||||
if (json is string str && str.TrimStart().StartsWith("{"))
|
||||
|
Reference in New Issue
Block a user