mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
primary key columns validation rule (#1461)
This commit is contained in:
@@ -30,7 +30,8 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
|
|||||||
new TemporalTableMustHavePrimaryKeyRule(),
|
new TemporalTableMustHavePrimaryKeyRule(),
|
||||||
new TableMustHaveAtLeastOneColumnRule(),
|
new TableMustHaveAtLeastOneColumnRule(),
|
||||||
new MemoryOptimizedTableIdentityColumnRule(),
|
new MemoryOptimizedTableIdentityColumnRule(),
|
||||||
new TableShouldAvoidHavingMultipleEdgeConstraintsRule()
|
new TableShouldAvoidHavingMultipleEdgeConstraintsRule(),
|
||||||
|
new ColumnCannotBeListedMoreThanOnceInPrimaryKeyRule()
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -523,4 +524,34 @@ namespace Microsoft.SqlTools.ServiceLayer.TableDesigner
|
|||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ColumnCannotBeListedMoreThanOnceInPrimaryKeyRule : ITableDesignerValidationRule
|
||||||
|
{
|
||||||
|
public List<TableDesignerIssue> Run(Dac.TableDesigner designer)
|
||||||
|
{
|
||||||
|
var table = designer.TableViewModel;
|
||||||
|
var errors = new List<TableDesignerIssue>();
|
||||||
|
if (table.PrimaryKey != null)
|
||||||
|
{
|
||||||
|
var existingNames = new HashSet<string>();
|
||||||
|
for (int i = 0; i < table.PrimaryKey.Columns.Count; i++)
|
||||||
|
{
|
||||||
|
var columnSpec = table.PrimaryKey.Columns[i];
|
||||||
|
if (existingNames.Contains(columnSpec.Column))
|
||||||
|
{
|
||||||
|
errors.Add(new TableDesignerIssue()
|
||||||
|
{
|
||||||
|
Description = string.Format("Cannot use duplicate column names in primary key, column name: {0}", columnSpec.Column),
|
||||||
|
PropertyPath = new object[] { TablePropertyNames.PrimaryKeyColumns, i, IndexColumnSpecificationPropertyNames.Column }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
existingNames.Add(columnSpec.Column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errors;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user