/
IndexDefinition.cs
116 lines (100 loc) · 3.8 KB
/
IndexDefinition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#region License
//
// Copyright (c) 2007-2024, Fluent Migrator Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using FluentMigrator.Infrastructure;
using FluentMigrator.Infrastructure.Extensions;
using FluentMigrator.Validation;
namespace FluentMigrator.Model
{
/// <summary>
/// The index definition
/// </summary>
public class IndexDefinition
: ICloneable,
#pragma warning disable 618
ICanBeValidated,
#pragma warning restore 618
ISupportAdditionalFeatures,
IValidatableObject,
IValidationChildren
{
private readonly IDictionary<string, object> _additionalFeatures = new Dictionary<string, object>();
/// <summary>
/// Gets or sets the index name
/// </summary>
[Required(ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = nameof(ErrorMessages.IndexNameCannotBeNullOrEmpty))]
public virtual string Name { get; set; }
/// <summary>
/// Gets or sets the schema name
/// </summary>
public virtual string SchemaName { get; set; }
/// <summary>
/// Gets or sets the table name
/// </summary>
[Required(ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = nameof(ErrorMessages.TableNameCannotBeNullOrEmpty))]
public virtual string TableName { get; set; }
/// <summary>
/// Gets or sets a value indicating whteher the index only allows unique values
/// </summary>
public virtual bool IsUnique { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the index is clustered
/// </summary>
public bool IsClustered { get; set; }
/// <summary>
/// Gets or sets a collection of index column definitions
/// </summary>
public virtual ICollection<IndexColumnDefinition> Columns { get; set; } = new List<IndexColumnDefinition>();
/// <inheritdoc />
public virtual IDictionary<string, object> AdditionalFeatures => _additionalFeatures;
/// <inheritdoc />
[Obsolete("Use the System.ComponentModel.DataAnnotations.Validator instead")]
public virtual void CollectValidationErrors(ICollection<string> errors)
{
this.CollectErrors(errors);
}
/// <inheritdoc />
public object Clone()
{
var result = new IndexDefinition
{
Name = Name,
SchemaName = SchemaName,
TableName = TableName,
IsUnique = IsUnique,
IsClustered = IsClustered,
Columns = Columns.CloneAll().ToList(),
};
_additionalFeatures.CloneTo(result._additionalFeatures);
return result;
}
/// <inheritdoc />
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Columns.Count == 0)
{
yield return new ValidationResult(ErrorMessages.IndexMustHaveOneOrMoreColumns);
}
}
/// <inheritdoc />
IEnumerable<object> IValidationChildren.Children => Columns;
}
}