Example #1
0
        /// <summary>
        /// Creates a QueryProperty for a property that is a non entity Collection
        /// </summary>
        /// <param name="library">Library Query Type</param>
        /// <param name="result">resulting Query Structural Type</param>
        /// <param name="collectionProperty">Member to calculate</param>
        /// <param name="pathToProperty">Path to the Property</param>
        /// <returns>A Query Property of the collectionType</returns>
        protected override QueryProperty CreateNonentityCollectionMember(QueryTypeLibrary library, QueryStructuralType result, MemberProperty collectionProperty, PathToProperty pathToProperty)
        {
            ExceptionUtilities.CheckArgumentNotNull(collectionProperty, "collectionProperty");

            var collectionType = collectionProperty.PropertyType as CollectionDataType;

            ExceptionUtilities.Assert(collectionType != null, "This type of property is not supported.");
            var collectionPrimitiveElementType      = collectionType.ElementDataType as PrimitiveDataType;
            var collectionComplexElementType        = collectionType.ElementDataType as ComplexDataType;
            QueryCollectionType queryCollectionType = null;

            if (collectionPrimitiveElementType != null)
            {
                QueryScalarType queryPropertyType = this.GetQueryTypeForMappedProperty(pathToProperty, library, collectionPrimitiveElementType);
                queryCollectionType = queryPropertyType.CreateCollectionType();
            }
            else
            {
                ExceptionUtilities.Assert(collectionComplexElementType != null, "This type of property is not supported.");
                QueryComplexType queryComplexType = this.CreateStubComplexType(collectionComplexElementType.Definition);
                CreateMembers(library, queryComplexType, collectionComplexElementType.Definition.Properties, pathToProperty);
                queryCollectionType = queryComplexType.CreateCollectionType();
            }

            return(QueryProperty.Create(collectionProperty.Name, queryCollectionType));
        }
        /// <summary>
        /// Creates new members on a structural type
        /// </summary>
        /// <param name="library">Type library</param>
        /// <param name="result">Structural Type to add members to</param>
        /// <param name="properties">Properties of the Structural Type member</param>
        /// <param name="pathToProperty">Path to the Property</param>
        protected void CreateMembers(QueryTypeLibrary library, QueryStructuralType result, IEnumerable <MemberProperty> properties, PathToProperty pathToProperty)
        {
            // TODO: Some Taupo framework pieces skip over StreamDataType properties
            foreach (var prop in properties.Where(p => !(p.PropertyType is StreamDataType)))
            {
                QueryProperty queryProperty = null;
                pathToProperty.PathStackWithinEntityType.Add(prop.Name);

                var pdt = prop.PropertyType as PrimitiveDataType;
                var cdt = prop.PropertyType as ComplexDataType;
                if (pdt != null)
                {
                    QueryScalarType queryPropertyType = this.GetQueryTypeForMappedProperty(pathToProperty, library, pdt);
                    queryProperty = QueryProperty.Create(prop.Name, queryPropertyType);
                }
                else if (cdt != null)
                {
                    ComplexType      ct = cdt.Definition;
                    QueryComplexType queryComplexType = this.CreateStubComplexType(ct);
                    this.CreateMembers(library, queryComplexType, ct.Properties, pathToProperty);
                    library.SetQueryComplexType(ct, queryComplexType);
                    queryProperty = QueryProperty.Create(prop.Name, queryComplexType);
                }
                else
                {
                    queryProperty = this.CreateNonentityCollectionMember(library, result, prop, pathToProperty);
                }

                pathToProperty.PathStackWithinEntityType.RemoveAt(pathToProperty.PathStackWithinEntityType.Count - 1);

                queryProperty.SetPrimaryKey(prop.IsPrimaryKey);
                result.Add(queryProperty);
            }
        }
        private void CreateNavigationMembers(QueryTypeLibrary library, QueryEntityType result, IEnumerable <NavigationProperty> navigationProperties, EntityContainer container)
        {
            foreach (var navprop in navigationProperties)
            {
                // handle MEST scenario where there are multiple association sets corresponding to a navigation property
                var asets = container.AssociationSets.Where(c => c.AssociationType == navprop.Association);
                var aset  = asets.Single(set => set.Ends.Any(end => end.AssociationEnd == navprop.FromAssociationEnd && end.EntitySet == result.EntitySet));

                var toSet            = aset.Ends.Single(e => e.AssociationEnd == navprop.ToAssociationEnd).EntitySet;
                var targetEntityType = library.GetQueryEntityType(toSet, navprop.ToAssociationEnd.EntityType);

                QueryProperty property;

                if (navprop.ToAssociationEnd.Multiplicity == EndMultiplicity.Many)
                {
                    // collection property
                    property = QueryProperty.Create(navprop.Name, targetEntityType.CreateCollectionType());
                }
                else
                {
                    // reference property
                    property = QueryProperty.Create(navprop.Name, targetEntityType);
                }

                result.Add(property);
            }
        }
        private void CreateStubEntityTypes(QueryTypeLibrary library, EntityContainer container)
        {
            foreach (var entitySet in container.EntitySets)
            {
                var rootType        = entitySet.EntityType;
                var allDerivedTypes = container.Model.EntityTypes.Where(et => et.IsKindOf(rootType));

                foreach (var entityType in allDerivedTypes)
                {
                    QueryEntityType stub = this.CreateStubEntityType(entityType, entitySet);
                    library.SetQueryEntityType(entitySet, entityType, stub);
                }

                // set up Parent for each type
                foreach (var childType in allDerivedTypes.Where(et => et != rootType))
                {
                    library.GetQueryEntityType(entitySet, childType).Parent = library.GetQueryEntityType(entitySet, childType.BaseType);
                }
            }

            // TODO: maybe this is wrong if >1 containers! Add unit test and fix.
            // construct DerivedTypes for each type
            foreach (var type in library.GetQueryEntityTypes())
            {
                for (var parent = type.Parent; parent != null; parent = parent.Parent)
                {
                    parent.DerivedTypes.Add(type);
                }
            }
        }
Example #5
0
        private void AddDefaultConstants(List <QueryScalarValue> scalarValues, QueryTypeLibrary queryTypeLibrary)
        {
            var intType    = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Int32);
            var stringType = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.String());
            var boolType   = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Boolean);

            scalarValues.Add(intType.CreateValue(1));
            scalarValues.Add(stringType.CreateValue("Foo"));
            scalarValues.Add(boolType.CreateValue(true));
            scalarValues.Add(boolType.CreateValue(false));
        }
        /// <summary>
        /// Factory method that creates a new instance of the QueryRepository.
        /// </summary>
        /// <param name="entityModelSchema">Entity Model Schema</param>
        /// <param name="queryTypeLibrary">Query Type Library</param>
        /// <param name="entityContainerData">Entity Container Data</param>
        /// <returns>An instance of the QueryRepository class.</returns>
        public virtual QueryRepository CreateQueryRepository(EntityModelSchema entityModelSchema, QueryTypeLibrary queryTypeLibrary, EntityContainerData entityContainerData)
        {
            this.RootDataTypes = new Dictionary<string, QueryStructuralType>();

            this.BuildPrimitiveTypes(queryTypeLibrary);
            this.BuildRootQueriesAndTypes(entityModelSchema, queryTypeLibrary);

            var dataSet = this.DataSetBuilder.Build(this.RootDataTypes, entityContainerData);

            this.BuildConstants(queryTypeLibrary, dataSet);

            QueryRepository repository = new QueryRepository(queryTypeLibrary, this.RootQueries, this.Constants, this.PrimitiveTypes, this.RootDataTypes, dataSet);

            return repository;
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the QueryRepository class.
        /// </summary>
        /// <param name="typeLibrary">The query type library.</param>
        /// <param name="rootQueries">The collection of QueryExpressions used to initialize the RootQueries property</param>
        /// <param name="constants">The collection of QueryConstants used to initialize the Constants property</param>
        /// <param name="scalarTypes">The collection of scalar types.</param>
        /// <param name="rootDataTypes">The root data types.</param>
        /// <param name="dataSet">The data set used to initialize the DataSet property.</param>
        public QueryRepository(QueryTypeLibrary typeLibrary, IEnumerable<QueryExpression> rootQueries, IEnumerable<QueryConstantExpression> constants, IEnumerable<QueryScalarType> scalarTypes, IDictionary<string, QueryStructuralType> rootDataTypes, IQueryDataSet dataSet)
        {
            ExceptionUtilities.CheckArgumentNotNull(typeLibrary, "typeLibrary");
            ExceptionUtilities.CheckArgumentNotNull(rootQueries, "rootQueries");
            ExceptionUtilities.CheckArgumentNotNull(constants, "constants");
            ExceptionUtilities.CheckArgumentNotNull(scalarTypes, "scalarTypes");
            ExceptionUtilities.CheckArgumentNotNull(rootDataTypes, "rootDataTypes");
            ExceptionUtilities.CheckArgumentNotNull(dataSet, "dataSet");

            this.TypeLibrary = typeLibrary;
            this.RootQueries = rootQueries.ToList().AsReadOnly();
            this.Constants = constants.ToList().AsReadOnly();
            this.ScalarTypes = scalarTypes.ToList().AsReadOnly();
            this.RootDataTypes = new Dictionary<string, QueryStructuralType>(rootDataTypes);
            this.DataSet = dataSet;
        }
        private void BuildStructuralTypes(QueryTypeLibrary library, EntityContainer container)
        {
            foreach (var entitySet in container.EntitySets)
            {
                var rootType        = entitySet.EntityType;
                var allDerivedTypes = container.Model.EntityTypes.Where(et => et.IsKindOf(rootType));

                foreach (var entityType in allDerivedTypes)
                {
                    var queryEntityType = library.GetQueryEntityType(entitySet, entityType);

                    var pathToProperty = new PathToProperty(container, entitySet, entityType);
                    this.CreateMembers(library, queryEntityType, entityType.AllProperties, pathToProperty);
                    ExceptionUtilities.Assert(pathToProperty.PathStackWithinEntityType.Count == 0, "Path to property stack is not empty.");

                    this.CreateNavigationMembers(library, queryEntityType, entityType.AllNavigationProperties, container);
                }
            }
        }
        /// <summary>
        /// Builds the query type library with out Clr type mapping information, given the entity model
        /// </summary>
        /// <param name="model">The given entity model</param>
        /// <returns>The query type library</returns>
        public virtual QueryTypeLibrary BuildLibraryWithoutClrTypeMapping(EntityModelSchema model)
        {
            ExceptionUtilities.CheckArgumentNotNull(model, "model");

            QueryTypeLibrary lib = this.CreateInitialQueryTypeLibrary();

            foreach (var enumType in model.EnumTypes)
            {
                var stubEnumType = this.CreateStubEnumType(enumType);
                lib.SetQueryEnumType(enumType, stubEnumType);
            }

            foreach (var container in model.EntityContainers)
            {
                this.CreateStubEntityTypes(lib, container);
                this.BuildStructuralTypes(lib, container);
            }

            return(lib);
        }
Example #10
0
        /// <summary>
        /// Build the collection of primitive types which will be set on the constructed repository
        /// </summary>
        /// <param name="queryTypeLibrary">Query Type Library to build Types from</param>
        protected override void BuildPrimitiveTypes(QueryTypeLibrary queryTypeLibrary)
        {
            this.intType    = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Int32);
            this.stringType = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.String());

            this.PrimitiveTypes = new List <QueryScalarType>()
            {
                // this is just a sample
                this.intType,
                this.stringType,
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Boolean),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.DateTime()),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Decimal()),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Int64),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Int16),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Byte),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Single),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Double),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Binary()),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Guid),
            };
        }
Example #11
0
        /// <summary>
        /// Build Library Without Clr Type Mappings based on the EntityModelSchema given
        /// </summary>
        /// <param name="model">Entity Model Schema to create Query Model of</param>
        /// <returns>Query Type Library of Entity Model Schema</returns>
        public override QueryTypeLibrary BuildLibraryWithoutClrTypeMapping(EntityModelSchema model)
        {
            QueryTypeLibrary queryTypeLibrary = base.BuildLibraryWithoutClrTypeMapping(model);

            // add new properties for each of the named streams annotations to the query entity type to be used for verification
            foreach (var container in model.EntityContainers)
            {
                foreach (var entitySet in container.EntitySets)
                {
                    foreach (EntityType entityType in model.EntityTypes.Where(t => t.IsKindOf(entitySet.EntityType)))
                    {
                        QueryEntityType queryEntityType = queryTypeLibrary.GetQueryEntityType(entitySet, entityType);

                        // add the named streams properties
                        foreach (var streamProperty in queryEntityType.EntityType.Properties.Where(p => p.IsStream()))
                        {
                            queryEntityType.Add(new QueryProperty <AstoriaQueryStreamType>(streamProperty.Name, new AstoriaQueryStreamType(this.strategy)));
                            foreach (QueryEntityType qet in queryEntityType.DerivedTypes)
                            {
                                qet.Add(new QueryProperty <AstoriaQueryStreamType>(streamProperty.Name, new AstoriaQueryStreamType(this.strategy)));
                            }
                        }

                        // add the default stream property
                        if (entityType.HasStream())
                        {
                            var queryStreamType = new AstoriaQueryStreamType(this.strategy);
                            queryEntityType.Add(new QueryProperty <AstoriaQueryStreamType>(AstoriaQueryStreamType.DefaultStreamPropertyName, queryStreamType));
                            foreach (QueryEntityType qet in queryEntityType.DerivedTypes)
                            {
                                qet.Add(new QueryProperty <AstoriaQueryStreamType>(AstoriaQueryStreamType.DefaultStreamPropertyName, new AstoriaQueryStreamType(this.strategy)));
                            }
                        }
                    }
                }
            }

            return(queryTypeLibrary);
        }
        /// <summary>
        /// Build the collection of primitive types which will be set on the constructed repository
        /// </summary>
        /// <param name="queryTypeLibrary">Query Type Library to build Types from</param>
        protected override void BuildPrimitiveTypes(QueryTypeLibrary queryTypeLibrary)
        {
            this.intType = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Int32);
            this.stringType = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.String());

            this.PrimitiveTypes = new List<QueryScalarType>()
            {
                // this is just a sample
                this.intType,
                this.stringType,
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Boolean),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.DateTime()),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Decimal()),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Int64),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Int16),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Byte),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Single),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Double),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Binary()),
                (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Guid),
            };
        }
 /// <summary>
 /// Creates a QueryProperty for a property that is a non entity Collection 
 /// </summary>
 /// <param name="library">Library Query Type</param>
 /// <param name="result">resulting Query Structural Type</param>
 /// <param name="collectionProperty">Member to calculate</param>
 /// <param name="pathToProperty">Path to the Property</param>
 /// <returns>A Query Property of the collectionType</returns>
 protected virtual QueryProperty CreateNonentityCollectionMember(QueryTypeLibrary library, QueryStructuralType result, MemberProperty collectionProperty, PathToProperty pathToProperty)
 {
     throw new TaupoNotSupportedException("This type of property is not supported.");
 }
        private void CreateNavigationMembers(QueryTypeLibrary library, QueryEntityType result, IEnumerable<NavigationProperty> navigationProperties, EntityContainer container)
        {
            foreach (var navprop in navigationProperties)
            {
                // handle MEST scenario where there are multiple association sets corresponding to a navigation property
                var asets = container.AssociationSets.Where(c => c.AssociationType == navprop.Association);
                var aset = asets.Single(set => set.Ends.Any(end => end.AssociationEnd == navprop.FromAssociationEnd && end.EntitySet == result.EntitySet));

                var toSet = aset.Ends.Single(e => e.AssociationEnd == navprop.ToAssociationEnd).EntitySet;
                var targetEntityType = library.GetQueryEntityType(toSet, navprop.ToAssociationEnd.EntityType);

                QueryProperty property;

                if (navprop.ToAssociationEnd.Multiplicity == EndMultiplicity.Many)
                {
                    // collection property
                    property = QueryProperty.Create(navprop.Name, targetEntityType.CreateCollectionType());
                }
                else
                {
                    // reference property
                    property = QueryProperty.Create(navprop.Name, targetEntityType);
                }

                result.Add(property);
            }
        }
Example #15
0
        /// <summary>
        /// Builds root queries
        /// </summary>
        /// <param name="entityModelSchema">Entity Model Schema</param>
        /// <param name="queryTypeLibrary">Query Type Library</param>
        protected virtual void BuildRootQueriesAndTypes(EntityModelSchema entityModelSchema, QueryTypeLibrary queryTypeLibrary)
        {
            var rootQueriesList = new List <QueryExpression>();

            foreach (var container in entityModelSchema.EntityContainers)
            {
                foreach (var entitySet in container.EntitySets)
                {
                    var entityType = queryTypeLibrary.GetQueryEntityTypeForEntitySet(entitySet);
                    this.RootDataTypes.Add(entitySet.Name, entityType);
                    if (this.ShouldCreateRootQuery(entitySet))
                    {
                        QueryRootExpression rootExpression = new QueryRootExpression(entitySet.Name, entityType.CreateCollectionType());
                        rootQueriesList.Add(rootExpression);
                    }
                }
            }

            this.RootQueries = rootQueriesList;
        }
Example #16
0
 /// <summary>
 /// Build the collection of primitive types which will be set on the constructed repository
 /// </summary>
 /// <param name="queryTypeLibrary">Query Type Library</param>
 protected abstract void BuildPrimitiveTypes(QueryTypeLibrary queryTypeLibrary);
 /// <summary>
 /// Get the query type for a scalar property (with mapped store semantics, if applicable)
 /// </summary>
 /// <param name="pathToProperty">The path to find the property</param>
 /// <param name="library">The query type library</param>
 /// <param name="modelType">The model data type for the property</param>
 /// <returns>The query scalar type for the property</returns>
 /// <remarks>Default implementation just returns default type (meaning no special store semantics from mapping). Derived class should override as appropriate.</remarks>
 protected virtual QueryScalarType GetQueryTypeForMappedProperty(PathToProperty pathToProperty, QueryTypeLibrary library, PrimitiveDataType modelType)
 {
     return((QueryScalarType)library.GetDefaultQueryType(modelType));
 }
        /// <summary>
        /// Builds root queries
        /// </summary>
        /// <param name="entityModelSchema">Entity Model Schema</param>
        /// <param name="queryTypeLibrary">Query Type Library</param>
        protected virtual void BuildRootQueriesAndTypes(EntityModelSchema entityModelSchema, QueryTypeLibrary queryTypeLibrary)
        {
            var rootQueriesList = new List<QueryExpression>();

            foreach (var container in entityModelSchema.EntityContainers)
            {
                foreach (var entitySet in container.EntitySets)
                {
                    var entityType = queryTypeLibrary.GetQueryEntityTypeForEntitySet(entitySet);
                    this.RootDataTypes.Add(entitySet.Name, entityType);
                    if (this.ShouldCreateRootQuery(entitySet))
                    {
                        QueryRootExpression rootExpression = new QueryRootExpression(entitySet.Name, entityType.CreateCollectionType());
                        rootQueriesList.Add(rootExpression);
                    }
                }
            }

            this.RootQueries = rootQueriesList;
        }
Example #19
0
        /// <summary>
        /// Factory method that creates a new instance of the QueryRepository.
        /// </summary>
        /// <param name="entityModelSchema">Entity Model Schema</param>
        /// <param name="queryTypeLibrary">Query Type Library</param>
        /// <param name="entityContainerData">Entity Container Data</param>
        /// <returns>An instance of the QueryRepository class.</returns>
        public virtual QueryRepository CreateQueryRepository(EntityModelSchema entityModelSchema, QueryTypeLibrary queryTypeLibrary, EntityContainerData entityContainerData)
        {
            this.RootDataTypes = new Dictionary <string, QueryStructuralType>();

            this.BuildPrimitiveTypes(queryTypeLibrary);
            this.BuildRootQueriesAndTypes(entityModelSchema, queryTypeLibrary);

            var dataSet = this.DataSetBuilder.Build(this.RootDataTypes, entityContainerData);

            this.BuildConstants(queryTypeLibrary, dataSet);

            QueryRepository repository = new QueryRepository(queryTypeLibrary, this.RootQueries, this.Constants, this.PrimitiveTypes, this.RootDataTypes, dataSet);

            return(repository);
        }
Example #20
0
 /// <summary>
 /// Factory method that creates a new instance of the QueryRepository.
 /// </summary>
 /// <param name="entityModelSchema">Entity Model Schema</param>
 /// <param name="queryTypeLibrary">Query Type Library</param>
 /// <param name="entityContainerData">Entity Container Data</param>
 /// <returns>An instance of the QueryRepository class.</returns>
 public override QueryRepository CreateQueryRepository(EntityModelSchema entityModelSchema, QueryTypeLibrary queryTypeLibrary, EntityContainerData entityContainerData)
 {
     return(this.CreateRepositoryFunc(entityModelSchema, queryTypeLibrary, entityContainerData));
 }
Example #21
0
        /// <summary>
        /// Override the default method, adding generation of root queries that call designated service operations
        /// </summary>
        /// <param name="entityModelSchema">Entity Model Schema</param>
        /// <param name="queryTypeLibrary">Query Type Library</param>
        protected override void BuildRootQueriesAndTypes(EntityModelSchema entityModelSchema, QueryTypeLibrary queryTypeLibrary)
        {
            base.BuildRootQueriesAndTypes(entityModelSchema, queryTypeLibrary);

            // For each service operation marked as root, add a root query that calls the operation
            var rootServiceOperationQueries = new List <QueryExpression>();

            foreach (var serviceOperation in entityModelSchema.Functions.Where(f => f.Annotations.OfType <FunctionBodyAnnotation>().Any(a => a.IsRoot)))
            {
                QueryExpression bodyExpression = serviceOperation.Annotations.OfType <FunctionBodyAnnotation>().Single().FunctionBody;
                ExceptionUtilities.CheckObjectNotNull(bodyExpression, "Root level function has null body expression");

                QueryType rootQueryType = queryTypeLibrary.GetDefaultQueryType(serviceOperation.ReturnType);
                var       rootQuery     = new QueryCustomFunctionCallExpression(rootQueryType, serviceOperation, bodyExpression, true, false);
                rootServiceOperationQueries.Add(rootQuery);

                QueryStructuralType structuralType = rootQueryType as QueryStructuralType;
                if (structuralType == null)
                {
                    QueryCollectionType collectionType = rootQueryType as QueryCollectionType;
                    if (collectionType != null)
                    {
                        structuralType = collectionType.ElementType as QueryStructuralType;
                    }
                }

                ExceptionUtilities.CheckObjectNotNull(structuralType, "Root level service op query must return structural type");
                this.RootDataTypes.Add(serviceOperation.Name, structuralType);
            }

            this.RootQueries = this.RootQueries.Concat(rootServiceOperationQueries);
        }
 /// <summary>
 /// Initializes a new instance of the LinqTypeResolutionVisitor class.
 /// </summary>
 /// <param name="typeLibrary">The query type library.</param>
 public LinqTypeResolutionVisitor(QueryTypeLibrary typeLibrary)
     : base(typeLibrary)
 {
     this.parameterMappings = new Dictionary <LinqParameterExpression, LinqParameterExpression>();
 }
        private void CreateStubEntityTypes(QueryTypeLibrary library, EntityContainer container)
        {
            foreach (var entitySet in container.EntitySets)
            {
                var rootType = entitySet.EntityType;
                var allDerivedTypes = container.Model.EntityTypes.Where(et => et.IsKindOf(rootType));

                foreach (var entityType in allDerivedTypes)
                {
                    QueryEntityType stub = this.CreateStubEntityType(entityType, entitySet);
                    library.SetQueryEntityType(entitySet, entityType, stub);
                }

                // set up Parent for each type
                foreach (var childType in allDerivedTypes.Where(et => et != rootType))
                {
                    library.GetQueryEntityType(entitySet, childType).Parent = library.GetQueryEntityType(entitySet, childType.BaseType);
                }
            }

            // TODO: maybe this is wrong if >1 containers! Add unit test and fix.
            // construct DerivedTypes for each type
            foreach (var type in library.GetQueryEntityTypes())
            {
                for (var parent = type.Parent; parent != null; parent = parent.Parent)
                {
                    parent.DerivedTypes.Add(type);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the LinqToAstoriaQueryResolver class.
 /// </summary>
 /// <param name="identifierGenerator">Identifier generator.</param>
 /// <param name="typeLibrary">The query type library.</param>
 public LinqToAstoriaQueryResolver(IIdentifierGenerator identifierGenerator, QueryTypeLibrary typeLibrary)
 {
     this.parameterNameResolutionVisitor = new LinqToAstoriaParameterNameResolutionVisitor(identifierGenerator);
     this.typeResolver = new LinqToAstoriaTypeResolutionVisitor(typeLibrary);
     this.customFunctionResolutionVisitor = new LinqToAstoriaCustomFunctionResolutionVisitor(this.typeResolver);
 }
 /// <summary>
 /// Get the query type for a scalar property (with mapped store semantics, if applicable)
 /// </summary>
 /// <param name="pathToProperty">The path to find the property</param>
 /// <param name="library">The query type library</param>
 /// <param name="modelType">The model data type for the property</param>
 /// <returns>The query scalar type for the property</returns>
 /// <remarks>Default implementation just returns default type (meaning no special store semantics from mapping). Derived class should override as appropriate.</remarks>
 protected virtual QueryScalarType GetQueryTypeForMappedProperty(PathToProperty pathToProperty, QueryTypeLibrary library, PrimitiveDataType modelType)
 {
     return (QueryScalarType)library.GetDefaultQueryType(modelType);
 }
        /// <summary>
        /// Creates a QueryProperty for a property that is a non entity Collection 
        /// </summary>
        /// <param name="library">Library Query Type</param>
        /// <param name="result">resulting Query Structural Type</param>
        /// <param name="collectionProperty">Member to calculate</param>
        /// <param name="pathToProperty">Path to the Property</param>
        /// <returns>A Query Property of the collectionType</returns>
        protected override QueryProperty CreateNonentityCollectionMember(QueryTypeLibrary library, QueryStructuralType result, MemberProperty collectionProperty, PathToProperty pathToProperty)
        {
            ExceptionUtilities.CheckArgumentNotNull(collectionProperty, "collectionProperty");

            var collectionType = collectionProperty.PropertyType as CollectionDataType;
            ExceptionUtilities.Assert(collectionType != null, "This type of property is not supported.");
            var collectionPrimitiveElementType = collectionType.ElementDataType as PrimitiveDataType;
            var collectionComplexElementType = collectionType.ElementDataType as ComplexDataType;
            QueryCollectionType queryCollectionType = null;
            if (collectionPrimitiveElementType != null)
            {
                QueryScalarType queryPropertyType = this.GetQueryTypeForMappedProperty(pathToProperty, library, collectionPrimitiveElementType);
                queryCollectionType = queryPropertyType.CreateCollectionType();
            }
            else
            {
                ExceptionUtilities.Assert(collectionComplexElementType != null, "This type of property is not supported.");
                QueryComplexType queryComplexType = this.CreateStubComplexType(collectionComplexElementType.Definition);
                CreateMembers(library, queryComplexType, collectionComplexElementType.Definition.Properties, pathToProperty);
                queryCollectionType = queryComplexType.CreateCollectionType();
            }

            return QueryProperty.Create(collectionProperty.Name, queryCollectionType);
        }
        protected virtual void BuildConstants(QueryTypeLibrary queryTypeLibrary, IQueryDataSet dataSet)
        {
            ExceptionUtilities.CheckObjectNotNull(this.RootQueries, "Build root queries first before building constants");

            List<QueryConstantExpression> queryConstants = new List<QueryConstantExpression>();
            var rootQueries = this.RootQueries.OfType<QueryRootExpression>();

            int constantsOfSameTypeLimit = 5;
            foreach (var rootQuery in rootQueries)
            {
                var entityElements = dataSet[rootQuery.Name].Elements;

                if (entityElements.Count() == 0)
                {
                    continue;
                }

                var dataRow = this.Random.ChooseFrom(entityElements.Cast<QueryStructuralValue>());
                var scalarDataValues = dataRow.Type.Properties.Where(p => p.PropertyType is QueryScalarType).Select(st => dataRow.GetScalarValue(st.Name)).ToList();
                var complexDataValues = dataRow.Type.Properties.Where(p => p.PropertyType is QueryComplexType).Select(ct => dataRow.GetStructuralValue(ct.Name)).ToList();

                while (complexDataValues.Count() > 0)
                {
                    var complexDataValue = complexDataValues.First();

                    foreach (var propertyName in complexDataValue.MemberNames)
                    {
                        var complexPropertyValue = complexDataValue.GetValue(propertyName);
                        var nestedComplexValue = complexPropertyValue as QueryStructuralValue;
                        var nestedScalarValue = complexPropertyValue as QueryScalarValue;

                        if (nestedComplexValue != null)
                        {
                            complexDataValues.Add(nestedComplexValue);
                        }
                        else if (nestedScalarValue != null)
                        {
                            scalarDataValues.Add(nestedScalarValue);
                        }
                    }

                    complexDataValues.Remove(complexDataValue);
                }

                this.AddDefaultConstants(scalarDataValues, queryTypeLibrary);

                foreach (var scalarTypeValue in scalarDataValues)
                {
                    if (queryConstants.Count(qce => qce.ExpressionType.IsSameQueryScalarType(scalarTypeValue.Type as QueryScalarType)) <= constantsOfSameTypeLimit)
                    {
                        queryConstants.Add(CommonQueryBuilder.Constant(scalarTypeValue));
                    }
                }
            }

            var nonMappedEnumTypes = queryTypeLibrary.GetNonMappedEnumTypes();

            foreach (var nonMappedEnum in nonMappedEnumTypes)
            {
                var enumMember = this.Random.ChooseFrom(nonMappedEnum.EnumType.Members);
                var enumObjectToAdd = Enum.Parse(nonMappedEnum.ClrType, enumMember.Name, false);

                queryConstants.Add(CommonQueryBuilder.Constant(enumObjectToAdd));
            }

            this.Constants = queryConstants.AsEnumerable();
        }
        /// <summary>
        /// Creates new members on a structural type
        /// </summary>
        /// <param name="library">Type library</param>
        /// <param name="result">Structural Type to add members to</param>
        /// <param name="properties">Properties of the Structural Type member</param>
        /// <param name="pathToProperty">Path to the Property</param>
        protected void CreateMembers(QueryTypeLibrary library, QueryStructuralType result, IEnumerable<MemberProperty> properties, PathToProperty pathToProperty)
        {
            // TODO: Some Taupo framework pieces skip over StreamDataType properties
            foreach (var prop in properties.Where(p => !(p.PropertyType is StreamDataType)))
            {
                QueryProperty queryProperty = null;
                pathToProperty.PathStackWithinEntityType.Add(prop.Name);

                var pdt = prop.PropertyType as PrimitiveDataType;
                var cdt = prop.PropertyType as ComplexDataType;
                if (pdt != null)
                {
                    QueryScalarType queryPropertyType = this.GetQueryTypeForMappedProperty(pathToProperty, library, pdt);
                    queryProperty = QueryProperty.Create(prop.Name, queryPropertyType);
                }
                else if (cdt != null)
                {
                    ComplexType ct = cdt.Definition;
                    QueryComplexType queryComplexType = this.CreateStubComplexType(ct);
                    this.CreateMembers(library, queryComplexType, ct.Properties, pathToProperty);
                    library.SetQueryComplexType(ct, queryComplexType);
                    queryProperty = QueryProperty.Create(prop.Name, queryComplexType);
                }
                else
                {
                    queryProperty = this.CreateNonentityCollectionMember(library, result, prop, pathToProperty);
                }

                pathToProperty.PathStackWithinEntityType.RemoveAt(pathToProperty.PathStackWithinEntityType.Count - 1);

                queryProperty.SetPrimaryKey(prop.IsPrimaryKey);
                result.Add(queryProperty);
            }
        }
        private void AddDefaultConstants(List<QueryScalarValue> scalarValues, QueryTypeLibrary queryTypeLibrary)
        {
            var intType = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Int32);
            var stringType = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.String());
            var boolType = (QueryScalarType)queryTypeLibrary.GetDefaultQueryType(EdmDataTypes.Boolean);

            scalarValues.Add(intType.CreateValue(1));
            scalarValues.Add(stringType.CreateValue("Foo"));
            scalarValues.Add(boolType.CreateValue(true));
            scalarValues.Add(boolType.CreateValue(false));
        }
 /// <summary>
 /// Factory method that creates a new instance of the QueryRepository.
 /// </summary>
 /// <param name="entityModelSchema">Entity Model Schema</param>
 /// <param name="queryTypeLibrary">Query Type Library</param>
 /// <param name="entityContainerData">Entity Container Data</param>
 /// <returns>An instance of the QueryRepository class.</returns>
 public override QueryRepository CreateQueryRepository(EntityModelSchema entityModelSchema, QueryTypeLibrary queryTypeLibrary, EntityContainerData entityContainerData)
 {
     return this.CreateRepositoryFunc(entityModelSchema, queryTypeLibrary, entityContainerData);
 }
 /// <summary>
 /// Build the collection of primitive types which will be set on the constructed repository
 /// </summary>
 /// <param name="queryTypeLibrary">Query Type Library</param>
 protected abstract void BuildPrimitiveTypes(QueryTypeLibrary queryTypeLibrary);
 /// <summary>
 /// Initializes a new instance of the CommonExpressionTypeResolutionVisitor class.
 /// </summary>
 /// <param name="typeLibrary">The query type library.</param>
 protected CommonExpressionTypeResolutionVisitor(QueryTypeLibrary typeLibrary)
 {
     ExceptionUtilities.CheckArgumentNotNull(typeLibrary, "typeLibrary");
     this.TypeLibrary = typeLibrary;
     this.CustomFunctionsCallStack = new Stack <Function>();
 }
Example #33
0
        protected virtual void BuildConstants(QueryTypeLibrary queryTypeLibrary, IQueryDataSet dataSet)
        {
            ExceptionUtilities.CheckObjectNotNull(this.RootQueries, "Build root queries first before building constants");

            List <QueryConstantExpression> queryConstants = new List <QueryConstantExpression>();
            var rootQueries = this.RootQueries.OfType <QueryRootExpression>();

            int constantsOfSameTypeLimit = 5;

            foreach (var rootQuery in rootQueries)
            {
                var entityElements = dataSet[rootQuery.Name].Elements;

                if (entityElements.Count() == 0)
                {
                    continue;
                }

                var dataRow           = this.Random.ChooseFrom(entityElements.Cast <QueryStructuralValue>());
                var scalarDataValues  = dataRow.Type.Properties.Where(p => p.PropertyType is QueryScalarType).Select(st => dataRow.GetScalarValue(st.Name)).ToList();
                var complexDataValues = dataRow.Type.Properties.Where(p => p.PropertyType is QueryComplexType).Select(ct => dataRow.GetStructuralValue(ct.Name)).ToList();

                while (complexDataValues.Count() > 0)
                {
                    var complexDataValue = complexDataValues.First();

                    foreach (var propertyName in complexDataValue.MemberNames)
                    {
                        var complexPropertyValue = complexDataValue.GetValue(propertyName);
                        var nestedComplexValue   = complexPropertyValue as QueryStructuralValue;
                        var nestedScalarValue    = complexPropertyValue as QueryScalarValue;

                        if (nestedComplexValue != null)
                        {
                            complexDataValues.Add(nestedComplexValue);
                        }
                        else if (nestedScalarValue != null)
                        {
                            scalarDataValues.Add(nestedScalarValue);
                        }
                    }

                    complexDataValues.Remove(complexDataValue);
                }

                this.AddDefaultConstants(scalarDataValues, queryTypeLibrary);

                foreach (var scalarTypeValue in scalarDataValues)
                {
                    if (queryConstants.Count(qce => qce.ExpressionType.IsSameQueryScalarType(scalarTypeValue.Type as QueryScalarType)) <= constantsOfSameTypeLimit)
                    {
                        queryConstants.Add(CommonQueryBuilder.Constant(scalarTypeValue));
                    }
                }
            }

            var nonMappedEnumTypes = queryTypeLibrary.GetNonMappedEnumTypes();

            foreach (var nonMappedEnum in nonMappedEnumTypes)
            {
                var enumMember      = this.Random.ChooseFrom(nonMappedEnum.EnumType.Members);
                var enumObjectToAdd = Enum.Parse(nonMappedEnum.ClrType, enumMember.Name, false);

                queryConstants.Add(CommonQueryBuilder.Constant(enumObjectToAdd));
            }

            this.Constants = queryConstants.AsEnumerable();
        }
Example #34
0
 /// <summary>
 /// Initializes a new instance of the LinqToAstoriaTypeResolutionVisitor class.
 /// </summary>
 /// <param name="typeLibrary">The query type library.</param>
 public LinqToAstoriaTypeResolutionVisitor(QueryTypeLibrary typeLibrary)
     : base(typeLibrary)
 {
 }
        /// <summary>
        /// Override the default method, adding generation of root queries that call designated service operations
        /// </summary>
        /// <param name="entityModelSchema">Entity Model Schema</param>
        /// <param name="queryTypeLibrary">Query Type Library</param>
        protected override void BuildRootQueriesAndTypes(EntityModelSchema entityModelSchema, QueryTypeLibrary queryTypeLibrary)
        {
            base.BuildRootQueriesAndTypes(entityModelSchema, queryTypeLibrary);

            // For each service operation marked as root, add a root query that calls the operation
            var rootServiceOperationQueries = new List<QueryExpression>();
            foreach (var serviceOperation in entityModelSchema.Functions.Where(f => f.Annotations.OfType<FunctionBodyAnnotation>().Any(a => a.IsRoot)))
            {
                QueryExpression bodyExpression = serviceOperation.Annotations.OfType<FunctionBodyAnnotation>().Single().FunctionBody;
                ExceptionUtilities.CheckObjectNotNull(bodyExpression, "Root level function has null body expression");

                QueryType rootQueryType = queryTypeLibrary.GetDefaultQueryType(serviceOperation.ReturnType);
                var rootQuery = new QueryCustomFunctionCallExpression(rootQueryType, serviceOperation, bodyExpression, true, false);
                rootServiceOperationQueries.Add(rootQuery);

                QueryStructuralType structuralType = rootQueryType as QueryStructuralType;
                if (structuralType == null)
                {
                    QueryCollectionType collectionType = rootQueryType as QueryCollectionType;
                    if (collectionType != null)
                    {
                        structuralType = collectionType.ElementType as QueryStructuralType;
                    }
                }

                ExceptionUtilities.CheckObjectNotNull(structuralType, "Root level service op query must return structural type");
                this.RootDataTypes.Add(serviceOperation.Name, structuralType);
            }

            this.RootQueries = this.RootQueries.Concat(rootServiceOperationQueries);
        }
        private void BuildStructuralTypes(QueryTypeLibrary library, EntityContainer container)
        {
            foreach (var entitySet in container.EntitySets)
            {
                var rootType = entitySet.EntityType;
                var allDerivedTypes = container.Model.EntityTypes.Where(et => et.IsKindOf(rootType));

                foreach (var entityType in allDerivedTypes)
                {
                    var queryEntityType = library.GetQueryEntityType(entitySet, entityType);

                    var pathToProperty = new PathToProperty(container, entitySet, entityType);
                    this.CreateMembers(library, queryEntityType, entityType.AllProperties, pathToProperty);
                    ExceptionUtilities.Assert(pathToProperty.PathStackWithinEntityType.Count == 0, "Path to property stack is not empty.");

                    this.CreateNavigationMembers(library, queryEntityType, entityType.AllNavigationProperties, container);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the LinqToAstoriaQueryResolver class.
 /// </summary>
 /// <param name="identifierGenerator">Identifier generator.</param>
 /// <param name="typeLibrary">The query type library.</param>
 public LinqToAstoriaQueryResolver(IIdentifierGenerator identifierGenerator, QueryTypeLibrary typeLibrary)
 {
     this.parameterNameResolutionVisitor = new LinqToAstoriaParameterNameResolutionVisitor(identifierGenerator);
     this.typeResolver = new LinqToAstoriaTypeResolutionVisitor(typeLibrary);
     this.customFunctionResolutionVisitor = new LinqToAstoriaCustomFunctionResolutionVisitor(this.typeResolver);
 }
 /// <summary>
 /// Creates a QueryProperty for a property that is a non entity Collection
 /// </summary>
 /// <param name="library">Library Query Type</param>
 /// <param name="result">resulting Query Structural Type</param>
 /// <param name="collectionProperty">Member to calculate</param>
 /// <param name="pathToProperty">Path to the Property</param>
 /// <returns>A Query Property of the collectionType</returns>
 protected virtual QueryProperty CreateNonentityCollectionMember(QueryTypeLibrary library, QueryStructuralType result, MemberProperty collectionProperty, PathToProperty pathToProperty)
 {
     throw new TaupoNotSupportedException("This type of property is not supported.");
 }