Skip to content

Commit

Permalink
- 修复 DateOnly/TimeOnly 映射问题;#1868 #1855 #1763 #939 #991
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Aug 20, 2024
1 parent 486015a commit 2334fe2
Show file tree
Hide file tree
Showing 36 changed files with 584 additions and 76 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<NoWarn>1701;1702;CS1573;CS8600;CS8601;CS8602;CS8603;CS8604;CS8605;CS8618;CS8619;CS8620;CS8622;CS8625;CS8629;CS8714;CS1591;CS0649;CA2200</NoWarn>
</PropertyGroup>

<!--
Expand Down
2 changes: 1 addition & 1 deletion FreeSql.DbContext/FreeSql.DbContext.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netstandard2.1;netcoreapp3.1;netstandard2.0;net45;net40</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
<Description>FreeSql is the ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, Firebird, Clickhouse, QuestDB, Odbc, Oledb, 达梦, 人大金仓, 南大通用, 虚谷, 神舟通用, 翰高, And Access</Description>
<Description>FreeSql is the ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, Firebird, Clickhouse, DuckDB, TDengine, QuestDB, Odbc, Oledb, 达梦, 人大金仓, 南大通用, 虚谷, 神舟通用, 翰高, And Access</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/DbContext</PackageProjectUrl>
<PackageTags>FreeSql ORM DbContext</PackageTags>
<RepositoryType>git</RepositoryType>
Expand Down
9 changes: 0 additions & 9 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion FreeSql.Repository/FreeSql.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netcoreapp3.1;netstandard2.1;netstandard2.0;net45;net40</TargetFrameworks>
<Authors>FreeSql;ncc;YeXiangQin</Authors>
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite/Firebird/Clickhouse/QuestDB/达梦/人大金仓/南大通用/虚谷/神舟通用/翰高/Access, and read/write separation、and split table.</Description>
<Description>FreeSql Implementation of General Repository, Support MySql/SqlServer/PostgreSQL/Oracle/Sqlite/Firebird/Clickhouse/DuckDB/TDengine/QuestDB/达梦/人大金仓/南大通用/虚谷/神舟通用/翰高/Access, and read/write separation、and split table.</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/Repository</PackageProjectUrl>
<PackageTags>FreeSql ORM Repository</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,68 @@ public class DuckdbCodeFirstTest
{
IFreeSql fsql => g.duckdb;


[Fact]
public void DateOnlyTimeOnly()
{
var item = new test_DateOnlyTimeOnly01 { testFieldDateOnly = DateOnly.FromDateTime(DateTime.Now) };
item.Id = (int)fsql.Insert(item).ExecuteIdentity();

var newitem = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item.Id).ToOne();

var now = DateTime.Parse("2024-8-20 23:00:11");
var item2 = new test_DateOnlyTimeOnly01
{
testFieldDateTime = now,
testFieldDateTimeNullable = now.AddDays(-1),
testFieldDateOnly = DateOnly.FromDateTime(now),
testFieldDateOnlyNullable = DateOnly.FromDateTime(now.AddDays(-1)),

testFieldTimeSpan = TimeSpan.FromHours(16),
testFieldTimeSpanNullable = TimeSpan.FromSeconds(90),
testFieldTimeOnly = TimeOnly.FromTimeSpan(TimeSpan.FromHours(11)),
testFieldTimeOnlyNullable = TimeOnly.FromTimeSpan(TimeSpan.FromSeconds(90)),
};

var sqlPar = fsql.Insert(item2).ToSql();
var sqlText = fsql.Insert(item2).NoneParameter().ToSql();
Assert.Equal(sqlText, "INSERT INTO \"test_dateonlytimeonly01\"(\"testfieldtimespan\", \"testfieldtimeonly\", \"testfielddatetime\", \"testfielddateonly\", \"testfieldtimespannullable\", \"testfieldtimeonlynullable\", \"testfielddatetimenullable\", \"testfielddateonlynullable\") VALUES(time '16:0:0.0', time '11:0:0', current_timestamp, date '2024-08-20', time '0:1:30.0', time '0:1:30', current_timestamp, date '2024-08-19')");
item2.Id = (int)fsql.Insert(item2).NoneParameter().ExecuteIdentity();
var item3NP = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item2.Id).ToOne();
Assert.Equal(item3NP.testFieldDateOnly, item2.testFieldDateOnly);
Assert.Equal(item3NP.testFieldDateOnlyNullable, item2.testFieldDateOnlyNullable);
Assert.True(Math.Abs((item3NP.testFieldTimeOnly - item2.testFieldTimeOnly).TotalSeconds) < 1);
Assert.True(Math.Abs((item3NP.testFieldTimeOnlyNullable - item2.testFieldTimeOnlyNullable).Value.TotalSeconds) < 1);

item2.Id = (int)fsql.Insert(item2).ExecuteIdentity();
item3NP = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item2.Id).ToOne();
Assert.Equal(item3NP.testFieldDateOnly, item2.testFieldDateOnly);
Assert.Equal(item3NP.testFieldDateOnlyNullable, item2.testFieldDateOnlyNullable);
Assert.True(Math.Abs((item3NP.testFieldTimeOnly - item2.testFieldTimeOnly).TotalSeconds) < 1);
Assert.True(Math.Abs((item3NP.testFieldTimeOnlyNullable - item2.testFieldTimeOnlyNullable).Value.TotalSeconds) < 1);

var items = fsql.Select<test_DateOnlyTimeOnly01>().ToList();
var itemstb = fsql.Select<test_DateOnlyTimeOnly01>().ToDataTable();
}
class test_DateOnlyTimeOnly01
{
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public TimeSpan testFieldTimeSpan { get; set; }
public TimeOnly testFieldTimeOnly { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime testFieldDateTime { get; set; }
public DateOnly testFieldDateOnly { get; set; }

public TimeSpan? testFieldTimeSpanNullable { get; set; }
public TimeOnly? testFieldTimeOnlyNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? testFieldDateTimeNullable { get; set; }
public DateOnly? testFieldDateOnlyNullable { get; set; }
}

[Fact]
public void UInt256Crud2()
{
Expand Down
63 changes: 63 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/KingbaseES/KingbaseESCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,69 @@ namespace FreeSql.Tests.KingbaseES
public class KingbaseESCodeFirstTest
{

[Fact]
public void DateOnlyTimeOnly()
{
var fsql = g.kingbaseES;

var item = new test_DateOnlyTimeOnly01 { testFieldDateOnly = DateOnly.FromDateTime(DateTime.Now) };
item.Id = (int)fsql.Insert(item).ExecuteIdentity();

var newitem = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item.Id).ToOne();

var now = DateTime.Parse("2024-8-20 23:00:11");
var item2 = new test_DateOnlyTimeOnly01
{
testFieldDateTime = now,
testFieldDateTimeNullable = now.AddDays(-1),
testFieldDateOnly = DateOnly.FromDateTime(now),
testFieldDateOnlyNullable = DateOnly.FromDateTime(now.AddDays(-1)),

testFieldTimeSpan = TimeSpan.FromHours(16),
testFieldTimeSpanNullable = TimeSpan.FromSeconds(90),
testFieldTimeOnly = TimeOnly.FromTimeSpan(TimeSpan.FromHours(11)),
testFieldTimeOnlyNullable = TimeOnly.FromTimeSpan(TimeSpan.FromSeconds(90)),
};

var sqlPar = fsql.Insert(item2).ToSql();
var sqlText = fsql.Insert(item2).NoneParameter().ToSql();
Assert.Equal(sqlText, "INSERT INTO \"test_dateonlytimeonly01\"(\"testfieldtimespan\", \"testfieldtimeonly\", \"testfielddatetime\", \"testfielddateonly\", \"testfieldtimespannullable\", \"testfieldtimeonlynullable\", \"testfielddatetimenullable\", \"testfielddateonlynullable\") VALUES('16:0:0', '11:0:0', current_timestamp, '2024-08-20', '0:1:30', '0:1:30', current_timestamp, '2024-08-19')");
item2.Id = (int)fsql.Insert(item2).NoneParameter().ExecuteIdentity();
var item3NP = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item2.Id).ToOne();
Assert.Equal(item3NP.testFieldDateOnly, item2.testFieldDateOnly);
Assert.Equal(item3NP.testFieldDateOnlyNullable, item2.testFieldDateOnlyNullable);
Assert.True(Math.Abs((item3NP.testFieldTimeOnly - item2.testFieldTimeOnly).TotalSeconds) < 1);
Assert.True(Math.Abs((item3NP.testFieldTimeOnlyNullable - item2.testFieldTimeOnlyNullable).Value.TotalSeconds) < 1);

item2.Id = (int)fsql.Insert(item2).ExecuteIdentity();
item3NP = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item2.Id).ToOne();
Assert.Equal(item3NP.testFieldDateOnly, item2.testFieldDateOnly);
Assert.Equal(item3NP.testFieldDateOnlyNullable, item2.testFieldDateOnlyNullable);
Assert.True(Math.Abs((item3NP.testFieldTimeOnly - item2.testFieldTimeOnly).TotalSeconds) < 1);
Assert.True(Math.Abs((item3NP.testFieldTimeOnlyNullable - item2.testFieldTimeOnlyNullable).Value.TotalSeconds) < 1);

var items = fsql.Select<test_DateOnlyTimeOnly01>().ToList();
var itemstb = fsql.Select<test_DateOnlyTimeOnly01>().ToDataTable();
}
class test_DateOnlyTimeOnly01
{
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public TimeSpan testFieldTimeSpan { get; set; }
public TimeOnly testFieldTimeOnly { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime testFieldDateTime { get; set; }
public DateOnly testFieldDateOnly { get; set; }

public TimeSpan? testFieldTimeSpanNullable { get; set; }
public TimeOnly? testFieldTimeOnlyNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? testFieldDateTimeNullable { get; set; }
public DateOnly? testFieldDateOnlyNullable { get; set; }
}

[Fact]
public void Test_0String()
{
Expand Down
64 changes: 64 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,70 @@ namespace FreeSql.Tests.MySql
{
public class MySqlCodeFirstTest
{

[Fact]
public void DateOnlyTimeOnly()
{
var fsql = g.mysql;

var item = new test_DateOnlyTimeOnly01 { testFieldDateOnly = DateOnly.FromDateTime(DateTime.Now) };
item.Id = (int)fsql.Insert(item).ExecuteIdentity();

var newitem = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item.Id).ToOne();

var now = DateTime.Parse("2024-8-20 23:00:11");
var item2 = new test_DateOnlyTimeOnly01
{
testFieldDateTime = now,
testFieldDateTimeNullable = now.AddDays(-1),
testFieldDateOnly = DateOnly.FromDateTime(now),
testFieldDateOnlyNullable = DateOnly.FromDateTime(now.AddDays(-1)),

testFieldTimeSpan = TimeSpan.FromHours(16),
testFieldTimeSpanNullable = TimeSpan.FromSeconds(90),
testFieldTimeOnly = TimeOnly.FromTimeSpan(TimeSpan.FromHours(11)),
testFieldTimeOnlyNullable = TimeOnly.FromTimeSpan(TimeSpan.FromSeconds(90)),
};

var sqlPar = fsql.Insert(item2).ToSql();
var sqlText = fsql.Insert(item2).NoneParameter().ToSql();
Assert.Equal(sqlText, "INSERT INTO `test_DateOnlyTimeOnly01`(`testFieldTimeSpan`, `testFieldTimeOnly`, `testFieldDateTime`, `testFieldDateOnly`, `testFieldTimeSpanNullable`, `testFieldTimeOnlyNullable`, `testFieldDateTimeNullable`, `testFieldDateOnlyNullable`) VALUES('16:0:0', '11:0:0', now(3), '2024-08-20', '0:1:30', '0:1:30', now(3), '2024-08-19')");
item2.Id = (int)fsql.Insert(item2).NoneParameter().ExecuteIdentity();
var item3NP = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item2.Id).ToOne();
Assert.Equal(item3NP.testFieldDateOnly, item2.testFieldDateOnly);
Assert.Equal(item3NP.testFieldDateOnlyNullable, item2.testFieldDateOnlyNullable);
Assert.True(Math.Abs((item3NP.testFieldTimeOnly - item2.testFieldTimeOnly).TotalSeconds) < 1);
Assert.True(Math.Abs((item3NP.testFieldTimeOnlyNullable - item2.testFieldTimeOnlyNullable).Value.TotalSeconds) < 1);

item2.Id = (int)fsql.Insert(item2).ExecuteIdentity();
item3NP = fsql.Select<test_DateOnlyTimeOnly01>().Where(a => a.Id == item2.Id).ToOne();
Assert.Equal(item3NP.testFieldDateOnly, item2.testFieldDateOnly);
Assert.Equal(item3NP.testFieldDateOnlyNullable, item2.testFieldDateOnlyNullable);
Assert.True(Math.Abs((item3NP.testFieldTimeOnly - item2.testFieldTimeOnly).TotalSeconds) < 1);
Assert.True(Math.Abs((item3NP.testFieldTimeOnlyNullable - item2.testFieldTimeOnlyNullable).Value.TotalSeconds) < 1);

var items = fsql.Select<test_DateOnlyTimeOnly01>().ToList();
var itemstb = fsql.Select<test_DateOnlyTimeOnly01>().ToDataTable();
}
class test_DateOnlyTimeOnly01
{
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public TimeSpan testFieldTimeSpan { get; set; }
public TimeOnly testFieldTimeOnly { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime testFieldDateTime { get; set; }
public DateOnly testFieldDateOnly { get; set; }

public TimeSpan? testFieldTimeSpanNullable { get; set; }
public TimeOnly? testFieldTimeOnlyNullable { get; set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime? testFieldDateTimeNullable { get; set; }
public DateOnly? testFieldDateOnlyNullable { get; set; }
}

[Fact]
public void Test_Bool01()
{
Expand Down
Loading

0 comments on commit 2334fe2

Please sign in to comment.