Skip to content

Commit 330b84b

Browse files
committed
- feat(DateOnly): 支持到 Clickhouse;
1 parent 16c9358 commit 330b84b

File tree

6 files changed

+61
-131
lines changed

6 files changed

+61
-131
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/FreeSql.xml

Lines changed: 0 additions & 109 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Providers/FreeSql.Provider.ClickHouse/ClickHouseAdo/ClickHouseAdo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public override object AddslashesProcessParam(object param, Type mapType, Column
6363
else if (param is DateTime?)
6464
return AddslashesTypeHandler(typeof(DateTime?), param) ?? string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss"), "'");
6565

66+
#if net60
67+
else if (param is DateOnly || param is DateOnly?)
68+
return AddslashesTypeHandler(typeof(DateOnly), param) ?? string.Concat("'", ((DateOnly)param).ToString("yyyy-MM-dd"), "'");
69+
else if (param is TimeOnly || param is TimeOnly?)
70+
{
71+
var ts = (TimeOnly)param;
72+
return $"'{ts.Hour}:{ts.Minute}:{ts.Second}'";
73+
}
74+
#endif
75+
6676
else if (param is TimeSpan || param is TimeSpan?)
6777
{
6878
var ts = (TimeSpan)param;

Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,16 @@ public ClickHouseCodeFirst(IFreeSql orm, CommonUtils commonUtils, CommonExpressi
5151
{ typeof(double?).FullName, CsToDb.New(DbType.Double, "Float64", "Nullable(Float64)", false, true, null) },
5252
{ typeof(float).FullName, CsToDb.New(DbType.Single, "Float32", "Float32", false, false, 0) },
5353
{ typeof(float?).FullName, CsToDb.New(DbType.Single, "Float32", "Nullable(Float32)", false, true, null) },
54-
{
55-
typeof(decimal).FullName,
56-
CsToDb.New(DbType.Decimal, "Decimal(38, 19)", "Decimal(38, 19)", false, false,
57-
0) //Nullable(Decimal(38, 19))
58-
},
59-
{
60-
typeof(decimal?).FullName,
61-
CsToDb.New(DbType.Decimal, "Nullable(Decimal(38, 19))", "Nullable(Decimal(38, 19))", false, true, null)
62-
},
54+
{ typeof(decimal).FullName, CsToDb.New(DbType.Decimal, "Decimal(38, 19)", "Decimal(38, 19)", false, false, 0) }, //Nullable(Decimal(38, 19))
55+
{ typeof(decimal?).FullName, CsToDb.New(DbType.Decimal, "Nullable(Decimal(38, 19))", "Nullable(Decimal(38, 19))", false, true, null) },
6356

64-
{
65-
typeof(DateTime).FullName,
66-
CsToDb.New(DbType.DateTime, "DateTime('Asia/Shanghai')", "DateTime('Asia/Shanghai')", false, false,
67-
new DateTime(1970, 1, 1))
68-
},
69-
{
70-
typeof(DateTime?).FullName,
71-
CsToDb.New(DbType.DateTime, "DateTime('Asia/Shanghai')", "Nullable(DateTime('Asia/Shanghai'))", false,
72-
true, null)
73-
},
57+
{ typeof(DateTime).FullName, CsToDb.New(DbType.DateTime, "DateTime('Asia/Shanghai')", "DateTime('Asia/Shanghai')", false, false, new DateTime(1970, 1, 1)) },
58+
{ typeof(DateTime?).FullName, CsToDb.New(DbType.DateTime, "DateTime('Asia/Shanghai')", "Nullable(DateTime('Asia/Shanghai'))", false, true, null) },
59+
60+
#if net60
61+
{ typeof(TimeOnly).FullName, CsToDb.New(DbType.Time, "time", "time NOT NULL", false, false, TimeOnly.MinValue) },{ typeof(TimeOnly?).FullName, CsToDb.New(DbType.Time, "time", "time", false, true, null) },
62+
{ typeof(DateOnly).FullName, CsToDb.New(DbType.Date, "date", "date NOT NULL", false, false, new DateTime(1970,1,1)) },{ typeof(DateOnly?).FullName, CsToDb.New(DbType.Date, "date", "date", false, true, null) },
63+
#endif
7464

7565
{ typeof(string).FullName, CsToDb.New(DbType.String, "String", "String", false, null, "") },
7666
{ typeof(char).FullName, CsToDb.New(DbType.String, "String", "String", false, false, "") },

Providers/FreeSql.Provider.ClickHouse/ClickHouseProvider.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
using FreeSql.Internal;
1+
using FreeSql.ClickHouse.Curd;
2+
using FreeSql.Internal;
23
using FreeSql.Internal.CommonProvider;
3-
using FreeSql.ClickHouse.Curd;
44
using System;
5+
using System.Collections;
56
using System.Data.Common;
67
using System.Linq.Expressions;
8+
using System.Numerics;
79
using System.Threading;
810

911
namespace FreeSql.ClickHouse
1012
{
1113

1214
public class ClickHouseProvider<TMark> : BaseDbProvider, IFreeSql<TMark>
1315
{
16+
static int _firstInit = 1;
17+
static void InitInternal()
18+
{
19+
if (Interlocked.Exchange(ref _firstInit, 0) == 1) //不能放在 static ctor .NetFramework 可能报初始化类型错误
20+
{
21+
#if net60
22+
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(DateOnly)] = true;
23+
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(TimeOnly)] = true;
24+
#endif
25+
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(BigInteger)] = true;
26+
Utils.dicExecuteArrayRowReadClassOrTuple[typeof(BitArray)] = true;
27+
Select0Provider._dicMethodDataReaderGetValue[typeof(Guid)] = typeof(DbDataReader).GetMethod("GetGuid", new Type[] { typeof(int) });
28+
}
29+
}
30+
1431
public override ISelect<T1> CreateSelectProvider<T1>(object dywhere) => new ClickHouseSelect<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
1532
public override IInsert<T1> CreateInsertProvider<T1>() => new ClickHouseInsert<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression);
1633
public override IUpdate<T1> CreateUpdateProvider<T1>(object dywhere) => new ClickHouseUpdate<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);
@@ -21,6 +38,7 @@ public override IInsertOrUpdate<T1> CreateInsertOrUpdateProvider<T1>()
2138
}
2239
public ClickHouseProvider(string masterConnectionString, string[] slaveConnectionString, Func<DbConnection> connectionFactory = null)
2340
{
41+
InitInternal();
2442
this.InternalCommonUtils = new ClickHouseUtils(this);
2543
this.InternalCommonExpression = new ClickHouseExpression(this.InternalCommonUtils);
2644

Providers/FreeSql.Provider.ClickHouse/FreeSql.Provider.ClickHouse.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<LangVersion>9</LangVersion>
5-
<TargetFrameworks>netstandard2.1</TargetFrameworks>
5+
<TargetFrameworks>net10.0;net9.0;net8.0;net7.0;net6.0;netstandard2.1</TargetFrameworks>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>FreeSql;ncc;YeXiangQin;ChenBo</Authors>
88
<Description>FreeSql 数据库实现,基于 ClickHouse.Client Ado.net</Description>
@@ -36,5 +36,9 @@
3636
<PackageReference Include="ClickHouse.Driver" Version="0.7.20" />
3737
</ItemGroup>
3838

39+
<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0' or '$(TargetFramework)' == 'net9.0' or '$(TargetFramework)' == 'net8.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net6.0'">
40+
<DefineConstants>net60</DefineConstants>
41+
</PropertyGroup>
42+
3943
</Project>
4044

0 commit comments

Comments
 (0)