private void ConvertExpressionConstantsNotation(SQLExpressionItem expression)
        {
            Debug.Assert(expression != null);

            List <AstNodeBase> astNodes = new List <AstNodeBase>();

            // get all children (and grand[grand...]children) AST nodes as a flat list
            expression.GetMyChildrenRecursive(astNodes);

            // add expression node itself to AST nodes list
            // (to check is the given expression is constant itself)
            astNodes.Add(expression);

            // remove all items from the list except TSQLExpressionConstant
            for (int i = astNodes.Count - 1; i >= 0; i--)
            {
                if (!(astNodes[i] is SQLExpressionConstant))
                {
                    astNodes.RemoveAt(i);
                }
            }

            // for each item in the list
            for (int i = 0; i < astNodes.Count; i++)
            {
                ConvertConstantNotation((SQLExpressionConstant)astNodes[i]);
            }
        }
        private void ReplaceWhereAccess(UnionSubQuery unionSubQuery, string newWhereStr)
        {
            // parse and prepare new WHERE expression
            SQLExpressionItem newWhere = ParseExpressionAccess(newWhereStr);

            if (newWhere != null)
            {
                FixupExpression(unionSubQuery, newWhere);
                ConvertExpressionConstantsNotation(newWhere);
            }

            // defer SQL updates
            unionSubQuery.BeginUpdate();

            try
            {
                // clear old WHERE expression
                ClearWhere(unionSubQuery);

                // load new WHERE expression (if exists)
                if (newWhere != null)
                {
                    unionSubQuery.QueryColumnList.LoadConditionFromAST(newWhere, ConditionType.Where);
                }
            }
            finally
            {
                // enable SQL updates
                unionSubQuery.EndUpdate();
            }
        }
        private void FixupExpression(QueryElement queryContext, SQLExpressionItem expression)
        {
            Debug.Assert(expression != null);

            List <SQLWithClauseItem> listCTE         = new List <SQLWithClauseItem>();
            List <SQLFromSource>     listFromSources = new List <SQLFromSource>();

            // fix up names in raw AST in given context
            queryContext.GatherPrepareAndFixupContext(listCTE, listFromSources, true);
            expression.PrepareAndFixupRecursive(listCTE, listFromSources);
        }
Ejemplo n.º 4
0
        private void DumpExpression(StringBuilder stringBuilder, string indent, SQLExpressionItem expression)
        {
            const string cIndentInc = "&nbsp;&nbsp;&nbsp;&nbsp;";

            string newIndent = indent + cIndentInc;

            if (expression == null) // NULL reference protection
            {
                stringBuilder.AppendLine(indent + "--nil--" + "<br />");
            }
            else if (expression is SQLExpressionBrackets)
            {
                // Expression is actually the brackets query structure node.
                // Create the "brackets" tree node and load content of
                // the brackets as children of the node.
                stringBuilder.AppendLine(indent + "()" + "<br />");
                DumpExpression(stringBuilder, newIndent, ((SQLExpressionBrackets)expression).LExpression);
            }
            else if (expression is SQLExpressionOr)
            {
                // Expression is actually the "OR" query structure node.
                // Create the "OR" tree node and load all items of
                // the "OR" collection as children of the tree node.
                stringBuilder.AppendLine(indent + "OR" + "<br />");

                for (int i = 0; i < ((SQLExpressionOr)expression).Count; i++)
                {
                    DumpExpression(stringBuilder, newIndent, ((SQLExpressionOr)expression)[i]);
                }
            }
            else if (expression is SQLExpressionAnd)
            {
                // Expression is actually the "AND" query structure node.
                // Create the "AND" tree node and load all items of
                // the "AND" collection as children of the tree node.
                stringBuilder.AppendLine(indent + "AND" + "<br />");

                for (int i = 0; i < ((SQLExpressionAnd)expression).Count; i++)
                {
                    DumpExpression(stringBuilder, newIndent, ((SQLExpressionAnd)expression)[i]);
                }
            }
            else if (expression is SQLExpressionNot)
            {
                // Expression is actually the "NOT" query structure node.
                // Create the "NOT" tree node and load content of
                // the "NOT" operator as children of the tree node.
                stringBuilder.AppendLine(indent + "NOT" + "<br />");
                DumpExpression(stringBuilder, newIndent, ((SQLExpressionNot)expression).LExpression);
            }
            else if (expression is SQLExpressionOperatorBinary)
            {
                // Expression is actually the "BINARY OPERATOR" query structure node.
                // Create a tree node containing the operator value and
                // two leaf nodes with the operator arguments.
                string s = ((SQLExpressionOperatorBinary)expression).OperatorObj.OperatorName;
                stringBuilder.AppendLine(indent + s + "<br />");
                // left argument of the binary operator
                DumpExpression(stringBuilder, newIndent, ((SQLExpressionOperatorBinary)expression).LExpression);
                // right argument of the binary operator
                DumpExpression(stringBuilder, newIndent, ((SQLExpressionOperatorBinary)expression).RExpression);
            }
            else
            {
                // other type of AST nodes - out as a text
                string s = expression.GetSQL(expression.SQLContext.SQLGenerationOptionsForServer);
                stringBuilder.AppendLine(indent + s + "<br />");
            }
        }
Ejemplo n.º 5
0
 public void DumpWhereInfo(StringBuilder stringBuilder, SQLExpressionItem where)
 {
     DumpExpression(stringBuilder, "", where);
 }
        private void AppendWhere(UnionSubQuery unionSubQuery, string newWhereStr)
        {
            SQLExpressionAnd resultWhere;

            // parse and prepare new WHERE expression
            SQLExpressionItem newWhere = ParseExpression(newWhereStr);

            // if there are no new expression - nothing to do
            if (newWhere == null)
            {
                return;
            }

            // extract old WHERE expression
            SQLExpressionItem oldWhere = unionSubQuery.QueryColumnList.GetConditionTree(new[] { ConditionType.Where });

            // normalize old WHERE expression
            if (oldWhere != null)
            {
                oldWhere.RestoreColumnPrefixRecursive(true);
            }

            // simplify old WHERE expression
            if (oldWhere != null)
            {
                // if old WHERE is a collection of ORed or ANDed expressions
                // with only one expression in the list - remove the external list
                while (oldWhere is SQLExpressionLogicalCollection &&
                       ((SQLExpressionLogicalCollection)oldWhere).Count == 1)
                {
                    using (SQLExpressionLogicalCollection tmp = (SQLExpressionLogicalCollection)oldWhere)
                    {
                        oldWhere = tmp.Extract(0);
                    }
                }
            }

            // combine old and new WHERE expressions
            resultWhere = new SQLExpressionAnd(queryBuilder1.SQLContext);

            if (oldWhere != null)
            {
                resultWhere.Add(oldWhere);
            }

            resultWhere.Add(newWhere);

            // fix up combined WHERE expression
            FixupExpression(unionSubQuery, resultWhere);

            // defer SQL updates
            unionSubQuery.BeginUpdate();

            try
            {
                // clear old WHERE expression
                ClearWhere(unionSubQuery);

                // load new WHERE expression (if exists)
                unionSubQuery.QueryColumnList.LoadConditionFromAST(resultWhere, ConditionType.Where);
            }
            finally
            {
                // enable SQL updates
                unionSubQuery.EndUpdate();
            }
        }