public static EdmProperty Primitive(string name, PrimitiveType primitiveType)
        {
            Check.NotEmpty(name, "name");
            Check.NotNull(primitiveType, "primitiveType");

            return CreateProperty(name, primitiveType);
        }
        [Fact] // CodePlex 881
        public void Generate_retains_store_facets_returned_by_the_provider_manifest()
        {
            var storeType = new PrimitiveType
                {
                    Name = "number",
                    DataSpace = DataSpace.SSpace
                };

            var typeUsage =
                TypeUsage.Create(
                    storeType,
                    new[]
                        {
                            CreateConstFacet("Precision", PrimitiveTypeKind.Byte, (byte)11),
                            CreateConstFacet("Scale", PrimitiveTypeKind.Byte, (byte)0)
                        });

            var mockManifest = new Mock<DbProviderManifest>();
            mockManifest.Setup(m => m.GetStoreType(It.IsAny<TypeUsage>())).Returns(typeUsage);

            var storeProperty = new TestMappingGenerator(mockManifest.Object)
                .MapTableColumn(EdmProperty.Primitive("P1", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));

            Assert.Equal("number", storeProperty.TypeUsage.EdmType.Name);

            var facets = storeProperty.TypeUsage.Facets;
            Assert.Equal((byte)11, facets.Where(f => f.Name == "Precision").Select(f => f.Value).Single());
            Assert.Equal((byte)0, facets.Where(f => f.Name == "Scale").Select(f => f.Value).Single());
        }
 // <summary>
 // The constructor for PrimitiveType, it takes in a CLR type containing the identity information
 // </summary>
 // <param name="clrType"> The CLR type object for this primitive type </param>
 // <param name="baseType"> The base type for this primitive type </param>
 // <param name="providerManifest"> The ProviderManifest of the provider of this type </param>
 internal PrimitiveType(
     Type clrType,
     PrimitiveType baseType,
     DbProviderManifest providerManifest)
     : this(Check.NotNull(clrType, "clrType").Name, clrType.NestingNamespace(),
         DataSpace.OSpace, baseType, providerManifest)
 {
     Debug.Assert(clrType == ClrEquivalentType, "not equivalent to ClrEquivalentType");
 }
 /// <summary>
 ///     The constructor for PrimitiveType, it takes in a CLR type containing the identity information
 /// </summary>
 /// <param name="clrType"> The CLR type object for this primitive type </param>
 /// <param name="baseType"> The base type for this primitive type </param>
 /// <param name="providerManifest"> The ProviderManifest of the provider of this type </param>
 internal PrimitiveType(
     Type clrType,
     PrimitiveType baseType,
     DbProviderManifest providerManifest)
     : this(EntityUtil.GenericCheckArgumentNull(clrType, "clrType").Name, clrType.Namespace,
         DataSpace.OSpace, baseType, providerManifest)
 {
     Debug.Assert(clrType == ClrEquivalentType, "not equivalent to ClrEquivalentType");
 }
Exemple #5
0
        // <summary>
        // Initializes a new instance of the EnumType class by using the specified <paramref name="name" />,
        // <paramref name="namespaceName" /> and <paramref name="isFlags" />.
        // </summary>
        // <param name="name"> The name of this enum type. </param>
        // <param name="namespaceName"> The namespace this enum type belongs to. </param>
        // <param name="underlyingType"> Underlying type of this enumeration type. </param>
        // <param name="isFlags"> Indicates whether the enum type is defined as flags (i.e. can be treated as a bit field). </param>
        // <param name="dataSpace"> DataSpace this enum type lives in. Can be either CSpace or OSpace </param>
        // <exception cref="System.ArgumentNullException">Thrown if name or namespace arguments are null</exception>
        // <remarks>
        // Note that enums live only in CSpace.
        // </remarks>
        internal EnumType(string name, string namespaceName, PrimitiveType underlyingType, bool isFlags, DataSpace dataSpace)
            : base(name, namespaceName, dataSpace)
        {
            DebugCheck.NotNull(underlyingType);
            Debug.Assert(Helper.IsSupportedEnumUnderlyingType(underlyingType.PrimitiveTypeKind), "Unsupported underlying type for enum.");
            Debug.Assert(dataSpace == DataSpace.CSpace || dataSpace == DataSpace.OSpace, "Enums can be only defined in CSpace or OSpace.");

            _isFlags = isFlags;
            _underlyingType = underlyingType;
        }
        /// <summary>
        ///     Returns the primitive type corresponding to the given CLR type
        /// </summary>
        /// <param name="clrType"> The CLR type for which the PrimitiveType object is retrieved </param>
        /// <param name="primitiveType"> The retrieved primitive type </param>
        /// <returns> True if a primitive type is returned </returns>
        internal bool TryGetPrimitiveType(Type clrType, out PrimitiveType primitiveType)
        {
            primitiveType = null;
            PrimitiveTypeKind resolvedTypeKind;
            if (TryGetPrimitiveTypeKind(clrType, out resolvedTypeKind))
            {
                InitializePrimitiveTypes();
                primitiveType = _primitiveTypes[(int)resolvedTypeKind];
                return true;
            }

            return false;
        }
        /// <summary>
        ///     The constructor for PrimitiveType.  It takes the required information to identify this type.
        /// </summary>
        /// <param name="name"> The name of this type </param>
        /// <param name="namespaceName"> The namespace name of this type </param>
        /// <param name="version"> The version of this type </param>
        /// <param name="dataSpace"> dataSpace in which this primitive type belongs to </param>
        /// <param name="baseType"> The primitive type that this type is derived from </param>
        /// <param name="providerManifest"> The ProviderManifest of the provider of this type </param>
        /// <exception cref="System.ArgumentNullException">Thrown if name, namespaceName, version, baseType or providerManifest arguments are null</exception>
        internal PrimitiveType(
            string name,
            string namespaceName,
            DataSpace dataSpace,
            PrimitiveType baseType,
            DbProviderManifest providerManifest)
            : base(name, namespaceName, dataSpace)
        {
            EntityUtil.GenericCheckArgumentNull(baseType, "baseType");
            EntityUtil.GenericCheckArgumentNull(providerManifest, "providerManifest");

            BaseType = baseType;

            Initialize(this, baseType.PrimitiveTypeKind, providerManifest);
        }
        internal PrimitiveType(
            string name,
            string namespaceName,
            DataSpace dataSpace,
            PrimitiveType baseType,
            DbProviderManifest providerManifest)
            : base(name, namespaceName, dataSpace)
        {
            Check.NotNull(baseType, "baseType");
            Check.NotNull(providerManifest, "providerManifest");

            BaseType = baseType;

            Initialize(this, baseType.PrimitiveTypeKind, providerManifest);
        }
        /// <summary>
        ///     Add the given primitive type to the primitive type cache
        /// </summary>
        /// <param name="type"> The primitive type to add </param>
        internal void Add(PrimitiveType type)
        {
            // Get to the list
            var primitiveTypes = EntityUtil.CheckArgumentOutOfRange(_primitiveTypeMap, (int)type.PrimitiveTypeKind, "primitiveTypeKind");

            // If there isn't a list for the given model type, create one and add it
            if (primitiveTypes == null)
            {
                primitiveTypes = new List<PrimitiveType>();
                primitiveTypes.Add(type);
                _primitiveTypeMap[(int)type.PrimitiveTypeKind] = primitiveTypes;
            }
            else
            {
                primitiveTypes.Add(type);
            }
        }
 internal static bool TryGetDbTypeFromPrimitiveType(PrimitiveType type, out DbType dbType)
 {
     switch (type.PrimitiveTypeKind)
     {
         case PrimitiveTypeKind.Binary:
             dbType = DbType.Binary;
             return true;
         case PrimitiveTypeKind.Boolean:
             dbType = DbType.Boolean;
             return true;
         case PrimitiveTypeKind.Byte:
             dbType = DbType.Byte;
             return true;
         case PrimitiveTypeKind.DateTime:
             dbType = DbType.DateTime;
             return true;
         case PrimitiveTypeKind.Time:
             dbType = DbType.Time;
             return true;
         case PrimitiveTypeKind.DateTimeOffset:
             dbType = DbType.DateTimeOffset;
             return true;
         case PrimitiveTypeKind.Decimal:
             dbType = DbType.Decimal;
             return true;
         case PrimitiveTypeKind.Double:
             dbType = DbType.Double;
             return true;
         case PrimitiveTypeKind.Guid:
             dbType = DbType.Guid;
             return true;
         case PrimitiveTypeKind.Single:
             dbType = DbType.Single;
             return true;
         case PrimitiveTypeKind.SByte:
             dbType = DbType.SByte;
             return true;
         case PrimitiveTypeKind.Int16:
             dbType = DbType.Int16;
             return true;
         case PrimitiveTypeKind.Int32:
             dbType = DbType.Int32;
             return true;
         case PrimitiveTypeKind.Int64:
             dbType = DbType.Int64;
             return true;
         case PrimitiveTypeKind.String:
             dbType = DbType.String;
             return true;
         default:
             dbType = default(DbType);
             return false;
     }
 }
            // Find "sanctioned" default value
            internal static void GetPropagatorResultForPrimitiveType(PrimitiveType primitiveType, out PropagatorResult result)
            {
                object value;
                if (!TryGetDefaultValue(primitiveType, out value))
                {
                    // If none exists, default to lowest common denominator for constants
                    value = default(byte);
                }

                // Return a new constant expression flagged as unknown since the value is only there for
                // show. (Not entirely for show, because null constraints may require a value for a record,
                // whether that record is a placeholder or not).
                result = PropagatorResult.CreateSimpleValue(PropagatorFlags.NoFlags, value);
            }
            /// <summary>
            ///     Attempts to retrieve the the default value for the specified primitive type.
            /// </summary>
            /// <param name="primitiveType">A primitive type.</param>
            /// <param name="defaultValue">The default value for the primitive type.</param>
            /// <returns>true if a default value was found, false otherwise.</returns>
            private static bool TryGetDefaultValue(PrimitiveType primitiveType, out object defaultValue)
            {
                var primitiveTypeKind = primitiveType.PrimitiveTypeKind;

                return Helper.IsSpatialType(primitiveType)
                           ? _spatialTypeDefaultMap.Value.TryGetValue(primitiveTypeKind, out defaultValue)
                           : _typeDefaultMap.TryGetValue(primitiveTypeKind, out defaultValue);
            }
Exemple #13
0
 /// <summary>
 ///     Returns the list of common super types of two primitive types.
 /// </summary>
 /// <param name="primitiveType1"> </param>
 /// <param name="primitiveType2"> </param>
 /// <returns> </returns>
 private static objectModel.ReadOnlyCollection <PrimitiveType> GetPrimitiveCommonSuperTypes(
     PrimitiveType primitiveType1, PrimitiveType primitiveType2)
 {
     ComputeCommonTypeClosure();
     return(_commonTypeClosure[(int)primitiveType1.PrimitiveTypeKind, (int)primitiveType2.PrimitiveTypeKind]);
 }
        private void Assert_Timestamp(BinaryPropertyConfiguration binaryPropertyConfiguration)
        {
            var property = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            binaryPropertyConfiguration.Configure(property);

            Assert.Equal("String", property.TypeName);
            Assert.Equal(false, property.Nullable);
            Assert.Equal(ConcurrencyMode.Fixed, property.ConcurrencyMode);
            Assert.Equal(StoreGeneratedPattern.Computed, property.GetStoreGeneratedPattern());
            Assert.Equal(8, property.MaxLength.Value);
            
            var primitiveType = new PrimitiveType();
            EdmType.Initialize(primitiveType, "rowversion", "N", DataSpace.SSpace, false, null);
            var mockDbProviderManifest = new Mock<DbProviderManifest>();
            mockDbProviderManifest.Setup(m => m.GetStoreTypes())
                .Returns(new ReadOnlyCollection<PrimitiveType>(new List<PrimitiveType> { primitiveType }));
            mockDbProviderManifest.Setup(m => m.GetFacetDescriptions(It.IsAny<EdmType>()))
                .Returns(new ReadOnlyCollection<FacetDescription>(new List<FacetDescription>()));
            PrimitiveType.Initialize(primitiveType, PrimitiveTypeKind.Binary, mockDbProviderManifest.Object);

            var column = EdmProperty.CreatePrimitive("P", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
            binaryPropertyConfiguration.Configure(
                column,
                new EntityType("E", "N", DataSpace.SSpace),
                mockDbProviderManifest.Object);

            Assert.Equal("rowversion", column.TypeName);
            Assert.Equal(true, column.Nullable);
            Assert.Equal(ConcurrencyMode.None, column.ConcurrencyMode);
            Assert.Null(column.GetStoreGeneratedPattern());
            Assert.Null(column.MaxLength);
        }
        /// <summary>
        ///     Initializes all the primitive types
        /// </summary>
        private void InitializePrimitiveTypes()
        {
            if (_primitiveTypes != null)
            {
                return;
            }

            var primitiveTypes = new PrimitiveType[s_PrimitiveTypeCount];
            primitiveTypes[(int)PrimitiveTypeKind.Binary] = CreatePrimitiveType(typeof(Byte[]), PrimitiveTypeKind.Binary);
            primitiveTypes[(int)PrimitiveTypeKind.Boolean] = CreatePrimitiveType(typeof(Boolean), PrimitiveTypeKind.Boolean);
            primitiveTypes[(int)PrimitiveTypeKind.Byte] = CreatePrimitiveType(typeof(Byte), PrimitiveTypeKind.Byte);
            primitiveTypes[(int)PrimitiveTypeKind.DateTime] = CreatePrimitiveType(typeof(DateTime), PrimitiveTypeKind.DateTime);
            primitiveTypes[(int)PrimitiveTypeKind.Time] = CreatePrimitiveType(typeof(TimeSpan), PrimitiveTypeKind.Time);
            primitiveTypes[(int)PrimitiveTypeKind.DateTimeOffset] = CreatePrimitiveType(
                typeof(DateTimeOffset), PrimitiveTypeKind.DateTimeOffset);
            primitiveTypes[(int)PrimitiveTypeKind.Decimal] = CreatePrimitiveType(typeof(Decimal), PrimitiveTypeKind.Decimal);
            primitiveTypes[(int)PrimitiveTypeKind.Double] = CreatePrimitiveType(typeof(Double), PrimitiveTypeKind.Double);
            primitiveTypes[(int)PrimitiveTypeKind.Geography] = CreatePrimitiveType(typeof(DbGeography), PrimitiveTypeKind.Geography);
            primitiveTypes[(int)PrimitiveTypeKind.Geometry] = CreatePrimitiveType(typeof(DbGeometry), PrimitiveTypeKind.Geometry);
            primitiveTypes[(int)PrimitiveTypeKind.Guid] = CreatePrimitiveType(typeof(Guid), PrimitiveTypeKind.Guid);
            primitiveTypes[(int)PrimitiveTypeKind.Int16] = CreatePrimitiveType(typeof(Int16), PrimitiveTypeKind.Int16);
            primitiveTypes[(int)PrimitiveTypeKind.Int32] = CreatePrimitiveType(typeof(Int32), PrimitiveTypeKind.Int32);
            primitiveTypes[(int)PrimitiveTypeKind.Int64] = CreatePrimitiveType(typeof(Int64), PrimitiveTypeKind.Int64);
            primitiveTypes[(int)PrimitiveTypeKind.SByte] = CreatePrimitiveType(typeof(SByte), PrimitiveTypeKind.SByte);
            primitiveTypes[(int)PrimitiveTypeKind.Single] = CreatePrimitiveType(typeof(Single), PrimitiveTypeKind.Single);
            primitiveTypes[(int)PrimitiveTypeKind.String] = CreatePrimitiveType(typeof(String), PrimitiveTypeKind.String);

            var readOnlyTypes = new ReadOnlyCollection<PrimitiveType>(primitiveTypes);

            // Set the result to _primitiveTypes at the end
            Interlocked.CompareExchange(ref _primitiveTypes, readOnlyTypes, null);
        }
Exemple #16
0
        public void WriteAssociationSetMapping_should_write_modification_function_mapping()
        {
            var fixture = new Fixture();

            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet  = new EntitySet("ES", "S", null, null, entityType);

            new EntityContainer("EC", DataSpace.SSpace).AddEntitySetBase(entitySet);
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationEndMember1 = new AssociationEndMember("Source", new EntityType("E", "N", DataSpace.CSpace));

            associationSet.AddAssociationSetEnd(new AssociationSetEnd(entitySet, associationSet, associationEndMember1));

            var associationEndMember2 = new AssociationEndMember("Target", new EntityType("E", "N", DataSpace.CSpace));

            associationSet.AddAssociationSetEnd(new AssociationSetEnd(entitySet, associationSet, associationEndMember2));

            var storageModificationFunctionMapping
                = new ModificationFunctionMapping(
                      associationSet,
                      associationSet.ElementType,
                      new EdmFunction("F", "N", DataSpace.SSpace, new EdmFunctionPayload()),
                      new[]
            {
                new ModificationFunctionParameterBinding(
                    new FunctionParameter(
                        "P",
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                        ParameterMode.In),
                    new ModificationFunctionMemberPath(
                        new EdmMember[]
                {
                    new EdmProperty("K"),
                    associationEndMember1
                },
                        associationSet),
                    true),
                new ModificationFunctionParameterBinding(
                    new FunctionParameter(
                        "P",
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                        ParameterMode.In),
                    new ModificationFunctionMemberPath(
                        new EdmMember[]
                {
                    new EdmProperty("K"),
                    associationEndMember2
                },
                        associationSet),
                    false)
            },
                      null,
                      null);

            var associationSetMapping
                = new AssociationSetMapping(
                      associationSet,
                      entitySet)
                {
                SourceEndMapping
                    = new EndPropertyMapping
                    {
                    AssociationEnd = associationEndMember1
                    },
                TargetEndMapping
                    = new EndPropertyMapping
                    {
                    AssociationEnd = associationEndMember2
                    },
                ModificationFunctionMapping = new AssociationSetModificationFunctionMapping(
                    associationSet,
                    storageModificationFunctionMapping,
                    storageModificationFunctionMapping)
                };

            fixture.Writer.WriteAssociationSetMappingElement(associationSetMapping);

            Assert.Equal(
                @"<AssociationSetMapping Name=""AS"" TypeName="".A"" StoreEntitySet=""E"">
  <EndProperty Name=""Source"" />
  <EndProperty Name=""Target"" />
  <ModificationFunctionMapping>
    <InsertFunction FunctionName=""N.F"">
      <EndProperty Name=""Source"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Current"" />
      </EndProperty>
      <EndProperty Name=""Target"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Original"" />
      </EndProperty>
    </InsertFunction>
    <DeleteFunction FunctionName=""N.F"">
      <EndProperty Name=""Source"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Current"" />
      </EndProperty>
      <EndProperty Name=""Target"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Original"" />
      </EndProperty>
    </DeleteFunction>
  </ModificationFunctionMapping>
</AssociationSetMapping>",
                fixture.ToString());
        }
        protected override void Visit(PrimitiveType primitiveType)
        {
            int index;
            if (!AddObjectToSeenListAndHashBuilder(primitiveType, out index))
            {
                return;
            }

            AddObjectStartDumpToHashBuilder(primitiveType, index);

            #region Inner data visit

            AddObjectContentToHashBuilder(primitiveType.Name);
            AddObjectContentToHashBuilder(primitiveType.NamespaceName);

            base.Visit(primitiveType);

            #endregion

            AddObjectEndDumpToHashBuilder();
        }
Exemple #18
0
 private static object GetSpatialValueFromProviderValue(
     object spatialValue, PrimitiveType parameterType, EntityConnection connection)
 {
     var providerServices = DbProviderServices.GetProviderServices(connection.StoreConnection);
     var storeItemCollection = (StoreItemCollection)connection.GetMetadataWorkspace().GetItemCollection(DataSpace.SSpace);
     var spatialServices = providerServices.GetSpatialServices(storeItemCollection.StoreProviderManifestToken);
     if (Helper.IsGeographicType(parameterType))
     {
         return spatialServices.GeographyFromProviderValue(spatialValue);
     }
     else
     {
         Debug.Assert(Helper.IsGeometricType(parameterType));
         return spatialServices.GeometryFromProviderValue(spatialValue);
     }
 }
Exemple #19
0
        private static bool TryGetCommonType(PrimitiveType primitiveType1, PrimitiveType primitiveType2, out EdmType commonType)
        {
            commonType = null;

            if (IsSubTypeOf(primitiveType1, primitiveType2))
            {
                commonType = primitiveType2;
                return true;
            }

            if (IsSubTypeOf(primitiveType2, primitiveType1))
            {
                commonType = primitiveType1;
                return true;
            }

            var superTypes = GetPrimitiveCommonSuperTypes(primitiveType1, primitiveType2);
            if (superTypes.Count > 0)
            {
                commonType = superTypes[0];
                return true;
            }

            return false;
        }
Exemple #20
0
        private static bool IsSubTypeOf(PrimitiveType subPrimitiveType, PrimitiveType superPrimitiveType)
        {
            if (ReferenceEquals(subPrimitiveType, superPrimitiveType))
            {
                return true;
            }

            if (Helper.AreSameSpatialUnionType(subPrimitiveType, superPrimitiveType))
            {
                return true;
            }

            var superTypes = EdmProviderManifest.Instance.GetPromotionTypes(subPrimitiveType);

            return (-1 != superTypes.IndexOf(superPrimitiveType));
        }
Exemple #21
0
 /// <summary>
 ///     Returns the list of common super types of two primitive types.
 /// </summary>
 /// <param name="primitiveType1"> </param>
 /// <param name="primitiveType2"> </param>
 /// <returns> </returns>
 private static objectModel.ReadOnlyCollection<PrimitiveType> GetPrimitiveCommonSuperTypes(
     PrimitiveType primitiveType1, PrimitiveType primitiveType2)
 {
     ComputeCommonTypeClosure();
     return _commonTypeClosure[(int)primitiveType1.PrimitiveTypeKind, (int)primitiveType2.PrimitiveTypeKind];
 }
 protected static bool TryGetPrimitiveType(Type type, out PrimitiveType primitiveType)
 {
     return ClrProviderManifest.Instance.TryGetPrimitiveType(Nullable.GetUnderlyingType(type) ?? type, out primitiveType);
 }
 /// <summary>
 ///     Construct an internal (not from schema) CDM scalar type
 /// </summary>
 /// <param name="parentElement"> the owning schema </param>
 /// <param name="typeName"> the naem of the type </param>
 /// <param name="primitiveType"> the PrimitiveTypeKind of the type </param>
 internal ScalarType(Schema parentElement, string typeName, PrimitiveType primitiveType)
     : base(parentElement)
 {
     Name = typeName;
     _primitiveType = primitiveType;
 }
        private static object GetSpatialValueFromProviderValue(
            object spatialValue, PrimitiveType parameterType, EntityConnection connection)
        {
            var spatialServices = DbProviderServices.GetSpatialServices(DbConfiguration.DependencyResolver, connection);

            if (Helper.IsGeographicType(parameterType))
            {
                return spatialServices.GeographyFromProviderValue(spatialValue);
            }

            Debug.Assert(Helper.IsGeometricType(parameterType));
            return spatialServices.GeometryFromProviderValue(spatialValue);
        }
        /// <summary>
        ///     Try and get the mapped type for the given primitiveTypeKind in the given dataspace
        /// </summary>
        /// <param name="primitiveTypeKind"> The primitive type kind of the primitive type to retrieve </param>
        /// <param name="facets"> The facets to use in picking the primitive type </param>
        /// <param name="type"> The resulting type </param>
        /// <returns> Whether a type was retrieved or not </returns>
        internal bool TryGetType(PrimitiveTypeKind primitiveTypeKind, IEnumerable<Facet> facets, out PrimitiveType type)
        {
            type = null;

            // Now, see if we have any types for this model type, if so, loop through to find the best matching one
            var primitiveTypes = EntityUtil.CheckArgumentOutOfRange(_primitiveTypeMap, (int)primitiveTypeKind, "primitiveTypeKind");
            if ((null != primitiveTypes)
                && (0 < primitiveTypes.Count))
            {
                if (primitiveTypes.Count == 1)
                {
                    type = primitiveTypes[0];
                    return true;
                }

                if (facets == null)
                {
                    var facetDescriptions = EdmProviderManifest.GetInitialFacetDescriptions(primitiveTypeKind);
                    if (facetDescriptions == null)
                    {
                        type = primitiveTypes[0];
                        return true;
                    }

                    Debug.Assert(facetDescriptions.Length > 0);
                    facets = CreateInitialFacets(facetDescriptions);
                }

                Debug.Assert(type == null, "type must be null here");
                var isMaxLengthSentinel = false;

                // Create a dictionary of facets for easy lookup
                foreach (var facet in facets)
                {
                    if ((primitiveTypeKind == PrimitiveTypeKind.String ||
                         primitiveTypeKind == PrimitiveTypeKind.Binary) &&
                        facet.Value != null &&
                        facet.Name == DbProviderManifest.MaxLengthFacetName
                        &&
                        Helper.IsUnboundedFacetValue(facet))
                    {
                        // MaxLength has the sentinel value. So this facet need not be added.
                        isMaxLengthSentinel = true;
                        continue;
                    }
                }

                var maxLength = 0;
                // Find a primitive type with the matching constraint
                foreach (var primitiveType in primitiveTypes)
                {
                    if (isMaxLengthSentinel)
                    {
                        if (type == null)
                        {
                            type = primitiveType;
                            maxLength =
                                Helper.GetFacet(primitiveType.FacetDescriptions, DbProviderManifest.MaxLengthFacetName).MaxValue.Value;
                        }
                        else
                        {
                            var newMaxLength =
                                Helper.GetFacet(primitiveType.FacetDescriptions, DbProviderManifest.MaxLengthFacetName).MaxValue.Value;
                            if (newMaxLength > maxLength)
                            {
                                type = primitiveType;
                                maxLength = newMaxLength;
                            }
                        }
                    }
                    else
                    {
                        type = primitiveType;
                        break;
                    }
                }

                Debug.Assert(type != null);
                return true;
            }

            return false;
        }
Exemple #26
0
        public static EnumType Create(
            string name,
            string namespaceName,
            PrimitiveType underlyingType,
            bool isFlags,
            IEnumerable<EnumMember> members,
            IEnumerable<MetadataProperty> metadataProperties)
        {
            Check.NotEmpty(name, "name");
            Check.NotEmpty(namespaceName, "namespaceName");
            Check.NotNull(underlyingType, "underlyingType");

            if (!Helper.IsSupportedEnumUnderlyingType(underlyingType.PrimitiveTypeKind))
            {
                throw new ArgumentException(Strings.InvalidEnumUnderlyingType, "underlyingType");
            }

            var instance = new EnumType(name, namespaceName, underlyingType, isFlags, DataSpace.CSpace);

            if (members != null)
            {
                foreach (var member in members)
                {
                    if (!Helper.IsEnumMemberValueInRange(
                        underlyingType.PrimitiveTypeKind, Convert.ToInt64(member.Value, CultureInfo.InvariantCulture)))
                    {
                        throw new ArgumentException(
                            Strings.EnumMemberValueOutOfItsUnderylingTypeRange(
                                member.Value, member.Name, underlyingType.Name),
                            "members");
                    }

                    instance.AddMember(member);
                }
            }

            if (metadataProperties != null)
            {
                instance.AddMetadataProperties(metadataProperties.ToList());
            }

            instance.SetReadOnly();

            return instance;
        }
 /// <summary>
 ///     Initialize the primitive type with the given
 /// </summary>
 /// <param name="clrType"> The CLR type of this type </param>
 /// <param name="primitiveTypeKind"> The primitive type kind of the primitive type </param>
 private PrimitiveType CreatePrimitiveType(Type clrType, PrimitiveTypeKind primitiveTypeKind)
 {
     // Figures out the base type
     var baseType = MetadataItem.EdmProviderManifest.GetPrimitiveType(primitiveTypeKind);
     var primitiveType = new PrimitiveType(clrType, baseType, this);
     primitiveType.SetReadOnly();
     return primitiveType;
 }
Exemple #28
0
        public void WriteEntityContainerMappingElement_should_write_function_import_elements()
        {
            var typeUsage =
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32));

            var complexTypeProperty1 = new EdmProperty("CTProperty1", typeUsage);
            var complexTypeProperty2 = new EdmProperty("CTProperty2", typeUsage);

            var complexType = new ComplexType("CT", "Ns", DataSpace.CSpace);

            complexType.AddMember(complexTypeProperty1);
            complexType.AddMember(complexTypeProperty2);

            var functionImport =
                new EdmFunction(
                    "f_c", "Ns", DataSpace.CSpace,
                    new EdmFunctionPayload
            {
                IsComposable     = true,
                IsFunctionImport = true,
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "ReturnValue",
                        TypeUsage.CreateDefaultTypeUsage(complexType.GetCollectionType()),
                        ParameterMode.ReturnValue)
                },
                Parameters =
                    new[]
                {
                    new FunctionParameter("param", typeUsage, ParameterMode.Out)
                }
            });

            typeUsage = ProviderRegistry.Sql2008_ProviderManifest.GetStoreType(typeUsage);
            var rowTypeProperty1 = new EdmProperty("RTProperty1", typeUsage);
            var rowTypeProperty2 = new EdmProperty("RTProperty2", typeUsage);
            var rowType          = new RowType(new[] { rowTypeProperty1, rowTypeProperty2 });

            var storeFunction =
                new EdmFunction(
                    "f_s", "Ns.Store", DataSpace.SSpace,
                    new EdmFunctionPayload
            {
                ReturnParameters =
                    new[]
                {
                    new FunctionParameter(
                        "Return",
                        TypeUsage.CreateDefaultTypeUsage(rowType),
                        ParameterMode.ReturnValue)
                },
                Parameters =
                    new[]
                {
                    new FunctionParameter("param", typeUsage, ParameterMode.Out)
                }
            });

            var structuralTypeMapping =
                new Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> >(
                    complexType, new List <ConditionPropertyMapping>(), new List <PropertyMapping>());

            structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(complexTypeProperty1, rowTypeProperty1));
            structuralTypeMapping.Item3.Add(new ScalarPropertyMapping(complexTypeProperty2, rowTypeProperty2));

            var functionImportMapping = new FunctionImportMappingComposable(
                functionImport,
                storeFunction,
                new List <Tuple <StructuralType, List <ConditionPropertyMapping>, List <PropertyMapping> > >
            {
                structuralTypeMapping
            });

            var containerMapping = new EntityContainerMapping(new EntityContainer("C", DataSpace.SSpace));

            containerMapping.AddFunctionImportMapping(functionImportMapping);

            var fixture = new Fixture();

            fixture.Writer.WriteEntityContainerMappingElement(containerMapping);

            Assert.Equal(
                @"<EntityContainerMapping StorageEntityContainer="""" CdmEntityContainer=""C"">
  <FunctionImportMapping FunctionName=""Ns.Store.f_s"" FunctionImportName=""f_c"">
    <ResultMapping>
      <ComplexTypeMapping TypeName=""Ns.CT"">
        <ScalarProperty Name=""CTProperty1"" ColumnName=""RTProperty1"" />
        <ScalarProperty Name=""CTProperty2"" ColumnName=""RTProperty2"" />
      </ComplexTypeMapping>
    </ResultMapping>
  </FunctionImportMapping>
</EntityContainerMapping>",
                fixture.ToString());
        }
 protected virtual void Visit(PrimitiveType primitiveType)
 {
 }
Exemple #30
0
 protected static bool TryGetPrimitiveType(Type type, out PrimitiveType primitiveType)
 {
     return(ClrProviderManifest.Instance.TryGetPrimitiveType(Nullable.GetUnderlyingType(type) ?? type, out primitiveType));
 }
Exemple #31
0
 // <summary>
 // Initializes a new instance of the EnumType class. This default constructor is used for bootstraping
 // </summary>
 internal EnumType()
 {
     _underlyingType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32);
     _isFlags = false;
 }
        // <summary>
        // Try and get the mapped type for the given primitiveTypeKind in the given dataspace
        // </summary>
        // <param name="primitiveTypeKind"> The primitive type kind of the primitive type to retrieve </param>
        // <param name="facets"> The facets to use in picking the primitive type </param>
        // <param name="type"> The resulting type </param>
        // <returns> Whether a type was retrieved or not </returns>
        internal bool TryGetType(PrimitiveTypeKind primitiveTypeKind, IEnumerable <Facet> facets, out PrimitiveType type)
        {
            type = null;

            // Now, see if we have any types for this model type, if so, loop through to find the best matching one
            var primitiveTypes = EntityUtil.CheckArgumentOutOfRange(_primitiveTypeMap, (int)primitiveTypeKind, "primitiveTypeKind");

            if ((null != primitiveTypes) &&
                (0 < primitiveTypes.Count))
            {
                if (primitiveTypes.Count == 1)
                {
                    type = primitiveTypes[0];
                    return(true);
                }

                if (facets == null)
                {
                    var facetDescriptions = EdmProviderManifest.GetInitialFacetDescriptions(primitiveTypeKind);
                    if (facetDescriptions == null)
                    {
                        type = primitiveTypes[0];
                        return(true);
                    }

                    Debug.Assert(facetDescriptions.Length > 0);
                    facets = CreateInitialFacets(facetDescriptions);
                }

                Debug.Assert(type == null, "type must be null here");
                var isMaxLengthSentinel = false;

                // Create a dictionary of facets for easy lookup
                foreach (var facet in facets)
                {
                    if ((primitiveTypeKind == PrimitiveTypeKind.String ||
                         primitiveTypeKind == PrimitiveTypeKind.Binary)
                        &&
                        facet.Value != null
                        &&
                        facet.Name == DbProviderManifest.MaxLengthFacetName
                        &&
                        Helper.IsUnboundedFacetValue(facet))
                    {
                        // MaxLength has the sentinel value. So this facet need not be added.
                        isMaxLengthSentinel = true;
                        continue;
                    }
                }

                var maxLength = 0;
                // Find a primitive type with the matching constraint
                foreach (var primitiveType in primitiveTypes)
                {
                    if (isMaxLengthSentinel)
                    {
                        if (type == null)
                        {
                            type      = primitiveType;
                            maxLength =
                                Helper.GetFacet(primitiveType.FacetDescriptions, DbProviderManifest.MaxLengthFacetName).MaxValue.Value;
                        }
                        else
                        {
                            var newMaxLength =
                                Helper.GetFacet(primitiveType.FacetDescriptions, DbProviderManifest.MaxLengthFacetName).MaxValue.Value;
                            if (newMaxLength > maxLength)
                            {
                                type      = primitiveType;
                                maxLength = newMaxLength;
                            }
                        }
                    }
                    else
                    {
                        type = primitiveType;
                        break;
                    }
                }

                Debug.Assert(type != null);
                return(true);
            }

            return(false);
        }
 /// <summary>
 ///     Perform initialization that's common across all constructors
 /// </summary>
 /// <param name="primitiveType"> The primitive type to initialize </param>
 /// <param name="primitiveTypeKind"> The primitive type kind of this primitive type </param>
 /// <param name="providerManifest"> The ProviderManifest of the provider of this type </param>
 internal static void Initialize(
     PrimitiveType primitiveType,
     PrimitiveTypeKind primitiveTypeKind,
     DbProviderManifest providerManifest)
 {
     primitiveType._primitiveTypeKind = primitiveTypeKind;
     primitiveType._providerManifest = providerManifest;
 }
        // <summary>
        // returns the primitive type for a given primitive type kind.
        // </summary>
        internal virtual bool TryGetMappedPrimitiveType(PrimitiveTypeKind primitiveTypeKind, out PrimitiveType primitiveType)
        {
            primitiveType = _metadataWorkspace.GetMappedPrimitiveType(primitiveTypeKind, DataSpace.CSpace);

            return (null != primitiveType);
        }
        private static bool ValidateFacetValueInRange(
            PrimitiveType storePrimitiveType, string facetName, int actualValue, TableDetailsRow row, ICollection<EdmSchemaError> errors)
        {
            Debug.Assert(storePrimitiveType != null, "storePrimitiveType != null");
            Debug.Assert(facetName != null, "facetName != null");
            Debug.Assert(row != null, "row != null");
            Debug.Assert(errors != null, "errors != null");

            var facetDescription = storePrimitiveType.FacetDescriptions.SingleOrDefault(f => f.FacetName == facetName);

            if (facetDescription != null
                && !facetDescription.IsConstant)
            {
                if (actualValue < facetDescription.MinValue
                    || actualValue > facetDescription.MaxValue)
                {
                    errors.Add(
                        new EdmSchemaError(
                            string.Format(
                                CultureInfo.InvariantCulture,
                                Resources_VersioningFacade.ColumnFacetValueOutOfRange,
                                facetName,
                                actualValue,
                                facetDescription.MinValue,
                                facetDescription.MaxValue,
                                row.ColumnName,
                                row.GetMostQualifiedTableName()),
                            (int)ModelBuilderErrorCode.FacetValueOutOfRange,
                            EdmSchemaErrorSeverity.Warning));

                    return false;
                }
            }

            return true;
        }
Exemple #36
0
 // <summary>
 // Initializes a new instance of the EnumType class. This default constructor is used for bootstraping
 // </summary>
 internal EnumType()
 {
     _underlyingType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32);
     _isFlags        = false;
 }