Skip to content

Commit a53cbdd

Browse files
committed
ClickHouse表分区支持
1 parent 989cf29 commit a53cbdd

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickhouseIssueTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using FreeSql.DataAnnotations;
7+
using FreeSql.Provider.ClickHouse.Attributes;
78
using Xunit;
89
using Xunit.Abstractions;
910

@@ -141,10 +142,41 @@ public class Person
141142

142143
public string Name { get; set; }
143144
public int Age { get; set; }
145+
144146
public DateTime CreateTime { get; set; }
147+
145148
public DateTime? UpdateTime { get; set; }
146149
}
147150

148151
#endregion
152+
153+
#region https: //github.com/dotnetcore/FreeSql/issues/1814
154+
155+
public class Test1814Table
156+
{
157+
[Column(IsPrimary = true, IsIdentity = true)]
158+
public int Id { get; set; }
159+
160+
public string Name { get; set; }
161+
162+
[ClickHousePartition]
163+
[Column(Name = "create_time")]
164+
public DateTime CreateTime { get; set; }
165+
}
166+
167+
[Fact]
168+
public void TestIssue1814()
169+
{
170+
_fsql.CodeFirst.SyncStructure<Test1814Table>();
171+
172+
var insert = _fsql.Insert(new Test1814Table
173+
{
174+
Name = "test",
175+
CreateTime = DateTime.Now
176+
}).ExecuteAffrows();
177+
178+
var query = _fsql.Select<Test1814Table>().ToList();
179+
}
180+
#endregion
149181
}
150182
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace FreeSql.Provider.ClickHouse.Attributes
6+
{
7+
[AttributeUsage(AttributeTargets.Property)]
8+
public class ClickHousePartitionAttribute : Attribute
9+
{
10+
public ClickHousePartitionAttribute(string format = "toYYYYMM({0})")
11+
{
12+
Format = format;
13+
}
14+
15+
public string Format { get; set; }
16+
}
17+
}

Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
using System.Text;
99
using System.Text.RegularExpressions;
1010
using System.Data.Common;
11+
using System.Reflection;
1112
using FreeSql.Internal.ObjectPool;
1213
using ClickHouse.Client.ADO;
14+
using FreeSql.Provider.ClickHouse.Attributes;
1315

1416
namespace FreeSql.ClickHouse
1517
{
@@ -95,10 +97,11 @@ public override DbInfoResult GetDbInfo(Type type)
9597
{
9698
var arrayDbType = $"Array({value.dbtype})";
9799
var defaultArray = new ArrayList(0);
98-
return new DbInfoResult(Convert.ToInt32(DbType.Object), arrayDbType, arrayDbType, false,defaultArray);
100+
return new DbInfoResult(Convert.ToInt32(DbType.Object), arrayDbType, arrayDbType, false,
101+
defaultArray);
99102
}
100-
101103
}
104+
102105
return null;
103106
}
104107

@@ -132,7 +135,7 @@ private Tuple<bool, Type> IsCollection(Type type)
132135
if (interfaces.Any(t => t.Name == "IEnumerable"))
133136
flag = true;
134137

135-
if (type.Name == "Array")
138+
if (type.Name == "Array")
136139
{
137140
flag = true;
138141
resultType = typeof(string);
@@ -173,7 +176,8 @@ protected override string GetComparisonDDLStatements(params TypeSchemaAndName[]
173176
if (tb == null)
174177
throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
175178
if (tb.Columns.Any() == false)
176-
throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
179+
throw new Exception(
180+
CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
177181
var tbname = _commonUtils.SplitTableName(tb.DbName);
178182
if (tbname?.Length == 1)
179183
tbname = new[] { database, tbname[0] };
@@ -258,7 +262,8 @@ protected override string GetComparisonDDLStatements(params TypeSchemaAndName[]
258262

259263
if (tb.Primarys.Any())
260264
{
261-
var primaryKeys = string.Join(",", tb.Primarys.Select(p => _commonUtils.QuoteSqlName(p.Attribute.Name)));
265+
var primaryKeys = string.Join(",",
266+
tb.Primarys.Select(p => _commonUtils.QuoteSqlName(p.Attribute.Name)));
262267
sb.Append(" \r\n PRIMARY KEY ");
263268
sb.Append($"( {primaryKeys} ) ");
264269
}
@@ -268,14 +273,32 @@ protected override string GetComparisonDDLStatements(params TypeSchemaAndName[]
268273

269274
if (tb.Primarys.Any())
270275
{
271-
var primaryKeys = string.Join(",", tb.Primarys.Select(p => _commonUtils.QuoteSqlName(p.Attribute.Name)));
276+
var primaryKeys = string.Join(",",
277+
tb.Primarys.Select(p => _commonUtils.QuoteSqlName(p.Attribute.Name)));
272278
sb.Append(" \r\nORDER BY ");
273279
sb.Append($"( {primaryKeys} ) ");
274280
}
275281

282+
//查找属性是否标记了分区特性
283+
var partitionColumnInfos = tb.Properties.Where(
284+
c => c.Value.GetCustomAttribute<ClickHousePartitionAttribute>() != null).ToList();
285+
286+
if (partitionColumnInfos != null && partitionColumnInfos.Any())
287+
{
288+
var partitionProperty = partitionColumnInfos.First();
289+
290+
var partitionColumnInfo = tb.Columns.FirstOrDefault(pair =>
291+
pair.Value.CsName == partitionProperty.Value.Name);
292+
var partitionName = _commonUtils.QuoteSqlName(partitionColumnInfo.Value.Attribute.Name);
293+
var partitionAttribute = partitionProperty.Value
294+
.GetCustomAttribute<ClickHousePartitionAttribute>();
295+
sb.Append($" \r\nPARTITION BY {string.Format(partitionAttribute.Format, partitionName)}");
296+
}
297+
298+
276299
//if (string.IsNullOrEmpty(tb.Comment) == false)
277300
// sb.Append(" Comment=").Append(_commonUtils.FormatSql("{0}", tb.Comment));
278-
sb.Append(" SETTINGS index_granularity = 8192;\r\n");
301+
sb.Append(" \r\nSETTINGS index_granularity = 8192;\r\n");
279302
continue;
280303
}
281304

@@ -477,7 +500,8 @@ from system.columns a
477500

478501
if (tb.Primarys.Any())
479502
{
480-
var primaryKeys = string.Join(",", tb.Primarys.Select(p => _commonUtils.QuoteSqlName(p.Attribute.Name)));
503+
var primaryKeys = string.Join(",",
504+
tb.Primarys.Select(p => _commonUtils.QuoteSqlName(p.Attribute.Name)));
481505
sb.Append(" \r\nORDER BY ( ");
482506
sb.Append(primaryKeys);
483507
sb.Append(" )");

0 commit comments

Comments
 (0)