Exemple #1
0
        public override ExpressionNode VisitCoalesceExpression(CoalesceExpression expression)
        {
            // First visit all expressions
            base.VisitCoalesceExpression(expression);

            // Since a COALESCE expression can be expressed using a CASE expression we convert
            // them to simplify the evaluation and optimization engine.
            //
            // COALESCE(expression1,...,expressionN) is equivalent to this CASE expression:
            //
            // CASE
            //   WHEN (expression1 IS NOT NULL) THEN expression1
            //   ...
            //   WHEN (expressionN IS NOT NULL) THEN expressionN
            // END

            CaseExpression caseExpression = new CaseExpression();

            caseExpression.WhenExpressions = new ExpressionNode[expression.Expressions.Length];
            caseExpression.ThenExpressions = new ExpressionNode[expression.Expressions.Length];

            for (int i = 0; i < expression.Expressions.Length; i++)
            {
                ExpressionNode whenPart = expression.Expressions[i];
                ExpressionNode thenPart = (ExpressionNode)whenPart.Clone();
                caseExpression.WhenExpressions[i] = new IsNullExpression(true, whenPart);
                caseExpression.ThenExpressions[i] = thenPart;
            }

            return(VisitExpression(caseExpression));
        }
Exemple #2
0
		public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements)
		{
			CoalesceExpression result = new CoalesceExpression();
			result.Expressions = new ExpressionNode[_expressions.Length];
			for (int i = 0; i < _expressions.Length; i++)
				result.Expressions[i] = (ExpressionNode)_expressions[i].Clone(alreadyClonedElements);
			return result;
		}
Exemple #3
0
        public virtual ExpressionNode VisitCoalesceExpression(CoalesceExpression expression)
        {
            for (int i = 0; i < expression.Expressions.Length; i++)
            {
                expression.Expressions[i] = VisitExpression(expression.Expressions[i]);
            }

            return(expression);
        }
        public override AstElement Clone(Dictionary <AstElement, AstElement> alreadyClonedElements)
        {
            CoalesceExpression result = new CoalesceExpression();

            result.Expressions = new ExpressionNode[_expressions.Length];
            for (int i = 0; i < _expressions.Length; i++)
            {
                result.Expressions[i] = (ExpressionNode)_expressions[i].Clone(alreadyClonedElements);
            }
            return(result);
        }
Exemple #5
0
        public override ExpressionNode VisitCoalesceExpression(CoalesceExpression expression)
        {
            _xmlWriter.WriteStartElement("coalesceExpression");
            WriteTypeAttribute(expression.ExpressionType);

            for (int i = 0; i < expression.Expressions.Length; i++)
            {
                _xmlWriter.WriteStartElement("expression");
                _xmlWriter.WriteAttributeString("index", XmlConvert.ToString(i));
                Visit(expression.Expressions[i]);
                _xmlWriter.WriteEndElement();
            }

            _xmlWriter.WriteEndElement();

            return(expression);
        }
Exemple #6
0
        private static bool VisitCoalesceExpression(CoalesceExpression node1, CoalesceExpression node2)
        {
            if (node2 == null)
            {
                return(false);
            }

            if (node1.Expressions.Length != node2.Expressions.Length)
            {
                return(false);
            }

            for (int i = 0; i < node1.Expressions.Length; i++)
            {
                if (!Visit(node1.Expressions[i], node2.Expressions[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #7
0
		private static bool VisitCoalesceExpression(CoalesceExpression node1, CoalesceExpression node2)
		{
			if (node2 == null)
				return false;

			if (node1.Expressions.Length != node2.Expressions.Length)
				return false;

			for (int i = 0; i < node1.Expressions.Length; i++)
				if (!Visit(node1.Expressions[i], node2.Expressions[i]))
					return false;

			return true;
		}
Exemple #8
0
        private ExpressionNode ParsePrimaryExpression()
        {
            switch (_token.Id)
            {
                case TokenId.NULL:
                    NextToken();
                    return LiteralExpression.FromNull();

                case TokenId.TRUE:
                case TokenId.FALSE:
                    return ParseBooleanLiteral();

                case TokenId.Date:
                    return ParseDateLiteral();

                case TokenId.Number:
                    return ParseNumberLiteral();

                case TokenId.String:
                    return LiteralExpression.FromString(ParseString());

                case TokenId.EXISTS:
                {
                    ExistsSubselect result = new ExistsSubselect();
                    NextToken();
                    Match(TokenId.LeftParentheses);
                    result.Query = ParseQuery();
                    Match(TokenId.RightParentheses);

                    return result;
                }

                case TokenId.ParameterMarker:
                {
                    _rangeRecorder.Begin();
                    NextToken();
                    Identifier name = ParseIdentifier();
                    SourceRange nameSourceRange = _rangeRecorder.End();

                    ParameterExpression result = new ParameterExpression();
                    result.Name = name;
                    result.NameSourceRange = nameSourceRange;
                    return result;
                }

                case TokenId.CAST:
                {
                    NextToken();
                    CastExpression castExpression = new CastExpression();
                    Match(TokenId.LeftParentheses);
                    castExpression.Expression = ParseExpression();
                    Match(TokenId.AS);
                    castExpression.TypeReference = ParseTypeReference();
                    Match(TokenId.RightParentheses);

                    return castExpression;
                }

                case TokenId.CASE:
                {
                    NextToken();

                    CaseExpression caseExpression = new CaseExpression();

                    if (_token.Id != TokenId.WHEN && _token.Id != TokenId.ELSE && _token.Id != TokenId.END)
                        caseExpression.InputExpression = ParseExpression();

                    List<ExpressionNode> whenExpressionList = new List<ExpressionNode>();
                    List<ExpressionNode> expressionList = new List<ExpressionNode>();

                    if (_token.Id != TokenId.WHEN)
                    {
                        Match(TokenId.WHEN);
                    }
                    else
                    {
                        while (_token.Id == TokenId.WHEN)
                        {
                            NextToken();

                            whenExpressionList.Add(ParseExpression());
                            Match(TokenId.THEN);
                            expressionList.Add(ParseExpression());
                        }
                    }

                    caseExpression.WhenExpressions = whenExpressionList.ToArray();
                    caseExpression.ThenExpressions = expressionList.ToArray();

                    if (_token.Id == TokenId.ELSE)
                    {
                        NextToken();
                        caseExpression.ElseExpression = ParseExpression();
                    }

                    Match(TokenId.END);

                    return caseExpression;
                }

                case TokenId.COALESCE:
                {
                    NextToken();
                    CoalesceExpression coalesceExpression = new CoalesceExpression();
                    coalesceExpression.Expressions = ParseExpressionList();
                    return coalesceExpression;
                }

                case TokenId.NULLIF:
                {
                    NextToken();
                    NullIfExpression nullIfExpression = new NullIfExpression();
                    Match(TokenId.LeftParentheses);
                    nullIfExpression.LeftExpression = ParseExpression();
                    Match(TokenId.Comma);
                    nullIfExpression.RightExpression = ParseExpression();
                    Match(TokenId.RightParentheses);
                    return nullIfExpression;
                }

                case TokenId.Identifier:
                {
                    _rangeRecorder.Begin();
                    Identifier name = ParseIdentifier();
                    SourceRange nameSourceRange = _rangeRecorder.End();

                    if (_token.Id != TokenId.LeftParentheses)
                    {
                        NameExpression result = new NameExpression();
                        result.Name = name;
                        result.NameSourceRange = nameSourceRange;
                        return result;
                    }
                    else
                    {
                        bool hasAsteriskModifier;
                        ExpressionNode[] args;

                        if (_lookahead.Id != TokenId.Multiply)
                        {
                            hasAsteriskModifier = false;
                            args = ParseExpressionList();
                        }
                        else
                        {
                            NextToken();
                            NextToken();
                            Match(TokenId.RightParentheses);

                            hasAsteriskModifier = true;
                            args = new ExpressionNode[0];
                        }

                        FunctionInvocationExpression result = new FunctionInvocationExpression();
                        result.Name = name;
                        result.NameSourceRange = nameSourceRange;
                        result.Arguments = args;
                        result.HasAsteriskModifier = hasAsteriskModifier;
                        return result;
                    }
                }

                case TokenId.LeftParentheses:
                {
                    NextToken();

                    ExpressionNode expr;

                    if (_token.Id != TokenId.SELECT)
                    {
                        expr = ParseExpression();
                    }
                    else
                    {
                        SingleRowSubselect singleRowSubselect = new SingleRowSubselect();
                        singleRowSubselect.Query = ParseQuery();
                        expr = singleRowSubselect;
                    }

                    Match(TokenId.RightParentheses);
                    return expr;
                }

                default:
                {
                    _errorReporter.SimpleExpressionExpected(_token.Range, _token.Text);
                    return LiteralExpression.FromNull();
                }
            }
        }
Exemple #9
0
		public override ExpressionNode VisitCoalesceExpression(CoalesceExpression expression)
		{
			// First visit all expressions
			base.VisitCoalesceExpression(expression);

			// Since a COALESCE expression can be expressed using a CASE expression we convert
			// them to simplify the evaluation and optimization engine.
			//
			// COALESCE(expression1,...,expressionN) is equivalent to this CASE expression:
			//
			// CASE
			//   WHEN (expression1 IS NOT NULL) THEN expression1
			//   ...
			//   WHEN (expressionN IS NOT NULL) THEN expressionN
			// END

			CaseExpression caseExpression = new CaseExpression();
			caseExpression.WhenExpressions = new ExpressionNode[expression.Expressions.Length];
			caseExpression.ThenExpressions = new ExpressionNode[expression.Expressions.Length];
			
			for (int i = 0; i < expression.Expressions.Length; i++)
			{
				ExpressionNode whenPart = expression.Expressions[i];
				ExpressionNode thenPart = (ExpressionNode) whenPart.Clone();
				caseExpression.WhenExpressions[i] = new IsNullExpression(true, whenPart);
				caseExpression.ThenExpressions[i] = thenPart;
			}

			return VisitExpression(caseExpression);
		}
Exemple #10
0
		public virtual ExpressionNode VisitCoalesceExpression(CoalesceExpression expression)
		{
			for (int i = 0; i < expression.Expressions.Length; i++)
				expression.Expressions[i] = VisitExpression(expression.Expressions[i]);

			return expression;
		}
Exemple #11
0
        private ExpressionNode ParsePrimaryExpression()
        {
            switch (_token.Id)
            {
            case TokenId.NULL:
                NextToken();
                return(LiteralExpression.FromNull());

            case TokenId.TRUE:
            case TokenId.FALSE:
                return(ParseBooleanLiteral());

            case TokenId.Date:
                return(ParseDateLiteral());

            case TokenId.Number:
                return(ParseNumberLiteral());

            case TokenId.String:
                return(LiteralExpression.FromString(ParseString()));

            case TokenId.EXISTS:
            {
                ExistsSubselect result = new ExistsSubselect();
                NextToken();
                Match(TokenId.LeftParentheses);
                result.Query = ParseQuery();
                Match(TokenId.RightParentheses);

                return(result);
            }

            case TokenId.ParameterMarker:
            {
                _rangeRecorder.Begin();
                NextToken();
                Identifier  name            = ParseIdentifier();
                SourceRange nameSourceRange = _rangeRecorder.End();

                ParameterExpression result = new ParameterExpression();
                result.Name            = name;
                result.NameSourceRange = nameSourceRange;
                return(result);
            }

            case TokenId.CAST:
            {
                NextToken();
                CastExpression castExpression = new CastExpression();
                Match(TokenId.LeftParentheses);
                castExpression.Expression = ParseExpression();
                Match(TokenId.AS);
                castExpression.TypeReference = ParseTypeReference();
                Match(TokenId.RightParentheses);

                return(castExpression);
            }

            case TokenId.CASE:
            {
                NextToken();

                CaseExpression caseExpression = new CaseExpression();

                if (_token.Id != TokenId.WHEN && _token.Id != TokenId.ELSE && _token.Id != TokenId.END)
                {
                    caseExpression.InputExpression = ParseExpression();
                }

                List <ExpressionNode> whenExpressionList = new List <ExpressionNode>();
                List <ExpressionNode> expressionList     = new List <ExpressionNode>();

                if (_token.Id != TokenId.WHEN)
                {
                    Match(TokenId.WHEN);
                }
                else
                {
                    while (_token.Id == TokenId.WHEN)
                    {
                        NextToken();

                        whenExpressionList.Add(ParseExpression());
                        Match(TokenId.THEN);
                        expressionList.Add(ParseExpression());
                    }
                }

                caseExpression.WhenExpressions = whenExpressionList.ToArray();
                caseExpression.ThenExpressions = expressionList.ToArray();

                if (_token.Id == TokenId.ELSE)
                {
                    NextToken();
                    caseExpression.ElseExpression = ParseExpression();
                }

                Match(TokenId.END);

                return(caseExpression);
            }

            case TokenId.COALESCE:
            {
                NextToken();
                CoalesceExpression coalesceExpression = new CoalesceExpression();
                coalesceExpression.Expressions = ParseExpressionList();
                return(coalesceExpression);
            }

            case TokenId.NULLIF:
            {
                NextToken();
                NullIfExpression nullIfExpression = new NullIfExpression();
                Match(TokenId.LeftParentheses);
                nullIfExpression.LeftExpression = ParseExpression();
                Match(TokenId.Comma);
                nullIfExpression.RightExpression = ParseExpression();
                Match(TokenId.RightParentheses);
                return(nullIfExpression);
            }

            case TokenId.Identifier:
            {
                _rangeRecorder.Begin();
                Identifier  name            = ParseIdentifier();
                SourceRange nameSourceRange = _rangeRecorder.End();

                if (_token.Id != TokenId.LeftParentheses)
                {
                    NameExpression result = new NameExpression();
                    result.Name            = name;
                    result.NameSourceRange = nameSourceRange;
                    return(result);
                }
                else
                {
                    bool             hasAsteriskModifier;
                    ExpressionNode[] args;

                    if (_lookahead.Id != TokenId.Multiply)
                    {
                        hasAsteriskModifier = false;
                        args = ParseExpressionList();
                    }
                    else
                    {
                        NextToken();
                        NextToken();
                        Match(TokenId.RightParentheses);

                        hasAsteriskModifier = true;
                        args = new ExpressionNode[0];
                    }

                    FunctionInvocationExpression result = new FunctionInvocationExpression();
                    result.Name                = name;
                    result.NameSourceRange     = nameSourceRange;
                    result.Arguments           = args;
                    result.HasAsteriskModifier = hasAsteriskModifier;
                    return(result);
                }
            }

            case TokenId.LeftParentheses:
            {
                NextToken();

                ExpressionNode expr;

                if (_token.Id != TokenId.SELECT)
                {
                    expr = ParseExpression();
                }
                else
                {
                    SingleRowSubselect singleRowSubselect = new SingleRowSubselect();
                    singleRowSubselect.Query = ParseQuery();
                    expr = singleRowSubselect;
                }

                Match(TokenId.RightParentheses);
                return(expr);
            }

            default:
            {
                _errorReporter.SimpleExpressionExpected(_token.Range, _token.Text);
                return(LiteralExpression.FromNull());
            }
            }
        }
Exemple #12
0
		public override ExpressionNode VisitCoalesceExpression(CoalesceExpression expression)
		{
			_xmlWriter.WriteStartElement("coalesceExpression");
			WriteTypeAttribute(expression.ExpressionType);

			for (int i = 0; i < expression.Expressions.Length; i++)
			{
				_xmlWriter.WriteStartElement("expression");
				_xmlWriter.WriteAttributeString("index", XmlConvert.ToString(i));
				Visit(expression.Expressions[i]);
				_xmlWriter.WriteEndElement();
			}

			_xmlWriter.WriteEndElement();

			return expression;
		}