From eec10f89b0cbb654ea9758d2df6a3aa7894ee038 Mon Sep 17 00:00:00 2001 From: d4ilys <963922242@qq.com> Date: Thu, 17 Aug 2023 14:38:42 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8DClickHouse=20CodeFirst=20?= =?UTF-8?q?=E5=A4=9A=E4=B8=BB=E9=94=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClickHouse/ClickHouseTest2.cs | 121 ++++++++++++++++++ .../ClickHouseCodeFirst.cs | 18 ++- 2 files changed, 133 insertions(+), 6 deletions(-) diff --git a/FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs b/FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs index 5b21741d..9bc76f41 100644 --- a/FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs +++ b/FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs @@ -23,5 +23,126 @@ namespace FreeSql.Tests.ClickHouse { fsql.CodeFirst.SyncStructure(typeof(CollectDataEntityUpdate01)); } + + [Fact] + public void Issuse1587Test() + { + fsql.CodeFirst.SyncStructure(typeof(PositionInfoModel)); + } + [Fact] + public void Issuse1587TestOnePrimary() + { + fsql.CodeFirst.SyncStructure(typeof(PositionInfoModel2)); + } + } + + [Table(Name = "table_1")] + [Index("stcd_index", "STCD", false)] + [Index("tm_index", "TM", false)] + [Index("type_index", "TYPE", false)] + public class PositionInfoModel + { + [Column(IsPrimary = true)] + public string STCD + { + set; get; + } + + [Column(IsPrimary = true)] + public DateTime TM + { + set; get; + } + + [Column(IsNullable = false)] + public decimal LON + { + set; get; + } + + [Column(IsNullable = false)] + public decimal LAT + { + set; get; + } + + [Column(IsNullable = false)] + public int TYPE + { + set; get; + } + + [Column(IsNullable = true)] + public decimal SPD + { + set; get; + } + + [Column(IsNullable = true)] + public decimal COG + { + set; get; + } + + [Column(IsNullable = true)] + public DateTime? UT + { + set; get; + } + } + + + [Table(Name = "table_2")] + [Index("stcd_index", "STCD", false)] + [Index("tm_index", "TM", false)] + [Index("type_index", "TYPE", false)] + public class PositionInfoModel2 + { + public string STCD + { + set; get; + } + + [Column(IsPrimary = true)] + public DateTime TM + { + set; get; + } + + [Column(IsNullable = false)] + public decimal LON + { + set; get; + } + + [Column(IsNullable = false)] + public decimal LAT + { + set; get; + } + + [Column(IsNullable = false)] + public int TYPE + { + set; get; + } + + [Column(IsNullable = true)] + public decimal SPD + { + set; get; + } + + [Column(IsNullable = true)] + public decimal COG + { + set; get; + } + + [Column(IsNullable = true)] + public DateTime? UT + { + set; get; + } } } \ No newline at end of file diff --git a/Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs b/Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs index b71c024d..d0c41132 100644 --- a/Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs +++ b/Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs @@ -199,7 +199,7 @@ namespace FreeSql.ClickHouse sb.Remove(sb.Length - 2, 2); sb.Append(" )"); sb.Append(" \r\nPRIMARY KEY "); - sb.Append(ls); + sb.Append($"({ls}) "); sb.Remove(sb.Length - 2, 2).Append(","); } @@ -415,7 +415,7 @@ where a.database in ({0}) and a.table in ({1})", tboldname ?? tbname); sb.Remove(sb.Length - 2, 2); sb.Append(" )"); sb.Append(" \r\nPRIMARY KEY "); - sb.Append(ls); + sb.Append($"({ls}) "); sb.Remove(sb.Length - 2, 2).Append(","); } @@ -455,7 +455,8 @@ where a.database in ({0}) and a.table in ({1})", tboldname ?? tbname); .Append(_commonUtils.QuoteSqlName(tbname[0], tbname[1])).Append(";\r\n"); } - return sb.Length == 0 ? null : sb.ToString(); + var res = sb.Length == 0 ? null : sb.ToString(); + return res; } finally { @@ -514,9 +515,14 @@ where a.database in ({0}) and a.table in ({1})", tboldname ?? tbname); } string CkNullableAdapter(string dbType, bool isPrimary) { - return isPrimary - ? dbType.Replace("Nullable(", "").Replace(")","").Replace(" NOT NULL", "") - : dbType.Replace(" NOT NULL", ""); + return isPrimary switch + { + true when dbType.Contains("Nullable") => dbType.Replace("Nullable(", "") + .Replace(")", "") + .Replace(" NOT NULL", ""), + true => dbType, + _ => dbType.Replace(" NOT NULL", "") + }; }