33using System . Reflection ;
44
55using Cassandra ;
6+ using Cassandra . Mapping ;
67using Cassandra . Mapping . Attributes ;
78
89using SkbKontur . DbViewer . Configuration ;
@@ -12,35 +13,107 @@ namespace SkbKontur.DbViewer.Cql
1213{
1314 public class CqlPropertyDescriptionBuilder : IPropertyDescriptionBuilder
1415 {
16+ private string PrettyPrintPropertyCqlAttributes ( PropertyInfo propertyInfo )
17+ {
18+ var columnMeta = $ "Тип: { propertyInfo . PropertyType . Name } \n ";
19+
20+ var columnAttribute = propertyInfo . GetCustomAttribute < ColumnAttribute > ( ) ;
21+ if ( columnAttribute != null )
22+ {
23+ var type = columnAttribute . Type == null ? string . Empty : $ ", Type = typeof({ columnAttribute . Type . Name } )";
24+ columnMeta += $ "[Column(\" { columnAttribute . Name } \" { type } )]\n ";
25+ }
26+
27+ var partitionKeyAttribute = propertyInfo . GetCustomAttribute < PartitionKeyAttribute > ( ) ;
28+ if ( partitionKeyAttribute != null )
29+ {
30+ columnMeta += $ "[PartitionKey({ partitionKeyAttribute . Index } )]";
31+ }
32+
33+ var clusteringKeyAttribute = propertyInfo . GetCustomAttribute < ClusteringKeyAttribute > ( ) ;
34+ if ( clusteringKeyAttribute != null )
35+ {
36+ var sort = clusteringKeyAttribute . ClusteringSortOrder == SortOrder . Unspecified ? null : $ ", SortOrder.{ clusteringKeyAttribute . ClusteringSortOrder } ";
37+ var name = string . IsNullOrEmpty ( clusteringKeyAttribute . Name ) ? null : $ ", Name = \" { clusteringKeyAttribute . Name } \" ";
38+ columnMeta += $ "[ClusteringKey({ clusteringKeyAttribute . Index } { sort } { name } )]";
39+ }
40+
41+ return columnMeta ;
42+ }
43+
1544 public PropertyMetaInformation Build ( PropertyInfo propertyInfo , Type typeInfo )
1645 {
1746 var result = new PropertyMetaInformation
1847 {
1948 Name = propertyInfo . Name ,
49+ Meta = PrettyPrintPropertyCqlAttributes ( propertyInfo ) ,
50+ RequiredForFilter = Array . Empty < FilterRequirement > ( ) ,
51+ RequiredForSort = new SortRequirements
52+ {
53+ RequiredFilters = Array . Empty < FilterRequirement > ( ) ,
54+ RequiredSorts = Array . Empty < string > ( ) ,
55+ } ,
2056 } ;
2157
22- if ( propertyInfo . CustomAttributes . Any ( x => x . AttributeType == typeof ( PartitionKeyAttribute ) ) )
58+ var partitionKeys = propertyInfo . ReflectedType
59+ . GetProperties ( BindingFlags . Public | BindingFlags . Instance )
60+ . Select ( x => ( Property : x , Attribute : x . GetCustomAttribute < PartitionKeyAttribute > ( ) ) )
61+ . Where ( x => x . Attribute != null )
62+ . OrderBy ( x => x . Attribute . Index )
63+ . ToArray ( ) ;
64+ var partitionKeyAttribute = propertyInfo . GetCustomAttribute < PartitionKeyAttribute > ( ) ;
65+ if ( partitionKeyAttribute != null )
2366 {
2467 result . IsSearchable = true ;
2568 result . IsIdentity = true ;
2669 result . IsRequired = true ;
27- result . AvailableFilters = new [ ] { ObjectFieldFilterOperator . Equals } ;
70+ result . AvailableFilters = equals ;
2871 }
2972
30- if ( propertyInfo . CustomAttributes . Any ( x => x . AttributeType == typeof ( ClusteringKeyAttribute ) ) )
73+ var clusteringKeys = propertyInfo . ReflectedType
74+ . GetProperties ( BindingFlags . Public | BindingFlags . Instance )
75+ . Select ( x => ( Property : x , Attribute : x . GetCustomAttribute < ClusteringKeyAttribute > ( ) ) )
76+ . Where ( x => x . Attribute != null )
77+ . OrderBy ( x => x . Attribute . Index )
78+ . ToArray ( ) ;
79+ var clusteringKeyAttribute = propertyInfo . GetCustomAttribute < ClusteringKeyAttribute > ( ) ;
80+ if ( clusteringKeyAttribute != null )
3181 {
3282 result . IsSearchable = true ;
3383 result . IsIdentity = true ;
3484 result . IsSortable = true ;
35- result . AvailableFilters = specialTypes . Contains ( propertyInfo . PropertyType )
36- ? new [ ] { ObjectFieldFilterOperator . Equals }
37- : defaultFilters ;
85+ result . AvailableFilters = specialTypes . Contains ( propertyInfo . PropertyType ) ? equals : defaultFilters ;
86+
87+ result . RequiredForFilter =
88+ partitionKeys
89+ . Select ( x => new FilterRequirement { AvailableFilters = equals , PropertyName = x . Property . Name } )
90+ . Concat (
91+ clusteringKeys
92+ . Where ( x => x . Attribute . Index < clusteringKeyAttribute . Index )
93+ . Select ( x => new FilterRequirement { AvailableFilters = equals , PropertyName = x . Property . Name } )
94+ )
95+ . ToArray ( ) ;
96+
97+ result . RequiredForSort = new SortRequirements
98+ {
99+ RequiredFilters =
100+ partitionKeys
101+ . Select ( x => new FilterRequirement { AvailableFilters = equals , PropertyName = x . Property . Name } )
102+ . ToArray ( ) ,
103+ OneDirectionSort = true ,
104+ RequiredSorts =
105+ clusteringKeys
106+ . Where ( x => x . Attribute . Index < clusteringKeyAttribute . Index )
107+ . Select ( x => x . Property . Name )
108+ . ToArray ( ) ,
109+ } ;
38110 }
39111
40112 return result ;
41113 }
42114
43115 private static readonly Type [ ] specialTypes = { typeof ( TimeUuid ) , typeof ( bool ) } ;
116+ private static readonly ObjectFieldFilterOperator [ ] equals = { ObjectFieldFilterOperator . Equals } ;
44117
45118 private static readonly ObjectFieldFilterOperator [ ] defaultFilters =
46119 {
0 commit comments