88using System . Text ;
99using System . Text . RegularExpressions ;
1010using System . Data . Common ;
11+ using System . Reflection ;
1112using FreeSql . Internal . ObjectPool ;
1213using ClickHouse . Client . ADO ;
14+ using FreeSql . Provider . ClickHouse . Attributes ;
1315
1416namespace FreeSql . ClickHouse
1517{
@@ -95,10 +97,11 @@ public override DbInfoResult GetDbInfo(Type type)
9597 {
9698 var arrayDbType = $ "Array({ value . dbtype } )";
9799 var defaultArray = new ArrayList ( 0 ) ;
98- return new DbInfoResult ( Convert . ToInt32 ( DbType . Object ) , arrayDbType , arrayDbType , false , defaultArray ) ;
100+ return new DbInfoResult ( Convert . ToInt32 ( DbType . Object ) , arrayDbType , arrayDbType , false ,
101+ defaultArray ) ;
99102 }
100-
101103 }
104+
102105 return null ;
103106 }
104107
@@ -132,7 +135,7 @@ private Tuple<bool, Type> IsCollection(Type type)
132135 if ( interfaces . Any ( t => t . Name == "IEnumerable" ) )
133136 flag = true ;
134137
135- if ( type . Name == "Array" )
138+ if ( type . Name == "Array" )
136139 {
137140 flag = true ;
138141 resultType = typeof ( string ) ;
@@ -173,7 +176,8 @@ protected override string GetComparisonDDLStatements(params TypeSchemaAndName[]
173176 if ( tb == null )
174177 throw new Exception ( CoreStrings . S_Type_IsNot_Migrable ( obj . tableSchema . Type . FullName ) ) ;
175178 if ( tb . Columns . Any ( ) == false )
176- throw new Exception ( CoreStrings . S_Type_IsNot_Migrable_0Attributes ( obj . tableSchema . Type . FullName ) ) ;
179+ throw new Exception (
180+ CoreStrings . S_Type_IsNot_Migrable_0Attributes ( obj . tableSchema . Type . FullName ) ) ;
177181 var tbname = _commonUtils . SplitTableName ( tb . DbName ) ;
178182 if ( tbname ? . Length == 1 )
179183 tbname = new [ ] { database , tbname [ 0 ] } ;
@@ -258,7 +262,8 @@ protected override string GetComparisonDDLStatements(params TypeSchemaAndName[]
258262
259263 if ( tb . Primarys . Any ( ) )
260264 {
261- var primaryKeys = string . Join ( "," , tb . Primarys . Select ( p => _commonUtils . QuoteSqlName ( p . Attribute . Name ) ) ) ;
265+ var primaryKeys = string . Join ( "," ,
266+ tb . Primarys . Select ( p => _commonUtils . QuoteSqlName ( p . Attribute . Name ) ) ) ;
262267 sb . Append ( " \r \n PRIMARY KEY " ) ;
263268 sb . Append ( $ "( { primaryKeys } ) ") ;
264269 }
@@ -268,14 +273,32 @@ protected override string GetComparisonDDLStatements(params TypeSchemaAndName[]
268273
269274 if ( tb . Primarys . Any ( ) )
270275 {
271- var primaryKeys = string . Join ( "," , tb . Primarys . Select ( p => _commonUtils . QuoteSqlName ( p . Attribute . Name ) ) ) ;
276+ var primaryKeys = string . Join ( "," ,
277+ tb . Primarys . Select ( p => _commonUtils . QuoteSqlName ( p . Attribute . Name ) ) ) ;
272278 sb . Append ( " \r \n ORDER BY " ) ;
273279 sb . Append ( $ "( { primaryKeys } ) ") ;
274280 }
275281
282+ //查找属性是否标记了分区特性
283+ var partitionColumnInfos = tb . Properties . Where (
284+ c => c . Value . GetCustomAttribute < ClickHousePartitionAttribute > ( ) != null ) . ToList ( ) ;
285+
286+ if ( partitionColumnInfos != null && partitionColumnInfos . Any ( ) )
287+ {
288+ var partitionProperty = partitionColumnInfos . First ( ) ;
289+
290+ var partitionColumnInfo = tb . Columns . FirstOrDefault ( pair =>
291+ pair . Value . CsName == partitionProperty . Value . Name ) ;
292+ var partitionName = _commonUtils . QuoteSqlName ( partitionColumnInfo . Value . Attribute . Name ) ;
293+ var partitionAttribute = partitionProperty . Value
294+ . GetCustomAttribute < ClickHousePartitionAttribute > ( ) ;
295+ sb . Append ( $ " \r \n PARTITION BY { string . Format ( partitionAttribute . Format , partitionName ) } ") ;
296+ }
297+
298+
276299 //if (string.IsNullOrEmpty(tb.Comment) == false)
277300 // sb.Append(" Comment=").Append(_commonUtils.FormatSql("{0}", tb.Comment));
278- sb . Append ( " SETTINGS index_granularity = 8192;\r \n " ) ;
301+ sb . Append ( " \r \n SETTINGS index_granularity = 8192;\r \n " ) ;
279302 continue ;
280303 }
281304
@@ -477,7 +500,8 @@ from system.columns a
477500
478501 if ( tb . Primarys . Any ( ) )
479502 {
480- var primaryKeys = string . Join ( "," , tb . Primarys . Select ( p => _commonUtils . QuoteSqlName ( p . Attribute . Name ) ) ) ;
503+ var primaryKeys = string . Join ( "," ,
504+ tb . Primarys . Select ( p => _commonUtils . QuoteSqlName ( p . Attribute . Name ) ) ) ;
481505 sb . Append ( " \r \n ORDER BY ( " ) ;
482506 sb . Append ( primaryKeys ) ;
483507 sb . Append ( " )" ) ;
0 commit comments