- 优化 MapType DateTime/DateTimeOffset 类型转换互通;

This commit is contained in:
28810 2019-09-06 16:07:28 +08:00
parent d105041858
commit d9cb932fae
7 changed files with 336 additions and 2 deletions

View File

@ -0,0 +1,54 @@
using FreeSql.DataAnnotations;
using System;
using System.Numerics;
using Xunit;
namespace FreeSql.Tests.MySqlConnectorMapType
{
public class DateTimeOffSetTest
{
class DateTimeOffSetTestMap
{
public Guid id { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset dtos_to_dt { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset? dtosnullable_to_dt { get; set; }
}
[Fact]
public void DateTimeToDateTimeOffSet()
{
//insert
var orm = g.mysql;
var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now, dtosnullable_to_dt = DateTimeOffset.Now };
Assert.Equal(1, orm.Insert<DateTimeOffSetTestMap>().AppendData(item).ExecuteAffrows());
var find = orm.Select<DateTimeOffSetTestMap>().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 all
item.dtos_to_dt = DateTimeOffset.Now;
Assert.Equal(1, orm.Update<DateTimeOffSetTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().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<DateTimeOffSetTestMap>().Where(a => a.id == item.id).Set(a => a.dtos_to_dt, item.dtos_to_dt = DateTimeOffset.Now).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
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"));
//delete
Assert.Equal(1, orm.Delete<DateTimeOffSetTestMap>().Where(a => a.id == item.id).ExecuteAffrows());
Assert.Null(orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First());
}
}
}

View File

@ -0,0 +1,54 @@
using FreeSql.DataAnnotations;
using System;
using System.Numerics;
using Xunit;
namespace FreeSql.Tests.MySqlMapType
{
public class DateTimeOffSetTest
{
class DateTimeOffSetTestMap
{
public Guid id { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset dtos_to_dt { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset? dtosnullable_to_dt { get; set; }
}
[Fact]
public void DateTimeToDateTimeOffSet()
{
//insert
var orm = g.mysql;
var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now, dtosnullable_to_dt = DateTimeOffset.Now };
Assert.Equal(1, orm.Insert<DateTimeOffSetTestMap>().AppendData(item).ExecuteAffrows());
var find = orm.Select<DateTimeOffSetTestMap>().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 all
item.dtos_to_dt = DateTimeOffset.Now;
Assert.Equal(1, orm.Update<DateTimeOffSetTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().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<DateTimeOffSetTestMap>().Where(a => a.id == item.id).Set(a => a.dtos_to_dt, item.dtos_to_dt = DateTimeOffset.Now).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
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"));
//delete
Assert.Equal(1, orm.Delete<DateTimeOffSetTestMap>().Where(a => a.id == item.id).ExecuteAffrows());
Assert.Null(orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First());
}
}
}

View File

@ -0,0 +1,54 @@
using FreeSql.DataAnnotations;
using System;
using System.Numerics;
using Xunit;
namespace FreeSql.Tests.OracleMapType
{
public class DateTimeOffSetTest
{
class Dtos_dt
{
public Guid id { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset dtos_to_dt { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset? dtofnil_to_dt { get; set; }
}
[Fact]
public void DateTimeToDateTimeOffSet()
{
//insert
var orm = g.oracle;
var item = new Dtos_dt { dtos_to_dt = DateTimeOffset.Now, dtofnil_to_dt = DateTimeOffset.Now };
Assert.Equal(1, orm.Insert<Dtos_dt>().AppendData(item).ExecuteAffrows());
var find = orm.Select<Dtos_dt>().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.dtofnil_to_dt.Value.ToString("g"), find.dtofnil_to_dt.Value.ToString("g"));
//update all
item.dtos_to_dt = DateTimeOffset.Now;
Assert.Equal(1, orm.Update<Dtos_dt>().SetSource(item).ExecuteAffrows());
find = orm.Select<Dtos_dt>().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.dtofnil_to_dt.Value.ToString("g"), find.dtofnil_to_dt.Value.ToString("g"));
//update set
Assert.Equal(1, orm.Update<Dtos_dt>().Where(a => a.id == item.id).Set(a => a.dtos_to_dt, item.dtos_to_dt = DateTimeOffset.Now).ExecuteAffrows());
find = orm.Select<Dtos_dt>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
Assert.Equal(item.dtos_to_dt.ToString("g"), find.dtos_to_dt.ToString("g"));
Assert.Equal(item.dtofnil_to_dt.Value.ToString("g"), find.dtofnil_to_dt.Value.ToString("g"));
//delete
Assert.Equal(1, orm.Delete<Dtos_dt>().Where(a => a.id == item.id).ExecuteAffrows());
Assert.Null(orm.Select<Dtos_dt>().Where(a => a.id == item.id).First());
}
}
}

View File

@ -0,0 +1,54 @@
using FreeSql.DataAnnotations;
using System;
using System.Numerics;
using Xunit;
namespace FreeSql.Tests.PostgreSQLMapType
{
public class DateTimeOffSetTest
{
class DateTimeOffSetTestMap
{
public Guid id { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset dtos_to_dt { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset? dtosnullable_to_dt { get; set; }
}
[Fact]
public void DateTimeToDateTimeOffSet()
{
//insert
var orm = g.pgsql;
var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now, dtosnullable_to_dt = DateTimeOffset.Now };
Assert.Equal(1, orm.Insert<DateTimeOffSetTestMap>().AppendData(item).ExecuteAffrows());
var find = orm.Select<DateTimeOffSetTestMap>().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 all
item.dtos_to_dt = DateTimeOffset.Now;
Assert.Equal(1, orm.Update<DateTimeOffSetTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().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<DateTimeOffSetTestMap>().Where(a => a.id == item.id).Set(a => a.dtos_to_dt, item.dtos_to_dt = DateTimeOffset.Now).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
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"));
//delete
Assert.Equal(1, orm.Delete<DateTimeOffSetTestMap>().Where(a => a.id == item.id).ExecuteAffrows());
Assert.Null(orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First());
}
}
}

View File

@ -0,0 +1,54 @@
using FreeSql.DataAnnotations;
using System;
using System.Numerics;
using Xunit;
namespace FreeSql.Tests.SqlServerMapType
{
public class DateTimeOffSetTest
{
class DateTimeOffSetTestMap
{
public Guid id { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset dtos_to_dt { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset? dtosnullable_to_dt { get; set; }
}
[Fact]
public void DateTimeToDateTimeOffSet()
{
//insert
var orm = g.sqlserver;
var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now, dtosnullable_to_dt = DateTimeOffset.Now };
Assert.Equal(1, orm.Insert<DateTimeOffSetTestMap>().AppendData(item).ExecuteAffrows());
var find = orm.Select<DateTimeOffSetTestMap>().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 all
item.dtos_to_dt = DateTimeOffset.Now;
Assert.Equal(1, orm.Update<DateTimeOffSetTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().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<DateTimeOffSetTestMap>().Where(a => a.id == item.id).Set(a => a.dtos_to_dt, item.dtos_to_dt = DateTimeOffset.Now).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
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"));
//delete
Assert.Equal(1, orm.Delete<DateTimeOffSetTestMap>().Where(a => a.id == item.id).ExecuteAffrows());
Assert.Null(orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First());
}
}
}

View File

@ -0,0 +1,54 @@
using FreeSql.DataAnnotations;
using System;
using System.Numerics;
using Xunit;
namespace FreeSql.Tests.SqliteMapType
{
public class DateTimeOffSetTest
{
class DateTimeOffSetTestMap
{
public Guid id { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset dtos_to_dt { get; set; }
[Column(MapType = typeof(DateTime))]
public DateTimeOffset? dtosnullable_to_dt { get; set; }
}
[Fact]
public void DateTimeToDateTimeOffSet()
{
//insert
var orm = g.sqlite;
var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now, dtosnullable_to_dt = DateTimeOffset.Now };
Assert.Equal(1, orm.Insert<DateTimeOffSetTestMap>().AppendData(item).ExecuteAffrows());
var find = orm.Select<DateTimeOffSetTestMap>().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 all
item.dtos_to_dt = DateTimeOffset.Now;
Assert.Equal(1, orm.Update<DateTimeOffSetTestMap>().SetSource(item).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().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<DateTimeOffSetTestMap>().Where(a => a.id == item.id).Set(a => a.dtos_to_dt, item.dtos_to_dt = DateTimeOffset.Now).ExecuteAffrows());
find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First();
Assert.NotNull(find);
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"));
//delete
Assert.Equal(1, orm.Delete<DateTimeOffSetTestMap>().Where(a => a.id == item.id).ExecuteAffrows());
Assert.Null(orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First());
}
}
}

View File

@ -1457,6 +1457,8 @@ namespace FreeSql.Internal
static MethodInfo MethodDateTimeOffsetTryParse = typeof(DateTimeOffset).GetMethod("TryParse", new[] { typeof(string), typeof(DateTimeOffset).MakeByRefType() });
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) });
public static ConcurrentBag<Func<LabelTarget, Expression, string, Expression>> GetDataReaderValueBlockExpressionSwitchTypeFullName = new ConcurrentBag<Func<LabelTarget, Expression, string, Expression>>();
public static Expression GetDataReaderValueBlockExpression(Type type, Expression value)
@ -1759,7 +1761,15 @@ namespace FreeSql.Internal
Expression.IfThenElse(
Expression.TypeEqual(valueExp, typeof(string)),
switchExp,
defaultRetExp
Expression.IfThenElse(
Expression.AndAlso(Expression.Equal(Expression.Constant(type), Expression.Constant(typeof(DateTime))), Expression.TypeEqual(valueExp, typeof(DateTimeOffset))),
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))),
defaultRetExp
)
)
)
);
};