mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-20 00:44:19 +08:00
Compare commits
9 Commits
237037db5a
...
fb7c2e387e
Author | SHA1 | Date | |
---|---|---|---|
![]() |
fb7c2e387e | ||
![]() |
97b6cc3b39 | ||
![]() |
a76036864c | ||
![]() |
1d0c58681f | ||
![]() |
a4c1e8fd51 | ||
![]() |
6226a9e296 | ||
![]() |
5bcb3018c6 | ||
![]() |
20c4bec503 | ||
![]() |
75189aa220 |
@ -18,6 +18,8 @@ namespace MongoDbTest
|
||||
QueryJson.Init();
|
||||
QueryJson2.Init();
|
||||
QueryJson3.Init();
|
||||
QueryJson4.Init();
|
||||
QueryJson5.Init();
|
||||
QueryLeftJoin.Init();
|
||||
QueryLeftJoin2.Init();
|
||||
QueryLeftJoin3.Init();
|
||||
|
@ -16,10 +16,11 @@ namespace MongoDbTest
|
||||
var db = DbHelper.GetNewDb();
|
||||
db.CodeFirst.InitTables<Student>();
|
||||
db.DbMaintenance.TruncateTable<Student>();
|
||||
var dt = DateTime.Now;
|
||||
db.Insertable(new Student()
|
||||
{
|
||||
Age = 1,
|
||||
Book = new Book() { SchoolId = ObjectId.GenerateNewId().ToString() }
|
||||
Book = new Book() { SchoolId = ObjectId.GenerateNewId().ToString(),DateTime=dt}
|
||||
}).ExecuteCommand();
|
||||
var data = db.Queryable<Student>().First();
|
||||
var list=db.Queryable<Student>().Where(s => s.Book.SchoolId == data.Book.SchoolId).ToList();
|
||||
@ -27,6 +28,7 @@ namespace MongoDbTest
|
||||
var ids = new List<string>() { data.Book.SchoolId};
|
||||
var list2 = db.Queryable<Student>().Where(s => ids.Contains( s.Book.SchoolId ) ).ToList();
|
||||
if (list2.Any() == false) Cases.ThrowUnitError();
|
||||
if(data.Book.DateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") != dt.ToString("yyyy-MM-dd HH:mm:ss.fff")) Cases.ThrowUnitError();
|
||||
}
|
||||
|
||||
[SqlSugar.SugarTable("UnitStudentdddd1")]
|
||||
@ -51,6 +53,7 @@ namespace MongoDbTest
|
||||
public string SchoolId { get; set; }
|
||||
|
||||
public decimal BookId { get; set; }
|
||||
public DateTime DateTime { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
44
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson3.cs
Normal file
44
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson3.cs
Normal file
@ -0,0 +1,44 @@
|
||||
|
||||
using SqlSugar;
|
||||
namespace MongoDbTest
|
||||
{
|
||||
public class QueryJson3
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
var db = DbHelper.GetNewDb();
|
||||
db.DbMaintenance.TruncateTable<NoticeEntity>();
|
||||
db.Insertable<NoticeEntity>(new NoticeEntity() { Id = 12312312L, NoticeTitle = "88888", noticeContentInfos = new List<NoticeContentInfo>() { new NoticeContentInfo { SubjectId = 1 } } }).ExecuteCommand();
|
||||
var da = db.Updateable<NoticeEntity>()
|
||||
.SetColumns(it => new NoticeEntity { NoticeTitle = "66666" }, true).Where(it => it.Id == 12312312L)
|
||||
.ExecuteCommand() > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 通知公告
|
||||
/// </summary>
|
||||
[SugarTable("NoticeInfoComponent")]
|
||||
public class NoticeEntity
|
||||
{
|
||||
[SugarColumn(ColumnName = "_id")]
|
||||
public long Id { get; set; }
|
||||
/// <summary>
|
||||
/// 通知公告标题
|
||||
/// </summary>
|
||||
public string NoticeTitle { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 通知公告内容
|
||||
/// </summary>
|
||||
[SugarColumn(IsJson = true)]
|
||||
public List<NoticeContentInfo> noticeContentInfos { get; set; } = null!;
|
||||
}
|
||||
|
||||
public class NoticeContentInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 模块
|
||||
/// </summary>
|
||||
public int SubjectId { get; set; }
|
||||
}
|
69
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson4.cs
Normal file
69
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson4.cs
Normal file
@ -0,0 +1,69 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Bson;
|
||||
using SqlSugar.MongoDb;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbTest
|
||||
{
|
||||
public class QueryJson4
|
||||
{
|
||||
internal static void Init()
|
||||
{
|
||||
var db = DbHelper.GetNewDb();
|
||||
db.CodeFirst.InitTables<Student>();
|
||||
db.DbMaintenance.TruncateTable<Student>();
|
||||
var dt = DateTime.Now;
|
||||
db.Insertable(new Student()
|
||||
{
|
||||
Age = 1,
|
||||
Book = new Book()
|
||||
{
|
||||
SchoolId = 1,
|
||||
Book2 = new Book() { SchoolId=1,Book2 = new Book() { SchoolId = 2 } }
|
||||
}
|
||||
}).ExecuteCommand();
|
||||
db.Insertable(new Student()
|
||||
{
|
||||
Age = 1,
|
||||
Book = new Book()
|
||||
{
|
||||
SchoolId = 222,
|
||||
Book2 = new Book() { SchoolId = 222, Book2 = new Book() { SchoolId = 2 } }
|
||||
}
|
||||
}).ExecuteCommand();
|
||||
var data1 = db.Queryable<Student>().Where(s=>s.Book.Book2.SchoolId==1).ToList();
|
||||
var data2 = db.Queryable<Student>().Where(s => s.Book.Book2.Book2.SchoolId == 2).ToList();
|
||||
if (data1.Count() != 1 || data1.Count() != 1) Cases.Init();
|
||||
if (data1.First().Book.Book2.SchoolId != 1) Cases.Init();
|
||||
if (data2.First().Book.Book2.Book2.SchoolId != 2) Cases.Init();
|
||||
}
|
||||
|
||||
[SqlSugar.SugarTable("UnitStudentd23351")]
|
||||
public class Student : MongoDbBase
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public string SchoolId { get; set; }
|
||||
|
||||
public int Age { get; set; }
|
||||
|
||||
public DateTime CreateDateTime { get; set; }
|
||||
|
||||
[SqlSugar.SugarColumn(IsJson = true)]
|
||||
public Book Book { get; set; }
|
||||
}
|
||||
|
||||
public class Book
|
||||
{
|
||||
public int SchoolId { get; set; }
|
||||
|
||||
[SqlSugar.SugarColumn(IsJson = true)]
|
||||
public Book Book2 { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
69
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson5.cs
Normal file
69
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson5.cs
Normal file
@ -0,0 +1,69 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Bson;
|
||||
using SqlSugar.MongoDb;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbTest
|
||||
{
|
||||
public class QueryJson5
|
||||
{
|
||||
internal static void Init()
|
||||
{
|
||||
var db = DbHelper.GetNewDb();
|
||||
db.CodeFirst.InitTables<Student>();
|
||||
db.DbMaintenance.TruncateTable<Student>();
|
||||
var dt = DateTime.Now;
|
||||
db.Insertable(new Student()
|
||||
{
|
||||
Age = 1,
|
||||
Book = new Book()
|
||||
{
|
||||
Id = 1,
|
||||
Book2 = new Book() { Id = 1, Book2 = new Book() { Id = 2 } }
|
||||
}
|
||||
}).ExecuteCommand();
|
||||
db.Insertable(new Student()
|
||||
{
|
||||
Age = 1,
|
||||
Book = new Book()
|
||||
{
|
||||
Id = 222,
|
||||
Book2 = new Book() { Id = 222, Book2 = new Book() { Id = 2 } }
|
||||
}
|
||||
}).ExecuteCommand();
|
||||
var data1 = db.Queryable<Student>().Where(s => s.Book.Book2.Id == 1).ToList();
|
||||
var data2 = db.Queryable<Student>().Where(s => s.Book.Book2.Book2.Id == 2).ToList();
|
||||
if (data1.Count() != 1 || data1.Count() != 1) Cases.Init();
|
||||
if (data1.First().Book.Book2.Id != 1) Cases.Init();
|
||||
if (data2.First().Book.Book2.Book2.Id != 2) Cases.Init();
|
||||
}
|
||||
|
||||
[SqlSugar.SugarTable("UnitStudentd2dfhj351")]
|
||||
public class Student : MongoDbBase
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public string SchoolId { get; set; }
|
||||
|
||||
public int Age { get; set; }
|
||||
|
||||
public DateTime CreateDateTime { get; set; }
|
||||
|
||||
[SqlSugar.SugarColumn(IsJson = true)]
|
||||
public Book Book { get; set; }
|
||||
}
|
||||
|
||||
public class Book
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[SqlSugar.SugarColumn(IsJson = true)]
|
||||
public Book Book2 { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
|
||||
using MongoDbTest;
|
||||
using SqlSugar;
|
||||
|
||||
public class QueryJson3
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
var db = DbHelper.GetNewDb();
|
||||
db.DbMaintenance.TruncateTable<NoticeEntity>();
|
||||
db.Insertable<NoticeEntity>(new NoticeEntity() { Id = 12312312L, NoticeTitle = "88888", noticeContentInfos = new List<NoticeContentInfo>() { new NoticeContentInfo { SubjectId = 1 } } }).ExecuteCommand();
|
||||
var da = db.Updateable<NoticeEntity>()
|
||||
.SetColumns(it => new NoticeEntity { NoticeTitle = "66666" },true ).Where(it => it.Id == 12312312L)
|
||||
.ExecuteCommand() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通知公告
|
||||
/// </summary>
|
||||
[SugarTable("NoticeInfoComponent")]
|
||||
public class NoticeEntity
|
||||
{
|
||||
[SugarColumn(ColumnName = "_id")]
|
||||
public long Id { get; set; }
|
||||
/// <summary>
|
||||
/// 通知公告标题
|
||||
/// </summary>
|
||||
public string NoticeTitle { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 通知公告内容
|
||||
/// </summary>
|
||||
[SugarColumn(IsJson = true)]
|
||||
public List<NoticeContentInfo> noticeContentInfos { get; set; } = null!;
|
||||
}
|
||||
|
||||
public class NoticeContentInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 模块
|
||||
/// </summary>
|
||||
public int SubjectId { get; set; }
|
||||
}
|
@ -12,11 +12,11 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
private BsonDocument GetComparisonOperation(BinaryExpression expr, BsonValue field, BsonValue value, bool leftIsMember, bool rightIsMember, string op)
|
||||
{
|
||||
var isLeftValue = IsLeftValue(leftIsMember, rightIsMember, op);
|
||||
var isRightValue = IsRightValue(leftIsMember, rightIsMember, op);
|
||||
var isKeyValue = isLeftValue || isRightValue;
|
||||
var isLeftMember = IsLeftValue(leftIsMember, rightIsMember, op);
|
||||
var isRightMember = IsRightValue(leftIsMember, rightIsMember, op);
|
||||
var isKeyValue = isLeftMember || isRightMember;
|
||||
if (isKeyValue)
|
||||
return ComparisonKeyValue(expr, field, value, op, isLeftValue);
|
||||
return ComparisonKeyValue(expr, field, value, op, isLeftMember);
|
||||
else
|
||||
return ComparisonNotKeyValue(field, value, op);
|
||||
}
|
||||
@ -37,11 +37,11 @@ namespace SqlSugar.MongoDb
|
||||
};
|
||||
}
|
||||
|
||||
private BsonDocument ComparisonKeyValue(BinaryExpression expr, BsonValue field, BsonValue value, string op, bool isLeftValue)
|
||||
private BsonDocument ComparisonKeyValue(BinaryExpression expr, BsonValue field, BsonValue value, string op, bool isLeftMember)
|
||||
{
|
||||
string leftValue = isLeftValue ? field.ToString() : value.ToString();
|
||||
BsonValue rightValue = isLeftValue ? value: field;
|
||||
var expression = isLeftValue ? MongoDbExpTools.RemoveConvert(expr.Left) as MemberExpression : MongoDbExpTools.RemoveConvert(expr.Right) as MemberExpression;
|
||||
string leftValue = isLeftMember ? field.ToString() : value.ToString();
|
||||
BsonValue rightValue = isLeftMember ? value: field;
|
||||
var expression = isLeftMember ? MongoDbExpTools.RemoveConvert(expr.Left) as MemberExpression : MongoDbExpTools.RemoveConvert(expr.Right) as MemberExpression;
|
||||
EntityColumnInfo CurrentColumnInfo = null;
|
||||
Type iSugarDataConverterType=UtilConstants.StringType;
|
||||
leftValue = GetLeftValue(leftValue, expression, ref CurrentColumnInfo);
|
||||
|
@ -75,7 +75,14 @@ namespace SqlSugar.MongoDb
|
||||
|
||||
while (expr is MemberExpression member)
|
||||
{
|
||||
parts.Push(member.Member.Name);
|
||||
if (member.Member.Name.EqualCase("Id"))
|
||||
{
|
||||
parts.Push("_id");
|
||||
}
|
||||
else
|
||||
{
|
||||
parts.Push(member.Member.Name);
|
||||
}
|
||||
expr = member.Expression!;
|
||||
}
|
||||
|
||||
|
@ -334,7 +334,13 @@ namespace SqlSugar.MongoDb
|
||||
string result = string.Format(this.AlterColumnToTableSql, tableName, columnName, dataType, dataSize, nullType, primaryKey, identity);
|
||||
return result;
|
||||
}
|
||||
|
||||
public override bool CreateDatabase(string databaseDirectory = null)
|
||||
{
|
||||
var newdb = this.Context.CopyNew();
|
||||
newdb.Open();
|
||||
newdb.Close();
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
///by current connection string
|
||||
/// </summary>
|
||||
|
@ -49,6 +49,7 @@ namespace SqlSugar.MongoDb
|
||||
}
|
||||
|
||||
var array = new BsonArray(list);
|
||||
ConvertBsonDateTimeToLocal(array);
|
||||
return array.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
else
|
||||
@ -56,6 +57,7 @@ namespace SqlSugar.MongoDb
|
||||
var realType = it.GetType();
|
||||
var bson = it.ToBson(realType); // → byte[]
|
||||
var doc = BsonSerializer.Deserialize<BsonDocument>(bson); // → BsonDocument
|
||||
ConvertBsonDateTimeToLocal(doc);
|
||||
var json = doc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
return json;
|
||||
}
|
||||
@ -204,7 +206,49 @@ namespace SqlSugar.MongoDb
|
||||
return json;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
private void ConvertBsonDateTimeToLocal(BsonValue bsonValue)
|
||||
{
|
||||
if (bsonValue == null || bsonValue.IsBsonNull)
|
||||
return;
|
||||
|
||||
if (bsonValue.IsBsonDocument)
|
||||
{
|
||||
var doc = bsonValue.AsBsonDocument;
|
||||
foreach (var element in doc.Elements.ToList())
|
||||
{
|
||||
var val = element.Value;
|
||||
if (val.BsonType == BsonType.DateTime)
|
||||
{
|
||||
var utcDate = val.ToUniversalTime();
|
||||
var localDate = utcDate.ToLocalTime();
|
||||
doc[element.Name] =UtilMethods.MyCreate(localDate);
|
||||
}
|
||||
else
|
||||
{
|
||||
ConvertBsonDateTimeToLocal(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (bsonValue.IsBsonArray)
|
||||
{
|
||||
var array = bsonValue.AsBsonArray;
|
||||
for (int i = 0; i < array.Count; i++)
|
||||
{
|
||||
var val = array[i];
|
||||
if (val.BsonType == BsonType.DateTime)
|
||||
{
|
||||
var utcDate = val.ToUniversalTime();
|
||||
var localDate = utcDate.ToLocalTime();
|
||||
array[i] = new BsonDateTime(localDate);
|
||||
}
|
||||
else
|
||||
{
|
||||
ConvertBsonDateTimeToLocal(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public override string SqlTemplate
|
||||
{
|
||||
get
|
||||
|
Loading…
Reference in New Issue
Block a user