Example #1
0
 protected internal override Expression VisitLike(LikeExpression like)
 {
     Visit(like.Expression);
     sb.Append(" LIKE ");
     Visit(like.Pattern);
     return(like);
 }
Example #2
0
        protected override Expression VisitLike(LikeExpression likeExpression)
        {
            Visit(likeExpression.Match);
            Visit(likeExpression.Pattern);

            return(likeExpression);
        }
Example #3
0
        public void TestCloneLikeExpression()
        {
            LikeExpression likeExpression = new LikeExpression()
            {
                Left = new ColumnReference()
                {
                    Identifiers = new List <string>()
                    {
                        "c1"
                    }
                },
                Right = new StringLiteral()
                {
                    Value = "test"
                },
                Not = false
            };

            var clone = likeExpression.Clone() as LikeExpression;

            Assert.AreEqual(likeExpression, clone);
            Assert.IsFalse(ReferenceEquals(likeExpression, clone));
            Assert.IsFalse(ReferenceEquals(likeExpression.Left, clone.Left));
            Assert.IsFalse(ReferenceEquals(likeExpression.Right, clone.Right));
        }
Example #4
0
        public QueryBuilder <T> Like(object value)
        {
            AbstractCriterion likeExpression = new LikeExpression(name, value);

            AddCriterion(likeExpression);
            return(this);
        }
        void HandleStringEndsWithExpression(LikeExpression likeExpression)
        {
            Expression right = ((BinaryExpression)likeExpression.Pattern).Right;
            string     text  = " LOCATE (";

            if (right.NodeType == ExpressionType.Constant || right.NodeType == ExpressionType.Parameter)
            {
                Visit(likeExpression.Match);
                Sql.Append(" LIKE ");
                Visit(likeExpression.Pattern);
            }
            else
            {
                Sql.Append(text);
                Visit(right);
                Sql.Append(" , RIGHT( ");
                Visit(likeExpression.Match);
                Sql.Append(" , ( LENGTH(");
                Visit(likeExpression.Match);
                Sql.Append(") + 1) - " + text);
                Visit(right);
                Sql.Append(" , ");
                Visit(likeExpression.Match);
                Sql.Append(" )))  = cast(1 AS int)");
            }
        }
Example #6
0
        protected override Expression VisitLike(QueryTranslatorContext context, LikeExpression expression)
        {
            // Is the left side a collection?
            if (expression.Left is CollectionExpression collection)
            {
                // Build the collection accessor parameter.
                var parameter = collection.Accept(this, context);

                // Build the predicate expression.
                context.PushParameter(collection.ItemType);
                var predicate = Expression.Lambda(
                    new LikeExpression(
                        collection.ItemAccessor,
                        expression.Right).Accept(this, context),
                    context.Parameter);
                context.PopParameter();

                // We need to use an Enumerable.Any together with the LIKE predicate.
                return(Expression.Call(typeof(Enumerable), "Any", new Type[] { collection.ItemType }, parameter, predicate));
            }

            var left  = expression.Left.Accept(this, context);
            var right = expression.Right.Accept(this, context);

            return(Expression.Call(
                       null, typeof(DbFunctionsExtensions).GetMethod("Like", new[] { typeof(DbFunctions), typeof(string), typeof(string) }),
                       Expression.Constant(EF.Functions), left, right));
        }
        public void Visit(LikeExpression op)
        {
            var leftArgs  = VisitChild(op.Left);
            var rightArgs = VisitChild(op.Right);

            _codeStack.Peek().CodeExpression = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Helper)), "Like", leftArgs.CodeExpression, rightArgs.CodeExpression);
        }
Example #8
0
        public QueryBuilder <T> Like(string value, MatchMode matchMode)
        {
            AbstractCriterion likeExpression = new LikeExpression(name, value, matchMode);

            AddCriterion(likeExpression);
            return(this);
        }
Example #9
0
        public void Visit(LikeExpression expression)
        {
            var searchString = expression.SearchString;
            var columnName   = expression.PropertyExpression.PropertyName;
            var actualValue  = (string)_row.GetColumnValue(columnName).Value;

            Func <string, bool> operation;

            if (expression.HasStartingWildCard && expression.HasEndingWildCard)
            {
                operation = actualValue.Contains;
            }
            else if (expression.HasStartingWildCard)
            {
                operation = actualValue.EndsWith;
            }
            else if (expression.HasEndingWildCard)
            {
                operation = actualValue.StartsWith;
            }
            else
            {
                throw new WeenyMapperException("Invalid like expression for column '{0}' and search string '{1}'", columnName, searchString);
            }

            if (!operation(searchString))
            {
                _isMatch = false;
            }
        }
Example #10
0
        public void TestLikeExpressionAccept()
        {
            Mock <KoraliumSqlVisitor> mock           = new Mock <KoraliumSqlVisitor>();
            LikeExpression            likeExpression = new LikeExpression();

            likeExpression.Accept(mock.Object);
            mock.Verify(x => x.VisitLikeExpression(likeExpression));
        }
Example #11
0
        public override Expression VisitLike(LikeExpression likeExpression)
        {
            Visit(likeExpression.Match);
            Sql.Append(" LIKE ('%' ");
            Visit(likeExpression.Pattern);
            Sql.Append(" '%')");

            return(likeExpression);
        }
        /// <summary>
        /// 访问Like表达式。
        /// </summary>
        /// <param name="likeExpression">表达式实例。</param>
        /// <returns>返回访问后的表达式实例对象。</returns>
        public virtual Expression VisitLike(LikeExpression likeExpression)
        {
            Check.NotNull(likeExpression, nameof(likeExpression));

            Visit(likeExpression.Match);
            _builder.Append(" LIKE ");

            Visit(likeExpression.Pattern);
            return(likeExpression);
        }
    protected internal virtual Expression VisitLike(LikeExpression like)
    {
        Expression exp     = Visit(like.Expression);
        Expression pattern = Visit(like.Pattern);

        if (exp != like.Expression || pattern != like.Pattern)
        {
            return(new LikeExpression(exp, pattern));
        }
        return(like);
    }
 public bool Filters(LikeExpression like)
 {
     //TODO: Enhancement - evaluate Like Pattern for superset/subset conditions.
     //e.g., "*" would filter "?*", or "?*" would filter "a*"
     //They go here...
     if (like.Operand.Equals(Operand) && Pattern.Equals($"\"*\"")) //The easiest one
     {
         return(true);
     }
     return(false);
 }
Example #15
0
        public void String_EndsWith_call_is_parsed_into_like_expression_with_only_starting_wildcard()
        {
            var expression = _parser.Parse <Book>(x => x.AuthorName.EndsWith("Steve"));

            var expectedExpression = new LikeExpression(new PropertyExpression("AuthorName", typeof(string)), "Steve")
            {
                HasStartingWildCard = true,
                HasEndingWildCard   = false
            };

            Assert.AreEqual(expectedExpression, expression);
        }
        public virtual Expression VisitLikeExpression(LikeExpression likeExpression)
        {
            Check.NotNull(likeExpression, "likeExpression");

            VisitExpression(likeExpression.Match);

            _sql.Append(" LIKE ");

            VisitExpression(likeExpression.Pattern);

            return(likeExpression);
        }
Example #17
0
        private SqlExpression ApplyTypeMappingOnLike(LikeExpression likeExpression)
        {
            var inferredTypeMapping = ExpressionExtensions.InferTypeMapping(
                likeExpression.Match, likeExpression.Pattern, likeExpression.EscapeChar)
                                      ?? _typeMappingSource.FindMapping(likeExpression.Match.Type);

            return(new LikeExpression(
                       ApplyTypeMapping(likeExpression.Match, inferredTypeMapping),
                       ApplyTypeMapping(likeExpression.Pattern, inferredTypeMapping),
                       ApplyTypeMapping(likeExpression.EscapeChar, inferredTypeMapping),
                       _boolTypeMapping));
        }
        private LikeExpression VisitLikeExpression(LikeExpression likeExpression)
        {
            var newMatch   = (SqlExpression)Visit(likeExpression.Match);
            var isNullable = _isNullable;
            var newPattern = (SqlExpression)Visit(likeExpression.Pattern);

            isNullable |= _isNullable;
            var newEscapeChar = (SqlExpression)Visit(likeExpression.EscapeChar);

            _isNullable |= isNullable;

            return(likeExpression.Update(newMatch, newPattern, newEscapeChar));
        }
Example #19
0
        public void LikeMissingAtSymb()
        {
            LikeExpression expression = null;
            Dictionary <string, object> parameters = null;
            string whereParam = null;

            expression = Expression.LikeValue("testtest");
            parameters = expression.GetDynamicParameters("p1");
            whereParam = expression.ToSqlExpression("[Table].[Field]", "p1");

            Assert.AreEqual(parameters["p1"], "testtest");
            Assert.AreEqual(whereParam, " and [Table].[Field] like @p1");
        }
Example #20
0
        protected override Expression VisitLike(LikeExpression likeExpression)
        {
            var parentSearchCondition = _isSearchCondition;

            _isSearchCondition = false;
            var match      = (SqlExpression)Visit(likeExpression.Match);
            var pattern    = (SqlExpression)Visit(likeExpression.Pattern);
            var escapeChar = (SqlExpression)Visit(likeExpression.EscapeChar);

            _isSearchCondition = parentSearchCondition;

            return(ApplyConversion(likeExpression.Update(match, pattern, escapeChar), condition: true));
        }
Example #21
0
        public override void VisitLikeExpression(LikeExpression likeExpression)
        {
            LikeVisitor likeVisitor = new LikeVisitor(_previousStage, _visitorMetadata);

            likeExpression.Accept(likeVisitor);

            //Add the properties that was found in the like visitor
            foreach (var usedProperty in likeVisitor.UsedProperties)
            {
                AddUsedProperty(usedProperty);
            }

            expressions.Push(likeVisitor.Expression);
        }
        protected override Expression VisitLike(LikeExpression likeExpression)
        {
            Visit(likeExpression.Match);
            _relationalCommandBuilder.Append(" LIKE ");
            Visit(likeExpression.Pattern);

            if (likeExpression.EscapeChar != null)
            {
                _relationalCommandBuilder.Append(" ESCAPE ");
                Visit(likeExpression.EscapeChar);
            }

            return(likeExpression);
        }
        /// <summary>
        ///     Visit a LikeExpression.
        /// </summary>
        /// <param name="likeExpression"> The like expression. </param>
        /// <returns>
        ///     An Expression.
        /// </returns>
        public override Expression VisitLike(LikeExpression likeExpression)
        {
            Check.NotNull(likeExpression, nameof(likeExpression));

            if (likeExpression.EscapeChar != null)
            {
                base.VisitLike(new LikeExpression(likeExpression.Match, likeExpression.Pattern));
            }
            else
            {
                base.VisitLike(likeExpression);
            }

            return(likeExpression);
        }
Example #24
0
        protected override Expression VisitLike(LikeExpression likeExpression)
        {
            Check.NotNull(likeExpression, nameof(likeExpression));

            var parentOptimize = _optimize;

            _optimize = false;
            var match      = (SqlExpression)Visit(likeExpression.Match);
            var pattern    = (SqlExpression)Visit(likeExpression.Pattern);
            var escapeChar = (SqlExpression)Visit(likeExpression.EscapeChar);

            _optimize = parentOptimize;

            return(ApplyConversion(likeExpression.Update(match, pattern, escapeChar), condition: true));
        }
        /// <summary>
        ///     Visit a LikeExpression.
        /// </summary>
        /// <param name="likeExpression"> The like expression. </param>
        /// <returns>
        ///     An Expression.
        /// </returns>
        protected override Expression VisitLike(LikeExpression likeExpression)
        {
            Check.NotNull(likeExpression, nameof(likeExpression));

            if (likeExpression.EscapeChar != null)
            {
                base.VisitLike(_sqlExpressionFactory.Like(likeExpression.Match, likeExpression.Pattern));
            }
            else
            {
                base.VisitLike(likeExpression);
            }

            return(likeExpression);
        }
Example #26
0
        public override void VisitLikeExpression(LikeExpression likeExpression)
        {
            likeExpression.Left.Accept(this);

            var leftExpression = PopStack();

            var visitResult = VisitInternal(likeExpression.Right);

            Expression expression = null;

            if (visitResult.StartsWith && visitResult.EndsWith)
            {
                expression = PredicateUtils.CallContains(
                    leftExpression,
                    visitResult.Expression,
                    _visitorMetadata.OperationsProvider);
            }
            //Starts with
            else if (visitResult.StartsWith)
            {
                expression = PredicateUtils.CallStartsWith(
                    leftExpression,
                    visitResult.Expression,
                    _visitorMetadata.OperationsProvider);
            }
            //Ends with
            else if (visitResult.EndsWith)
            {
                expression = PredicateUtils.CallEndsWith(
                    leftExpression,
                    visitResult.Expression,
                    _visitorMetadata.OperationsProvider);
            }
            else
            {
                expression = PredicateUtils.CreateComparisonExpression(
                    leftExpression,
                    visitResult.Expression,
                    BooleanComparisonType.Equals,
                    _visitorMetadata.OperationsProvider);
            }

            if (likeExpression.Not)
            {
                expression = Expression.Not(expression);
            }
            AddExpressionToStack(expression);
        }
        public virtual Expression VisitLike(LikeExpression likeExpression)
        {
            Check.NotNull(likeExpression, nameof(likeExpression));

            var parentIsUnicode = _isUnicode;

            _isUnicode = InferUnicodeFromColumn(likeExpression.Match) ?? _isUnicode;

            Visit(likeExpression.Match);

            _relationalCommandBuilder.Append(" LIKE ");

            Visit(likeExpression.Pattern);

            _isUnicode = parentIsUnicode;

            return(likeExpression);
        }
        public void TestVisitLikeExpression()
        {
            Mock <ScalarExpression> left  = new Mock <ScalarExpression>();
            Mock <ScalarExpression> right = new Mock <ScalarExpression>();

            LikeExpression likeExpression = new LikeExpression()
            {
                Left  = left.Object,
                Right = right.Object
            };

            KoraliumSqlVisitor koraliumSqlVisitor = new KoraliumSqlVisitor();

            koraliumSqlVisitor.Visit(likeExpression);

            left.Verify(x => x.Accept(koraliumSqlVisitor));
            right.Verify(x => x.Accept(koraliumSqlVisitor));
        }
        void HandleStringStartsWithExpression(LikeExpression likeExpression)
        {
            Expression left = ((BinaryExpression)likeExpression.Pattern).Left;

            if (left.NodeType == ExpressionType.Constant || left.NodeType == ExpressionType.Parameter)
            {
                Visit(likeExpression.Match);
                Sql.Append(" LIKE ");
                Visit(likeExpression.Pattern);
            }
            else
            {
                Sql.Append(" LOCATE (");
                Visit(left);
                Sql.Append(" , ");
                Visit(likeExpression.Match);
                Sql.Append(" ) = cast(1 AS int)");
            }
        }
        void HandleStringContainExpression(LikeExpression likeExpression)
        {
            Expression expression = VisitLikeExpression((BinaryExpression)likeExpression.Pattern);

            if (expression.NodeType == ExpressionType.Constant || expression.NodeType == ExpressionType.Parameter)
            {
                Visit(likeExpression.Match);
                Sql.Append(" LIKE '%' || ");
                Visit(expression);
                Sql.Append(" || '%'");
            }
            else
            {
                Sql.Append(" LOCATE (");
                Visit(expression);
                Sql.Append(" , ");
                Visit(likeExpression.Match);
                Sql.Append(" ) >= cast(1 AS int)");
            }
        }
        public void Visit(LikeExpression expression)
        {
            var searchString = expression.SearchString;
            var columnName = expression.PropertyExpression.PropertyName;
            var actualValue = (string)_row.GetColumnValue(columnName).Value;

            Func<string, bool> operation;

            if (expression.HasStartingWildCard && expression.HasEndingWildCard)
            {
                operation = actualValue.Contains;
            }
            else if (expression.HasStartingWildCard)
            {
                operation = actualValue.EndsWith;
            }
            else if (expression.HasEndingWildCard)
            {
                operation = actualValue.StartsWith;
            }
            else
            {
                throw new WeenyMapperException("Invalid like expression for column '{0}' and search string '{1}'", columnName, searchString);
            }

            if (!operation(searchString))
                _isMatch = false;
        }
Example #32
0
 protected override Expression VisitLike(LikeExpression like)
 {
     Visit(like.Expression);
     sb.Append(" LIKE ");
     Visit(like.Pattern);
     return like;
 }
 private string GetExpression(LikeExpression expression, ref List<OleDbParameter> parameters)
 {
     return " ( " + GetExpressionDispatch(expression.Children[0], ref parameters) + " Like " +
       GetExpressionDispatch(expression.Children[1], ref parameters) + " ) ";
 }
        public void String_StartsWith_call_is_parsed_into_like_expression_with_only_ending_wildcard()
        {
            var expression = _parser.Parse<Book>(x => x.AuthorName.StartsWith("Steve"));

            var expectedExpression = new LikeExpression(new PropertyExpression("AuthorName", typeof(string)), "Steve")
                {
                    HasStartingWildCard = false,
                    HasEndingWildCard = true
                };

            Assert.AreEqual(expectedExpression, expression);
        }
Example #35
0
 protected virtual Expression VisitLike(LikeExpression like)
 {
     Expression exp = Visit(like.Expression);
     Expression pattern = Visit(like.Pattern);
     if (exp != like.Expression || pattern != like.Pattern)
         return new LikeExpression(exp, pattern);
     return like;
 }