private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { IEdmProperty edmProperty = null; switch (property.Kind) { case PropertyKind.Primitive: PrimitivePropertyConfiguration primitiveProperty = (PrimitivePropertyConfiguration)property; EdmPrimitiveTypeKind typeKind = primitiveProperty.TargetEdmTypeKind ?? GetTypeKind(primitiveProperty.PropertyInfo.PropertyType); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( typeKind, primitiveProperty.OptionalProperty); edmProperty = type.AddStructuralProperty( primitiveProperty.Name, primitiveTypeReference, defaultValue: null); break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType; edmProperty = type.AddStructuralProperty( complexProperty.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property); break; case PropertyKind.Enum: edmProperty = CreateStructuralTypeEnumPropertyBody(type, (EnumPropertyConfiguration)property); break; default: break; } if (edmProperty != null) { if (property.PropertyInfo != null) { _properties[property.PropertyInfo] = edmProperty; } if (property.IsRestricted) { _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property); } } } }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { IEdmProperty edmProperty = null; switch (property.Kind) { case PropertyKind.Primitive: PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration; CreatePrimitiveProperty(primitiveProperty, type, config, out edmProperty); break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType; edmProperty = type.AddStructuralProperty( complexProperty.PropertyInfo.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration; IEdmTypeReference elementTypeReference = null; if (_types.ContainsKey(collectionProperty.ElementType)) { IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType; elementTypeReference = new EdmComplexTypeReference(elementType, false); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); } edmProperty = type.AddStructuralProperty( collectionProperty.PropertyInfo.Name, new EdmCollectionTypeReference( new EdmCollectionType(elementTypeReference), collectionProperty.OptionalProperty)); break; default: break; } if (edmProperty != null && property.PropertyInfo != null) { _properties[property.PropertyInfo] = edmProperty; } } }
/// <summary> /// Implement the creation of an EDM Entity Type or EDM Complex Type. /// </summary> /// <param name="clrType">The CLR type of the new schema element to create.</param> /// <param name="context">The query context.</param> /// <param name="entryEdmType">The new Schema element.</param> private static void CreateSchemaType(Type clrType, ODataQueryContext context, EdmStructuredType entryEdmType) { foreach (var pi in clrType.GetProperties()) { if (pi.Name == "ComparerInstance") { continue; } if (pi.PropertyType.IsPrimitive || pi.PropertyType.FullName == "System.String") { entryEdmType.AddStructuralProperty(pi.Name, GetPrimitiveTypeKind(pi.PropertyType), true); } else if (pi.PropertyType.IsEnum) { var enumType = GetEnumTypeKind(pi.PropertyType, context); if (enumType != null) { entryEdmType.AddStructuralProperty(pi.Name, enumType); } else { entryEdmType.AddStructuralProperty(pi.Name, EdmPrimitiveTypeKind.Int32); } } else { var propEdmType = context.Model.FindDeclaredType(pi.PropertyType.FullName); if (propEdmType != null) { entryEdmType.AddStructuralProperty(pi.Name, propEdmType.ToEdmTypeReference(true)); } else { //var t = Type.GetType(pi.PropertyType.FullName); var t = pi.PropertyType; var edmType = context.Model.GetEdmTypeReference(t); if (edmType != null) { entryEdmType.AddStructuralProperty(pi.Name, edmType); } else { throw new InvalidOperationException(string.Format("Could not find EDM type of {0}", pi.PropertyType.FullName)); } } } } }
private void AddPropertiesToEdmType(EdmStructuredType entityType) { foreach (var property in this.properties.Values) { entityType.AddStructuralProperty(property.EdmPropertyName, property.DataType.GetEdmTypeReference(property.CanBeNull)); } }
private IEdmProperty CreateStructuralTypeEnumPropertyBody(EdmStructuredType type, StructuralTypeConfiguration config, EnumPropertyConfiguration enumProperty) { Type enumPropertyType = TypeHelper.GetUnderlyingTypeOrSelf(enumProperty.RelatedClrType); IEdmType edmType = GetEdmType(enumPropertyType); if (edmType == null) { throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, enumPropertyType.Name); } IEdmEnumType enumType = (IEdmEnumType)edmType; IEdmTypeReference enumTypeReference = new EdmEnumTypeReference(enumType, enumProperty.OptionalProperty); // Set concurrency token if is entity type, and concurrency token is true EdmConcurrencyMode enumConcurrencyMode = EdmConcurrencyMode.None; if (config.Kind == EdmTypeKind.Entity && enumProperty.ConcurrencyToken) { enumConcurrencyMode = EdmConcurrencyMode.Fixed; } return(type.AddStructuralProperty( enumProperty.Name, enumTypeReference, defaultValue: null, concurrencyMode: enumConcurrencyMode)); }
public void AddToEdmType(EdmStructuredType edmType, IEnumerable <Language> languages, string schemaName, Func <EdmComplexType, EdmComplexType> typeResolver) { Guard.NotNull(edmType, nameof(edmType)); Guard.NotNull(languages, nameof(languages)); Guard.NotNull(typeResolver, nameof(typeResolver)); if (!RawProperties.IsLocalizable) { languages = new[] { Language.Invariant }; } var edmValueType = CreateEdmType(); if (edmValueType == null) { return; } var languageType = typeResolver(new EdmComplexType("Squidex", $"{schemaName}{Name.ToPascalCase()}Property")); foreach (var language in languages) { languageType.AddStructuralProperty(language.Iso2Code, edmValueType); } edmType.AddStructuralProperty(Name, new EdmComplexTypeReference(languageType, false)); }
private void AddComponents(EdmModel model, EdmStructuredType entity, IEnumerable <ComponentInfo> components) { foreach (ComponentInfo componentInfo in components) { var edmSchemaElement = ((IEdmSchemaElement)entity); EdmComplexType complexType = new EdmComplexType(edmSchemaElement.Namespace, componentInfo.Name); foreach (var databaseColumn in componentInfo.Columns) { AddPropertyToEntity(complexType, databaseColumn); } //// add created type model.AddElement(complexType); if (componentInfo.ChildComponents != null && componentInfo.ChildComponents.Any()) { //// we are passing here acutaly created complexType because it can be nested AddComponents(model, complexType, componentInfo.ChildComponents); } //// adding referenece to actual entity entity.AddStructuralProperty(componentInfo.Name.Remove(0, (edmSchemaElement.Name + _separator).Length), new EdmComplexTypeReference(complexType, false)); } }
private void CreateStructuralTypeBody(EdmStructuredType type, IStructuralTypeConfiguration config) { foreach (PrimitivePropertyConfiguration prop in config.Properties.OfType <PrimitivePropertyConfiguration>()) { type.AddStructuralProperty( prop.PropertyInfo.Name, GetTypeKind(prop.PropertyInfo.PropertyType), prop.OptionalProperty); } foreach (ComplexPropertyConfiguration prop in config.Properties.OfType <ComplexPropertyConfiguration>()) { IEdmComplexType complexType = _types[prop.RelatedClrType.FullName] as IEdmComplexType; type.AddStructuralProperty( prop.PropertyInfo.Name, new EdmComplexTypeReference(complexType, prop.OptionalProperty)); } }
private static IEdmStructuralProperty AddPropertyToEntity(EdmStructuredType entity, DatabaseColumn column) { var typeKind = EdmPrimitiveTypeKind.String; var typeMap = BuildEdmTypeMap(); if (typeMap.ContainsKey(column.DataType)) { typeKind = typeMap[column.DataType]; } //// because of posibility of changing column table nullability from which view is generated we always set nullable to TRUE. Then it is prepared for null value. return(entity.AddStructuralProperty(column.Name, typeKind, true)); }
private IEdmProperty CreateStructuralTypeCollectionPropertyBody(EdmStructuredType type, CollectionPropertyConfiguration collectionProperty) { IEdmTypeReference elementTypeReference = null; Type clrType = TypeHelper.GetUnderlyingTypeOrSelf(collectionProperty.ElementType); if (clrType.GetTypeInfo().IsEnum) { IEdmType edmType = GetEdmType(clrType); if (edmType == null) { throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, clrType.Name); } IEdmEnumType enumElementType = (IEdmEnumType)edmType; bool isNullable = collectionProperty.ElementType != clrType; elementTypeReference = new EdmEnumTypeReference(enumElementType, isNullable); } else { IEdmType edmType = GetEdmType(collectionProperty.ElementType); if (edmType != null) { IEdmComplexType elementType = edmType as IEdmComplexType; // Work around for primitive types (ex: Int32 would be typed to System.Int32 instead of EdmInt) if (elementType != null) { elementTypeReference = new EdmComplexTypeReference(elementType, collectionProperty.OptionalProperty); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); } } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); } Contract.Assert(elementTypeReference != null); } return(type.AddStructuralProperty( collectionProperty.Name, new EdmCollectionTypeReference(new EdmCollectionType(elementTypeReference)))); }
private IEdmProperty CreateStructuralTypeEnumPropertyBody(EdmStructuredType type, EnumPropertyConfiguration enumProperty) { Type enumPropertyType = TypeHelper.GetUnderlyingTypeOrSelf(enumProperty.RelatedClrType); IEdmType edmType = GetEdmType(enumPropertyType); if (edmType == null) { throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, enumPropertyType.Name); } IEdmEnumType enumType = (IEdmEnumType)edmType; IEdmTypeReference enumTypeReference = new EdmEnumTypeReference(enumType, enumProperty.NullableProperty); return(type.AddStructuralProperty( enumProperty.Name, enumTypeReference, defaultValue: enumProperty.DefaultValueString)); }
private IEdmProperty CreateStructuralTypeCollectionPropertyBody(EdmStructuredType type, CollectionPropertyConfiguration collectionProperty) { IEdmTypeReference elementTypeReference; Type clrType = TypeHelper.GetUnderlyingTypeOrSelf(collectionProperty.ElementType); if (clrType == typeof(object)) { elementTypeReference = EdmCoreModel.Instance.GetUntyped(); } else if (TypeHelper.IsEnum(clrType)) { IEdmType edmType = GetEdmType(clrType); if (edmType == null) { throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, clrType.Name); } IEdmEnumType enumElementType = (IEdmEnumType)edmType; bool isNullable = collectionProperty.ElementType != clrType; elementTypeReference = new EdmEnumTypeReference(enumElementType, isNullable); } else { IEdmType edmType = GetEdmType(collectionProperty.ElementType); if (edmType != null) { IEdmComplexType elementType = edmType as IEdmComplexType; Contract.Assert(elementType != null); elementTypeReference = new EdmComplexTypeReference(elementType, collectionProperty.NullableProperty); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); Contract.Assert(elementTypeReference != null); } } return(type.AddStructuralProperty( collectionProperty.Name, new EdmCollectionTypeReference(new EdmCollectionType(elementTypeReference)))); }
public void AddToEdmType(EdmStructuredType edmType, PartitionResolver partitionResolver, string schemaName, Func <EdmComplexType, EdmComplexType> typeResolver) { Guard.NotNull(edmType, nameof(edmType)); Guard.NotNull(typeResolver, nameof(typeResolver)); Guard.NotNull(partitionResolver, nameof(partitionResolver)); var edmValueType = CreateEdmType(); if (edmValueType == null) { return; } var partitionType = typeResolver(new EdmComplexType("Squidex", $"{schemaName}{Name.ToPascalCase()}Property")); var partition = partitionResolver(partitioning); foreach (var partitionItem in partition) { partitionType.AddStructuralProperty(partitionItem.Key, edmValueType); } edmType.AddStructuralProperty(Name.EscapeEdmField(), new EdmComplexTypeReference(partitionType, false)); }
private IEdmProperty CreateProperty(EdmStructuredType declaringType, ResourceProperty resourceProperty) { IEdmProperty property; List <KeyValuePair <string, object> > annotations = (resourceProperty.CustomAnnotations == null) ? null : resourceProperty.CustomAnnotations.ToList <KeyValuePair <string, object> >(); ODataNullValueBehaviorKind nullValueReadBehaviorKind = ODataNullValueBehaviorKind.Default; if (resourceProperty.IsOfKind(ResourcePropertyKind.Primitive) || resourceProperty.IsOfKind(ResourcePropertyKind.Stream)) { IEdmPrimitiveTypeReference typeReference = MetadataProviderUtils.CreatePrimitiveTypeReference(resourceProperty.ResourceType, annotations); if (resourceProperty.IsOfKind(ResourcePropertyKind.Key)) { if (typeReference.IsNullable) { typeReference = (IEdmPrimitiveTypeReference)typeReference.Clone(false); } nullValueReadBehaviorKind = ODataNullValueBehaviorKind.IgnoreValue; } else if (MetadataProviderUtils.ShouldDisablePrimitivePropertyNullValidation(resourceProperty, typeReference)) { nullValueReadBehaviorKind = ODataNullValueBehaviorKind.DisableValidation; } string andRemoveDefaultValue = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations); EdmConcurrencyMode concurrencyMode = resourceProperty.IsOfKind(ResourcePropertyKind.ETag) ? EdmConcurrencyMode.Fixed : EdmConcurrencyMode.None; property = declaringType.AddStructuralProperty(resourceProperty.Name, typeReference, andRemoveDefaultValue, concurrencyMode); string mimeType = resourceProperty.MimeType; if (!string.IsNullOrEmpty(mimeType)) { this.SetMimeType(property, mimeType); } } else if (resourceProperty.IsOfKind(ResourcePropertyKind.ComplexType)) { IEdmTypeReference reference2 = this.EnsureTypeReference(resourceProperty.ResourceType, annotations); string defaultValue = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations); property = declaringType.AddStructuralProperty(resourceProperty.Name, reference2, defaultValue, EdmConcurrencyMode.None); if (this.metadataProvider.IsV1Provider && !reference2.IsNullable) { nullValueReadBehaviorKind = ODataNullValueBehaviorKind.DisableValidation; } } else if (resourceProperty.IsOfKind(ResourcePropertyKind.Collection)) { string str4 = MetadataProviderUtils.GetAndRemoveDefaultValue(annotations); IEdmTypeReference reference3 = this.EnsureTypeReference(resourceProperty.ResourceType, annotations); property = declaringType.AddStructuralProperty(resourceProperty.Name, reference3, str4, EdmConcurrencyMode.None); } else { if (!resourceProperty.IsOfKind(ResourcePropertyKind.ResourceSetReference) && !resourceProperty.IsOfKind(ResourcePropertyKind.ResourceReference)) { throw new InvalidOperationException(System.Data.Services.Strings.MetadataProviderEdmModel_UnsupportedResourcePropertyKind(resourceProperty.Kind.ToString())); } EdmEntityType type = (EdmEntityType)declaringType; IEdmTypeReference reference4 = resourceProperty.IsOfKind(ResourcePropertyKind.ResourceSetReference) ? this.EnsureEntityPrimitiveOrComplexCollectionTypeReference(resourceProperty.ResourceType, annotations) : this.EnsureTypeReference(resourceProperty.ResourceType, annotations); property = new MetadataProviderEdmNavigationProperty(type, resourceProperty.Name, reference4); type.AddProperty(property); } this.SetNullValueReaderBehavior(property, nullValueReadBehaviorKind); MetadataProviderUtils.ConvertCustomAnnotations(this, annotations, property); return(property); }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { IEdmProperty edmProperty = null; switch (property.Kind) { case PropertyKind.Primitive: PrimitivePropertyConfiguration primitiveProperty = (PrimitivePropertyConfiguration)property; EdmPrimitiveTypeKind typeKind = primitiveProperty.TargetEdmTypeKind ?? GetTypeKind(primitiveProperty.PropertyInfo.PropertyType); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( typeKind, primitiveProperty.NullableProperty); if (typeKind == EdmPrimitiveTypeKind.Decimal) { DecimalPropertyConfiguration decimalProperty = primitiveProperty as DecimalPropertyConfiguration; if (decimalProperty.Precision.HasValue || decimalProperty.Scale.HasValue) { primitiveTypeReference = new EdmDecimalTypeReference( (IEdmPrimitiveType)primitiveTypeReference.Definition, primitiveTypeReference.IsNullable, decimalProperty.Precision, decimalProperty.Scale.HasValue ? decimalProperty.Scale : 0); } } else if (EdmLibHelpers.HasPrecision(typeKind)) { PrecisionPropertyConfiguration precisionProperty = primitiveProperty as PrecisionPropertyConfiguration; primitiveTypeReference = AddPrecisionConfigInPrimitiveTypeReference( precisionProperty, primitiveTypeReference); } else if (EdmLibHelpers.HasLength(typeKind)) { LengthPropertyConfiguration lengthProperty = primitiveProperty as LengthPropertyConfiguration; primitiveTypeReference = AddLengthConfigInPrimitiveTypeReference( lengthProperty, primitiveTypeReference); } edmProperty = type.AddStructuralProperty( primitiveProperty.Name, primitiveTypeReference, defaultValue: primitiveProperty.DefaultValueString); break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType; edmProperty = type.AddStructuralProperty( complexProperty.Name, new EdmComplexTypeReference(complexType, complexProperty.NullableProperty)); break; case PropertyKind.Collection: edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property); break; case PropertyKind.Enum: edmProperty = CreateStructuralTypeEnumPropertyBody(type, (EnumPropertyConfiguration)property); break; default: break; } if (edmProperty != null) { if (property.PropertyInfo != null) { _properties[property.PropertyInfo] = edmProperty; } if (property.IsRestricted) { _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property); } //if (property.QueryConfiguration.ModelBoundQuerySettings != null) //{ // _propertiesQuerySettings.Add(edmProperty, property.QueryConfiguration.ModelBoundQuerySettings); //} } } }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { switch (property.Kind) { case PropertyKind.Primitive: PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration; EdmPrimitiveTypeKind typeKind = GetTypeKind(primitiveProperty.PropertyInfo.PropertyType); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( typeKind, primitiveProperty.OptionalProperty); var primitiveProp = new EdmStructuralProperty( type, primitiveProperty.PropertyInfo.Name, primitiveTypeReference); type.AddProperty(primitiveProp); // Set Annotation StoreGeneratedPattern if (config.Kind == EdmTypeKind.Entity && primitiveProperty.StoreGeneratedPattern != DatabaseGeneratedOption.None) { _directValueAnnotations.Add( new StoreGeneratedPatternAnnotation(primitiveProp, primitiveProperty.StoreGeneratedPattern)); } break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = _types[complexProperty.RelatedClrType] as IEdmComplexType; type.AddStructuralProperty( complexProperty.PropertyInfo.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: CollectionPropertyConfiguration collectionProperty = property as CollectionPropertyConfiguration; IEdmTypeReference elementTypeReference = null; if (_types.ContainsKey(collectionProperty.ElementType)) { IEdmComplexType elementType = _types[collectionProperty.ElementType] as IEdmComplexType; elementTypeReference = new EdmComplexTypeReference(elementType, false); } else { elementTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType); } type.AddStructuralProperty( collectionProperty.PropertyInfo.Name, new EdmCollectionTypeReference( new EdmCollectionType(elementTypeReference), collectionProperty.OptionalProperty)); break; default: break; } } }
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config) { foreach (PropertyConfiguration property in config.Properties) { IEdmProperty edmProperty = null; switch (property.Kind) { case PropertyKind.Primitive: PrimitivePropertyConfiguration primitiveProperty = property as PrimitivePropertyConfiguration; if (primitiveProperty.IsIgnored) { continue; } EdmPrimitiveTypeKind typeKind = GetTypeKind(primitiveProperty.PropertyInfo.PropertyType); IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive( typeKind, primitiveProperty.OptionalProperty); // Set concurrency token if is entity type, and concurrency token is true EdmConcurrencyMode concurrencyMode = EdmConcurrencyMode.None; if (config.Kind == EdmTypeKind.Entity && primitiveProperty.ConcurrencyToken) { concurrencyMode = EdmConcurrencyMode.Fixed; } edmProperty = type.AddStructuralProperty( primitiveProperty.Name, primitiveTypeReference, defaultValue: null, concurrencyMode: concurrencyMode); break; case PropertyKind.Complex: ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration; IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType; edmProperty = type.AddStructuralProperty( complexProperty.Name, new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty)); break; case PropertyKind.Collection: edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property); break; case PropertyKind.Enum: edmProperty = CreateStructuralTypeEnumPropertyBody(type, config, (EnumPropertyConfiguration)property); break; default: break; } if (edmProperty != null) { if (property.PropertyInfo != null) { _properties[property.PropertyInfo] = edmProperty; } if (property.IsRestricted) { _propertiesRestrictions[edmProperty] = new QueryableRestrictions(property); } } } }