Beispiel #1
0
        private static EdmType CreateResultType(DbModel model, ResultDescriptor result)
        {
            EdmType edmType = GetSimpleEdmType(model, result.Type);

            if (edmType == null)
            {
                edmType = GetStructuralEdmType(model, result.Type);
            }
            if (edmType == null)
            {
                throw new InvalidOperationException(string.Format("Edm type is not found for result type {0}.", result.Type.FullName));
            }

            switch (edmType.BuiltInTypeKind)
            {
            case BuiltInTypeKind.EntityType:
                var propertyMappings = ((EntityType)edmType).Properties.Join(
                    model.ConceptualToStoreMapping.EntitySetMappings
                    .SelectMany(t => t.EntityTypeMappings)
                    .Where(t => edmType.Yield(e => e.BaseType, (e, b) => b, e => e != null).Contains(t.EntityType))
                    .SelectMany(tm => tm.Fragments.SelectMany(t => t.PropertyMappings))
                    .OfType <ScalarPropertyMapping>(),
                    p => p,
                    pm => pm.Property,
                    (p, pm) => new
                {
                    Property  = p,
                    TypeUsage = TypeUsage.Create(pm.Column.TypeUsage.EdmType, pm.Column.TypeUsage.Facets.Where(f => !new[] { "StoreGeneratedPattern", "ConcurrencyMode" }.Contains(f.Name)))
                });
                return(RowType.Create(propertyMappings.Select(pm => EdmProperty.Create(pm.Property.Name, pm.TypeUsage)), null));

            case BuiltInTypeKind.ComplexType:
                return(RowType.Create(((StructuralType)edmType).Members.Select(m =>
                {
                    string columnName = null;
                    MetadataProperty metadata;
                    //if (!m.MetadataProperties.TryGetValue("Configuration", true, out metadata) || !metadata.Value.TryGetProperty("ColumnName", out columnName))
                    if (m.MetadataProperties.TryGetValue("ClrAttributes", true, out metadata))
                    {
                        var columnAttr = ((IEnumerable)m.MetadataProperties["ClrAttributes"].Value).OfType <ColumnAttribute>().SingleOrDefault();
                        if (columnAttr != null && !string.IsNullOrEmpty(columnAttr.Name))
                        {
                            columnName = columnAttr.Name;
                        }
                    }
                    return EdmProperty.Create(columnName ?? m.Name, model.ProviderManifest.GetStoreType(m.TypeUsage));
                }), null));

            case BuiltInTypeKind.EnumType:
                return(RowType.Create(new[] { EdmProperty.CreateEnum(result.ColumnName, (EnumType)edmType) }, null));

            case BuiltInTypeKind.PrimitiveType:
                return(RowType.Create(new[] { EdmProperty.CreatePrimitive(result.ColumnName, (PrimitiveType)edmType) }, null));

            default:
                throw new NotSupportedException();
            }
        }
Beispiel #2
0
        private static EdmFunction CreateFunctionImport(DbModel model, FunctionDescriptor descriptor)
        {
            var parameters = descriptor.Parameters.Select(p =>
            {
                TypeUsage typeUsage = GetTypeUsage(GetSimpleEdmType(model, p.Type), p);
                return(FunctionParameter.Create(p.Name, typeUsage.EdmType,
                                                p.Direction == ParameterDirection.Output ? ParameterMode.Out :
                                                p.Direction == ParameterDirection.InputOutput ? ParameterMode.InOut :
                                                p.Direction == ParameterDirection.ReturnValue ? ParameterMode.ReturnValue :
                                                ParameterMode.In));
            }).ToArray();

            var entitySets = new EntitySet[descriptor.Results.Count];

            var results = descriptor.Results.Select((r, i) =>
            {
                EdmType edmType = GetStructuralEdmType(model, r.Type);
                if (edmType == null)
                {
                    edmType = GetTypeUsage(GetSimpleEdmType(model, r.Type), r).EdmType;
                }
                entitySets[i] = edmType.BuiltInTypeKind != BuiltInTypeKind.EntityType ? null : model.ConceptualModel.Container.EntitySets.FirstOrDefault(s => edmType.Yield(t => t.BaseType, (t, b) => b, t => t != null).Contains(s.ElementType));
                return(FunctionParameter.Create(string.Format("Result_{0}", i), edmType.GetCollectionType(), ParameterMode.ReturnValue));
            }).ToArray();

            var payload = new EdmFunctionPayload
            {
                StoreFunctionName      = descriptor.FunctionName,
                IsFunctionImport       = true,
                IsAggregate            = descriptor.IsAggregate,
                IsBuiltIn              = descriptor.IsBuiltIn,
                IsComposable           = descriptor.IsComposable,
                IsNiladic              = descriptor.IsNiladic,
                ParameterTypeSemantics = descriptor.ParameterTypeSemantics,
                Schema           = descriptor.DatabaseSchema,
                Parameters       = parameters,
                ReturnParameters = results,
                EntitySets       = entitySets,
            };

            return(EdmFunction.Create(descriptor.FunctionName, model.ConceptualModel.Container.Name, DataSpace.CSpace, payload, null));
        }