Example #1
0
		private static ResultAlgebraNode Convert(CommonTableBinding currentCommonTableBinding, QueryNode queryNode)
		{
			Algebrizer algebrizer = new Algebrizer(currentCommonTableBinding);
			AlgebraNode result = algebrizer.ConvertAstNode(queryNode);
			result = new SubqueryExpander().VisitAlgebraNode(result);
			return (ResultAlgebraNode)result;
		}
Example #2
0
        public CommonTableBinding DeclareCommonTableExpression(Identifier identifier, QueryNode anchorMember)
        {
            CommonTableBinding derivedTableBinding = new CommonTableBinding(identifier.Text, anchorMember);

            _commonTables.Add(derivedTableBinding);
            return(derivedTableBinding);
        }
Example #3
0
 public virtual QueryNode VisitQuery(QueryNode query)
 {
     return((QueryNode)Visit(query));
 }
Example #4
0
		public virtual QueryNode VisitQuery(QueryNode query)
		{
			return (QueryNode)Visit(query);
		}
Example #5
0
 public CommonTableBinding(string name, QueryNode anchorMember)
 {
     _name         = name;
     _anchorMember = anchorMember;
 }
Example #6
0
        private QueryNode ParseSelectQuery()
        {
            if (_token.Id == TokenId.LeftParentheses)
            {
                NextToken();
                QueryNode result = ParseQuery();
                Match(TokenId.RightParentheses);
                return(result);
            }

            if (!Match(TokenId.SELECT))
            {
                return(null);
            }

            SelectQuery selectQuery = new SelectQuery();

            // DISTINCT

            if (_token.Id == TokenId.DISTINCT)
            {
                selectQuery.IsDistinct = true;
                NextToken();
            }

            // TOP

            if (_token.Id == TokenId.TOP)
            {
                NextToken();

                TopClause topClause = new TopClause();
                topClause.Value = (int)ParseInteger();

                if (_token.Id == TokenId.WITH)
                {
                    NextToken();
                    Match(TokenId.TIES);
                    topClause.WithTies = true;
                }
                selectQuery.TopClause = topClause;
            }

            // SelectColumns

            selectQuery.SelectColumns = ParseColumnSources();

            // FROM

            if (_token.Id == TokenId.FROM)
            {
                NextToken();
                selectQuery.TableReferences = ParseTableReferences();
            }

            // WHERE

            if (_token.Id == TokenId.WHERE)
            {
                NextToken();
                selectQuery.WhereClause = ParseExpression();
            }

            // GROUP BY

            if (_token.Id == TokenId.GROUP)
            {
                NextToken();
                Match(TokenId.BY);
                selectQuery.GroupByColumns = ParseExpressions();
            }

            // HAVING

            if (_token.Id == TokenId.HAVING)
            {
                NextToken();
                selectQuery.HavingClause = ParseExpression();
            }

            return(selectQuery);
        }
Example #7
0
		public DerivedTableBinding(string name, QueryNode query)
		{
			_name = name;
			_query = query;
		}
Example #8
0
 private void VisitQueryExpression(QueryNode query)
 {
     _nestingLevel++;
     base.Visit(query);
     _nestingLevel--;
 }
Example #9
0
        public override QueryNode VisitSelectQuery(SelectQuery query)
        {
            PushQueryScope(query.QueryScope);

            // Validate all embedded AST entries that need to check a query context.

            _queryNestingLevel++;
            QueryNode result = base.VisitSelectQuery(query);

            _queryNestingLevel--;

            // Validate DISTINCT

            if (query.IsDistinct)
            {
                // Ensure that all column sources are datatypes that are comparable.

                foreach (SelectColumn columnSource in query.SelectColumns)
                {
                    if (!CheckIfTypeIsComparable(columnSource.Expression.ExpressionType))
                    {
                        _errorReporter.InvalidDataTypeInSelectDistinct(columnSource.Expression.ExpressionType);
                    }
                }
            }

            // Validate TOP

            if (query.TopClause != null)
            {
                if (query.TopClause.WithTies && query.OrderByColumns == null)
                {
                    _errorReporter.TopWithTiesRequiresOrderBy();
                }
            }

            // Ensure that all ORDER BY datatypes are comparable.

            if (query.OrderByColumns != null)
            {
                ValidateOrderByClause(query.OrderByColumns);
            }

            // Ensure that if both DISTINCT and ORDER BY are presents all expressions in ORDER BY are also part in SELECT.

            if (query.IsDistinct && query.OrderByColumns != null)
            {
                bool allColumnsAreInInput = GetAllColumnsAreInInput(query.SelectColumns, query.OrderByColumns);
                if (!allColumnsAreInInput)
                {
                    _errorReporter.OrderByItemsMustBeInSelectListIfDistinctSpecified();
                }
            }

            // Validate GROUP BY and aggregation-only queries.

            if (query.GroupByColumns == null)
            {
                if (query.IsAggregated || query.HavingClause != null)
                {
                    // No grouping applied but at least one aggregation function present. That
                    // means we have an aggregation-only query.
                    //
                    // Check that all expressions in SELECT are either aggregated or do not
                    // reference any column.

                    foreach (SelectColumn columnSource in query.SelectColumns)
                    {
                        foreach (ColumnRefBinding referencedColumn in AstUtil.GetUngroupedAndUnaggregatedColumns(null, columnSource.Expression))
                        {
                            if (referencedColumn.Scope == query.QueryScope)
                            {
                                // The column is not an outer reference so this is an error.
                                _errorReporter.SelectExpressionNotAggregatedAndNoGroupBy(referencedColumn);
                            }
                        }
                    }

                    // Check that all expressions in HAVING are either aggregated or do not
                    // reference any column.

                    if (query.HavingClause != null)
                    {
                        foreach (ColumnRefBinding referencedColumn in AstUtil.GetUngroupedAndUnaggregatedColumns(null, query.HavingClause))
                        {
                            if (referencedColumn.Scope == query.QueryScope)
                            {
                                // The column is not an outer reference so this is an error.
                                _errorReporter.HavingExpressionNotAggregatedOrGrouped(referencedColumn);
                            }
                        }
                    }

                    // Check that all expressions in ORDER BY are either aggregated or do not
                    // reference any column.

                    if (query.OrderByColumns != null)
                    {
                        foreach (OrderByColumn orderByColumn in query.OrderByColumns)
                        {
                            foreach (ColumnRefBinding referencedColumn in AstUtil.GetUngroupedAndUnaggregatedColumns(null, orderByColumn.Expression))
                            {
                                if (referencedColumn.Scope == query.QueryScope)
                                {
                                    // The column is not an outer reference so this is an error.
                                    _errorReporter.OrderByExpressionNotAggregatedAndNoGroupBy(referencedColumn);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                // Grouped query:
                //
                // 1. All expression in GROUP BY must have a datatype that is comparable.
                //
                // 2. All expressions in GROUP BY must not be aggregated
                //
                // 3. All expressions in SELECT, ORDER BY, and HAVING must be aggregated, grouped or must not reference
                //    columns.

                // Check that all GROUP BY expressions are not aggregated.

                foreach (ExpressionNode groupExpression in query.GroupByColumns)
                {
                    if (!CheckIfTypeIsComparable(groupExpression.ExpressionType))
                    {
                        _errorReporter.InvalidDataTypeInGroupBy(groupExpression.ExpressionType);
                    }

                    MetaInfo metaInfo = AstUtil.GetMetaInfo(groupExpression);
                    if (metaInfo.ColumnDependencies.Length == 0)
                    {
                        _errorReporter.GroupByItemDoesNotReferenceAnyColumns();
                    }
                }

                // Check that all expressions in SELECT are either part of the GROUP BY or are referencing only those
                // columns that are part of the GROUP BY.

                foreach (SelectColumn columnSource in query.SelectColumns)
                {
                    foreach (ColumnRefBinding referencedColumn in AstUtil.GetUngroupedAndUnaggregatedColumns(query.GroupByColumns, columnSource.Expression))
                    {
                        if (referencedColumn.Scope == query.QueryScope)
                        {
                            // The column is not an outer reference so this is an error.
                            _errorReporter.SelectExpressionNotAggregatedOrGrouped(referencedColumn);
                        }
                    }
                }

                // Check that all expressions in HAVING are either part of the GROUP BY or are referencing only those
                // columns that are part of the GROUP BY.

                if (query.HavingClause != null)
                {
                    foreach (ColumnRefBinding referencedColumn in AstUtil.GetUngroupedAndUnaggregatedColumns(query.GroupByColumns, query.HavingClause))
                    {
                        if (referencedColumn.Scope == query.QueryScope)
                        {
                            // The column is not an outer reference so this is an error.
                            _errorReporter.HavingExpressionNotAggregatedOrGrouped(referencedColumn);
                        }
                    }
                }

                // Check that all expressions in the ORDER BY clause are either part of the GROUP BY or are
                // referencing only those columns that are part of the GROUP BY.

                if (query.OrderByColumns != null)
                {
                    foreach (OrderByColumn orderByColumn in query.OrderByColumns)
                    {
                        foreach (ColumnRefBinding referencedColumn in AstUtil.GetUngroupedAndUnaggregatedColumns(query.GroupByColumns, orderByColumn.Expression))
                        {
                            if (referencedColumn.Scope == query.QueryScope)
                            {
                                // The column is not an outer reference so this is an error.
                                _errorReporter.OrderByExpressionNotAggregatedOrGrouped(referencedColumn);
                            }
                        }
                    }
                }
            }

            PopQueryScope();
            return(result);
        }
Example #10
0
		public CommonTableBinding(string name, QueryNode anchorMember)
		{
			_name = name;
			_anchorMember = anchorMember;
		}
 public DerivedTableBinding(string name, QueryNode query)
 {
     _name  = name;
     _query = query;
 }
Example #12
0
 private void EnsureQueryHasOneColumnOnly(QueryNode query)
 {
     SelectColumn[] selectColumns = query.GetColumns();
     if (selectColumns.Length != 1)
         _errorReporter.TooManyExpressionsInSelectListOfSubquery();
 }
Example #13
0
        private void EnsureQueryHasNoOrderByUnlessTopSpecified(QueryNode query)
        {
            SelectQuery selectQuery = query as SelectQuery;
            if (selectQuery == null)
                return;

            if (selectQuery.OrderByColumns != null && selectQuery.TopClause == null)
                _errorReporter.OrderByInvalidInSubqueryUnlessTopIsAlsoSpecified();
        }
Example #14
0
		public static ResultAlgebraNode Convert(QueryNode queryNode)
		{
			return Convert(null, queryNode);
		}
Example #15
0
 public static ResultAlgebraNode Convert(QueryNode queryNode)
 {
     return(Convert(null, queryNode));
 }
Example #16
0
        private static bool IsRecursive(QueryNode query, Identifier tableName)
        {
            CommonTableExpressionRecursiveMemberChecker checker = new CommonTableExpressionRecursiveMemberChecker(tableName);
            checker.Visit(query);

            return checker.RecursiveReferences > 0 ||
                   checker.RecursiveReferenceInSubquery;
        }
Example #17
0
        private static ResultAlgebraNode Convert(CommonTableBinding currentCommonTableBinding, QueryNode queryNode)
        {
            Algebrizer  algebrizer = new Algebrizer(currentCommonTableBinding);
            AlgebraNode result     = algebrizer.ConvertAstNode(queryNode);

            result = new SubqueryExpander().VisitAlgebraNode(result);
            return((ResultAlgebraNode)result);
        }
Example #18
0
		private void VisitQueryExpression(QueryNode query)
		{
			_nestingLevel++;
			base.Visit(query);
			_nestingLevel--;
		}