From 415e09f0daaa61af34e5358a7b804624ec1b48a0 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Fri, 6 Sep 2019 19:07:32 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=AE=8C=E5=96=84=20ExpressionTree=20DateT?= =?UTF-8?q?ime/DateTimeOffset=20=E6=95=B0=E6=8D=AE=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GetDataReaderValueBlockExpressionTest.cs | 56 +++++++++++++++++++ .../MySql/MapType/DateTimeOffSetTest.cs | 10 +++- FreeSql/Internal/UtilsExpressionTree.cs | 5 +- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/FreeSql.Tests/FreeSql.Tests/ExpressionTree/GetDataReaderValueBlockExpressionTest.cs b/FreeSql.Tests/FreeSql.Tests/ExpressionTree/GetDataReaderValueBlockExpressionTest.cs index 6ae782b5..ee4873c3 100644 --- a/FreeSql.Tests/FreeSql.Tests/ExpressionTree/GetDataReaderValueBlockExpressionTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/ExpressionTree/GetDataReaderValueBlockExpressionTest.cs @@ -418,6 +418,34 @@ namespace FreeSql.ExpressionTree Assert.Null(Utils.GetDataReaderValue(typeof(DateTime?), "aaa")); } + [Fact] + public void DateTime2_By_DateTimeOffset() + { + var exp1 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime), Expression.Constant(DateTimeOffset.MinValue)); + Assert.Equal(DateTime.MinValue, Utils.GetDataReaderValue(typeof(DateTime), DateTimeOffset.MinValue)); + var exp2 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime), Expression.Constant(DateTimeOffset.MaxValue)); + Assert.Equal(DateTime.MaxValue, Utils.GetDataReaderValue(typeof(DateTime), DateTimeOffset.MaxValue)); + var exp3 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime), Expression.Constant("2000-1-1")); + Assert.Equal(DateTime.Parse("2000-1-1"), Utils.GetDataReaderValue(typeof(DateTime), "2000-1-1")); + + var exp11 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime?), Expression.Constant(DateTimeOffset.MinValue)); + Assert.Equal(DateTime.MinValue, Utils.GetDataReaderValue(typeof(DateTime?), DateTimeOffset.MinValue)); + var exp22 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime?), Expression.Constant(DateTimeOffset.MaxValue)); + Assert.Equal(DateTime.MaxValue, Utils.GetDataReaderValue(typeof(DateTime?), DateTimeOffset.MaxValue)); + var exp33 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime?), Expression.Constant("2000-1-1")); + Assert.Equal(DateTime.Parse("2000-1-1"), Utils.GetDataReaderValue(typeof(DateTime?), "2000-1-1")); + + var exp111 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime), Expression.Constant(null)); + Assert.Equal(default(DateTime), Utils.GetDataReaderValue(typeof(DateTime), null)); + var exp222 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime), Expression.Constant("aaa")); + Assert.Equal(default(DateTime), Utils.GetDataReaderValue(typeof(DateTime), "aaa")); + + var exp1111 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime?), Expression.Constant(null)); + Assert.Null(Utils.GetDataReaderValue(typeof(DateTime?), null)); + var exp2222 = Utils.GetDataReaderValueBlockExpression(typeof(DateTime?), Expression.Constant("aaa")); + Assert.Null(Utils.GetDataReaderValue(typeof(DateTime?), "aaa")); + } + [Fact] public void DateTimeOffset2() { @@ -445,5 +473,33 @@ namespace FreeSql.ExpressionTree var exp2222 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset?), Expression.Constant("aaa")); Assert.Null(Utils.GetDataReaderValue(typeof(DateTimeOffset?), "aaa")); } + + [Fact] + public void DateTimeOffset2_By_DateTime() + { + var exp1 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset), Expression.Constant(DateTime.MinValue)); + Assert.Equal(DateTimeOffset.MinValue, Utils.GetDataReaderValue(typeof(DateTimeOffset), DateTime.MinValue)); + var exp2 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset), Expression.Constant(DateTime.MaxValue)); + Assert.Equal(DateTimeOffset.MaxValue, Utils.GetDataReaderValue(typeof(DateTimeOffset), DateTime.MaxValue)); + var exp3 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset), Expression.Constant("2000-1-1")); + Assert.Equal(DateTimeOffset.Parse("2000-1-1"), Utils.GetDataReaderValue(typeof(DateTimeOffset), "2000-1-1")); + + var exp11 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset?), Expression.Constant(DateTime.MinValue)); + Assert.Equal(DateTimeOffset.MinValue, Utils.GetDataReaderValue(typeof(DateTimeOffset?), DateTime.MinValue)); + var exp22 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset?), Expression.Constant(DateTime.MaxValue)); + Assert.Equal(DateTimeOffset.MaxValue, Utils.GetDataReaderValue(typeof(DateTimeOffset?), DateTime.MaxValue)); + var exp33 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset?), Expression.Constant("2000-1-1")); + Assert.Equal(DateTimeOffset.Parse("2000-1-1"), Utils.GetDataReaderValue(typeof(DateTimeOffset?), "2000-1-1")); + + var exp111 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset), Expression.Constant(null)); + Assert.Equal(default(DateTimeOffset), Utils.GetDataReaderValue(typeof(DateTimeOffset), null)); + var exp222 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset), Expression.Constant("aaa")); + Assert.Equal(default(DateTimeOffset), Utils.GetDataReaderValue(typeof(DateTimeOffset), "aaa")); + + var exp1111 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset?), Expression.Constant(null)); + Assert.Null(Utils.GetDataReaderValue(typeof(DateTimeOffset?), null)); + var exp2222 = Utils.GetDataReaderValueBlockExpression(typeof(DateTimeOffset?), Expression.Constant("aaa")); + Assert.Null(Utils.GetDataReaderValue(typeof(DateTimeOffset?), "aaa")); + } } } diff --git a/FreeSql.Tests/FreeSql.Tests/MySql/MapType/DateTimeOffSetTest.cs b/FreeSql.Tests/FreeSql.Tests/MySql/MapType/DateTimeOffSetTest.cs index 38fa8965..5f9e4c63 100644 --- a/FreeSql.Tests/FreeSql.Tests/MySql/MapType/DateTimeOffSetTest.cs +++ b/FreeSql.Tests/FreeSql.Tests/MySql/MapType/DateTimeOffSetTest.cs @@ -21,7 +21,7 @@ namespace FreeSql.Tests.MySqlMapType { //insert var orm = g.mysql; - var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now, dtosnullable_to_dt = DateTimeOffset.Now }; + var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now }; Assert.Equal(1, orm.Insert().AppendData(item).ExecuteAffrows()); var find = orm.Select().Where(a => a.id == item.id).First(); Assert.NotNull(find); @@ -38,6 +38,14 @@ namespace FreeSql.Tests.MySqlMapType Assert.Equal(item.dtos_to_dt.ToString("g"), find.dtos_to_dt.ToString("g")); Assert.Equal(item.dtosnullable_to_dt.Value.ToString("g"), find.dtosnullable_to_dt.Value.ToString("g")); + item.dtosnullable_to_dt = DateTimeOffset.Now; + Assert.Equal(1, orm.Update().SetSource(item).ExecuteAffrows()); + find = orm.Select().Where(a => a.id == item.id).First(); + Assert.NotNull(find); + Assert.Equal(item.id, find.id); + Assert.Equal(item.dtos_to_dt.ToString("g"), find.dtos_to_dt.ToString("g")); + Assert.Equal(item.dtosnullable_to_dt.Value.ToString("g"), find.dtosnullable_to_dt.Value.ToString("g")); + //update set Assert.Equal(1, orm.Update().Where(a => a.id == item.id).Set(a => a.dtos_to_dt, item.dtos_to_dt = DateTimeOffset.Now).ExecuteAffrows()); find = orm.Select().Where(a => a.id == item.id).First(); diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index e747b13d..76dd4aaf 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -1458,7 +1458,7 @@ namespace FreeSql.Internal static MethodInfo MethodToString = typeof(Utils).GetMethod("ToStringConcat", BindingFlags.NonPublic | BindingFlags.Static, null, new[] { typeof(object) }, null); static MethodInfo MethodBigIntegerParse = typeof(Utils).GetMethod("ToBigInteger", BindingFlags.NonPublic | BindingFlags.Static, null, new[] { typeof(string) }, null); static PropertyInfo PropertyDateTimeOffsetDateTime = typeof(DateTimeOffset).GetProperty("DateTime", BindingFlags.Instance | BindingFlags.Public); - static ConstructorInfo CtorDateTimeOffsetArgsDateTime = typeof(DateTimeOffset).GetConstructor(new[] { typeof(DateTime) }); + static ConstructorInfo CtorDateTimeOffsetArgsDateTime = typeof(DateTimeOffset).GetConstructor(new[] { typeof(DateTime), typeof(TimeSpan) }); public static ConcurrentBag> GetDataReaderValueBlockExpressionSwitchTypeFullName = new ConcurrentBag>(); public static Expression GetDataReaderValueBlockExpression(Type type, Expression value) @@ -1766,7 +1766,8 @@ namespace FreeSql.Internal Expression.Return(returnTarget, Expression.Convert(Expression.MakeMemberAccess(Expression.Convert(valueExp, typeof(DateTimeOffset)), PropertyDateTimeOffsetDateTime), typeof(object))), Expression.IfThenElse( Expression.AndAlso(Expression.Equal(Expression.Constant(type), Expression.Constant(typeof(DateTimeOffset))), Expression.TypeEqual(valueExp, typeof(DateTime))), - Expression.Return(returnTarget, Expression.Convert(Expression.New(CtorDateTimeOffsetArgsDateTime, Expression.Convert(valueExp, typeof(DateTime))), typeof(object))), + Expression.Return(returnTarget, Expression.Convert( + Expression.New(CtorDateTimeOffsetArgsDateTime, Expression.Convert(valueExp, typeof(DateTime)), Expression.Constant(TimeSpan.Zero)), typeof(object))), defaultRetExp ) )