Пример #1
0
        public override void VisitBinaryExpression(SqlParser.Expressions.BinaryExpression binaryExpression)
        {
            binaryExpression.Left.Accept(this);
            binaryExpression.Right.Accept(this);


            var rightExpression = PopStack();
            var leftExpression  = PopStack();

            var expression = BinaryUtils.CreateBinaryExpression(leftExpression, rightExpression, binaryExpression.Type);

            AddExpressionToStack(expression);
        }
Пример #2
0
        private VisitResult VisitBinaryExpression_Internal(SqlParser.Expressions.BinaryExpression binaryExpression)
        {
            var leftResult = VisitInternal(binaryExpression.Left);

            //Left result should only contain: '%text' or 'text'
            if (leftResult.StartsWith && !leftResult.WildcardOnly)
            {
                throw new SqlErrorException("Like expression can only support starts with ('text%'), ends with ('%text') and contains ('%text%') at this time");
            }

            var rightResult = VisitInternal(binaryExpression.Right);

            //Right result should only contain 'text%' or 'text'
            if (rightResult.EndsWith && !rightResult.WildcardOnly)
            {
                throw new SqlErrorException("Like expression can only support starts with ('text%'), ends with ('%text') and contains ('%text%') at this time");
            }

            if (binaryExpression.Type != BinaryType.Add)
            {
                throw new SqlErrorException("Like expression can only do string concatination at this time");
            }

            //If one of the results are null, they have been removed since they were just '%'
            //We only return the other result since concat is not required
            if (leftResult.Expression == null)
            {
                return(rightResult);
            }

            //Remove empty strings that was just for wildcarding
            if (leftResult.WildcardOnly && rightResult.WildcardOnly)
            {
                return(new VisitResult()
                {
                    EndsWith = true,
                    StartsWith = true,
                    WildcardOnly = true,
                    Expression = Expression.Constant("")
                });
            }
            else if (leftResult.WildcardOnly)
            {
                return(new VisitResult()
                {
                    EndsWith = true,
                    StartsWith = rightResult.StartsWith,
                    Expression = rightResult.Expression
                });
            }
            else if (rightResult.WildcardOnly)
            {
                return(new VisitResult()
                {
                    StartsWith = true,
                    Expression = leftResult.Expression,
                    EndsWith = leftResult.EndsWith
                });
            }

            var expression = BinaryUtils.CreateBinaryExpression(leftResult.Expression, rightResult.Expression, BinaryType.Add);

            return(new VisitResult()
            {
                StartsWith = rightResult.StartsWith || rightResult.WildcardOnly,
                EndsWith = leftResult.EndsWith || leftResult.WildcardOnly,
                Expression = expression
            });
        }