[Table Designer] Support filter predicate and included columns for index (#1619)

This commit is contained in:
Hai Cao
2022-08-05 15:04:44 -07:00
committed by GitHub
parent 1342a8a085
commit a9fe77589d
8 changed files with 400 additions and 2 deletions

View File

@@ -33,7 +33,11 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
new MemoryOptimizedTableIdentityColumnRule(),
new TableShouldAvoidHavingMultipleEdgeConstraintsRule(),
new ColumnCannotBeListedMoreThanOnceInPrimaryKeyRule(),
new MutipleCreateTableStatementsInScriptRule()
new MutipleCreateTableStatementsInScriptRule(),
new ClusteredIndexCannotHaveFilterPredicate(),
new ClusteredIndexCannotHaveIncludedColumnsRule(),
new ColumnCanOnlyAppearOnceInIndexIncludedColumnsRule(),
new ColumnCannotDuplicateWitIndexKeyColumnsRule()
};
/// <summary>
@@ -77,6 +81,50 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
}
}
public class ClusteredIndexCannotHaveIncludedColumnsRule : ITableDesignerValidationRule
{
public List<TableDesignerIssue> Run(Dac.TableDesigner designer)
{
var table = designer.TableViewModel;
var errors = new List<TableDesignerIssue>();
for (int i = 0; i < table.Indexes.Items.Count; i++)
{
var index = table.Indexes.Items[i];
if (index.IsClustered && index.IncludedColumns.Count != 0)
{
errors.Add(new TableDesignerIssue()
{
Description = SR.ClusteredIndexCannotHaveIncludedColumnsRuleDescription,
PropertyPath = new object[] { TablePropertyNames.Indexes, i, IndexPropertyNames.IncludedColumns, 0 }
});
}
}
return errors;
}
}
public class ClusteredIndexCannotHaveFilterPredicate : ITableDesignerValidationRule
{
public List<TableDesignerIssue> Run(Dac.TableDesigner designer)
{
var table = designer.TableViewModel;
var errors = new List<TableDesignerIssue>();
for (int i = 0; i < table.Indexes.Items.Count; i++)
{
var index = table.Indexes.Items[i];
if (index.IsClustered && index.FilterPredicate != null)
{
errors.Add(new TableDesignerIssue()
{
Description = SR.ClusteredIndexCannotHaveFilterPredicateRuleDescription,
PropertyPath = new object[] { TablePropertyNames.Indexes, i, IndexPropertyNames.FilterPredicate }
});
}
}
return errors;
}
}
public class ForeignKeyMustHaveColumnsRule : ITableDesignerValidationRule
{
public List<TableDesignerIssue> Run(Dac.TableDesigner designer)
@@ -130,6 +178,64 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
}
}
public class ColumnCanOnlyAppearOnceInIndexIncludedColumnsRule : ITableDesignerValidationRule
{
public List<TableDesignerIssue> Run(Dac.TableDesigner designer)
{
var table = designer.TableViewModel;
var errors = new List<TableDesignerIssue>();
for (int i = 0; i < table.Indexes.Items.Count; i++)
{
var index = table.Indexes.Items[i];
var existingColumns = new HashSet<string>();
for (int j = 0; j < index.IncludedColumns.Count; j++)
{
var col = index.IncludedColumns[j];
if (existingColumns.Contains(col))
{
errors.Add(new TableDesignerIssue()
{
Description = SR.ColumnCanOnlyAppearOnceInIndexIncludedColumnsRuleDescription(col, index.Name, j + 1),
PropertyPath = new object[] { TablePropertyNames.Indexes, i, IndexPropertyNames.IncludedColumns, j }
});
}
else
{
existingColumns.Add(col);
}
}
}
return errors;
}
}
public class ColumnCannotDuplicateWitIndexKeyColumnsRule : ITableDesignerValidationRule
{
public List<TableDesignerIssue> Run(Dac.TableDesigner designer)
{
var table = designer.TableViewModel;
var errors = new List<TableDesignerIssue>();
for (int i = 0; i < table.Indexes.Items.Count; i++)
{
var index = table.Indexes.Items[i];
var keyCols = new HashSet<string>(index.Columns.Select(s => s.Column));
for (int j = 0; j < index.IncludedColumns.Count; j++)
{
var col = index.IncludedColumns[j];
if (keyCols.Contains(col))
{
errors.Add(new TableDesignerIssue()
{
Description = SR.ColumnCannotDuplicateWitIndexKeyColumnsRuleDescription(col, index.Name, j + 1),
PropertyPath = new object[] { TablePropertyNames.Indexes, i, IndexPropertyNames.IncludedColumns, j }
});
}
}
}
return errors;
}
}
public class ColumnCanOnlyAppearOnceInForeignKeyRule : ITableDesignerValidationRule
{
public List<TableDesignerIssue> Run(Dac.TableDesigner designer)