Skip to content

Commit 9aef33e

Browse files
committed
解决ClickHouse 批量更新DateTime问题
1 parent 54e1ed6 commit 9aef33e

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ClickhouseIssueTest(ITestOutputHelper output)
2222
_fsql = new FreeSqlBuilder().UseConnectionString(DataType.ClickHouse,
2323
"Host=192.168.1.123;Port=8123;Database=test_issue;Compress=True;Min Pool Size=1")
2424
.UseMonitorCommand(cmd => _output.WriteLine($"线程:{cmd.CommandText}\r\n"))
25-
.UseNoneCommandParameter(true)
25+
.UseNoneCommandParameter(false)
2626
.UseAdoConnectionPool(true)
2727
.Build();
2828
}
@@ -32,25 +32,35 @@ public ClickhouseIssueTest(ITestOutputHelper output)
3232
[Fact]
3333
public void TestIssue1813()
3434
{
35-
//var personsUpdate = new List<Person>
36-
//{
37-
// new Person
38-
// {
39-
// Id = 1,
40-
// Name = $"test2{DateTime.Now.Millisecond}",
41-
// Age = 20,
42-
// CreateTime = DateTime.Now
43-
// },
44-
// new Person
45-
// {
46-
// Id = 2,
47-
// Name = "test3"+ 286,
48-
// Age = 22,
49-
// CreateTime = DateTime.Now
50-
// }
51-
//};
52-
53-
//_fsql.Update<Person>().SetSource()
35+
//普通修改
36+
_fsql.Update<Person>()
37+
.Set(p => p.Name == "update_name")
38+
.Set(p => p.UpdateTime == DateTime.Now)
39+
.Where(p => p.Id == "25e8d92e-29f2-43ff-b861-9ade0eec4041")
40+
.ExecuteAffrows();
41+
42+
//批量修改
43+
var updatePerson = new List<Person>();
44+
updatePerson.Add(new Person
45+
{
46+
Id = "9cd7af52-85cc-4d26-898a-4020cadb0491",
47+
Name = "update_name1",
48+
UpdateTime = DateTime.Now
49+
});
50+
51+
updatePerson.Add(new Person
52+
{
53+
Id = "bd9f9ed6-bd03-4675-abb4-12b7fdac7678",
54+
Name = "update_name2",
55+
UpdateTime = DateTime.Now
56+
});
57+
58+
_fsql.Update<Person>().SetSource(updatePerson).UpdateColumns(person => new
59+
{
60+
person.Name,
61+
person.UpdateTime,
62+
}).ExecuteAffrows();
63+
5464
}
5565

5666
[Fact]

Providers/FreeSql.Provider.ClickHouse/ClickHouseUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public override string GetNoneParamaterSqlValue(List<DbParameter> specialParams,
158158
var ts = (TimeSpan)value;
159159
value = $"{Math.Floor(ts.TotalHours)}:{ts.Minutes}:{ts.Seconds}";
160160
}
161-
else if (value is Array)
161+
else if (value is Array)
162162
{
163163
var valueArr = value as Array;
164164
var eleType = type.GetElementType();

Providers/FreeSql.Provider.ClickHouse/Curd/ClickHouseUpdate.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using DateTime = System.DateTime;
1011

1112
namespace FreeSql.ClickHouse.Curd
1213
{
@@ -167,6 +168,16 @@ public override void ToSqlExtension110(StringBuilder sb, bool isAsTableSplited)
167168

168169
var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
169170
_commonUtils.QuoteWriteParamterAdapter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
171+
172+
//判断是否是DateTime类型,如果是DateTime类型,需要转换成ClickHouse支持的时间格式
173+
if (col.Attribute.MapType == typeof(DateTime) || col.Attribute.MapType == typeof(DateTime?) )
174+
{
175+
//获取当前实时区
176+
var timeZone = TimeZoneInfo.Local;
177+
178+
colsql = $"toDateTime({colsql},'Asia/Shanghai')";
179+
}
180+
170181
cwsb.Append(_commonUtils.RewriteColumn(col, colsql));
171182
if (_noneParameter == false)
172183
_commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);

0 commit comments

Comments
 (0)