public static void TryGetAdjustedName_correctly_retrieves_the_adjusted_identifier_associated_with_an_object()
        {
            const string identifier = "Identifier";
            const string adjustedIdentifier1 = "Identifier1";
            const string adjustedIdentifier2 = "Identifier2";
            const string adjustedIdentifier3 = "Identifier3";
            string adjustedIdentifier;

            var value1 = new object();
            var value2 = new object();
            var value3 = new object();

            var service = new UniqueIdentifierService();
            service.RegisterUsedIdentifier(identifier);

            Assert.Equal(adjustedIdentifier1, service.AdjustIdentifier(identifier, value1));
            Assert.Equal(adjustedIdentifier2, service.AdjustIdentifier(identifier, value2));
            Assert.Equal(adjustedIdentifier3, service.AdjustIdentifier(identifier, value3));
            Assert.True(service.TryGetAdjustedName(value1, out adjustedIdentifier));
            Assert.Equal(adjustedIdentifier, adjustedIdentifier1);
            Assert.True(service.TryGetAdjustedName(value2, out adjustedIdentifier));
            Assert.Equal(adjustedIdentifier, adjustedIdentifier2);
            Assert.True(service.TryGetAdjustedName(value3, out adjustedIdentifier));
            Assert.Equal(adjustedIdentifier, adjustedIdentifier3);
        }
        public static void AdjustIdentifier_with_Ordinal_comparer_and_identity_transform_returns_expected_result()
        {
            const string identifier = "Identifier";
            const string adjustedIdentifier1 = "Identifier1";
            const string adjustedIdentifier2 = "Identifier2";
            const string adjustedIdentifier3 = "Identifier3";

            var service = new UniqueIdentifierService(StringComparer.Ordinal);
            service.RegisterUsedIdentifier(identifier);

            Assert.Equal(adjustedIdentifier1, service.AdjustIdentifier(identifier));
            Assert.Equal(adjustedIdentifier2, service.AdjustIdentifier(identifier));
            Assert.Equal(adjustedIdentifier3, service.AdjustIdentifier(identifier));
        }
Esempio n. 3
0
        private static string CreateModelName(string storeName, UniqueIdentifierService uniqueNameService)
        {
            Debug.Assert(!string.IsNullOrEmpty(storeName), "storeName cannot be null or empty string");
            Debug.Assert(uniqueNameService != null, "uniqueNameService != null");

            return(uniqueNameService.AdjustIdentifier(CreateModelName(storeName)));
        }
        public static void AdjustIdentifier_with_OrdinalIgnoreCase_comparer_and_custom_transform_returns_expected_result()
        {
            const string identifier = "My.Identifier";
            const string usedIdentifier = "My_IdENtIfiEr";
            const string adjustedIdentifier1 = "My_Identifier1";
            const string adjustedIdentifier2 = "My_Identifier2";
            const string adjustedIdentifier3 = "My_Identifier3";

            Func<string, string> transform = s => s.Replace(".", "_");

            var service = new UniqueIdentifierService(StringComparer.OrdinalIgnoreCase, transform);
            service.RegisterUsedIdentifier(usedIdentifier);

            Assert.Equal(adjustedIdentifier1, service.AdjustIdentifier(identifier));
            Assert.Equal(adjustedIdentifier2, service.AdjustIdentifier(identifier));
            Assert.Equal(adjustedIdentifier3, service.AdjustIdentifier(identifier));
        }
Esempio n. 5
0
        // internal for testing
        internal ComplexType CreateComplexTypeFromRowType(SimpleMappingContext mappingContext, RowType rowType, string typeName)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(!string.IsNullOrEmpty(typeName), "typeName cannot be null or empty string.");
            Debug.Assert(rowType != null, "rowType != null");

            var uniquePropertyNameService = new UniqueIdentifierService();

            uniquePropertyNameService.AdjustIdentifier(typeName);

            return
                (ComplexType.Create(
                     typeName,
                     _namespaceName,
                     DataSpace.CSpace,
                     rowType.Properties.Select(p => GenerateScalarProperty(mappingContext, p, uniquePropertyNameService)),
                     null));
        }
Esempio n. 6
0
        // internal for testing
        internal EntityType GenerateEntityType(
            SimpleMappingContext mappingContext, EntityType storeEntityType, UniqueIdentifierService globallyUniqueTypeNames)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(storeEntityType != null, "storeEntityType != null");
            Debug.Assert(globallyUniqueTypeNames != null, "globallyUniqueTypeNames != null");

            var conceptualEntityTypeName = CreateModelName(
                (_pluralizationService != null) ? _pluralizationService.Singularize(storeEntityType.Name) : storeEntityType.Name,
                globallyUniqueTypeNames);

            var uniquePropertyNameService = new UniqueIdentifierService();

            uniquePropertyNameService.AdjustIdentifier(conceptualEntityTypeName);

            var edmMembers     = new List <EdmMember>();
            var keyMemberNames = new List <string>();

            foreach (var storeProperty in storeEntityType.Properties)
            {
                // cannot skip this even if the store property is foreign key and generating foreign keys is disabled
                // since it creates property mappings the will be used when mapping association types.
                var conceptualProperty = GenerateScalarProperty(mappingContext, storeProperty, uniquePropertyNameService);

                if (_generateForeignKeyProperties ||
                    !mappingContext.StoreForeignKeyProperties.Contains(storeProperty) ||
                    storeEntityType.KeyMembers.Contains(storeProperty))
                {
                    edmMembers.Add(conceptualProperty);
                    if (storeEntityType.KeyMembers.Contains(storeProperty))
                    {
                        keyMemberNames.Add(conceptualProperty.Name);
                    }
                }
            }

            var conceptualEntity = EntityType.Create(
                conceptualEntityTypeName, _namespaceName, DataSpace.CSpace, keyMemberNames, edmMembers, null);

            mappingContext.AddMapping(storeEntityType, conceptualEntity);

            return(conceptualEntity);
        }
        // internal for testing
        internal FunctionParameter CreateFunctionParameter(
            FunctionDetailsRowView functionDetailsRow, UniqueIdentifierService uniqueIdentifierService, int parameterIndex,
            IList<EdmSchemaError> errors)
        {
            Debug.Assert(functionDetailsRow != null, "functionDetailsRow != null");
            Debug.Assert(uniqueIdentifierService != null, "uniqueIdentifierService != null");
            Debug.Assert(errors != null, "errors != null");

            var parameterType = GetFunctionParameterType(functionDetailsRow, parameterIndex, errors);
            if (parameterType == null)
            {
                return null;
            }

            ParameterMode parameterMode;
            if (!functionDetailsRow.TryGetParameterMode(out parameterMode))
            {
                errors.Add(
                    new EdmSchemaError(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            Resources_VersioningFacade.ParameterDirectionNotValid,
                            functionDetailsRow.ProcedureName,
                            functionDetailsRow.ParameterName,
                            functionDetailsRow.ProcParameterMode),
                        (int)ModelBuilderErrorCode.ParameterDirectionNotValid,
                        EdmSchemaErrorSeverity.Warning));
                return null;
            }

            var parameterName =
                uniqueIdentifierService.AdjustIdentifier(
                    ModelGeneratorUtils.CreateValidEcmaName(functionDetailsRow.ParameterName, 'p'));

            return FunctionParameter.Create(parameterName, parameterType, parameterMode);
        }
        internal AssociationSet TryCreateAssociationSet(
            List<RelationshipDetailsRow> relationshipDetailsRows,
            EntityRegister entityRegister,
            List<AssociationType> associationTypes)
        {
            Debug.Assert(relationshipDetailsRows.Count > 0, "relationshipDetailsRows.Count > 0");

            var firstRow = relationshipDetailsRows.First();
            var errors = new List<EdmSchemaError>();

            AssociationType associationType;
            var isValidAssociationType = false;
            var typeName = _usedTypeNames.AdjustIdentifier(firstRow.RelationshipName);
            AssociationEndMember pkEnd = null;
            AssociationEndMember fkEnd = null;
            ReferentialConstraint constraint = null;

            var pkEntityType = TryGetEndEntity(entityRegister, firstRow.GetMostQualifiedPrimaryKey(), errors);
            var fkEntityType = TryGetEndEntity(entityRegister, firstRow.GetMostQualifiedForeignKey(), errors);

            if (ValidateEndEntities(relationshipDetailsRows, pkEntityType, fkEntityType, errors))
            {
                var someFKColumnsAreNullable =
                    _targetEntityFrameworkVersion == EntityFrameworkVersion.Version1
                        ? AreAllFKColumnsNullable(relationshipDetailsRows, fkEntityType)
                        : AreAnyFKColumnsNullable(relationshipDetailsRows, fkEntityType);

                var usedEndNames = new UniqueIdentifierService(StringComparer.OrdinalIgnoreCase);

                pkEnd = AssociationEndMember.Create(
                    usedEndNames.AdjustIdentifier(pkEntityType.Name),
                    pkEntityType.GetReferenceType(),
                    someFKColumnsAreNullable
                        ? RelationshipMultiplicity.ZeroOrOne
                        : RelationshipMultiplicity.One,
                    firstRow.RelationshipIsCascadeDelete
                        ? OperationAction.Cascade
                        : OperationAction.None,
                    null);

                fkEnd = AssociationEndMember.Create(
                    usedEndNames.AdjustIdentifier(fkEntityType.Name),
                    fkEntityType.GetReferenceType(),
                    !someFKColumnsAreNullable && AreRelationshipColumnsTheFullKey(relationshipDetailsRows, fkEntityType, r => r.FKColumn)
                        ? RelationshipMultiplicity.ZeroOrOne
                        : RelationshipMultiplicity.Many,
                    OperationAction.None,
                    null);

                constraint = TryCreateReferentialConstraint(relationshipDetailsRows, pkEnd, fkEnd, errors);
                if (constraint != null
                    && ValidateReferentialConstraint(constraint, _generateForeignKeyProperties, typeName, associationTypes, errors))
                {
                    isValidAssociationType = true;
                }
            }

            associationType = AssociationType.Create(
                typeName,
                _namespaceName,
                false,
                DataSpace.SSpace,
                pkEnd,
                fkEnd,
                constraint,
                CreateMetadataProperties(!isValidAssociationType, errors));

            associationTypes.Add(associationType);

            return isValidAssociationType
                       ? CreateAssociationSet(associationType, entityRegister)
                       : null;
        }
            public void GenerateFunction_returns_function_with_unique_return_type_name()
            {
                var storeReturnType = CreateRowType(CreateProperty("foo", PrimitiveTypeKind.Int32)).GetCollectionType();
                var returnParameter =
                    FunctionParameter.Create("ReturnType", storeReturnType, ParameterMode.ReturnValue);

                var storeFunction =
                    EdmFunction.Create(
                        "foo*",
                        "bar",
                        DataSpace.SSpace,
                        new EdmFunctionPayload { ReturnParameters = new[] { returnParameter } },
                        null);

                var uniqueContainerNames = new UniqueIdentifierService();
                uniqueContainerNames.AdjustIdentifier("foo_");

                var globallyUniqueTypeNames = new UniqueIdentifierService();
                globallyUniqueTypeNames.AdjustIdentifier("foo_1_Result");

                var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
                var functionImport =
                    CreateOneToOneMappingBuilder()
                        .GenerateFunction(mappingContext, storeFunction, uniqueContainerNames, globallyUniqueTypeNames);

                Assert.NotNull(functionImport);
                Assert.Equal(
                    "myModel.foo_1_Result1",
                    ((CollectionType)functionImport.ReturnParameter.TypeUsage.EdmType).TypeUsage.EdmType.FullName);
                Assert.Empty(mappingContext.Errors);
            }
            public void GenerateEntityType_entity_type_name_is_sanitized_and_uniquified()
            {
                var storeEntityType =
                    EntityType.Create(
                        "foo$", "bar", DataSpace.SSpace, new[] { "Id" },
                        new[] { EdmProperty.CreatePrimitive("Id", GetStoreEdmType("int")) }, null);

                var uniqueEntityTypeName = new UniqueIdentifierService();
                uniqueEntityTypeName.AdjustIdentifier("foo_");
                var conceptualEntityType =
                    CreateOneToOneMappingBuilder()
                        .GenerateEntityType(
                            new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true),
                            storeEntityType,
                            uniqueEntityTypeName);

                Assert.Equal("foo_1", conceptualEntityType.Name);
            }
            public void GenerateScalarProperty_converts_and_uniquifies_property_names()
            {
                var uniquePropertyNameService = new UniqueIdentifierService();
                uniquePropertyNameService.AdjustIdentifier("p_1");

                var storeProperty = EdmProperty.CreatePrimitive("p*1", GetStoreEdmType("int"));

                var conceptualProperty =
                    OneToOneMappingBuilder
                        .GenerateScalarProperty(
                            new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true),
                            storeProperty,
                            uniquePropertyNameService);

                Assert.Equal("p_11", conceptualProperty.Name);
            }
            public void GenerateEntitySet_entity_set_name_sanitized_and_uniquified()
            {
                var storeEntityType =
                    EntityType.Create(
                        "foo", "bar", DataSpace.SSpace, new[] { "Id" },
                        new[] { EdmProperty.CreatePrimitive("Id", GetStoreEdmType("int")) }, null);

                var storeEntitySet = EntitySet.Create("foo$", "bar", null, null, storeEntityType, null);

                var uniqueEntityContainerNames = new UniqueIdentifierService();
                uniqueEntityContainerNames.AdjustIdentifier("foo_");

                var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);

                CreateOneToOneMappingBuilder()
                    .GenerateEntitySet(
                        mappingContext,
                        storeEntitySet,
                        uniqueEntityContainerNames,
                        new UniqueIdentifierService());

                var conceptualModelEntitySet = mappingContext[storeEntitySet];

                Assert.Equal("foo_1", conceptualModelEntitySet.Name);
                Assert.Equal("foo", conceptualModelEntitySet.ElementType.Name);
            }
            public void CreateFunctionParameter_applies_ECMA_name_conversion_and_uniquifies_parameter_name()
            {
                var errors = new List<EdmSchemaError>();
                var uniquifiedIdentifierService = new UniqueIdentifierService();
                uniquifiedIdentifierService.AdjustIdentifier("p_r_m");

                var parameter = CreateStoreModelBuilder()
                    .CreateFunctionParameter(
                        CreateFunctionDetailsRow(
                            functionName: "function", paramName: "p@r@m", paramTypeName: "smallint", parameterDirection: "INOUT"),
                        uniquifiedIdentifierService,
                        1, errors);

                Assert.NotNull(parameter);
                Assert.Empty(errors);
                Assert.Equal("p_r_m1", parameter.Name);
                Assert.Equal("smallint", parameter.TypeUsage.EdmType.Name);
                Assert.Equal(ParameterMode.InOut, parameter.Mode);
            }
        // internal for testing
        internal EntityType GenerateEntityType(
            SimpleMappingContext mappingContext, EntityType storeEntityType, UniqueIdentifierService globallyUniqueTypeNames)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(storeEntityType != null, "storeEntityType != null");
            Debug.Assert(globallyUniqueTypeNames != null, "globallyUniqueTypeNames != null");

            var conceptualEntityTypeName = CreateModelName(
                (_pluralizationService != null) ? _pluralizationService.Singularize(storeEntityType.Name) : storeEntityType.Name,
                globallyUniqueTypeNames);

            var uniquePropertyNameService = new UniqueIdentifierService();
            uniquePropertyNameService.AdjustIdentifier(conceptualEntityTypeName);

            var edmMembers = new List<EdmMember>();
            var keyMemberNames = new List<string>();

            foreach (var storeProperty in storeEntityType.Properties)
            {
                // cannot skip this even if the store property is foreign key and generating foreign keys is disabled 
                // since it creates property mappings the will be used when mapping association types.
                var conceptualProperty = GenerateScalarProperty(mappingContext, storeProperty, uniquePropertyNameService);

                if (_generateForeignKeyProperties
                    || !mappingContext.StoreForeignKeyProperties.Contains(storeProperty)
                    || storeEntityType.KeyMembers.Contains(storeProperty))
                {
                    edmMembers.Add(conceptualProperty);
                    if (storeEntityType.KeyMembers.Contains(storeProperty))
                    {
                        keyMemberNames.Add(conceptualProperty.Name);
                    }
                }
            }

            var conceptualEntity = EntityType.Create(
                conceptualEntityTypeName, _namespaceName, DataSpace.CSpace, keyMemberNames, edmMembers, null);

            mappingContext.AddMapping(storeEntityType, conceptualEntity);

            return conceptualEntity;
        }
        private static string CreateModelName(string storeName, UniqueIdentifierService uniqueNameService)
        {
            Debug.Assert(!string.IsNullOrEmpty(storeName), "storeName cannot be null or empty string");
            Debug.Assert(uniqueNameService != null, "uniqueNameService != null");

            return uniqueNameService.AdjustIdentifier(CreateModelName(storeName));
        }
        // internal for testing
        internal ComplexType CreateComplexTypeFromRowType(SimpleMappingContext mappingContext, RowType rowType, string typeName)
        {
            Debug.Assert(mappingContext != null, "mappingContext != null");
            Debug.Assert(!string.IsNullOrEmpty(typeName), "typeName cannot be null or empty string.");
            Debug.Assert(rowType != null, "rowType != null");

            var uniquePropertyNameService = new UniqueIdentifierService();
            uniquePropertyNameService.AdjustIdentifier(typeName);

            return
                ComplexType.Create(
                    typeName,
                    _namespaceName,
                    DataSpace.CSpace,
                    rowType.Properties.Select(p => GenerateScalarProperty(mappingContext, p, uniquePropertyNameService)),
                    null);
        }