1- // Copyright (C) 2013 Xtensive LLC.
2- // All rights reserved .
3- // For conditions of distribution and use, see license .
1+ // Copyright (C) 2013-2022 Xtensive LLC.
2+ // This code is distributed under MIT license terms .
3+ // See the License.txt file in the project root for more information .
44// Created by: Alexey Kulakov
55// Created: 2013.08.16
66
7+ using System ;
78using System . Text ;
89using Xtensive . Core ;
910
1011namespace Xtensive . Orm . Configuration
1112{
1213 /// <summary>
13- /// Ignore rules for presistent types
14+ /// Ignore rule for items in database (tables, columns, indexes).
1415 /// </summary>
1516 public sealed class IgnoreRule : LockableBase
1617 {
1718 private string database ;
1819 private string schema ;
1920 private string table ;
2021 private string column ;
21-
22+ private string index ;
2223
2324 /// <summary>
24- /// Gets database that is assigned to ignored type when this rule is applied
25+ /// Gets database that the rule should be applied to.
2526 /// If this property is set to null or empty value <see cref="DomainConfiguration.DefaultDatabase"/>
2627 /// is used instead.
2728 /// </summary>
2829 public string Database
2930 {
30- get { return database ; }
31- set
32- {
31+ get => database ;
32+ set {
3333 EnsureNotLocked ( ) ;
3434 database = value ;
3535 }
3636 }
3737
3838 /// <summary>
39- /// Get schema that is assigned to ignored type when this rule is applied
39+ /// Get schema that the rule should be applied to.
4040 /// If this property is set to null or empty value <see cref="DomainConfiguration.DefaultSchema"/>
4141 /// is used instead.
4242 /// </summary>
4343 public string Schema
4444 {
45- get { return schema ; }
46- set
47- {
45+ get => schema ;
46+ set {
4847 EnsureNotLocked ( ) ;
4948 schema = value ;
5049 }
5150 }
5251
5352
5453 /// <summary>
55- /// Gets table condition.
56- /// When type is declared in the specified table, this rule is applied.
54+ /// Gets table condition (exact name, prefix or suffix by using asterisk).
5755 /// If this property is set to null value, any table matches this rule.
5856 /// </summary>
5957 public string Table
6058 {
61- get { return table ; }
62- set
63- {
59+ get => table ;
60+ set {
6461 EnsureNotLocked ( ) ;
6562 table = value ;
6663 }
6764 }
6865
6966 /// <summary>
70- /// Gets column condition
71- /// When type is declared in the specified column, this rule is applied.
67+ /// Gets column condition (exact name, prefix or suffix by using asterisk).
7268 /// If this property is set to null value, any culumn matches this rule.
7369 /// </summary>
70+ /// <remarks>Either <see cref="Column"/> or <see cref="Index"/> can be declared.</remarks>
7471 public string Column
7572 {
76- get { return column ; }
77- set
78- {
73+ get => column ;
74+ set {
75+ EnsureNotLocked ( ) ;
76+ SetColumnWithCheck ( value ) ;
77+ }
78+ }
79+
80+ /// <summary>
81+ /// Gets index condition (exact name, prefix or suffix by using asterisk).
82+ /// If this property is set to null value, any index matches this rule.
83+ /// </summary>
84+ /// <remarks>Either <see cref="Index"/> or <see cref="Column"/> can be declared.</remarks>
85+ public string Index
86+ {
87+ get => index ;
88+ set {
7989 EnsureNotLocked ( ) ;
80- column = value ;
90+ SetIndexWithCheck ( value ) ;
8191 }
8292 }
8393
8494 /// <inheritdoc />
8595 public override string ToString ( )
8696 {
8797 var separator = ", " ;
88- StringBuilder sb = new StringBuilder ( ) ;
89-
98+
9099 var databasePart = ! string . IsNullOrEmpty ( database ) ? $ "Database = { database } " : "<default database>" ;
91100 var schemaPart = ! string . IsNullOrEmpty ( schema ) ? $ "Schema = { schema } " : "<default schema>" ;
92101 var tablePart = ! string . IsNullOrEmpty ( table ) ? $ "Table = { table } " : "<any table>" ;
93- var columnPart = ! string . IsNullOrEmpty ( column ) ? $ "Column = { column } " : "<any column>" ;
94- sb . Append ( databasePart )
102+
103+ var columnOrIndexPart = ! string . IsNullOrEmpty ( column )
104+ ? $ "Column = { column } "
105+ : ( ! string . IsNullOrEmpty ( index ) )
106+ ? $ "Index = { index } "
107+ : "<any column> OR <any index>" ;
108+
109+ return new StringBuilder ( databasePart )
95110 . Append ( separator )
96111 . Append ( schemaPart )
97112 . Append ( separator )
98113 . Append ( tablePart )
99114 . Append ( separator )
100- . Append ( columnPart ) ;
101-
102- return sb . ToString ( ) ;
115+ . Append ( columnOrIndexPart )
116+ . ToString ( ) ;
103117 }
104118
105119 /// <summary>
@@ -108,7 +122,23 @@ public override string ToString()
108122 /// <returns>Cloned instance</returns>
109123 public IgnoreRule Clone ( )
110124 {
111- return new IgnoreRule ( database , schema , table , column ) ;
125+ return new IgnoreRule ( database , schema , table , column , index ) ;
126+ }
127+
128+ private void SetColumnWithCheck ( in string columnToSet )
129+ {
130+ if ( ! string . IsNullOrEmpty ( columnToSet ) && ! string . IsNullOrEmpty ( index ) ) {
131+ throw new InvalidOperationException ( string . Format ( Strings . ExIgnoreRuleIsAlreadyConfiguredForX , nameof ( Index ) ) ) ;
132+ }
133+ column = columnToSet ;
134+ }
135+
136+ private void SetIndexWithCheck ( in string indexToSet )
137+ {
138+ if ( ! string . IsNullOrEmpty ( indexToSet ) && ! string . IsNullOrEmpty ( column ) ) {
139+ throw new InvalidOperationException ( string . Format ( Strings . ExIgnoreRuleIsAlreadyConfiguredForX , nameof ( Column ) ) ) ;
140+ }
141+ index = indexToSet ;
112142 }
113143
114144 //Constructors
@@ -127,12 +157,14 @@ public IgnoreRule()
127157 /// <param name="schema">Value for <see cref="Schema"/></param>
128158 /// <param name="table">Value for <see cref="Table"/></param>
129159 /// <param name="column">Value for <see cref="Column"/></param>
130- public IgnoreRule ( string database , string schema , string table , string column )
160+ /// <param name="index">Value for <see cref="Index"/></param>
161+ internal IgnoreRule ( in string database , in string schema , in string table , in string column , in string index )
131162 {
132163 Database = database ;
133164 Schema = schema ;
134165 Table = table ;
135166 Column = column ;
167+ Index = index ;
136168 }
137169 }
138170}
0 commit comments