Example #1
0
        private QueryRewriter GenerateViewsForExtentAndType(
            EdmType generatedType, ViewgenContext context, CqlIdentifiers identifiers, ViewSet views, ViewGenMode mode)
        {
            Debug.Assert(mode != ViewGenMode.GenerateAllViews, "By definition this method can not handle generating views for all extents");

            var queryRewriter = new QueryRewriter(generatedType, context, mode);

            queryRewriter.GenerateViewComponents();

            // Get the basic view
            var basicView = queryRewriter.BasicView;

            if (m_config.IsNormalTracing)
            {
                Helpers.StringTrace("Basic View: ");
                Helpers.StringTraceLine(basicView.ToString());
            }

            var simplifiedView = GenerateSimplifiedView(basicView, queryRewriter.UsedCells);

            if (m_config.IsNormalTracing)
            {
                Helpers.StringTraceLine(String.Empty);
                Helpers.StringTrace("Simplified View: ");
                Helpers.StringTraceLine(simplifiedView.ToString());
            }

            var cqlGen = new CqlGenerator(
                simplifiedView,
                queryRewriter.CaseStatements,
                identifiers,
                context.MemberMaps.ProjectedSlotMap,
                queryRewriter.UsedCells.Count,
                queryRewriter.TopLevelWhereClause,
                m_entityContainerMapping.StorageMappingItemCollection);

            string             eSQLView;
            DbQueryCommandTree commandTree;

            if (m_config.GenerateEsql)
            {
                eSQLView    = cqlGen.GenerateEsql();
                commandTree = null;
            }
            else
            {
                eSQLView    = null;
                commandTree = cqlGen.GenerateCqt();
            }

            var generatedView = GeneratedView.CreateGeneratedView(
                context.Extent, generatedType, commandTree, eSQLView, m_entityContainerMapping.StorageMappingItemCollection, m_config);

            views.Add(context.Extent, generatedView);

            return(queryRewriter);
        }
Example #2
0
        internal DbQueryCommandTree GetCommandTree()
        {
            Exception parserException;

            if (this.m_commandTree == null && !GeneratedView.TryParseView(this.m_eSQL, false, this.m_extent, this.m_mappingItemCollection, this.m_config, out this.m_commandTree, out this.m_discriminatorMap, out parserException))
            {
                throw new MappingException(Strings.Mapping_Invalid_QueryView((object)this.m_extent.Name, (object)parserException.Message));
            }
            return(this.m_commandTree);
        }
Example #3
0
        internal static bool TryParseUserSpecifiedView(
            EntitySetBaseMapping setMapping,
            EntityTypeBase type,
            string eSQL,
            bool includeSubtypes,
            StorageMappingItemCollection mappingItemCollection,
            ConfigViewGenerator config,
            IList <EdmSchemaError> errors,
            out GeneratedView generatedView)
        {
            bool flag = false;
            DbQueryCommandTree commandTree;
            DiscriminatorMap   discriminatorMap;
            Exception          parserException;

            if (!GeneratedView.TryParseView(eSQL, true, setMapping.Set, mappingItemCollection, config, out commandTree, out discriminatorMap, out parserException))
            {
                EdmSchemaError edmSchemaError = new EdmSchemaError(Strings.Mapping_Invalid_QueryView2((object)setMapping.Set.Name, (object)parserException.Message), 2068, EdmSchemaErrorSeverity.Error, setMapping.EntityContainerMapping.SourceLocation, setMapping.StartLineNumber, setMapping.StartLinePosition, parserException);
                errors.Add(edmSchemaError);
                flag = true;
            }
            else
            {
                foreach (EdmSchemaError edmSchemaError in ViewValidator.ValidateQueryView(commandTree, setMapping, type, includeSubtypes))
                {
                    errors.Add(edmSchemaError);
                    flag = true;
                }
                CollectionType edmType = commandTree.Query.ResultType.EdmType as CollectionType;
                if (edmType == null || !setMapping.Set.ElementType.IsAssignableFrom(edmType.TypeUsage.EdmType))
                {
                    EdmSchemaError edmSchemaError = new EdmSchemaError(Strings.Mapping_Invalid_QueryView_Type((object)setMapping.Set.Name), 2069, EdmSchemaErrorSeverity.Error, setMapping.EntityContainerMapping.SourceLocation, setMapping.StartLineNumber, setMapping.StartLinePosition);
                    errors.Add(edmSchemaError);
                    flag = true;
                }
            }
            if (!flag)
            {
                generatedView = new GeneratedView(setMapping.Set, (EdmType)type, commandTree, eSQL, discriminatorMap, mappingItemCollection, config);
                return(true);
            }
            generatedView = (GeneratedView)null;
            return(false);
        }
Example #4
0
        private QueryRewriter GenerateViewsForExtentAndType(
            EdmType generatedType,
            ViewgenContext context,
            CqlIdentifiers identifiers,
            KeyToListMap <EntitySetBase, GeneratedView> views,
            ViewGenMode mode)
        {
            QueryRewriter queryRewriter = new QueryRewriter(generatedType, context, mode);

            queryRewriter.GenerateViewComponents();
            CellTreeNode basicView = queryRewriter.BasicView;

            if (this.m_config.IsNormalTracing)
            {
                Helpers.StringTrace("Basic View: ");
                Helpers.StringTraceLine(basicView.ToString());
            }
            CellTreeNode simplifiedView = ViewGenerator.GenerateSimplifiedView(basicView, queryRewriter.UsedCells);

            if (this.m_config.IsNormalTracing)
            {
                Helpers.StringTraceLine(string.Empty);
                Helpers.StringTrace("Simplified View: ");
                Helpers.StringTraceLine(simplifiedView.ToString());
            }
            CqlGenerator       cqlGenerator = new CqlGenerator(simplifiedView, queryRewriter.CaseStatements, identifiers, context.MemberMaps.ProjectedSlotMap, queryRewriter.UsedCells.Count, queryRewriter.TopLevelWhereClause, this.m_entityContainerMapping.StorageMappingItemCollection);
            string             eSQL;
            DbQueryCommandTree commandTree;

            if (this.m_config.GenerateEsql)
            {
                eSQL        = cqlGenerator.GenerateEsql();
                commandTree = (DbQueryCommandTree)null;
            }
            else
            {
                eSQL        = (string)null;
                commandTree = cqlGenerator.GenerateCqt();
            }
            GeneratedView generatedView = GeneratedView.CreateGeneratedView(context.Extent, generatedType, commandTree, eSQL, this.m_entityContainerMapping.StorageMappingItemCollection, this.m_config);

            views.Add(context.Extent, generatedView);
            return(queryRewriter);
        }
Example #5
0
        // <summary>
        // Creates generated view object for the combination of the <paramref name="setMapping" />.Set and the
        // <paramref
        //     name="type" />
        // .
        // This constructor is used for user-defined query views only.
        // </summary>
        internal static bool TryParseUserSpecifiedView(
            EntitySetBaseMapping setMapping,
            EntityTypeBase type,
            string eSQL,
            bool includeSubtypes,
            StorageMappingItemCollection mappingItemCollection,
            ConfigViewGenerator config,
            /*out*/ IList <EdmSchemaError> errors,
            out GeneratedView generatedView)
        {
            var failed = false;

            DbQueryCommandTree commandTree;
            DiscriminatorMap   discriminatorMap;
            Exception          parserException;

            if (
                !TryParseView(
                    eSQL, true, setMapping.Set, mappingItemCollection, config, out commandTree, out discriminatorMap, out parserException))
            {
                var error = new EdmSchemaError(
                    Strings.Mapping_Invalid_QueryView2(setMapping.Set.Name, parserException.Message),
                    (int)MappingErrorCode.InvalidQueryView, EdmSchemaErrorSeverity.Error,
                    setMapping.EntityContainerMapping.SourceLocation, setMapping.StartLineNumber, setMapping.StartLinePosition,
                    parserException);
                errors.Add(error);
                failed = true;
            }
            else
            {
                Debug.Assert(commandTree != null, "commandTree not set after parsing the view");

                // Verify that all expressions appearing in the view are supported.
                foreach (var error in ViewValidator.ValidateQueryView(commandTree, setMapping, type, includeSubtypes))
                {
                    errors.Add(error);
                    failed = true;
                }

                // Verify that the result type of the query view is assignable to the element type of the entityset
                var queryResultType = (commandTree.Query.ResultType.EdmType) as CollectionType;
                if ((queryResultType == null) ||
                    (!setMapping.Set.ElementType.IsAssignableFrom(queryResultType.TypeUsage.EdmType)))
                {
                    var error = new EdmSchemaError(
                        Strings.Mapping_Invalid_QueryView_Type(setMapping.Set.Name),
                        (int)MappingErrorCode.InvalidQueryViewResultType, EdmSchemaErrorSeverity.Error,
                        setMapping.EntityContainerMapping.SourceLocation, setMapping.StartLineNumber, setMapping.StartLinePosition);
                    errors.Add(error);
                    failed = true;
                }
            }

            if (!failed)
            {
                generatedView = new GeneratedView(setMapping.Set, type, commandTree, eSQL, discriminatorMap, mappingItemCollection, config);
                return(true);
            }
            else
            {
                generatedView = null;
                return(false);
            }
        }
 // <summary>
 // Tries to generate the Oftype or OfTypeOnly query view for a given entity set and type.
 // Returns false if the view could not be generated.
 // Possible reasons for failing are
 // 1) Passing in OfTypeOnly on an abstract type
 // 2) In user-specified query views mode a query for the given type is absent
 // </summary>
 internal bool TryGetGeneratedViewOfType(
     EntitySetBase entity, EntityTypeBase type, bool includeSubtypes, out GeneratedView generatedView)
 {
     var key = new OfTypeQVCacheKey(entity, new Pair<EntityTypeBase, bool>(type, includeSubtypes));
     generatedView = _generatedViewOfTypeMemoizer.Evaluate(key);
     return (generatedView != null);
 }
            // <summary>
            // Generates a single query view for a given Extent and type. It is used to generate OfType and OfTypeOnly views.
            // </summary>
            // <param name="includeSubtypes"> Whether the view should include extents that are subtypes of the given entity </param>
            private bool TryGenerateQueryViewOfType(
                EntityContainer entityContainer, EntitySetBase entity, EntityTypeBase type, bool includeSubtypes,
                out GeneratedView generatedView)
            {
                DebugCheck.NotNull(entityContainer);
                DebugCheck.NotNull(entity);
                DebugCheck.NotNull(type);

                if (type.Abstract)
                {
                    generatedView = null;
                    return false;
                }

                //Get the mapping that has the entity container mapped.
                var entityContainerMap = MappingMetadataHelper.GetEntityContainerMap(_storageMappingItemCollection, entityContainer);
                Debug.Assert(!entityContainerMap.IsEmpty, "There are no entity set maps");

                bool success;
                var viewGenResults = ViewgenGatekeeper.GenerateTypeSpecificQueryView(
                    entityContainerMap, _config, entity, type, includeSubtypes, out success);
                if (!success)
                {
                    generatedView = null;
                    return false; //could not generate view
                }

                var extentMappingViews = viewGenResults.Views;

                if (viewGenResults.HasErrors)
                {
                    throw new MappingException(Helper.CombineErrorMessage(viewGenResults.Errors));
                }

                Debug.Assert(extentMappingViews.AllValues.Count() == 1, "Viewgen should have produced only one view");
                generatedView = extentMappingViews.AllValues.First();

                return true;
            }
 // <summary>
 // this method will be called in metadatworkspace, the signature is the same as the one in ViewDictionary
 // </summary>
 internal bool TryGetGeneratedViewOfType(
     EntitySetBase entity, EntityTypeBase type, bool includeSubtypes, out GeneratedView generatedView)
 {
     return m_viewDictionary.TryGetGeneratedViewOfType(entity, type, includeSubtypes, out generatedView);
 }
        /// <summary>
        ///     Creates generated view object for the combination of the <paramref name="setMapping" />.Set and the
        ///     <paramref
        ///         name="type" />
        ///     .
        ///     This constructor is used for user-defined query views only.
        /// </summary>
        internal static bool TryParseUserSpecifiedView(
            StorageSetMapping setMapping,
            EntityTypeBase type,
            string eSQL,
            bool includeSubtypes,
            StorageMappingItemCollection mappingItemCollection,
            ConfigViewGenerator config,
            /*out*/ IList<EdmSchemaError> errors,
            out GeneratedView generatedView)
        {
            var failed = false;

            DbQueryCommandTree commandTree;
            DiscriminatorMap discriminatorMap;
            Exception parserException;
            if (
                !TryParseView(
                    eSQL, true, setMapping.Set, mappingItemCollection, config, out commandTree, out discriminatorMap, out parserException))
            {
                var error = new EdmSchemaError(
                    Strings.Mapping_Invalid_QueryView2(setMapping.Set.Name, parserException.Message),
                    (int)StorageMappingErrorCode.InvalidQueryView, EdmSchemaErrorSeverity.Error,
                    setMapping.EntityContainerMapping.SourceLocation, setMapping.StartLineNumber, setMapping.StartLinePosition,
                    parserException);
                errors.Add(error);
                failed = true;
            }
            else
            {
                Debug.Assert(commandTree != null, "commandTree not set after parsing the view");

                // Verify that all expressions appearing in the view are supported.
                foreach (var error in ViewValidator.ValidateQueryView(commandTree, setMapping, type, includeSubtypes))
                {
                    errors.Add(error);
                    failed = true;
                }

                // Verify that the result type of the query view is assignable to the element type of the entityset
                var queryResultType = (commandTree.Query.ResultType.EdmType) as CollectionType;
                if ((queryResultType == null)
                    || (!setMapping.Set.ElementType.IsAssignableFrom(queryResultType.TypeUsage.EdmType)))
                {
                    var error = new EdmSchemaError(
                        Strings.Mapping_Invalid_QueryView_Type(setMapping.Set.Name),
                        (int)StorageMappingErrorCode.InvalidQueryViewResultType, EdmSchemaErrorSeverity.Error,
                        setMapping.EntityContainerMapping.SourceLocation, setMapping.StartLineNumber, setMapping.StartLinePosition);
                    errors.Add(error);
                    failed = true;
                }
            }

            if (!failed)
            {
                generatedView = new GeneratedView(setMapping.Set, type, commandTree, eSQL, discriminatorMap, mappingItemCollection, config);
                return true;
            }
            else
            {
                generatedView = null;
                return false;
            }
        }