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
92f5c68c57
commit
c0ef4c4494
@ -51,6 +51,15 @@ namespace MongoDbTest
|
||||
int32 = Convert.ToInt32(it.Age),
|
||||
dateTime = Convert.ToDateTime(it.CreateDateTime),
|
||||
}).ToList(); ;
|
||||
var list4 = db.Queryable<Student>().Select(it => new
|
||||
{
|
||||
Day = it.CreateDateTime.AddDays(1),
|
||||
Year = it.CreateDateTime.AddYears(1),
|
||||
AddMonth = it.CreateDateTime.AddMonths(1)
|
||||
}).ToList();
|
||||
if (list4.First().Day.Date != dt.Date.AddDays(1)) Cases.ThrowUnitError();
|
||||
if (list4.First().Year.Date != dt.Date.AddYears(1)) Cases.ThrowUnitError();
|
||||
if (list4.First().AddMonth.Date != dt.Date.AddMonths(1)) Cases.ThrowUnitError();
|
||||
}
|
||||
[SqlSugar.SugarTable("UnitStudent1231sds3z1")]
|
||||
public class Student : MongoDbBase
|
||||
|
@ -28,8 +28,6 @@ namespace SqlSugar.MongoDb
|
||||
if (name == "ToDateTime")
|
||||
name = "ToDate";
|
||||
BsonValue result = null;
|
||||
if (typeof(IDbMethods).GetMethods().Any(it => it.Name == name))
|
||||
{
|
||||
var context = new MongoDbMethod() { context = this._context };
|
||||
MethodCallExpressionModel model = new MethodCallExpressionModel();
|
||||
var args = methodCallExpression.Arguments;
|
||||
@ -39,6 +37,8 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
model.Args.Add(new MethodCallExpressionArgs() { MemberValue = item });
|
||||
}
|
||||
if (typeof(IDbMethods).GetMethods().Any(it => it.Name == name))
|
||||
{
|
||||
if (name == nameof(ToString))
|
||||
{
|
||||
var funcString = context.ToString(model);
|
||||
@ -55,6 +55,29 @@ namespace SqlSugar.MongoDb
|
||||
result = UtilMethods.MyCreate(funcString);
|
||||
}
|
||||
}
|
||||
else if (name.StartsWith("Add"))
|
||||
{
|
||||
// 根据方法名推断DateType
|
||||
DateType dateType = DateType.Minute;
|
||||
if (name.Equals("AddDays", StringComparison.OrdinalIgnoreCase))
|
||||
dateType = DateType.Day;
|
||||
else if (name.Equals("AddHours", StringComparison.OrdinalIgnoreCase))
|
||||
dateType = DateType.Hour;
|
||||
else if (name.Equals("AddMinutes", StringComparison.OrdinalIgnoreCase))
|
||||
dateType = DateType.Minute;
|
||||
else if (name.Equals("AddSeconds", StringComparison.OrdinalIgnoreCase))
|
||||
dateType = DateType.Second;
|
||||
else if (name.Equals("AddMilliseconds", StringComparison.OrdinalIgnoreCase))
|
||||
dateType = DateType.Millisecond;
|
||||
else if (name.Equals("AddMonths", StringComparison.OrdinalIgnoreCase))
|
||||
dateType = DateType.Month;
|
||||
else if (name.Equals("AddYears", StringComparison.OrdinalIgnoreCase))
|
||||
dateType = DateType.Year;
|
||||
// 追加DateType参数
|
||||
model.Args.Add(new MethodCallExpressionArgs() { MemberValue = dateType });
|
||||
var value = context.DateAddByType(model);
|
||||
result = BsonDocument.Parse(value?.ToString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -363,6 +363,36 @@ namespace SqlSugar.MongoDb
|
||||
});
|
||||
return dateTruncDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
public override string DateAddByType(MethodCallExpressionModel model)
|
||||
{
|
||||
var dateExpr = model.DataObject;
|
||||
var numberExpr = model.Args[0].MemberValue;
|
||||
var typeExpr = model.Args[1].MemberValue;
|
||||
BsonValue dateValue = new ExpressionVisitor(context).Visit(dateExpr as Expression);
|
||||
BsonValue numberValue = new ExpressionVisitor(context).Visit(numberExpr as Expression);
|
||||
var dateType = (DateType)typeExpr;
|
||||
|
||||
string unit = dateType switch
|
||||
{
|
||||
DateType.Year => "year",
|
||||
DateType.Month => "month",
|
||||
DateType.Day => "day",
|
||||
DateType.Hour => "hour",
|
||||
DateType.Minute => "minute",
|
||||
DateType.Second => "second",
|
||||
DateType.Millisecond => "millisecond",
|
||||
_ => throw new NotSupportedException($"不支持的DateType: {dateType}")
|
||||
};
|
||||
|
||||
var dateAddDoc = new BsonDocument("$dateAdd", new BsonDocument
|
||||
{
|
||||
{ "startDate", $"${dateValue}" },
|
||||
{ "unit", unit },
|
||||
{ "amount", numberValue }
|
||||
});
|
||||
|
||||
return dateAddDoc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
}
|
||||
public override string DateValue(MethodCallExpressionModel model)
|
||||
{
|
||||
var item = model.Args.First().MemberValue;
|
||||
|
Loading…
Reference in New Issue
Block a user