internal static void AddChangeComplexTypePropertiesCommands( ComplexType complexType, IDictionary <string, Property> complexTypePropertiesMap, IList <IRawDataSchemaColumn> columns, IList <Command> commands) { Debug.Assert(complexTypePropertiesMap != null, "Parameter complexTypePropertiesMap is null"); if (complexTypePropertiesMap != null) { var columnsDictionary = columns.ToDictionary(c => c.Name); var createdProperties = new HashSet <string>(); var storageModel = complexType.Artifact.StorageModel(); // Iterate current properties decide whether to delete, create, update or skip. foreach (var propertyName in complexTypePropertiesMap.Keys) { // If the column is not found in columns dictionary delete it. if (!columnsDictionary.ContainsKey(propertyName)) { commands.Add(complexTypePropertiesMap[propertyName].GetDeleteCommand()); } // Match is found between schema column and complex type property. else { var complexTypeProperty = complexTypePropertiesMap[propertyName]; var schemaColumn = columnsDictionary[propertyName]; // Special case if the property is a complex property, we just need to delete it. if (complexTypeProperty is ComplexConceptualProperty) { // Delete the complex property commands.Add(complexTypeProperty.GetDeleteCommand()); // Create a new complex property AddCreateComplexTypePropertyCommands(storageModel, schemaColumn, null, complexType, commands); // Add the property to the "created-list" so we don't create the property in the second pass. createdProperties.Add(propertyName); } // If ProviderDataType == -1 (Unsupported type) we should skip the sync operation else if (schemaColumn.ProviderDataType != -1) { // Update the ComplexType's property to look like the schemaColumn var primitiveType = ModelHelper.GetPrimitiveType( storageModel, schemaColumn.NativeDataType, schemaColumn.ProviderDataType); // PrimitiveType is null if there is no compatible EF type for the schema column type. if (primitiveType != null) { // sync the property type commands.Add(new ChangePropertyTypeCommand(complexTypeProperty, primitiveType.GetEdmPrimitiveType().Name)); // sync the property facets. // We only update the facets that are displayed in Function Import dialog return type view list: // - Nullable. // - Max Size. // - Precision. // - Scale. commands.Add(new ChangePropertyTypeNullableCommand(complexTypeProperty, schemaColumn.IsNullable)); commands.Add( new SetPropertyFacetsCommand( complexTypeProperty , null // Default value , ModelHelper.GetMaxLengthFacetValue(schemaColumn.Size) // Size or MaxLength , null // Fixed Length , DefaultableValueUIntOrNone.GetFromNullableUInt(schemaColumn.Precision) , DefaultableValueUIntOrNone.GetFromNullableUInt(schemaColumn.Scale) , null // unicode , null // collation , null // concurrency mode )); } } } } // Second pass: Iterate columns list to add columns that are not in complex type properties foreach (var columnName in columnsDictionary.Keys) { if (false == complexTypePropertiesMap.ContainsKey(columnName) && false == createdProperties.Contains(columnName)) { AddCreateComplexTypePropertyCommands(storageModel, columnsDictionary[columnName], null, complexType, commands); } } } }
/// <summary> /// Add commands to create and set the facets for a new complex type property to match the column. /// </summary> internal static void AddCreateComplexTypePropertyCommands( StorageEntityModel storageModel, IRawDataSchemaColumn column, CreateComplexTypeCommand cmdNewComplexType, ComplexType complexType, IList <Command> commands) { // Assert if both cmdNewComplexType and complexType are null or if both are not null. Debug.Assert( ((cmdNewComplexType != null && complexType == null) || (cmdNewComplexType == null && complexType != null)), "Both cmdNewComplexType and complexType are null or both are not null. cmdNewComplexType is null : " + (cmdNewComplexType == null).ToString() + ", complexType is null : " + (complexType == null).ToString()); if ((cmdNewComplexType != null && complexType == null) || (cmdNewComplexType == null && complexType != null)) { // Skip creating the complex type property for a column if the column type is unknown or not supported ( providerDataType == -1 ). if (column.ProviderDataType != -1) { var primitiveType = ModelHelper.GetPrimitiveType(storageModel, column.NativeDataType, column.ProviderDataType); // We only create complex type property if primitive type is known. if (primitiveType != null) { CreateComplexTypePropertyCommand cmdNewComplexTypeProperty = null; // if complex type is not created yet. if (cmdNewComplexType != null) { cmdNewComplexTypeProperty = new CreateComplexTypePropertyCommand( // Automatically "fix" the property Name if it contains bad character. // We need to do this since we don't let the user to change the property name from the Function Import dialog. ModelHelper.CreateValidSimpleIdentifier(column.Name), cmdNewComplexType, primitiveType.GetEdmPrimitiveType().Name, column.IsNullable); commands.Add(cmdNewComplexTypeProperty); } else { cmdNewComplexTypeProperty = new CreateComplexTypePropertyCommand( // Automatically "fix" the property Name if it contains bad character. // We need to do this since we don't let the user to change the property name from the Function Import dialog. ModelHelper.CreateValidSimpleIdentifier(column.Name), complexType, primitiveType.GetEdmPrimitiveType().Name, column.IsNullable); commands.Add(cmdNewComplexTypeProperty); } // We only update the facets that are displayed in Function Import dialog return type view list: // - Nullable. // - Max Size. // - Precision. // - Scale. var cmdSetPropertyFacets = new SetPropertyFacetsCommand( cmdNewComplexTypeProperty , null // Default value , ModelHelper.GetMaxLengthFacetValue(column.Size) , null // Fixed Length , DefaultableValueUIntOrNone.GetFromNullableUInt(column.Precision) , DefaultableValueUIntOrNone.GetFromNullableUInt(column.Scale) , null // unicode , null // collation , null // concurrency mode ); commands.Add(cmdSetPropertyFacets); } } } }