/// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in EDM.
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating a store type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating an EDM type and a set of facets</returns>
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            if (storeType == null)
            {
                throw new ArgumentNullException("storeType");
            }

            string storeTypeName = storeType.EdmType.Name.ToLowerInvariant();

            if (!base.StoreTypeNameToEdmPrimitiveType.ContainsKey(storeTypeName))
            {
                throw new ArgumentException(String.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }

            PrimitiveType edmPrimitiveType = base.StoreTypeNameToEdmPrimitiveType[storeTypeName];

            int  maxLength   = 0;
            bool isUnicode   = true;
            bool isFixedLen  = false;
            bool isUnbounded = true;

            PrimitiveTypeKind newPrimitiveTypeKind;

            switch (storeTypeName)
            {
            // for some types we just go with simple type usage with no facets
            case "tinyint":
            case "smallint":
            case "bigint":
            case "bit":
            case "uniqueidentifier":
            case "int":
            case "guid":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

            case "nvarchar":
            case "varchar":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = false;
                break;

            case "nchar":
            case "char":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = true;
                break;

            case "nvarchar(max)":
            case "varchar(max)":
            case "ntext":
            case "text":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            case "binary":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = true;
                break;

            case "varbinary":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = false;
                break;

            case "varbinary(max)":
            case "image":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            case "float":
            case "real":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

            case "decimal":
            case "numeric":
            {
                byte precision;
                byte scale;
                if (storeType.TryGetPrecision(out precision) && storeType.TryGetScale(out scale))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, precision, scale));
                }
                else
                {
                    return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType));
                }
            }

            case "money":
                return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, 19, 4));

            case "datetime":
                return(TypeUsage.CreateDateTimeTypeUsage(edmPrimitiveType, null));

            case "time":
                return(TypeUsage.CreateTimeTypeUsage(edmPrimitiveType, null));

            default:
                throw new NotSupportedException(String.Format("Jet does not support the type '{0}'.", storeTypeName));
            }

            Debug.Assert(newPrimitiveTypeKind == PrimitiveTypeKind.String || newPrimitiveTypeKind == PrimitiveTypeKind.Binary, "at this point only string and binary types should be present");

            switch (newPrimitiveTypeKind)
            {
            case PrimitiveTypeKind.String:
                if (!isUnbounded)
                {
                    return(TypeUsage.CreateStringTypeUsage(edmPrimitiveType, isUnicode, isFixedLen, maxLength));
                }
                else
                {
                    return(TypeUsage.CreateStringTypeUsage(edmPrimitiveType, isUnicode, isFixedLen));
                }

            case PrimitiveTypeKind.Binary:
                if (!isUnbounded)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, isFixedLen, maxLength));
                }
                else
                {
                    return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, isFixedLen));
                }

            default:
                throw new NotSupportedException(String.Format("Jet does not support the type '{0}'.", storeTypeName));
            }
        }
        public void Build_builds_valid_DbDatabaseMapping_for_functions()
        {
            var rowTypeProperty = CreateStoreProperty("p1", "int");

            var complexTypeProperty =
                EdmProperty.Create(
                    "p2",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));

            var functionImportReturnComplexType =
                ComplexType.Create(
                    "CT",
                    "entityModel",
                    DataSpace.CSpace,
                    new[] { complexTypeProperty }, null);

            var storeFunction = EdmFunction.Create(
                "f_s",
                "storeModel",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        RowType.Create(new[] { rowTypeProperty }, null).GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                null);

            var functionImport =
                EdmFunction.Create(
                    "f_c",
                    "entityModel",
                    DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = true,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        functionImportReturnComplexType.GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                    null);

            var modelContainer = EntityContainer.Create("C_C", DataSpace.CSpace, new EntitySet[0], new[] { functionImport }, null);
            var storeContainer = EntityContainer.Create("C_S", DataSpace.SSpace, new EntitySet[0], null, null);

            var storeModel = EdmModel.CreateStoreModel(storeContainer, null, null);

            storeModel.AddItem(storeFunction);

            var mappingContext = new SimpleMappingContext(storeModel, true);

            mappingContext.AddMapping(rowTypeProperty, complexTypeProperty);
            mappingContext.AddMapping(storeFunction, functionImport);
            mappingContext.AddMapping(storeContainer, modelContainer);

            var entityModel = DbDatabaseMappingBuilder.Build(mappingContext).ConceptualModel;

            Assert.NotNull(entityModel);
            Assert.Equal(new[] { "f_c" }, entityModel.Containers.Single().FunctionImports.Select(f => f.Name));
            Assert.Equal(new[] { "CT" }, entityModel.ComplexTypes.Select(t => t.Name));
        }
Esempio n. 3
0
 private static EdmType GetEdmType(DbModel model, ReadOnlyCollection <PrimitiveType> storeTypes, PrimitiveTypeKind typeKind)
 {
     return(model.ProviderManifest.GetStoreType(TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(typeKind))).EdmType);
 }
        public void BuildAssociationSetMappings_builds_conceptual_assocation_set_mapping_for_collapsed_store_entity_sets()
        {
            #region Setting up many to many relationship in the SSpace Teacher * -- 1 TeacherStudents 1 -- * Teachers

            var joinStoreEntityType =
                EntityType.Create(
                    "TeacherStudents", "ns.Store", DataSpace.SSpace,
                    new[] { "JoinTeacherId", "JoinStudentId" },
                    new[]
            {
                CreateStoreProperty("JoinTeacherId", "int"),
                CreateStoreProperty("JoinStudentId", "int")
            }, null);

            var joinStoreEntitySet =
                EntitySet.Create("TeacherStudentsSet", "dbo", "TeacherStudentTable", null, joinStoreEntityType, null);

            var storeTeacherEntityType =
                EntityType.Create(
                    "Teacher", "ns.Store", DataSpace.SSpace, new[] { "TeacherId" },
                    new[] { CreateStoreProperty("TeacherId", "int") }, null);
            var storeTeacherEntitySet =
                EntitySet.Create("TeachersSet", "dbo", "Teachers", null, storeTeacherEntityType, null);

            var storeStudentEntityType =
                EntityType.Create(
                    "Student", "ns.Store", DataSpace.SSpace, new[] { "StudentId" },
                    new[] { CreateStoreProperty("StudentId", "int") }, null);
            var storeStudentEntitySet =
                EntitySet.Create("StudentSet", "dbo", "Students", null, storeStudentEntityType, null);

            var storeTeachersEndMember =
                AssociationEndMember.Create(
                    "Teachers", storeTeacherEntityType.GetReferenceType(), RelationshipMultiplicity.Many,
                    OperationAction.None, null);

            var storeTeacherStudentsfromTeachersEndMember =
                AssociationEndMember.Create(
                    "TeacherStudents_fromTeachers", joinStoreEntityType.GetReferenceType(), RelationshipMultiplicity.One,
                    OperationAction.None, null);

            var storeTeacherAssociationType =
                AssociationType.Create(
                    "Teacher_TeacherStudentsAssociationType", "ns.Store", false, DataSpace.SSpace,
                    storeTeachersEndMember, storeTeacherStudentsfromTeachersEndMember,
                    new ReferentialConstraint(
                        storeTeachersEndMember, storeTeacherStudentsfromTeachersEndMember, storeTeacherEntityType.KeyProperties,
                        joinStoreEntityType.KeyProperties.Where(p => p.Name == "JoinTeacherId")),
                    null);

            var storeTeacherAssociationSet =
                AssociationSet.Create(
                    "Teacher_TeacherStudents", storeTeacherAssociationType, storeTeacherEntitySet, joinStoreEntitySet, null);

            var storeStudentsEndMember =
                AssociationEndMember.Create(
                    "Students", storeStudentEntityType.GetReferenceType(), RelationshipMultiplicity.Many,
                    OperationAction.None, null);

            var storeTeacherStudentsfromStudentsEndMember =
                AssociationEndMember.Create(
                    "TeacherStudents_fromStudents", joinStoreEntityType.GetReferenceType(), RelationshipMultiplicity.One,
                    OperationAction.None, null);

            var storeStudentAssociationType =
                AssociationType.Create(
                    "Student_TeacherStudentsAssociationType", "ns.Store", false, DataSpace.SSpace,
                    storeStudentsEndMember,
                    storeTeacherStudentsfromStudentsEndMember,
                    new ReferentialConstraint(
                        storeStudentsEndMember, storeTeacherStudentsfromStudentsEndMember, storeStudentEntityType.KeyProperties,
                        joinStoreEntityType.KeyProperties.Where(p => p.Name == "JoinStudentId")),
                    null);

            var storeStudentAssociationSet =
                AssociationSet.Create(
                    "Student_TeacherStudents", storeStudentAssociationType, storeStudentEntitySet, joinStoreEntitySet, null);

            var collapsedAssociationSet = new CollapsibleEntityAssociationSets(joinStoreEntitySet);
            collapsedAssociationSet.AssociationSets.Add(storeTeacherAssociationSet);
            collapsedAssociationSet.AssociationSets.Add(storeStudentAssociationSet);

            #endregion

            #region Setting up many to many relationship in the CSpace Teacher * -- * Teachers

            var conceptualContainer = EntityContainer.Create("ConceptualContainer", DataSpace.CSpace, null, null, null);

            var edmIntTypeUsage =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            var conceptualTeacherEntityType =
                EntityType.Create(
                    "Teacher", "ns", DataSpace.CSpace, new[] { "TeacherId" },
                    new[] { EdmProperty.Create("TeacherId", edmIntTypeUsage) }, null);

            var conceptualTeacherEntitySet =
                EntitySet.Create("TeachersSet", null, null, null, conceptualTeacherEntityType, null);

            var conceptualStudentEntityType =
                EntityType.Create(
                    "Student", "ns", DataSpace.CSpace, new[] { "StudentId" },
                    new[] { EdmProperty.Create("StudentId", edmIntTypeUsage) }, null);

            var conceptualStudentEntitySet =
                EntitySet.Create("StudentSet", "dbo", "Students", null, conceptualStudentEntityType, null);

            var conceptualTeachersEndMember =
                AssociationEndMember.Create(
                    "TeachersEnd", conceptualTeacherEntityType.GetReferenceType(), RelationshipMultiplicity.Many,
                    OperationAction.None, null);

            var conceptualStudentsEndMember =
                AssociationEndMember.Create(
                    "StudentsEnd", conceptualStudentEntityType.GetReferenceType(), RelationshipMultiplicity.Many,
                    OperationAction.None, null);

            var conceptualAssociationType =
                AssociationType.Create(
                    "TeacherStudentAssociation",
                    "ns.Model",
                    false,
                    DataSpace.CSpace,
                    conceptualTeachersEndMember,
                    conceptualStudentsEndMember,
                    new ReferentialConstraint(
                        conceptualTeachersEndMember, conceptualStudentsEndMember,
                        conceptualTeacherEntityType.KeyProperties, conceptualStudentEntityType.KeyProperties),
                    null);

            var conceptualAssociationSet =
                AssociationSet.Create(
                    "TeacherStudentSet", conceptualAssociationType, conceptualTeacherEntitySet,
                    conceptualStudentEntitySet, null);

            #endregion

            var mappingContext = new SimpleMappingContext(new EdmModel(DataSpace.SSpace), true);
            mappingContext.AddMapping(collapsedAssociationSet, conceptualAssociationSet);
            mappingContext.AddMapping(storeTeachersEndMember, conceptualTeachersEndMember);
            mappingContext.AddMapping(storeStudentsEndMember, conceptualStudentsEndMember);
            mappingContext.AddMapping(
                storeTeacherAssociationSet.AssociationSetEnds.ElementAt(0),
                conceptualAssociationSet.AssociationSetEnds.ElementAt(0));
            mappingContext.AddMapping(
                storeStudentAssociationSet.AssociationSetEnds.ElementAt(0),
                conceptualAssociationSet.AssociationSetEnds.ElementAt(1));
            mappingContext.AddMapping(
                storeStudentEntityType.KeyProperties.Single(), conceptualStudentEntityType.KeyProperties.Single());
            mappingContext.AddMapping(
                storeTeacherEntityType.KeyProperties.Single(), conceptualTeacherEntityType.KeyProperties.Single());

            var storageEntitySetMapping =
                new EntityContainerMapping(conceptualContainer, null, null, false, false);

            var associationSetMapping =
                DbDatabaseMappingBuilder.BuildAssociationSetMappings(storageEntitySetMapping, mappingContext)
                .SingleOrDefault();
            Assert.NotNull(associationSetMapping);

            var mappingFragment = associationSetMapping.TypeMappings.SingleOrDefault();
            Assert.NotNull(mappingFragment);

            var propertyMappings = mappingFragment.MappingFragments.Single().PropertyMappings;
            Assert.Equal(2, propertyMappings.Count);
            Assert.Same(conceptualTeachersEndMember, ((EndPropertyMapping)propertyMappings[0]).AssociationEnd);
            Assert.Same(conceptualStudentsEndMember, ((EndPropertyMapping)propertyMappings[1]).AssociationEnd);

            var scalarPropertyMapping = ((EndPropertyMapping)propertyMappings[0]).PropertyMappings.Single();
            Assert.Same(conceptualTeacherEntityType.KeyMembers.Single(), scalarPropertyMapping.Property);
            Assert.Same(
                joinStoreEntityType.KeyMembers.Single(m => m.Name == "JoinTeacherId"),
                scalarPropertyMapping.Column);

            scalarPropertyMapping = ((EndPropertyMapping)propertyMappings[1]).PropertyMappings.Single();
            Assert.Same(conceptualStudentEntityType.KeyMembers.Single(), scalarPropertyMapping.Property);
            Assert.Same(
                joinStoreEntityType.KeyMembers.Single(m => m.Name == "JoinStudentId"),
                scalarPropertyMapping.Column);
        }
        public void BuildComposableFunctionMapping_creates_valid_function_mapping()
        {
            var rowTypeProperty = CreateStoreProperty("p1", "int");

            var complexTypeProperty =
                EdmProperty.Create(
                    "p2",
                    TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));

            var functionImportReturnComplexType =
                ComplexType.Create(
                    "c",
                    "entityModel",
                    DataSpace.CSpace,
                    new[] { complexTypeProperty }, null);

            var storeFunction = EdmFunction.Create(
                "f_s",
                "storeModel",
                DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        RowType.Create(new[] { rowTypeProperty }, null).GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                null);

            var functionImport =
                EdmFunction.Create(
                    "f_c",
                    "entityModel",
                    DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = false,
                ReturnParameters =
                    new[]
                {
                    FunctionParameter.Create(
                        "ReturnType",
                        functionImportReturnComplexType.GetCollectionType(),
                        ParameterMode.ReturnValue)
                }
            },
                    null);

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

            mappingContext.AddMapping(rowTypeProperty, complexTypeProperty);
            mappingContext.AddMapping(storeFunction, functionImport);

            var functionImportMapping =
                DbDatabaseMappingBuilder.BuildComposableFunctionMapping(storeFunction, mappingContext);

            Assert.NotNull(functionImportMapping);
            Assert.Same(storeFunction, functionImportMapping.TargetFunction);
            Assert.Same(functionImport, functionImportMapping.FunctionImport);

            var structuralTypeMappings = functionImportMapping.StructuralTypeMappings;

            Assert.NotNull(structuralTypeMappings);

            Assert.Same(functionImportReturnComplexType, structuralTypeMappings.Single().Item1);
            Assert.Empty(structuralTypeMappings.Single().Item2);
            Assert.Same(complexTypeProperty, structuralTypeMappings.Single().Item3.Single().Property);
            Assert.Same(rowTypeProperty, ((ScalarPropertyMapping)structuralTypeMappings.Single().Item3.Single()).Column);
        }
Esempio n. 6
0
        public void Can_create_composable_function_import_with_entity_type_hierarchy()
        {
            DbProviderManifest providerManifest;
            var containerMapping = GetContainerMapping(out providerManifest);

            var cTypeUsageInt = TypeUsage.Create(
                PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32),
                new[]
            {
                Facet.Create(MetadataItem.NullableFacetDescription, false)
            });
            var sTypeUsageInt = TypeUsage.Create(
                providerManifest.GetStoreType(cTypeUsageInt).EdmType,
                new[]
            {
                Facet.Create(MetadataItem.NullableFacetDescription, false)
            });
            var cTypeUsageString = TypeUsage.CreateDefaultTypeUsage(
                PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            var sTypeUsageString = providerManifest.GetStoreType(cTypeUsageString);

            var itemCollection = containerMapping.StorageMappingItemCollection.EdmItemCollection.GetItems <EntityType>();
            var baseEntityType = itemCollection.Single(et => et.Name == "E");
            var entityType1    = itemCollection.Single(et => et.Name == "E1");
            var entityType2    = itemCollection.Single(et => et.Name == "E2");

            var rowType = RowType.Create(
                new[]
            {
                EdmProperty.Create("CId", sTypeUsageInt),
                EdmProperty.Create("C", sTypeUsageString),
                EdmProperty.Create("C1", sTypeUsageString),
                EdmProperty.Create("C2", sTypeUsageString),
                EdmProperty.Create("CD", sTypeUsageString)
            },
                null);

            var functionImport = EdmFunction.Create(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("R", baseEntityType.GetCollectionType(), ParameterMode.ReturnValue)
                }
            },
                null);

            var targetFunction = EdmFunction.Create(
                "SF", "N", DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("R", rowType.GetCollectionType(), ParameterMode.ReturnValue)
                }
            },
                null);

            var resultMapping = new FunctionImportResultMapping();

            var typeMapping = new FunctionImportEntityTypeMapping(
                new[] { baseEntityType },
                Enumerable.Empty <EntityType>(),
                new Collection <FunctionImportReturnTypePropertyMapping>()
            {
                new FunctionImportReturnTypeScalarPropertyMapping("Id", "CId"),
                new FunctionImportReturnTypeScalarPropertyMapping("P", "C"),
                new FunctionImportReturnTypeScalarPropertyMapping("Discriminator", "CD"),
            },
                Enumerable.Empty <FunctionImportEntityTypeMappingConditionValue>());

            resultMapping.AddTypeMapping(typeMapping);

            typeMapping = new FunctionImportEntityTypeMapping(
                Enumerable.Empty <EntityType>(),
                new[] { entityType1 },
                new Collection <FunctionImportReturnTypePropertyMapping>()
            {
                new FunctionImportReturnTypeScalarPropertyMapping("P1", "C1"),
            },
                new  []
            {
                new FunctionImportEntityTypeMappingConditionValue("CD", "E1")
            });
            resultMapping.AddTypeMapping(typeMapping);

            typeMapping = new FunctionImportEntityTypeMapping(
                Enumerable.Empty <EntityType>(),
                new[] { entityType2 },
                new Collection <FunctionImportReturnTypePropertyMapping>()
            {
                new FunctionImportReturnTypeScalarPropertyMapping("P2", "C2"),
            },
                new []
            {
                new FunctionImportEntityTypeMappingConditionValue("CD", "E2")
            });
            resultMapping.AddTypeMapping(typeMapping);

            var functionImportMapping = new FunctionImportMappingComposable(
                functionImport,
                targetFunction,
                resultMapping,
                containerMapping);

            Assert.Same(resultMapping, functionImportMapping.ResultMapping);
            Assert.Equal(2, functionImportMapping.StructuralTypeMappings.Count);
            Assert.Equal(1, functionImportMapping.TvfKeys.Length);

            Assert.Equal(typeof(E1).Name, functionImportMapping.StructuralTypeMappings[0].Item1.Name);
            Assert.Equal(1, functionImportMapping.StructuralTypeMappings[0].Item2.Count());
            Assert.Equal(3, functionImportMapping.StructuralTypeMappings[0].Item3.Count());

            Assert.Equal(typeof(E2).Name, functionImportMapping.StructuralTypeMappings[1].Item1.Name);
            Assert.Equal(1, functionImportMapping.StructuralTypeMappings[0].Item2.Count());
            Assert.Equal(3, functionImportMapping.StructuralTypeMappings[0].Item3.Count());
        }
        private static TypeUsage ConvertFromLegacyEdmTypeUsage(LegacyMetadata.TypeUsage legacyTypeUsage)
        {
            Debug.Assert(legacyTypeUsage != null, "legacyTypeUsage != null");
            Debug.Assert(legacyTypeUsage.EdmType is LegacyMetadata.PrimitiveType, "primitive type expected.");
            Debug.Assert(
                (LegacyMetadata.DataSpace) typeof(LegacyMetadata.EdmType)
                .GetProperty("DataSpace", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(legacyTypeUsage.EdmType) == LegacyMetadata.DataSpace.CSpace,
                "Expected CSpace type.");

            var legacyPrimitiveEdmType = (LegacyMetadata.PrimitiveType)legacyTypeUsage.EdmType;
            var primitiveEdmType       = FromLegacyPrimitiveType(legacyPrimitiveEdmType);

            switch (legacyPrimitiveEdmType.PrimitiveTypeKind)
            {
            case LegacyMetadata.PrimitiveTypeKind.Boolean:
            case LegacyMetadata.PrimitiveTypeKind.Byte:
            case LegacyMetadata.PrimitiveTypeKind.SByte:
            case LegacyMetadata.PrimitiveTypeKind.Int16:
            case LegacyMetadata.PrimitiveTypeKind.Int32:
            case LegacyMetadata.PrimitiveTypeKind.Int64:
            case LegacyMetadata.PrimitiveTypeKind.Guid:
            case LegacyMetadata.PrimitiveTypeKind.Double:
            case LegacyMetadata.PrimitiveTypeKind.Single:
            case LegacyMetadata.PrimitiveTypeKind.Geography:
            case LegacyMetadata.PrimitiveTypeKind.GeographyPoint:
            case LegacyMetadata.PrimitiveTypeKind.GeographyLineString:
            case LegacyMetadata.PrimitiveTypeKind.GeographyPolygon:
            case LegacyMetadata.PrimitiveTypeKind.GeographyMultiPoint:
            case LegacyMetadata.PrimitiveTypeKind.GeographyMultiLineString:
            case LegacyMetadata.PrimitiveTypeKind.GeographyMultiPolygon:
            case LegacyMetadata.PrimitiveTypeKind.GeographyCollection:
            case LegacyMetadata.PrimitiveTypeKind.Geometry:
            case LegacyMetadata.PrimitiveTypeKind.GeometryPoint:
            case LegacyMetadata.PrimitiveTypeKind.GeometryLineString:
            case LegacyMetadata.PrimitiveTypeKind.GeometryPolygon:
            case LegacyMetadata.PrimitiveTypeKind.GeometryMultiPoint:
            case LegacyMetadata.PrimitiveTypeKind.GeometryMultiLineString:
            case LegacyMetadata.PrimitiveTypeKind.GeometryMultiPolygon:
            case LegacyMetadata.PrimitiveTypeKind.GeometryCollection:
                return(TypeUsage.CreateDefaultTypeUsage(primitiveEdmType));

            case LegacyMetadata.PrimitiveTypeKind.Decimal:
                var precisionFacetValue = legacyTypeUsage.Facets[PrecisionFacetName].Value;

                if (precisionFacetValue == null ||
                    precisionFacetValue == TypeUsageHelper.LegacyUnboundedValue)
                {
                    Debug.Assert(
                        legacyTypeUsage.Facets[ScaleFacetName].Value == precisionFacetValue,
                        "Precision and Scale facets are expected to be both unbounded (Max) or null");

                    return(TypeUsage.CreateDecimalTypeUsage(primitiveEdmType));
                }
                else
                {
                    var scaleFacetValue = legacyTypeUsage.Facets[ScaleFacetName].Value;

                    Debug.Assert(
                        precisionFacetValue is byte && scaleFacetValue is byte,
                        "Precision and Scale facets are expected to be both unbounded (Max) or both of byte type");

                    return
                        (TypeUsage.CreateDecimalTypeUsage(
                             primitiveEdmType,
                             (byte)precisionFacetValue,
                             (byte)scaleFacetValue));
                }

            case LegacyMetadata.PrimitiveTypeKind.Binary:
                Debug.Assert(
                    !(legacyTypeUsage.Facets[FixedLengthFacetName].Value == null
                      ^ legacyTypeUsage.Facets[MaxLengthFacetName].Value == null),
                    "Both Fixed Length and Max Length facet values should be null or none should be null");

                var fixedLengthFacetValue = legacyTypeUsage.Facets[FixedLengthFacetName].Value;
                if (fixedLengthFacetValue == null)
                {
                    return(TypeUsage.CreateDefaultTypeUsage(primitiveEdmType));
                }
                else
                {
                    var maxLengthBinaryFacetValue = legacyTypeUsage.Facets[MaxLengthFacetName].Value;

                    return
                        (maxLengthBinaryFacetValue == TypeUsageHelper.LegacyUnboundedValue
                                ? TypeUsage.CreateBinaryTypeUsage(
                             primitiveEdmType,
                             (bool)fixedLengthFacetValue)
                                : TypeUsage.CreateBinaryTypeUsage(
                             primitiveEdmType,
                             (bool)fixedLengthFacetValue,
                             (int)maxLengthBinaryFacetValue));
                }

            case LegacyMetadata.PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDateTimeTypeUsage(
                           primitiveEdmType,
                           (byte?)legacyTypeUsage.Facets[PrecisionFacetName].Value));

            case LegacyMetadata.PrimitiveTypeKind.DateTimeOffset:
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(
                           primitiveEdmType,
                           (byte?)legacyTypeUsage.Facets[PrecisionFacetName].Value));

            case LegacyMetadata.PrimitiveTypeKind.Time:
                return(TypeUsage.CreateTimeTypeUsage(
                           primitiveEdmType,
                           (byte?)legacyTypeUsage.Facets[PrecisionFacetName].Value));

            case LegacyMetadata.PrimitiveTypeKind.String:
                var maxLengthStringFacetValue = legacyTypeUsage.Facets[MaxLengthFacetName].Value;
                if (maxLengthStringFacetValue == null)
                {
                    return(TypeUsage.CreateDefaultTypeUsage(primitiveEdmType));
                }
                else
                {
                    return(maxLengthStringFacetValue == TypeUsageHelper.LegacyUnboundedValue
                                   ? TypeUsage.CreateStringTypeUsage(
                               primitiveEdmType,
                               (bool)legacyTypeUsage.Facets[UnicodeFacetName].Value,
                               (bool)legacyTypeUsage.Facets[FixedLengthFacetName].Value)
                                   : TypeUsage.CreateStringTypeUsage(
                               primitiveEdmType,
                               (bool)legacyTypeUsage.Facets[UnicodeFacetName].Value,
                               (bool)legacyTypeUsage.Facets[FixedLengthFacetName].Value,
                               (int)maxLengthStringFacetValue));
                }
            }

            Debug.Fail("Unknown primitive type kind.");

            throw new NotSupportedException();
        }
Esempio n. 8
0
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            Check.NotNull(edmType, "edmType");
            Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            var primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(Strings.ProviderDoesNotSupportType(edmType.EdmType.Name));
            }

            var facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Geography:
            case PrimitiveTypeKind.GeographyPoint:
            case PrimitiveTypeKind.GeographyLineString:
            case PrimitiveTypeKind.GeographyPolygon:
            case PrimitiveTypeKind.GeographyMultiPoint:
            case PrimitiveTypeKind.GeographyMultiLineString:
            case PrimitiveTypeKind.GeographyMultiPolygon:
            case PrimitiveTypeKind.GeographyCollection:
                return(GetStorePrimitiveTypeIfPostSql9("geography", edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));

            case PrimitiveTypeKind.Geometry:
            case PrimitiveTypeKind.GeometryPoint:
            case PrimitiveTypeKind.GeometryLineString:
            case PrimitiveTypeKind.GeometryPolygon:
            case PrimitiveTypeKind.GeometryMultiPoint:
            case PrimitiveTypeKind.GeometryMultiLineString:
            case PrimitiveTypeKind.GeometryMultiPolygon:
            case PrimitiveTypeKind.GeometryCollection:
                return(GetStorePrimitiveTypeIfPostSql9("geometry", edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["uniqueidentifier"]));

            case PrimitiveTypeKind.HierarchyId:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["hierarchyid"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal:     // decimal, numeric, smallmoney, money
            {
                byte precision;
                if (!edmType.TryGetPrecision(out precision))
                {
                    precision = 18;
                }

                byte scale;
                if (!edmType.TryGetScale(out scale))
                {
                    scale = 0;
                }
                var tu = TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale);
                return(tu);
            }

            case PrimitiveTypeKind.Binary:     // binary, varbinary, varbinary(max), image, timestamp, rowversion
            {
                var isFixedLength = null != facets[FixedLengthFacetName].Value && (bool)facets[FixedLengthFacetName].Value;
                var f             = facets[MaxLengthFacetName];
                var isMaxLength   = f.IsUnbounded || null == f.Value || (int)f.Value > binaryMaxSize;
                var maxLength     = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(
                        StoreTypeNameToStorePrimitiveType["binary"], true, (isMaxLength ? binaryMaxSize : maxLength));
                }
                else
                {
                    if (isMaxLength)
                    {
                        if (_version != SqlVersion.Sql8)
                        {
                            tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary(max)"], false);
                            Debug.Assert(tu.Facets[MaxLengthFacetName].Description.IsConstant, "varbinary(max) is not constant!");
                        }
                        else
                        {
                            tu = TypeUsage.CreateBinaryTypeUsage(
                                StoreTypeNameToStorePrimitiveType["varbinary"], false, binaryMaxSize);
                        }
                    }
                    else
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength);
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.String:
                // char, nchar, varchar, nvarchar, varchar(max), nvarchar(max), ntext, text, xml
            {
                var isUnicode     = null == facets[UnicodeFacetName].Value || (bool)facets[UnicodeFacetName].Value;
                var isFixedLength = null != facets[FixedLengthFacetName].Value && (bool)facets[FixedLengthFacetName].Value;
                var f             = facets[MaxLengthFacetName];
                // maxlen is true if facet value is unbounded, the value is bigger than the limited string sizes *or* the facet
                // value is null. this is needed since functions still have maxlength facet value as null
                var isMaxLength = f.IsUnbounded || null == f.Value ||
                                  (int)f.Value > (isUnicode ? nvarcharMaxSize : varcharMaxSize);
                var maxLength = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;

                if (isUnicode)
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(
                            StoreTypeNameToStorePrimitiveType["nchar"], true, true, (isMaxLength ? nvarcharMaxSize : maxLength));
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            // nvarchar(max) (SQL 9) or ntext (SQL 8)
                            if (_version != SqlVersion.Sql8)
                            {
                                tu = TypeUsage.CreateStringTypeUsage(
                                    StoreTypeNameToStorePrimitiveType["nvarchar(max)"], true, false);
                                Debug.Assert(tu.Facets[MaxLengthFacetName].Description.IsConstant, "NVarchar(max) is not constant!");
                            }
                            else
                            {
                                // if it is unknown, fallback to nvarchar[4000] instead of ntext since it has limited store semantics
                                tu = TypeUsage.CreateStringTypeUsage(
                                    StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, nvarcharMaxSize);
                            }
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(
                                StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, maxLength);
                        }
                    }
                }
                else         // !isUnicode
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(
                            StoreTypeNameToStorePrimitiveType["char"], false, true,
                            (isMaxLength ? varcharMaxSize : maxLength));
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            // nvarchar(max) (SQL 9) or ntext (SQL 8)
                            if (_version != SqlVersion.Sql8)
                            {
                                tu = TypeUsage.CreateStringTypeUsage(
                                    StoreTypeNameToStorePrimitiveType["varchar(max)"], false, false);
                                Debug.Assert(tu.Facets[MaxLengthFacetName].Description.IsConstant, "varchar(max) is not constant!");
                            }
                            else
                            {
                                // if it is unknown, fallback to varchar[8000] instead of text since it has limited store semantics
                                tu = TypeUsage.CreateStringTypeUsage(
                                    StoreTypeNameToStorePrimitiveType["varchar"], false, false, varcharMaxSize);
                            }
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(
                                StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength);
                        }
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.DateTimeOffset:
                return(GetStorePrimitiveTypeIfPostSql9("datetimeoffset", edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));

            case PrimitiveTypeKind.Time:
                return(GetStorePrimitiveTypeIfPostSql9("time", edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));

            default:
                throw new NotSupportedException(Strings.NoStoreTypeForEdmType(edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));
            }
        }
Esempio n. 9
0
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            Check.NotNull <TypeUsage>(storeType, nameof(storeType));
            string lowerInvariant = storeType.EdmType.Name.ToLowerInvariant();

            if (!this.StoreTypeNameToEdmPrimitiveType.ContainsKey(lowerInvariant))
            {
                throw new ArgumentException(Strings.ProviderDoesNotSupportType((object)lowerInvariant));
            }
            PrimitiveType     primitiveType = this.StoreTypeNameToEdmPrimitiveType[lowerInvariant];
            int               maxLength     = 0;
            bool              isUnicode     = true;
            PrimitiveTypeKind primitiveTypeKind;
            bool              flag;
            bool              isFixedLength;

            switch (lowerInvariant)
            {
            case "tinyint":
            case "smallint":
            case "bigint":
            case "bit":
            case "uniqueidentifier":
            case "int":
            case "geography":
            case "geometry":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)primitiveType));

            case "varchar":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode     = false;
                isFixedLength = false;
                break;

            case "char":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode     = false;
                isFixedLength = true;
                break;

            case "nvarchar":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode     = true;
                isFixedLength = false;
                break;

            case "nchar":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode     = true;
                isFixedLength = true;
                break;

            case "varchar(max)":
            case "text":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = true;
                isUnicode     = false;
                isFixedLength = false;
                break;

            case "nvarchar(max)":
            case "ntext":
            case "xml":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = true;
                isUnicode     = true;
                isFixedLength = false;
                break;

            case "binary":
                primitiveTypeKind = PrimitiveTypeKind.Binary;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLength = true;
                break;

            case "varbinary":
                primitiveTypeKind = PrimitiveTypeKind.Binary;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLength = false;
                break;

            case "varbinary(max)":
            case "image":
                primitiveTypeKind = PrimitiveTypeKind.Binary;
                flag          = true;
                isFixedLength = false;
                break;

            case "timestamp":
            case "rowversion":
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, true, 8));

            case "float":
            case "real":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)primitiveType));

            case "decimal":
            case "numeric":
                byte precision;
                byte scale;
                if (storeType.TryGetPrecision(out precision) && storeType.TryGetScale(out scale))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(primitiveType, precision, scale));
                }
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType));

            case "money":
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, (byte)19, (byte)4));

            case "smallmoney":
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, (byte)10, (byte)4));

            case "datetime":
            case "datetime2":
            case "smalldatetime":
                return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, new byte?()));

            case "date":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)primitiveType));

            case "time":
                return(TypeUsage.CreateTimeTypeUsage(primitiveType, new byte?()));

            case "datetimeoffset":
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, new byte?()));

            default:
                throw new NotSupportedException(Strings.ProviderDoesNotSupportType((object)lowerInvariant));
            }
            switch (primitiveTypeKind)
            {
            case PrimitiveTypeKind.Binary:
                if (!flag)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(primitiveType, isFixedLength, maxLength));
                }
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, isFixedLength));

            case PrimitiveTypeKind.String:
                if (!flag)
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, isFixedLength, maxLength));
                }
                return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, isFixedLength));

            default:
                throw new NotSupportedException(Strings.ProviderDoesNotSupportType((object)lowerInvariant));
            }
        }
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in Ingres, taking the store version into consideration.
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating an EDM type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating a store type and a set of facets</returns>
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }
            Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            var primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format("The underlying provider does not support the type '{0}'.", edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.SByte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["integer"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal:     // decimal, money
                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], TypeHelpers.GetPrecision(edmType, 18), TypeHelpers.GetScale(edmType, 0)));

            case PrimitiveTypeKind.Binary:     // byte, byte varying, long byte
            {
                var isFixedLength  = facets["FixedLength"].GetValue <bool>(false);
                var maxLengthFacet = facets["MaxLength"];
                var maxLength      = maxLengthFacet.GetValue <int?>();
                if (maxLengthFacet.IsUnbounded || maxLength == null || maxLength.Value > binaryMaxSize)
                {
                    maxLength = null;
                }

                var storeTypeName = isFixedLength ? "byte" : maxLength == null ? "long byte" : "byte varying";

                return(CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType[storeTypeName], isFixedLength, maxLength));
            }

            case PrimitiveTypeKind.String:     // char, nchar, varchar, nvarchar, long varchar, long nvarchar
            {
                var isUnicode      = facets["Unicode"].GetValue <bool>(false);
                var isFixedLength  = facets["FixedLength"].GetValue <bool>(false);
                var maxLengthFacet = facets["MaxLength"];
                var maxLength      = maxLengthFacet.GetValue <int?>();
                if (maxLengthFacet.IsUnbounded || maxLength == null || maxLength.Value > varcharMaxSize)
                {
                    maxLength = null;
                }

                var storeTypeName = isFixedLength ? "char" : "varchar";
                storeTypeName = isUnicode ? "n" + storeTypeName : storeTypeName;
                storeTypeName = (maxLength == null) ? "long " + storeTypeName : storeTypeName;

                return(CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[storeTypeName], isUnicode, isFixedLength, maxLength));
            }

            case PrimitiveTypeKind.DateTime:     // ingresdate
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["ingresdate"]));

            case PrimitiveTypeKind.Time:     // time
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

            default:
                throw new NotSupportedException(String.Format("There is no store type corresponding to the EDM type '{0}' of primitive type '{1}'.", edmType, primitiveType.PrimitiveTypeKind));
            }
        }
Esempio n. 11
0
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            Check.NotNull(storeType, "storeType");

            var storeTypeName = storeType.EdmType.Name.ToLowerInvariant();

            if (!base.StoreTypeNameToEdmPrimitiveType.ContainsKey(storeTypeName))
            {
                throw new ArgumentException(Strings.ProviderDoesNotSupportType(storeTypeName));
            }

            var edmPrimitiveType = base.StoreTypeNameToEdmPrimitiveType[storeTypeName];

            var maxLength   = 0;
            var isUnicode   = true;
            var isFixedLen  = false;
            var isUnbounded = true;

            PrimitiveTypeKind newPrimitiveTypeKind;

            switch (storeTypeName)
            {
            // for some types we just go with simple type usage with no facets
            case "tinyint":
            case "smallint":
            case "bigint":
            case "bit":
            case "uniqueidentifier":
            case "hierarchyid":
            case "int":
            case "geography":
            case "geometry":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

            case "varchar":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode            = false;
                isFixedLen           = false;
                break;

            case "char":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode            = false;
                isFixedLen           = true;
                break;

            case "nvarchar":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode            = true;
                isFixedLen           = false;
                break;

            case "nchar":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode            = true;
                isFixedLen           = true;
                break;

            case "varchar(max)":
            case "text":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = true;
                isUnicode            = false;
                isFixedLen           = false;
                break;

            case "nvarchar(max)":
            case "ntext":
            case "xml":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = true;
                isUnicode            = true;
                isFixedLen           = false;
                break;

            case "binary":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = true;
                break;

            case "varbinary":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = false;
                break;

            case "varbinary(max)":
            case "image":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            case "timestamp":
            case "rowversion":
                return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, true, 8));

            case "float":
            case "real":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

            case "decimal":
            case "numeric":
            {
                byte precision;
                byte scale;
                if (storeType.TryGetPrecision(out precision) &&
                    storeType.TryGetScale(out scale))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, precision, scale));
                }
                else
                {
                    return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType));
                }
            }

            case "money":
                return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, 19, 4));

            case "smallmoney":
                return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, 10, 4));

            case "datetime":
            case "datetime2":
            case "smalldatetime":
                return(TypeUsage.CreateDateTimeTypeUsage(edmPrimitiveType, null));

            case "date":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

            case "time":
                return(TypeUsage.CreateTimeTypeUsage(edmPrimitiveType, null));

            case "datetimeoffset":
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(edmPrimitiveType, null));

            default:
                throw new NotSupportedException(Strings.ProviderDoesNotSupportType(storeTypeName));
            }

            Debug.Assert(
                newPrimitiveTypeKind == PrimitiveTypeKind.String || newPrimitiveTypeKind == PrimitiveTypeKind.Binary,
                "at this point only string and binary types should be present");

            switch (newPrimitiveTypeKind)
            {
            case PrimitiveTypeKind.String:
                if (!isUnbounded)
                {
                    return(TypeUsage.CreateStringTypeUsage(edmPrimitiveType, isUnicode, isFixedLen, maxLength));
                }
                else
                {
                    return(TypeUsage.CreateStringTypeUsage(edmPrimitiveType, isUnicode, isFixedLen));
                }

            case PrimitiveTypeKind.Binary:
                if (!isUnbounded)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, isFixedLen, maxLength));
                }
                else
                {
                    return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, isFixedLen));
                }

            default:
                throw new NotSupportedException(Strings.ProviderDoesNotSupportType(storeTypeName));
            }
        }
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in EDM.
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating a store type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating an EDM type and a set of facets</returns>
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            if (storeType == null)
            {
                throw new ArgumentNullException("storeType");
            }

            string storeTypeName = storeType.EdmType.Name.ToLowerInvariant();

            if (!base.StoreTypeNameToEdmPrimitiveType.ContainsKey(storeTypeName))
            {
                throw new ArgumentException(String.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }

            PrimitiveType edmPrimitiveType = base.StoreTypeNameToEdmPrimitiveType[storeTypeName];

            switch (storeTypeName)
            {
            // for some types we just go with simple type usage with no facets
            case "tinyint":
            case "smallint":
            case "integer":
            case "bigint":
            case "integer1":
            case "integer2":
            case "integer4":
            case "integer8":
            case "bool":
            case "int1":
            case "int2":
            case "int4":
            case "int8":
            case "float":
            case "float4":
            case "float8":
            case "double":
            case "real":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

            case "decimal":
                return(CreateDecimalTypeUsage(edmPrimitiveType, TypeHelpers.GetPrecision(storeType), TypeHelpers.GetScale(storeType)));

            case "money":
                return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, 14, 2));

            case "varchar":
                return(CreateStringTypeUsage(edmPrimitiveType, false, false, TypeHelpers.GetMaxLength(storeType)));

            case "char":
                return(CreateStringTypeUsage(edmPrimitiveType, false, true, TypeHelpers.GetMaxLength(storeType)));

            case "nvarchar":
                return(CreateStringTypeUsage(edmPrimitiveType, true, false, TypeHelpers.GetMaxLength(storeType)));

            case "nchar":
                return(CreateStringTypeUsage(edmPrimitiveType, true, true, TypeHelpers.GetMaxLength(storeType)));

            case "long varchar":
                return(CreateStringTypeUsage(edmPrimitiveType, false, false));

            case "byte":
                return(CreateBinaryTypeUsage(edmPrimitiveType, true, TypeHelpers.GetMaxLength(storeType)));

            case "byte varying":
                return(CreateBinaryTypeUsage(edmPrimitiveType, false, TypeHelpers.GetMaxLength(storeType)));

            case "long byte":
                return(CreateBinaryTypeUsage(edmPrimitiveType, false));

            case "timestamp":
            case "rowversion":
                return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, true, 8));

            case "date":
            case "ingresdate":
            case "ansidate":
                return(TypeUsage.CreateDateTimeTypeUsage(edmPrimitiveType, null));

            default:
                throw new NotSupportedException(String.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }
        }
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format(Resources.TypeNotSupported, edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bool"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["utinyint"]));

            case PrimitiveTypeKind.SByte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["guid"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["double"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Decimal:
            {
                byte  precision = DEFAULT_DECIMAL_PRECISION;
                byte  scale     = DEFAULT_DECIMAL_SCALE;
                Facet facet;

                if (edmType.Facets.TryGetValue("Precision", false, out facet))
                {
                    if (!facet.IsUnbounded && facet.Value != null)
                    {
                        precision = (byte)facet.Value;
                    }
                }

                if (edmType.Facets.TryGetValue("Scale", false, out facet))
                {
                    if (!facet.IsUnbounded && facet.Value != null)
                    {
                        scale = (byte)facet.Value;
                    }
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary:
            {
                bool  isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                Facet f             = facets["MaxLength"];
                bool  isMaxLength   = f.IsUnbounded || null == f.Value || (int)f.Value > MEDIUMBLOB_MAXLEN;
                int   maxLength     = !isMaxLength ? (int)f.Value : LONGBLOB_MAXLEN;

                string typeName = String.Empty;

                // now this applies for both isFixedLength and !isFixedLength
                if (maxLength < CHAR_MAXLEN)
                {
                    typeName = "tinyblob";
                }
                else if (maxLength < MEDIUMBLOB_MAXLEN)
                {
                    typeName = "blob";
                }
                else if (maxLength < LONGTEXT_MAXLEN)
                {
                    typeName = "mediumblob";
                }
                else
                {
                    typeName = "longblob";
                }

                return(TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isFixedLength, maxLength));
            }

            case PrimitiveTypeKind.String:
            {
                string typeName      = String.Empty;
                bool   isUnicode     = null != facets["Unicode"].Value && (bool)facets["Unicode"].Value;
                bool   isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                int    maxLenghtValue;

                Facet maxLengthFacet = facets["MaxLength"];
                if (isFixedLength)
                {
                    typeName = isUnicode ? "nchar" : "char";
                    if (maxLengthFacet.Value != null && Int32.TryParse(maxLengthFacet.Value.ToString(), out maxLenghtValue) && maxLenghtValue <= CHAR_MAXLEN)
                    {
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength, (int)maxLengthFacet.Value));
                    }
                    else if (maxLengthFacet.Value != null && maxLengthFacet.Value.ToString() == "Max")
                    {
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength, CHAR_MAXLEN));
                    }
                    else
                    {
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength));
                    }
                }
                else
                {
                    typeName = isUnicode ? "nvarchar" : "varchar";
                    if (maxLengthFacet.Value != null && Int32.TryParse(maxLengthFacet.Value.ToString(), out maxLenghtValue))
                    {
                        if (maxLenghtValue > VARCHAR_MAXLEN && maxLenghtValue <= MEDIUMTEXT_MAXLEN)
                        {
                            typeName = "mediumtext";
                        }
                        else if ((int)maxLengthFacet.Value > MEDIUMTEXT_MAXLEN)
                        {
                            typeName = "longtext";
                        }
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength, (int)maxLengthFacet.Value));
                    }
                    else
                    {
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["longtext"], isUnicode, isFixedLength, LONGBLOB_MAXLEN));
                    }
                }
            }

            case PrimitiveTypeKind.DateTimeOffset:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["timestamp"]));

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.Time:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

#if NET_45_OR_GREATER
            case PrimitiveTypeKind.Geometry:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["geometry"]));
#endif
            default:
                throw new NotSupportedException(String.Format(Resources.NoStoreTypeForEdmType, edmType, primitiveType.PrimitiveTypeKind));
            }
        }
        private TypeUsage ConvertFromLegacyStoreTypeUsage(LegacyMetadata.TypeUsage legacyStoreTypeUsage)
        {
            Debug.Assert(legacyStoreTypeUsage != null, "legacyStoreTypeUsage != null");
            Debug.Assert(legacyStoreTypeUsage.EdmType is LegacyMetadata.PrimitiveType, "primitive type expected");
            Debug.Assert(
                (LegacyMetadata.DataSpace) typeof(LegacyMetadata.EdmType)
                .GetProperty("DataSpace", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(legacyStoreTypeUsage.EdmType) == LegacyMetadata.DataSpace.SSpace,
                "Expected SSpace type.");

            var legacyStorePrimitiveType = (LegacyMetadata.PrimitiveType)legacyStoreTypeUsage.EdmType;
            var storePrimitiveType       = _storeTypes.Single(t => t.FullName == legacyStorePrimitiveType.FullName);

            switch (storePrimitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
            case PrimitiveTypeKind.Byte:
            case PrimitiveTypeKind.SByte:
            case PrimitiveTypeKind.Int16:
            case PrimitiveTypeKind.Int32:
            case PrimitiveTypeKind.Int64:
            case PrimitiveTypeKind.Guid:
            case PrimitiveTypeKind.Double:
            case PrimitiveTypeKind.Single:
            case PrimitiveTypeKind.Geography:
            case PrimitiveTypeKind.GeographyPoint:
            case PrimitiveTypeKind.GeographyLineString:
            case PrimitiveTypeKind.GeographyPolygon:
            case PrimitiveTypeKind.GeographyMultiPoint:
            case PrimitiveTypeKind.GeographyMultiLineString:
            case PrimitiveTypeKind.GeographyMultiPolygon:
            case PrimitiveTypeKind.GeographyCollection:
            case PrimitiveTypeKind.Geometry:
            case PrimitiveTypeKind.GeometryPoint:
            case PrimitiveTypeKind.GeometryLineString:
            case PrimitiveTypeKind.GeometryPolygon:
            case PrimitiveTypeKind.GeometryMultiPoint:
            case PrimitiveTypeKind.GeometryMultiLineString:
            case PrimitiveTypeKind.GeometryMultiPolygon:
            case PrimitiveTypeKind.GeometryCollection:
                return(TypeUsage.CreateDefaultTypeUsage(storePrimitiveType));

            case PrimitiveTypeKind.Decimal:
                return(TypeUsage.CreateDecimalTypeUsage(
                           storePrimitiveType,
                           (byte)legacyStoreTypeUsage.Facets[PrecisionFacetName].Value,
                           (byte)legacyStoreTypeUsage.Facets[ScaleFacetName].Value));

            case PrimitiveTypeKind.Binary:
                return(TypeUsage.CreateBinaryTypeUsage(
                           storePrimitiveType,
                           (bool)legacyStoreTypeUsage.Facets[FixedLengthFacetName].Value,
                           (int)legacyStoreTypeUsage.Facets[MaxLengthFacetName].Value));

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDateTimeTypeUsage(
                           storePrimitiveType,
                           (byte?)legacyStoreTypeUsage.Facets[PrecisionFacetName].Value));

            case PrimitiveTypeKind.DateTimeOffset:
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(
                           storePrimitiveType,
                           (byte?)legacyStoreTypeUsage.Facets[PrecisionFacetName].Value));

            case PrimitiveTypeKind.Time:
                return(TypeUsage.CreateTimeTypeUsage(
                           storePrimitiveType,
                           (byte?)legacyStoreTypeUsage.Facets[PrecisionFacetName].Value));

            case PrimitiveTypeKind.String:
                return(TypeUsage.CreateStringTypeUsage(
                           storePrimitiveType,
                           (bool)legacyStoreTypeUsage.Facets[UnicodeFacetName].Value,
                           (bool)legacyStoreTypeUsage.Facets[FixedLengthFacetName].Value,
                           (int)legacyStoreTypeUsage.Facets[MaxLengthFacetName].Value));
            }

            Debug.Fail("Unknown primitive type kind.");

            throw new NotSupportedException();
        }
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in Jet
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating an EDM type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating a store type and a set of facets</returns>
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            System.Diagnostics.Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format("The underlying provider does not support the type '{0}'.", edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["guid"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal:     // decimal, numeric, smallmoney, money
            {
                byte precision;
                if (!edmType.TryGetPrecision(out precision))
                {
                    precision = 18;
                }

                byte scale;
                if (!edmType.TryGetScale(out scale))
                {
                    scale = 0;
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary:     // binary, varbinary, image
            {
                bool isFixedLength = edmType.GetIsFixedLength();
                bool isMaxLength   = edmType.GetMaxLength() > BINARY_MAXSIZE;
                int  maxLength     = edmType.GetMaxLength();

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["binary"], true, maxLength);
                }
                else if (isMaxLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["image"], false);
                    System.Diagnostics.Debug.Assert(tu.Facets["MaxLength"].Description.IsConstant, "varbinary(max) is not constant!");
                }
                else
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength);
                }

                return(tu);
            }

            case PrimitiveTypeKind.String:
                // char, varchar, text
            {
                bool isUnicode     = edmType.GetIsUnicode();     // We do not handle unicode (everything's unicode in Jet)
                bool isFixedLength = edmType.GetIsFixedLength();
                bool isMaxLength   = edmType.GetMaxLength() > VARCHAR_MAXSIZE;
                int  maxLength     = edmType.GetMaxLength();

                TypeUsage tu;

                if (isFixedLength)
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["char"], false, true, maxLength);
                }
                else if (isMaxLength)
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["text"], false, false);
                }
                else
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength);
                }

                return(tu);
            }

            case PrimitiveTypeKind.DateTime:     // datetime
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.Time:     // time
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

            default:
                throw new NotSupportedException(String.Format("There is no store type corresponding to the EDM type '{0}' of primitive type '{1}'.", edmType, primitiveType.PrimitiveTypeKind));
            }
        }
Esempio n. 16
0
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            Check.NotNull <TypeUsage>(edmType, nameof(edmType));
            PrimitiveType edmType1 = edmType.EdmType as PrimitiveType;

            if (edmType1 == null)
            {
                throw new ArgumentException(Strings.ProviderDoesNotSupportType((object)edmType.EdmType.Name));
            }
            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (edmType1.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Binary:
                bool  flag1      = facets["FixedLength"].Value != null && (bool)facets["FixedLength"].Value;
                Facet facet1     = facets["MaxLength"];
                bool  flag2      = facet1.IsUnbounded || facet1.Value == null || (int)facet1.Value > 8000;
                int   maxLength1 = !flag2 ? (int)facet1.Value : int.MinValue;
                return(!flag1 ? (!flag2 ? TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength1) : (this._version == SqlVersion.Sql8 ? TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["varbinary"], false, 8000) : TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["varbinary(max)"], false))) : TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["binary"], true, flag2 ? 8000 : maxLength1));

            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.Decimal:
                byte precision;
                if (!edmType.TryGetPrecision(out precision))
                {
                    precision = (byte)18;
                }
                byte scale;
                if (!edmType.TryGetScale(out scale))
                {
                    scale = (byte)0;
                }
                return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["uniqueidentifier"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.String:
                bool  flag3      = facets["Unicode"].Value == null || (bool)facets["Unicode"].Value;
                bool  flag4      = facets["FixedLength"].Value != null && (bool)facets["FixedLength"].Value;
                Facet facet2     = facets["MaxLength"];
                bool  flag5      = facet2.IsUnbounded || facet2.Value == null || (int)facet2.Value > (flag3 ? 4000 : 8000);
                int   maxLength2 = !flag5 ? (int)facet2.Value : int.MinValue;
                return(!flag3 ? (!flag4 ? (!flag5 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength2) : (this._version == SqlVersion.Sql8 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["varchar"], false, false, 8000) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["varchar(max)"], false, false))) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["char"], false, true, flag5 ? 8000 : maxLength2)) : (!flag4 ? (!flag5 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, maxLength2) : (this._version == SqlVersion.Sql8 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, 4000) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nvarchar(max)"], true, false))) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nchar"], true, true, flag5 ? 4000 : maxLength2)));

            case PrimitiveTypeKind.Time:
                return(this.GetStorePrimitiveTypeIfPostSql9("time", edmType.EdmType.Name, edmType1.PrimitiveTypeKind));

            case PrimitiveTypeKind.DateTimeOffset:
                return(this.GetStorePrimitiveTypeIfPostSql9("datetimeoffset", edmType.EdmType.Name, edmType1.PrimitiveTypeKind));

            case PrimitiveTypeKind.Geometry:
            case PrimitiveTypeKind.GeometryPoint:
            case PrimitiveTypeKind.GeometryLineString:
            case PrimitiveTypeKind.GeometryPolygon:
            case PrimitiveTypeKind.GeometryMultiPoint:
            case PrimitiveTypeKind.GeometryMultiLineString:
            case PrimitiveTypeKind.GeometryMultiPolygon:
            case PrimitiveTypeKind.GeometryCollection:
                return(this.GetStorePrimitiveTypeIfPostSql9("geometry", edmType.EdmType.Name, edmType1.PrimitiveTypeKind));

            case PrimitiveTypeKind.Geography:
            case PrimitiveTypeKind.GeographyPoint:
            case PrimitiveTypeKind.GeographyLineString:
            case PrimitiveTypeKind.GeographyPolygon:
            case PrimitiveTypeKind.GeographyMultiPoint:
            case PrimitiveTypeKind.GeographyMultiLineString:
            case PrimitiveTypeKind.GeographyMultiPolygon:
            case PrimitiveTypeKind.GeographyCollection:
                return(this.GetStorePrimitiveTypeIfPostSql9("geography", edmType.EdmType.Name, edmType1.PrimitiveTypeKind));

            default:
                throw new NotSupportedException(Strings.NoStoreTypeForEdmType((object)edmType.EdmType.Name, (object)edmType1.PrimitiveTypeKind));
            }
        }
Esempio n. 17
0
        public void Can_create_composable_function_import_with_complex_type_collection_result()
        {
            DbProviderManifest providerManifest;
            var containerMapping = GetContainerMapping(out providerManifest);

            var cTypeUsageString = TypeUsage.CreateDefaultTypeUsage(
                PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            var sTypeUsageString = providerManifest.GetStoreType(cTypeUsageString);

            var complexType = ComplexType.Create(
                "RT", "N", DataSpace.CSpace,
                new[]
            {
                EdmProperty.Create("P1", cTypeUsageString),
                EdmProperty.Create("P2", cTypeUsageString)
            },
                null);


            var rowType = RowType.Create(
                new[]
            {
                EdmProperty.Create("C1", sTypeUsageString),
                EdmProperty.Create("C2", sTypeUsageString)
            },
                null);

            var functionImport = EdmFunction.Create(
                "F", "N", DataSpace.CSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("R", complexType.GetCollectionType(), ParameterMode.ReturnValue)
                }
            },
                null);

            var targetFunction = EdmFunction.Create(
                "SF", "N", DataSpace.SSpace,
                new EdmFunctionPayload
            {
                IsComposable     = true,
                ReturnParameters = new[]
                {
                    FunctionParameter.Create("R", rowType.GetCollectionType(), ParameterMode.ReturnValue)
                }
            },
                null);

            var typeMapping = new FunctionImportComplexTypeMapping(
                complexType,
                new Collection <FunctionImportReturnTypePropertyMapping>()
            {
                new FunctionImportReturnTypeScalarPropertyMapping("P1", "C1"),
                new FunctionImportReturnTypeScalarPropertyMapping("P2", "C2"),
            });

            var resultMapping = new FunctionImportResultMapping();

            resultMapping.AddTypeMapping(typeMapping);

            var functionImportMapping = new FunctionImportMappingComposable(
                functionImport,
                targetFunction,
                resultMapping,
                containerMapping);

            Assert.Same(resultMapping, functionImportMapping.ResultMapping);
            Assert.Equal(1, functionImportMapping.StructuralTypeMappings.Count);
            Assert.Null(functionImportMapping.TvfKeys);
        }
Esempio n. 18
0
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException("Store does not support specified edm type");
            }

            // TODO: come up with way to determin if unicode is used
            bool  isUnicode = true;
            Facet facet;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bool"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int2"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int4"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int8"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float4"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float8"]));

            case PrimitiveTypeKind.Decimal:
            {
                byte scale;
                byte precision;
                if (edmType.Facets.TryGetValue(ScaleFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    scale = (byte)facet.Value;
                    if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                        !facet.IsUnbounded && facet.Value != null)
                    {
                        precision = (byte)facet.Value;
                        return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["numeric"], precision, scale));
                    }
                }
                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["numeric"]));
            }

            case PrimitiveTypeKind.String:
            {
                // TODO: could get character, character varying, text
                if (edmType.Facets.TryGetValue(FixedLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    PrimitiveType characterPrimitive = StoreTypeNameToStorePrimitiveType["bpchar"];
                    if (edmType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                        !facet.IsUnbounded && facet.Value != null)
                    {
                        return(TypeUsage.CreateStringTypeUsage(characterPrimitive, isUnicode, true, (int)facet.Value));
                    }
                    // this may not work well
                    return(TypeUsage.CreateStringTypeUsage(characterPrimitive, isUnicode, true));
                }
                if (edmType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], isUnicode, false, (int)facet.Value));
                }
                // assume text since it is not fixed length and has no max length
                return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["text"], isUnicode, false));
            }

            case PrimitiveTypeKind.DateTime:
                if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["timestamp"], (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["timestamp"], null));
                }

            case PrimitiveTypeKind.DateTimeOffset:
                if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], null));
                }

            case PrimitiveTypeKind.Time:
                if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["interval"], (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["interval"], null));
                }

            case PrimitiveTypeKind.Binary:
            {
                if (edmType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["bytea"], false, (int)facet.Value));
                }
                return(TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["bytea"], false));
            }

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["uuid"]));
                // notably missing
                // PrimitiveTypeKind.Byte:
                // PrimitiveTypeKind.SByte:
            }

            throw new NotSupportedException();
        }
Esempio n. 19
0
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            if (storeType == null)
            {
                throw new ArgumentNullException("storeType");
            }

            string        storeTypeName = storeType.EdmType.Name;
            PrimitiveType primitiveType = StoreTypeNameToEdmPrimitiveType[storeTypeName];
            // TODO: come up with way to determin if unicode is used
            bool  isUnicode = true;
            Facet facet;

            switch (storeTypeName)
            {
            case "bool":
            case "int2":
            case "int4":
            case "int8":
            case "float4":
            case "float8":
            case "uuid":
                return(TypeUsage.CreateDefaultTypeUsage(primitiveType));

            case "numeric":
            {
                byte scale;
                byte precision;
                if (storeType.Facets.TryGetValue(ScaleFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    scale = (byte)facet.Value;
                    if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                        !facet.IsUnbounded && facet.Value != null)
                    {
                        precision = (byte)facet.Value;
                        return(TypeUsage.CreateDecimalTypeUsage(primitiveType, precision, scale));
                    }
                }
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType));
            }

            case "bpchar":
                if (storeType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, true, (int)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, true));
                }

            case "varchar":
                if (storeType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, false, (int)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, false));
                }

            case "text":
            case "xml":
                return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, false));

            case "timestamp":
                // TODO: make sure the arguments are correct here
                if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, null));
                }

            case "date":
                return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, 0));

            case "timestamptz":
                if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, null));
                }

            case "time":
            case "interval":
                if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateTimeTypeUsage(primitiveType, (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateTimeTypeUsage(primitiveType, null));
                }

            case "bytea":
            {
                if (storeType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(primitiveType, false, (int)facet.Value));
                }
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, false));
            }

            case "rowversion":
            {
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, true, 8));
            }
                //TypeUsage.CreateBinaryTypeUsage
                //TypeUsage.CreateDateTimeTypeUsage
                //TypeUsage.CreateDecimalTypeUsage
                //TypeUsage.CreateStringTypeUsage
            }
            throw new NotSupportedException("Not supported store type: " + storeTypeName);
        }
Esempio n. 20
0
        private TypeUsage BuildTypeUsage()
        {
            var primitiveType = PrimitiveType.GetEdmPrimitiveType(Type);

            if (Type == PrimitiveTypeKind.Binary)
            {
                if (MaxLength != null)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(
                               primitiveType,
                               IsFixedLength ?? false,
                               MaxLength.Value));
                }

                return(TypeUsage.CreateBinaryTypeUsage(
                           primitiveType,
                           IsFixedLength ?? false));
            }

            if (Type == PrimitiveTypeKind.String)
            {
                if (MaxLength != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(
                               primitiveType,
                               IsUnicode ?? true,
                               IsFixedLength ?? false,
                               MaxLength.Value));
                }

                return(TypeUsage.CreateStringTypeUsage(
                           primitiveType,
                           IsUnicode ?? true,
                           IsFixedLength ?? false));
            }

            if (Type == PrimitiveTypeKind.DateTime)
            {
                return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, Precision));
            }

            if (Type == PrimitiveTypeKind.DateTimeOffset)
            {
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, Precision));
            }

            if (Type == PrimitiveTypeKind.Decimal)
            {
                if ((Precision != null) ||
                    (Scale != null))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(
                               primitiveType,
                               Precision ?? 18,
                               Scale ?? 0));
                }

                return(TypeUsage.CreateDecimalTypeUsage(primitiveType));
            }

            return((Type == PrimitiveTypeKind.Time)
                       ? TypeUsage.CreateTimeTypeUsage(primitiveType, Precision)
                       : TypeUsage.CreateDefaultTypeUsage(primitiveType));
        }