Skip to content

Commit 2754fe2

Browse files
authored
Merge pull request #1815 from d4ilys/master
修复ClickHouse 批量更新DateTime问题,增加ClickHouse表分区功能(Preview)
2 parents bffc1ac + a53cbdd commit 2754fe2

File tree

7 files changed

+561
-25
lines changed

7 files changed

+561
-25
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using FreeSql.DataAnnotations;
7+
using FreeSql.Provider.ClickHouse.Attributes;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace FreeSql.Tests.ClickHouse
12+
{
13+
public class ClickhouseIssueTest
14+
{
15+
private readonly ITestOutputHelper _output;
16+
17+
private static IFreeSql _fsql;
18+
19+
public ClickhouseIssueTest(ITestOutputHelper output)
20+
{
21+
_output = output;
22+
23+
_fsql = new FreeSqlBuilder().UseConnectionString(DataType.ClickHouse,
24+
"Host=192.168.1.123;Port=8123;Database=test_issue;Compress=True;Min Pool Size=1")
25+
.UseMonitorCommand(cmd => _output.WriteLine($"线程:{cmd.CommandText}\r\n"))
26+
.UseNoneCommandParameter(true)
27+
.UseAdoConnectionPool(true)
28+
.Build();
29+
}
30+
31+
#region https: //github.com/dotnetcore/FreeSql/issues/1813
32+
33+
[Fact]
34+
public void TestIssue1813()
35+
{
36+
//普通修改
37+
_fsql.Update<Person>()
38+
.Set(p => p.Name == "update_name")
39+
.Set(p => p.UpdateTime == DateTime.Now)
40+
.Where(p => p.Id == "25e8d92e-29f2-43ff-b861-9ade0eec4041")
41+
.ExecuteAffrows();
42+
43+
//批量修改
44+
var updatePerson = new List<Person>();
45+
updatePerson.Add(new Person
46+
{
47+
Id = "9cd7af52-85cc-4d26-898a-4020cadb0491",
48+
Name = "update_name1",
49+
UpdateTime = DateTime.Now,
50+
CreateTime = DateTime.Parse("2024-05-30 10:01:02")
51+
});
52+
53+
updatePerson.Add(new Person
54+
{
55+
Id = "bd9f9ed6-bd03-4675-abb4-12b7fdac7678",
56+
Name = "update_name2",
57+
UpdateTime = DateTime.Now,
58+
CreateTime = DateTime.Parse("2024-05-30 10:01:02")
59+
});
60+
61+
var sql = _fsql.Update<Person>().SetSource(updatePerson)
62+
.UpdateColumns(person => new
63+
{
64+
person.Name,
65+
person.UpdateTime,
66+
person.CreateTime
67+
}).ToSql();
68+
}
69+
70+
[Fact]
71+
public void TestIssue1813CodeFirst()
72+
{
73+
_fsql.CodeFirst.SyncStructure<Person>();
74+
var insertSingle = _fsql.Insert(new Person
75+
{
76+
Name = $"test{DateTime.Now.Millisecond}",
77+
Age = 18,
78+
CreateTime = DateTime.Now
79+
}).ExecuteAffrows();
80+
81+
_output.WriteLine(insertSingle.ToString());
82+
83+
var persons = new List<Person>
84+
{
85+
new Person
86+
{
87+
Name = $"test2{DateTime.Now.Millisecond}",
88+
Age = 20,
89+
CreateTime = DateTime.Now
90+
},
91+
new Person
92+
{
93+
Name = "test3" + 286,
94+
Age = 22,
95+
CreateTime = DateTime.Now
96+
}
97+
};
98+
99+
var insertMany = _fsql.Insert(persons).ExecuteAffrows();
100+
}
101+
102+
[Fact]
103+
public void TestIssue1813CodeFirst2()
104+
{
105+
_fsql.CodeFirst.SyncStructure<Person>();
106+
var insertSingle = _fsql.Insert(new Person
107+
{
108+
Id = Guid.NewGuid().ToString(),
109+
Name = $"test{DateTime.Now.Millisecond}",
110+
Age = 18,
111+
CreateTime = DateTime.Now
112+
}).ExecuteAffrows();
113+
114+
_output.WriteLine(insertSingle.ToString());
115+
116+
var persons = new List<Person>
117+
{
118+
new Person
119+
{
120+
Id = Guid.NewGuid().ToString(),
121+
Name = $"test2{DateTime.Now.Millisecond}",
122+
Age = 20,
123+
CreateTime = DateTime.Now
124+
},
125+
new Person
126+
{
127+
Id = Guid.NewGuid().ToString(),
128+
Name = "test3" + 286,
129+
Age = 22,
130+
CreateTime = DateTime.Now
131+
}
132+
};
133+
134+
var insertMany = _fsql.Insert(persons).ExecuteAffrows();
135+
}
136+
137+
138+
public class Person
139+
{
140+
[Column(IsPrimary = true, IsIdentity = true)]
141+
public string Id { get; set; }
142+
143+
public string Name { get; set; }
144+
public int Age { get; set; }
145+
146+
public DateTime CreateTime { get; set; }
147+
148+
public DateTime? UpdateTime { get; set; }
149+
}
150+
151+
#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
181+
}
182+
}

0 commit comments

Comments
 (0)