From c40c740a73f69301987a975ab5bb914d6ef3ba03 Mon Sep 17 00:00:00 2001 From: Leila Lali Date: Fri, 9 Feb 2018 08:51:41 -0800 Subject: [PATCH] fixed the bug with loading default constraints in oe (#582) * fixed the bug with loading default constraints in oe --- docs/guide/object_explorer.md | 2 +- .../ObjectExplorer/SmoModel/SmoQueryModel.cs | 37 +++++++++++++--- .../ObjectExplorer/SmoModel/SmoQueryModel.tt | 43 +++++++++++++------ .../SmoModel/SmoQueryModelDefinition.xml | 9 +++- .../Baselines/AllSqlObjects.txt | 10 +++++ 5 files changed, 80 insertions(+), 21 deletions(-) diff --git a/docs/guide/object_explorer.md b/docs/guide/object_explorer.md index edaa8209..36409da6 100644 --- a/docs/guide/object_explorer.md +++ b/docs/guide/object_explorer.md @@ -45,7 +45,7 @@ ### How to add a new SQL object type To add a new object type, * 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 diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.cs index 9a6c5454..837258e5 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.cs @@ -487,17 +487,42 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel public override IEnumerable Query(SmoQueryContext context, string filter, bool refresh, IEnumerable extraProperties) { - Column parentColumn = context.Parent as Column; - if (parentColumn != null) + Table parentTable = context.Parent as Table; + if (parentTable != null) { - var retValue = parentColumn.DefaultConstraint; + var retValue = parentTable.Columns; if (retValue != null) { - if (refresh) + retValue.ClearAndInitialize(filter, extraProperties); + List subFieldResult = new List(); + 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 subFieldResult = new List(); + 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(); diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.tt b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.tt index e5f442ad..f68b2bfc 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.tt +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.tt @@ -71,7 +71,10 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel WriteLine("{"); 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)); @@ -83,7 +86,27 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel if (IsCollection(nodeElement)) { 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 { @@ -142,27 +165,23 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel 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(); 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("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(); - 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"); + return navPathElement == null ? null : navPathElement.GetAttribute(attributeName); } - 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)) { return navPathField; diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModelDefinition.xml b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModelDefinition.xml index c0367964..7112a4bb 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModelDefinition.xml +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModelDefinition.xml @@ -37,7 +37,7 @@ - + @@ -51,7 +51,12 @@ - + + Table + UserDefinedTableType + + + diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test.Common/TestData/ObjectExplorer/Baselines/AllSqlObjects.txt b/test/Microsoft.SqlTools.ServiceLayer.Test.Common/TestData/ObjectExplorer/Baselines/AllSqlObjects.txt index 4ae253fd..37403474 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test.Common/TestData/ObjectExplorer/Baselines/AllSqlObjects.txt +++ b/test/Microsoft.SqlTools.ServiceLayer.Test.Common/TestData/ObjectExplorer/Baselines/AllSqlObjects.txt @@ -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_SickLeaveHours 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: PK_Employee_BusinessEntityID (Unique, Clustered) SubType:PrimaryKey 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: Constraint Label: CK_Person_EmailPromotion 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: Index Label: PK_Person_BusinessEntityID (Unique, Clustered) SubType:PrimaryKey Status: NodeType: Statistic Label: PK_Person_BusinessEntityID SubType: Status: