fixed the bug with loading default constraints in oe (#582)

* fixed the bug with loading default constraints in oe
This commit is contained in:
Leila Lali
2018-02-09 08:51:41 -08:00
committed by GitHub
parent fc4282b701
commit c40c740a73
5 changed files with 80 additions and 21 deletions

View File

@@ -45,7 +45,7 @@
### How to add a new SQL object type ### How to add a new SQL object type
To add a new object type, To add a new object type,
* Add the type to TreeNodeDefinition.xml and SmoQueryModelDefinition.xml. * Add the type to TreeNodeDefinition.xml and SmoQueryModelDefinition.xml.
* Regenerate the classes by running Build.cmd/build.sh -target=SRGen * Regenerate the classes by running Build.cmd/build.sh -target=CodeGen

View File

@@ -487,17 +487,42 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
public override IEnumerable<SqlSmoObject> Query(SmoQueryContext context, string filter, bool refresh, IEnumerable<string> extraProperties) public override IEnumerable<SqlSmoObject> Query(SmoQueryContext context, string filter, bool refresh, IEnumerable<string> extraProperties)
{ {
Column parentColumn = context.Parent as Column; Table parentTable = context.Parent as Table;
if (parentColumn != null) if (parentTable != null)
{ {
var retValue = parentColumn.DefaultConstraint; var retValue = parentTable.Columns;
if (retValue != null) if (retValue != null)
{ {
if (refresh) retValue.ClearAndInitialize(filter, extraProperties);
List<DefaultConstraint> subFieldResult = new List<DefaultConstraint>();
foreach(Column field in retValue)
{ {
parentColumn.DefaultConstraint.Refresh(); DefaultConstraint subField = field.DefaultConstraint;
if (subField != null)
{
subFieldResult.Add(subField);
}
} }
return new SqlSmoObject[] { retValue }; return subFieldResult.Where(c => PassesFinalFilters(parentTable, c));
}
}
UserDefinedTableType parentUserDefinedTableType = context.Parent as UserDefinedTableType;
if (parentUserDefinedTableType != null)
{
var retValue = parentUserDefinedTableType.Columns;
if (retValue != null)
{
retValue.ClearAndInitialize(filter, extraProperties);
List<DefaultConstraint> subFieldResult = new List<DefaultConstraint>();
foreach(Column field in retValue)
{
DefaultConstraint subField = field.DefaultConstraint;
if (subField != null)
{
subFieldResult.Add(subField);
}
}
return subFieldResult.Where(c => PassesFinalFilters(parentUserDefinedTableType, c));
} }
} }
return Enumerable.Empty<SqlSmoObject>(); return Enumerable.Empty<SqlSmoObject>();

View File

@@ -71,7 +71,10 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
WriteLine("{"); WriteLine("{");
PushIndent(indent); PushIndent(indent);
string navigationPath = GetNavigationPath(nodeElement, xmlFile, nodeName, parentType); XmlElement navPathElement = GetNavPathElement(xmlFile, nodeName, parentType);
string navigationPath = GetNavigationPath(nodeElement, nodeName, navPathElement);
string subField = GetNavPathAttribute(navPathElement, "SubField");
string fieldType = GetNavPathAttribute(navPathElement, "FieldType");
WriteLine(string.Format("var retValue = {0}.{1};", parentVar, navigationPath)); WriteLine(string.Format("var retValue = {0}.{1};", parentVar, navigationPath));
@@ -83,7 +86,27 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
if (IsCollection(nodeElement)) if (IsCollection(nodeElement))
{ {
WriteLine(string.Format("retValue.ClearAndInitialize(filter, extraProperties);")); WriteLine(string.Format("retValue.ClearAndInitialize(filter, extraProperties);"));
WriteLine(string.Format("return new SmoCollectionWrapper<{0}>(retValue).Where(c => PassesFinalFilters({1}, c));", nodeType, parentVar)); if (string.IsNullOrEmpty(subField) )
{
WriteLine(string.Format("return new SmoCollectionWrapper<{0}>(retValue).Where(c => PassesFinalFilters({1}, c));", nodeType, parentVar));
}
else
{
WriteLine(string.Format("List<{0}> subFieldResult = new List<{0}>();", nodeType));
WriteLine(string.Format("foreach({0} field in retValue)", fieldType));
WriteLine("{");
PushIndent(indent);
WriteLine(string.Format("{0} subField = field.{1};", nodeType, subField));
WriteLine(string.Format("if (subField != null)"));
WriteLine("{");
PushIndent(indent);
WriteLine(string.Format("subFieldResult.Add(subField);"));
PopIndent();
WriteLine("}");
PopIndent();
WriteLine("}");
WriteLine(string.Format("return subFieldResult.Where(c => PassesFinalFilters({1}, c));", nodeType, parentVar));
}
} }
else else
{ {
@@ -142,27 +165,23 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
return (XmlElement)doc.SelectSingleNode(string.Format("/SmoQueryModel/Node[@Name='{0}']", nodeName)); return (XmlElement)doc.SelectSingleNode(string.Format("/SmoQueryModel/Node[@Name='{0}']", nodeName));
} }
public static string GetNavPathField(string xmlFile, string nodeName, string parent) public static XmlElement GetNavPathElement(string xmlFile, string nodeName, string parent)
{ {
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
doc.Load(xmlFile); doc.Load(xmlFile);
XmlElement navPathElement = (XmlElement)doc.SelectSingleNode(string.Format("/SmoQueryModel/Node[@Name='{0}']/NavigationPath[@Parent='{1}']", nodeName, parent)); XmlElement navPathElement = (XmlElement)doc.SelectSingleNode(string.Format("/SmoQueryModel/Node[@Name='{0}']/NavigationPath[@Parent='{1}']", nodeName, parent));
return navPathElement == null ? null : navPathElement.GetAttribute("Field"); return navPathElement;
} }
public static string GetNavPathFieldForUrn(string xmlFile, string nodeName, string parent) public static string GetNavPathAttribute(XmlElement navPathElement, string attributeName)
{ {
XmlDocument doc = new XmlDocument(); return navPathElement == null ? null : navPathElement.GetAttribute(attributeName);
doc.Load(xmlFile);
XmlElement navPathElement = (XmlElement)doc.SelectSingleNode(string.Format("/SmoQueryModel/Node[@Name='{0}']/NavigationPath[@Parent='{1}']", nodeName, parent));
return navPathElement == null ? null : navPathElement.GetAttribute("FieldForUrn");
} }
public static string GetNavigationPath(XmlElement nodeElement, string xmlFile, string nodeName, string parentName) public static string GetNavigationPath(XmlElement nodeElement, string nodeName, XmlElement navPathElement)
{ {
string navPathField = GetNavPathField(xmlFile, nodeName, parentName); string navPathField = GetNavPathAttribute(navPathElement, "Field");
if (!string.IsNullOrEmpty(navPathField)) if (!string.IsNullOrEmpty(navPathField))
{ {
return navPathField; return navPathField;

View File

@@ -37,7 +37,7 @@
<Node Name="SqlTable" Parent="Database" /> <Node Name="SqlTable" Parent="Database" />
<Node Name="SqlHistoryTable" Type="Table" Parent="Table" > <Node Name="SqlHistoryTable" Type="Table" Parent="Table" >
<NavigationPath Parent="Table" Type="Table" Field="Parent.Tables" FieldForUrn="Parent" /> <NavigationPath Parent="Table" Type="Table" Field="Parent.Tables" />
</Node> </Node>
<Node Name="SqlView" Parent="Database" /> <Node Name="SqlView" Parent="Database" />
@@ -51,7 +51,12 @@
<Node Name="SqlCheck" Parent="Table"/> <Node Name="SqlCheck" Parent="Table"/>
<Node Name="SqlForeignKeyConstraint" Type="ForeignKey" Parent="Table" /> <Node Name="SqlForeignKeyConstraint" Type="ForeignKey" Parent="Table" />
<Node Name="SqlDefaultConstraint" Parent="Column" Collection="False" /> <Node Name="SqlDefaultConstraint" Collection="True" >
<Parent>Table</Parent>
<Parent>UserDefinedTableType</Parent>
<NavigationPath Parent="Table" Field="Columns" SubField="DefaultConstraint" FieldType="Column" />
<NavigationPath Parent="UserDefinedTableType" Field="Columns" SubField="DefaultConstraint" FieldType="Column" />
</Node>
<Node Name="SqlDmlTrigger" Type="Trigger" Parent="Table" ValidFor="NotSqlDw" /> <Node Name="SqlDmlTrigger" Type="Trigger" Parent="Table" ValidFor="NotSqlDw" />
<Node Name="SqlFullTextIndex" Parent="Table" Collection="False" ValidFor="NotSqlDw" /> <Node Name="SqlFullTextIndex" Parent="Table" Collection="False" ValidFor="NotSqlDw" />
<Node Name="SqlStatistic" Parent="TableViewBase"/> <Node Name="SqlStatistic" Parent="TableViewBase"/>

View File

@@ -41,6 +41,12 @@ NodeType: Constraint Label: CK_Employee_HireDate SubType: Status:
NodeType: Constraint Label: CK_Employee_MaritalStatus SubType: Status: NodeType: Constraint Label: CK_Employee_MaritalStatus SubType: Status:
NodeType: Constraint Label: CK_Employee_SickLeaveHours SubType: Status: NodeType: Constraint Label: CK_Employee_SickLeaveHours SubType: Status:
NodeType: Constraint Label: CK_Employee_VacationHours SubType: Status: NodeType: Constraint Label: CK_Employee_VacationHours SubType: Status:
NodeType: Constraint Label: DF_Employee_SalariedFlag SubType: Status:
NodeType: Constraint Label: DF_Employee_VacationHours SubType: Status:
NodeType: Constraint Label: DF_Employee_SickLeaveHours SubType: Status:
NodeType: Constraint Label: DF_Employee_CurrentFlag SubType: Status:
NodeType: Constraint Label: DF_Employee_rowguid SubType: Status:
NodeType: Constraint Label: DF_Employee_ModifiedDate SubType: Status:
NodeType: Index Label: NonClusteredIndex-Login (Non-Unique, Non-Clustered) SubType: Status: NodeType: Index Label: NonClusteredIndex-Login (Non-Unique, Non-Clustered) SubType: Status:
NodeType: Index Label: PK_Employee_BusinessEntityID (Unique, Clustered) SubType:PrimaryKey Status: NodeType: Index Label: PK_Employee_BusinessEntityID (Unique, Clustered) SubType:PrimaryKey Status:
NodeType: Statistic Label: NonClusteredIndex-Login SubType: Status: NodeType: Statistic Label: NonClusteredIndex-Login SubType: Status:
@@ -94,6 +100,10 @@ NodeType: Column Label: ModifiedDate (datetime, not null) SubType: Status:
NodeType: Key Label: PK_Person_BusinessEntityID SubType:PrimaryKey Status: NodeType: Key Label: PK_Person_BusinessEntityID SubType:PrimaryKey Status:
NodeType: Constraint Label: CK_Person_EmailPromotion SubType: Status: NodeType: Constraint Label: CK_Person_EmailPromotion SubType: Status:
NodeType: Constraint Label: CK_Person_PersonType SubType: Status: NodeType: Constraint Label: CK_Person_PersonType SubType: Status:
NodeType: Constraint Label: DF_Person_NameStyle SubType: Status:
NodeType: Constraint Label: DF_Person_EmailPromotion SubType: Status:
NodeType: Constraint Label: DF_Person_rowguid SubType: Status:
NodeType: Constraint Label: DF_Person_ModifiedDate SubType: Status:
NodeType: Trigger Label: TableTrigger SubType: Status: NodeType: Trigger Label: TableTrigger SubType: Status:
NodeType: Index Label: PK_Person_BusinessEntityID (Unique, Clustered) SubType:PrimaryKey Status: NodeType: Index Label: PK_Person_BusinessEntityID (Unique, Clustered) SubType:PrimaryKey Status:
NodeType: Statistic Label: PK_Person_BusinessEntityID SubType: Status: NodeType: Statistic Label: PK_Person_BusinessEntityID SubType: Status: