Skip to content

Commit

Permalink
- 修复ClickHouse CodeFirst 多主键问题
Browse files Browse the repository at this point in the history
  • Loading branch information
d4ilys committed Aug 17, 2023
1 parent c9aa335 commit eec10f8
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 6 deletions.
121 changes: 121 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/ClickHouse/ClickHouseTest2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,126 @@ public void CodeFirst()
{
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;
}
}
}
18 changes: 12 additions & 6 deletions Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ protected override string GetComparisonDDLStatements(params TypeAndName[] object
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(",");
}

Expand Down Expand Up @@ -415,7 +415,7 @@ from system.columns a
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(",");
}

Expand Down Expand Up @@ -455,7 +455,8 @@ from system.columns a
.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
{
Expand Down Expand Up @@ -514,9 +515,14 @@ string CkNullablePrimaryAdapter(string dbType, bool isPrimary)
}
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", "")
};
}


Expand Down

0 comments on commit eec10f8

Please sign in to comment.