mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 17:20:49 +08:00 
			
		
		
		
	解决ClickHouse 批量更新DateTime问题
This commit is contained in:
		@@ -22,7 +22,7 @@ namespace FreeSql.Tests.ClickHouse
 | 
				
			|||||||
            _fsql = new FreeSqlBuilder().UseConnectionString(DataType.ClickHouse,
 | 
					            _fsql = new FreeSqlBuilder().UseConnectionString(DataType.ClickHouse,
 | 
				
			||||||
                    "Host=192.168.1.123;Port=8123;Database=test_issue;Compress=True;Min Pool Size=1")
 | 
					                    "Host=192.168.1.123;Port=8123;Database=test_issue;Compress=True;Min Pool Size=1")
 | 
				
			||||||
                .UseMonitorCommand(cmd => _output.WriteLine($"线程:{cmd.CommandText}\r\n"))
 | 
					                .UseMonitorCommand(cmd => _output.WriteLine($"线程:{cmd.CommandText}\r\n"))
 | 
				
			||||||
                .UseNoneCommandParameter(true)
 | 
					                .UseNoneCommandParameter(false)
 | 
				
			||||||
                .UseAdoConnectionPool(true)
 | 
					                .UseAdoConnectionPool(true)
 | 
				
			||||||
                .Build();
 | 
					                .Build();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -32,25 +32,35 @@ namespace FreeSql.Tests.ClickHouse
 | 
				
			|||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void TestIssue1813()
 | 
					        public void TestIssue1813()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            //var personsUpdate = new List<Person>
 | 
					            //普通修改
 | 
				
			||||||
            //{
 | 
					            _fsql.Update<Person>()
 | 
				
			||||||
            //    new Person
 | 
					                .Set(p => p.Name == "update_name")
 | 
				
			||||||
            //    {
 | 
					                .Set(p => p.UpdateTime == DateTime.Now)
 | 
				
			||||||
            //        Id = 1,
 | 
					                .Where(p => p.Id == "25e8d92e-29f2-43ff-b861-9ade0eec4041")
 | 
				
			||||||
            //        Name = $"test2{DateTime.Now.Millisecond}",
 | 
					                .ExecuteAffrows();
 | 
				
			||||||
            //        Age = 20,
 | 
					
 | 
				
			||||||
            //        CreateTime = DateTime.Now
 | 
					            //批量修改
 | 
				
			||||||
            //    },
 | 
					            var updatePerson = new List<Person>();
 | 
				
			||||||
            //    new Person
 | 
					            updatePerson.Add(new Person
 | 
				
			||||||
            //    {
 | 
					            {
 | 
				
			||||||
            //        Id = 2,
 | 
					                Id = "9cd7af52-85cc-4d26-898a-4020cadb0491",
 | 
				
			||||||
            //        Name = "test3"+ 286,
 | 
					                Name = "update_name1",
 | 
				
			||||||
            //        Age = 22,
 | 
					                UpdateTime = DateTime.Now
 | 
				
			||||||
            //        CreateTime = DateTime.Now
 | 
					            });
 | 
				
			||||||
            //    }
 | 
					
 | 
				
			||||||
            //};
 | 
					            updatePerson.Add(new Person
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                Id = "bd9f9ed6-bd03-4675-abb4-12b7fdac7678",
 | 
				
			||||||
 | 
					                Name = "update_name2",
 | 
				
			||||||
 | 
					                UpdateTime = DateTime.Now
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            _fsql.Update<Person>().SetSource(updatePerson).UpdateColumns(person => new
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                person.Name,
 | 
				
			||||||
 | 
					                person.UpdateTime,
 | 
				
			||||||
 | 
					            }).ExecuteAffrows();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //_fsql.Update<Person>().SetSource()
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -158,7 +158,7 @@ namespace FreeSql.ClickHouse
 | 
				
			|||||||
                var ts = (TimeSpan)value;
 | 
					                var ts = (TimeSpan)value;
 | 
				
			||||||
                value = $"{Math.Floor(ts.TotalHours)}:{ts.Minutes}:{ts.Seconds}";
 | 
					                value = $"{Math.Floor(ts.TotalHours)}:{ts.Minutes}:{ts.Seconds}";
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
			else if (value is Array)
 | 
					            else if (value is Array)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				var valueArr = value as Array;
 | 
									var valueArr = value as Array;
 | 
				
			||||||
				var eleType = type.GetElementType();
 | 
									var eleType = type.GetElementType();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,7 @@ using System.Linq;
 | 
				
			|||||||
using System.Text;
 | 
					using System.Text;
 | 
				
			||||||
using System.Threading;
 | 
					using System.Threading;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					using DateTime = System.DateTime;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace FreeSql.ClickHouse.Curd
 | 
					namespace FreeSql.ClickHouse.Curd
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -167,6 +168,16 @@ namespace FreeSql.ClickHouse.Curd
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                                var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
 | 
					                                var colsql = _noneParameter ? _commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col, col.Attribute.MapType, val) :
 | 
				
			||||||
                                    _commonUtils.QuoteWriteParamterAdapter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
 | 
					                                    _commonUtils.QuoteWriteParamterAdapter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"p_{_paramsSource.Count}"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                                //判断是否是DateTime类型,如果是DateTime类型,需要转换成ClickHouse支持的时间格式
 | 
				
			||||||
 | 
					                                if (col.Attribute.MapType == typeof(DateTime) || col.Attribute.MapType == typeof(DateTime?) )
 | 
				
			||||||
 | 
					                                {
 | 
				
			||||||
 | 
					                                    //获取当前实时区
 | 
				
			||||||
 | 
					                                    var timeZone = TimeZoneInfo.Local;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                                    colsql = $"toDateTime({colsql},'Asia/Shanghai')";
 | 
				
			||||||
 | 
					                                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                                cwsb.Append(_commonUtils.RewriteColumn(col, colsql));
 | 
					                                cwsb.Append(_commonUtils.RewriteColumn(col, colsql));
 | 
				
			||||||
                                if (_noneParameter == false)
 | 
					                                if (_noneParameter == false)
 | 
				
			||||||
                                    _commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
 | 
					                                    _commonUtils.AppendParamter(_paramsSource, null, col, col.Attribute.MapType, val);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user