Exemplo n.º 1
0
        /* ================================================================= STATIC PART */

        public static string ToString(SimplePredicate predicate)
        {
            var visitor = new SnQueryToStringVisitor();

            visitor.VisitSimplePredicate(predicate);
            return(visitor.Output);
        }
Exemplo n.º 2
0
        // ========================================================================================

        public override SnQueryPredicate VisitSimplePredicate(SimplePredicate simplePredicate)
        {
            var result = false;

            var value = simplePredicate.Value;

            if (_indexDoc.Fields.TryGetValue(simplePredicate.FieldName, out var field))
            {
                if (IsWildcardPredicate(field, value))
                {
                    result = GetResultByWildcard(field.StringValue, value.StringValue);
                }
                else if (field.Type == IndexValueType.StringArray && value.Type == IndexValueType.String)
                {
                    result = field.StringArrayValue.Contains(value.StringValue);
                }
                else if (field.Type == value.Type && field.CompareTo(value) == 0)
                {
                    result = true;
                }
            }
            _hitStack.Push(result);

            return(simplePredicate);
        }
        protected override IQuery <Group> ApplyWhereClause(IQuery <Group> query, GroupFilterDto filter)
        {
            var simplePredicate = new SimplePredicate(nameof(Group.Name), ValueComparingOperator.Equal, filter.GroupName);

            return(string.IsNullOrEmpty(filter.GroupName)
                ? query
                : query.Where(simplePredicate));
        }
Exemplo n.º 4
0
 private static string GetNullWhereCondition(SimplePredicate simplePredicate)
 {
     if (simplePredicate.ValueComparingOperator.Equals(ValueComparingOperator.NotEqual))
     {
         return(simplePredicate.TargetPropertyName + " IS NOT NULL");
     }
     return(simplePredicate.TargetPropertyName + " IS NULL");
 }
Exemplo n.º 5
0
        protected override IQuery <Message> ApplyWhereClause(IQuery <Message> query, MessageFilterDto filter)
        {
            var simplePredicate = new SimplePredicate(nameof(Message.FriendshipId), ValueComparingOperator.Equal, filter.FriendshipId);

            return(filter.FriendshipId.Equals(null)
                ? query
                : query.Where(simplePredicate));
        }
        protected override IQuery <Comment> ApplyWhereClause(IQuery <Comment> query, CommentFilterDto filter)
        {
            var simplePredicate = new SimplePredicate(nameof(Comment.PostId), ValueComparingOperator.Equal, filter.PostId);

            return(filter.PostId.Equals(null)
                ? query
                : query.Where(simplePredicate));
        }
Exemplo n.º 7
0
        protected override IQuery <User> ApplyWhereClause(IQuery <User> query, UserFilterDto filter)
        {
            var simplePredicate = new SimplePredicate(nameof(User.NickName), ValueComparingOperator.StringContains, filter.SubName);

            return(filter.Equals(null)
                ? query
                : query.Where(simplePredicate));
        }
        protected override IQuery <Group> ApplyWhereClause(IQuery <Group> query, GroupFilterDto filter)
        {
            var simplePredicate = new SimplePredicate(nameof(Group.Name), ValueComparingOperator.StringContains, filter.SubName);

            return(filter.SubName.Equals("")
                ? query
                : query.Where(simplePredicate));
        }
        public static Expression GetExpression(this SimplePredicate simplePredicate, ParameterExpression parameterExpression)
        {
            var memberExpression   = Expression.PropertyOrField(parameterExpression, simplePredicate.TargetPropertyName);
            var memberType         = GetMemberType(simplePredicate, memberExpression);
            var constantExpression = Expression.Constant(simplePredicate.ComparedValue, memberType);

            return(TransformToExpression(simplePredicate.ValueComparingOperator, memberExpression, constantExpression));
        }
Exemplo n.º 10
0
 public override SnQueryPredicate VisitSimplePredicate(SimplePredicate simplePredicate)
 {
     if (simplePredicate.Value.ValueAsString != "V2")
     {
         return(simplePredicate);
     }
     return(new SimplePredicate(simplePredicate.FieldName, new IndexValue("V2222")));
 }
Exemplo n.º 11
0
        public override SnQueryPredicate VisitSimplePredicate(SimplePredicate simplePredicate)
        {
            var value = Escape(simplePredicate.Value);

            _output.Append($"{simplePredicate.FieldName}:{value}");
            BoostTostring(simplePredicate.Boost);
            FuzzyToString(simplePredicate.FuzzyValue);

            return(base.VisitSimplePredicate(simplePredicate));
        }
Exemplo n.º 12
0
            public override SnQueryPredicate VisitSimplePredicate(SimplePredicate simplePredicate)
            {
                var visited = (SimplePredicate)base.VisitSimplePredicate(simplePredicate);

                if (simplePredicate.FieldName == SharingVisitor.Sharing)
                {
                    SharingRelatedPredicates.Add(visited);
                }
                return(visited);
            }
Exemplo n.º 13
0
            public override SnQueryPredicate VisitSimplePredicate(SimplePredicate simplePredicate)
            {
                var visitedField = simplePredicate.FieldName;

                if (!_fieldNames.Contains(visitedField))
                {
                    _fieldNames.Add(visitedField);
                }
                return(base.VisitSimplePredicate(simplePredicate));
            }
Exemplo n.º 14
0
 public override SnQueryPredicate VisitSimplePredicate(SimplePredicate simplePredicate)
 {
     if (!SharingRelatedFieldNames.Contains(simplePredicate.FieldName))
     {
         _isSharingStack.Push(false);
         return(simplePredicate);
     }
     _isSharingStack.Push(true);
     return(SharingComposerVisitor.CreateSharingSimplePredicate(simplePredicate.Value));
 }
Exemplo n.º 15
0
        protected override IQuery <Currency> ApplyWhereClause(IQuery <Currency> query, CurrencyFilterDto filter)
        {
            if (filter.CodeName.IsNullOrEmpty())
            {
                return(query);
            }

            var predicate = new SimplePredicate(nameof(Currency.Code), ValueComparingOperator.Equal, filter.CodeName);

            return(query.Where(predicate));
        }
Exemplo n.º 16
0
        public override SnQueryPredicate VisitSimplePredicate(SimplePredicate simplePredicate)
        {
            var query = CreateStringValueQuery(simplePredicate.FieldName, simplePredicate.Value, simplePredicate.FuzzyValue);

            if (simplePredicate.Boost.HasValue)
            {
                query.SetBoost(Convert.ToSingle(simplePredicate.Boost.Value));
            }
            _queryTree.Push(query);
            return(simplePredicate);
        }
Exemplo n.º 17
0
 /// <summary>
 /// Advance the XmlReader until the given predicate is satisfied
 /// </summary>
 /// <exception cref="IOException">Unable to read from source stream</exception>
 /// <exception cref="InvalidDataException">Source document is corrupt</exception>
 private static void ReadUntil(XmlReader reader, SimplePredicate predicate, string eofMessage)
 {
     while (!predicate())
     {
         if (reader.EOF)
         {
             throw new InvalidDataException("Invalid XML: " + eofMessage);
         }
         reader.Read();
     }
 }
Exemplo n.º 18
0
        public async Task ApplyWhereClause_SimpleFilterWithMinimalPrice_ReturnsCorrectSimplePredicate()
        {
            var mockManager        = new QueryMockManager();
            var expectedPredicate  = new SimplePredicate(nameof(Song.Price), ValueComparingOperator.GreaterThanOrEqual, 1000m);
            var mapperMock         = mockManager.ConfigureMapperMock <Song, ProductDto, ProductFilterDto>();
            var queryMock          = mockManager.ConfigureQueryMock <Song>();
            var productQueryObject = new ProductQueryObject(mapperMock.Object, queryMock.Object);

            var unused = await productQueryObject.ExecuteQuery(new ProductFilterDto { MinimalPrice = 1000 });

            Assert.AreEqual(mockManager.CapturedPredicate, expectedPredicate);
        }
Exemplo n.º 19
0
 private static string GetWhereCondition(SimplePredicate simplePredicate)
 {
     if (!BinaryOperations.ContainsKey(simplePredicate.ValueComparingOperator))
     {
         throw new InvalidOperationException($"Transformation of value comparing operator: {simplePredicate.ValueComparingOperator} to where condition is not supported!");
     }
     if (simplePredicate.ComparedValue == null)
     {
         return(GetNullWhereCondition(simplePredicate));
     }
     return(GetEscapedWhereCondition(simplePredicate));
 }
Exemplo n.º 20
0
        public async Task AppleyWhereClause_FilterByDisesaeName_ReturnsCorrectCount()
        {
            var diseaseQuery = Initializer.Container.Resolve <IQuery <Disease> >();
            var predicate    = new SimplePredicate(nameof(Disease.Name), ValueComparingOperator.Equal, "Death");
            QueryResult <Disease> actualQueryResult;

            using (unitOfWorkProvider.Create())
            {
                actualQueryResult = await diseaseQuery.Where(predicate).ExecuteAsync();
            }
            Assert.IsTrue(actualQueryResult.Items.Count == 1);
        }
Exemplo n.º 21
0
        public async Task ExecuteAsync_SimpleWherePredicate_ReturnsCorrectQueryResult()
        {
            QueryResult <Product> actualQueryResult;
            var productQuery        = Initializer.Container.Resolve <IQuery <Product> >();
            var expectedQueryResult = new QueryResult <Product>(new List <Product>
            {
                new Product
                {
                    Id          = samsungGalaxyJ7Id,
                    CategoryId  = androidCategoryId,
                    Description =
                        "Designed with all the features you love, the Samsung Galaxy J7 is the smartphone you’ve been waiting for. Watching a movie or reading a book is more enjoyable on the large 5.5 HD Super AMOLED display.And while the 13MP main camera captures clearer photos, the 5MP front camera gives you more flattering selfies in any light. Best of all, the long-lasting battery means the Samsung Galaxy J7 keeps up with you.",
                    DiscountPercentage = 5,
                    DiscountType       = DiscountType.Percentage,
                    Name          = "Samsung Galaxy J7",
                    Price         = 7490,
                    ProductImgUri = @"\Content\Images\Products\samsung_galaxy_J7.jpeg"
                },
                new Product
                {
                    Id          = lgG5Id,
                    CategoryId  = androidCategoryId,
                    Description =
                        "LG G5 comes with a 5.30-inch touchscreen display with a resolution of 1440 pixels by 2560 pixels at a PPI of 554 pixels per inch. The LG G5 is powered by 2.15GHz quad - core Qualcomm Snapdragon 820 processor and it comes with 4GB of RAM.The phone packs 32GB of internal storage that can be expanded up to 200GB via a microSD card.As far as the cameras are concerned, the LG G5 packs a 16-megapixel primary camera on the rear and a 8-megapixel front shooter for selfies. The LG G5 runs Android 6.0.1 and is powered by a 2800mAh removable battery.It measures 149.40 x 73.90 x 7.70 (height x width x thickness) and weighs 159.00 grams. The LG G5 is a dual SIM(GSM and GSM) smartphone that accepts two Nano-SIM.Connectivity options include Wi-Fi, GPS, Bluetooth, NFC, FM, 3G, 4G(with support for Band 40 used by some LTE networks in India). Sensors on the phone include Proximity sensor, Ambient light sensor, Accelerometer, and Gyroscope.",
                    DiscountPercentage = 0,
                    DiscountType       = DiscountType.Percentage,
                    Name          = "LG G5",
                    Price         = 15490,
                    ProductImgUri = @"\Content\Images\Products\LG-G5.jpg"
                },
                new Product
                {
                    Id          = htc10Id,
                    CategoryId  = androidCategoryId,
                    Description =
                        "HTC 10 smartphone was launched in April 2016. The phone comes with a 5.20-inch touchscreen display with a resolution of 1440 pixels by 2560 pixels at a PPI of 564 pixels per inch. The HTC 10 is powered by 1.6GHz quad - core Qualcomm Snapdragon 820 processor and it comes with 4GB of RAM.The phone packs 32GB of internal storage that can be expanded up to 2000GB via a microSD card. As far as the cameras are concerned, the HTC 10 packs a 12-Ultrapixel primary camera on the rear and a 5-megapixel front shooter for selfies.The HTC 10 runs Android 6 and is powered by a 3000mAh non removable battery.It measures 145.90 x 71.90 x 9.00 (height x width x thickness) and weighs 161.00 grams. The HTC 10 is a single SIM(GSM) smartphone that accepts a Nano-SIM.Connectivity options include Wi-Fi, GPS, Bluetooth, NFC, 4G(with support for Band 40 used by some LTE networks in India). Sensors on the phone include Proximity sensor, Ambient light sensor, Accelerometer, and Gyroscope. ",
                    DiscountPercentage = 0,
                    DiscountType       = DiscountType.Percentage,
                    Name          = "HTC 10",
                    Price         = 21990,
                    ProductImgUri = @"\Content\Images\Products\HTC_10.jpg"
                }
            }, 3);

            var predicate = new SimplePredicate(nameof(Product.CategoryId), ValueComparingOperator.Equal, androidCategoryId);

            using (unitOfWorkProvider.Create())
            {
                actualQueryResult = await productQuery.Where(predicate).ExecuteAsync();
            }

            Assert.AreEqual(actualQueryResult, expectedQueryResult);
        }
Exemplo n.º 22
0
 private static string GetEscapedWhereCondition(SimplePredicate simplePredicate)
 {/*
   * const string atChar = "@";
   * if (simplePredicate.ComparedValue is string value && value.Contains(atChar))
   * {
   *     string escapedValue = value.Insert(value.IndexOf(atChar, StringComparison.Ordinal), atChar);
   *     return simplePredicate.TargetPropertyName + BinaryOperations[simplePredicate.ValueComparingOperator]
   *                .Invoke(escapedValue);
   * }*/
     return(simplePredicate.TargetPropertyName + BinaryOperations[simplePredicate.ValueComparingOperator]
            .Invoke(ConvertOperandToString(simplePredicate.ComparedValue)));
 }
Exemplo n.º 23
0
        public async Task ApplyWhereClause_ComplexFilterByAllParameters_ReturnsCorrectPredicate()
        {
            const int      filteredUserAge           = 30;
            Gender         filteredGenderRestriction = Gender.Female;
            PostVisibility filteredPostVisibility    = PostVisibility.FriendsOnly;
            Guid           filteredUserId            = Guid.NewGuid();
            var            mockManager = new QueryMockManager();

            var agePredicates = new List <IPredicate>
            {
                new SimplePredicate(nameof(Post.HasAgeRestriction), ValueComparingOperator.Equal, false)
            };
            var innerAgePredicates = new List <IPredicate>
            {
                new SimplePredicate(nameof(Post.AgeRestrictionTo), ValueComparingOperator.LessThanOrEqual, filteredUserAge),
                new SimplePredicate(nameof(Post.AgeRestrictionFrom), ValueComparingOperator.GreaterThanOrEqual, filteredUserAge)
            };

            agePredicates.Add(new CompositePredicate(innerAgePredicates, LogicalOperator.AND));

            var genderPredicate = new CompositePredicate(
                new List <IPredicate>
            {
                new SimplePredicate(nameof(Post.GenderRestriction), ValueComparingOperator.Equal, filteredGenderRestriction),
                new SimplePredicate(nameof(Post.GenderRestriction), ValueComparingOperator.Equal, Gender.NoInformation)
            }, LogicalOperator.OR);

            var userIdPredicate = new SimplePredicate(nameof(Post.UserId), ValueComparingOperator.Equal, filteredUserId);

            var visibilityPredicate = new SimplePredicate(nameof(Post.Visibility), ValueComparingOperator.Equal, filteredPostVisibility);

            var expectedPredicate = new CompositePredicate(
                new List <IPredicate>
            {
                new CompositePredicate(agePredicates, LogicalOperator.OR),
                genderPredicate,
                userIdPredicate,
                visibilityPredicate
            }, LogicalOperator.AND);
            var mapperMock      = mockManager.ConfigureMapperMock <Post, PostDto, PostFilterDto>();
            var queryMock       = mockManager.ConfigureQueryMock <Post>();
            var postQueryObject = new PostQueryObject(mapperMock.Object, queryMock.Object);

            var filter = new PostFilterDto
            {
                UserAge           = filteredUserAge,
                GenderRestriction = filteredGenderRestriction,
                UserId            = filteredUserId
            };
            var temp = await postQueryObject.ExecuteQuery(filter);

            Assert.AreEqual(expectedPredicate, mockManager.CapturedPredicate);
        }
Exemplo n.º 24
0
        public async Task ExecuteUserQuery()
        {
            var mockManager        = new QueryMockManager();
            var expectedPredicate  = new SimplePredicate(nameof(User.NickName), ValueComparingOperator.StringContains, "Misko");
            var mapperMock         = mockManager.ConfigureMapperMock <User, UserDto, UserFilterDto>();
            var queryMock          = mockManager.ConfigureQueryMock <User>();
            var productQueryObject = new UserQueryObject(mapperMock.Object, queryMock.Object);

            var unused = await productQueryObject.ExecuteQuery(new UserFilterDto { SubName = "Misko" });

            Assert.AreEqual(mockManager.CapturedPredicate, expectedPredicate);
        }
Exemplo n.º 25
0
        private static string GetEscapedWhereCondition(SimplePredicate simplePredicate)
        {
            const string atChar = "@";

            if (simplePredicate.ComparedValue is string value && value.Contains(atChar))
            {
                var escapedValue = (object)value.Insert(value.IndexOf(atChar, StringComparison.Ordinal), atChar);
                return(simplePredicate.TargetPropertyName + BinaryOperations[simplePredicate.ValueComparingOperator]
                       .Invoke(escapedValue));
            }
            return(simplePredicate.TargetPropertyName + BinaryOperations[simplePredicate.ValueComparingOperator]
                   .Invoke(simplePredicate.ComparedValue));
        }
Exemplo n.º 26
0
        public async Task ApplyWhereClause_SimpleFilterWithSpecialization_ReturnsCorrectSimplePredicate()
        {
            var mockManager       = new QueryMockManager();
            var expectedPredicate = new SimplePredicate(nameof(Doctor.Specialization), ValueComparingOperator.Equal, Specialization.Ophthalmologist);

            var mapperMock        = mockManager.ConfigureMapperMock <Doctor, DoctorDto, DoctorFilterDto>();
            var queryMock         = mockManager.ConfigureQueryMock <Doctor>();
            var doctorQueryObject = new DoctorQueryObject(mapperMock.Object, queryMock.Object);

            var unused = await doctorQueryObject.ExecuteQuery(new DoctorFilterDto { Specialization = "Ophthalmologist" });

            Assert.AreEqual(mockManager.CapturedPredicate, expectedPredicate);
        }
        public async Task ApplyWhereClause_SimpleFilterWithIdentificationNumber_ReturnsCorrectSimplePredicate()
        {
            var mockManager       = new QueryMockManager();
            var expectedPredicate = new SimplePredicate(nameof(Patient.IdentificationNumber), ValueComparingOperator.Equal, "999999");

            var mapperMock         = mockManager.ConfigureMapperMock <Patient, PatientDto, PatientFilterDto>();
            var queryMock          = mockManager.ConfigureQueryMock <Patient>();
            var patientQueryObject = new PatientQueryObject(mapperMock.Object, queryMock.Object);

            var unused = await patientQueryObject.ExecuteQuery(new PatientFilterDto { IdentificationNumber = "999999" });

            Assert.AreEqual(mockManager.CapturedPredicate, expectedPredicate);
        }
Exemplo n.º 28
0
        public async Task ApplyWhereClause_SimpleFilterWithName_ReturnsCorrectSimplePredicate()
        {
            var mockManager       = new QueryMockManager();
            var expectedPredicate = new SimplePredicate(nameof(Disease.Name), ValueComparingOperator.StringContains, "Death");

            var mapperMock         = mockManager.ConfigureMapperMock <Disease, DiseaseDto, DiseaseFilterDto>();
            var queryMock          = mockManager.ConfigureQueryMock <Disease>();
            var diseaseQueryObject = new DiseaseQueryObject(mapperMock.Object, queryMock.Object);

            var unused = await diseaseQueryObject.ExecuteQuery(new DiseaseFilterDto { DiseaseName = "Death" });

            Assert.AreEqual(mockManager.CapturedPredicate, expectedPredicate);
        }
        public async Task ApplyWhereClause_FilterByUsername_ReturnsCorrectSimplePredicate()
        {
            Guid filteredPostId       = Guid.NewGuid();
            var  mockManager          = new QueryMockManager();
            var  expectedPredicate    = new SimplePredicate(nameof(PostReply.PostId), ValueComparingOperator.Equal, filteredPostId);
            var  mapperMock           = mockManager.ConfigureMapperMock <PostReply, PostReplyDto, PostReplyFilterDto>();
            var  queryMock            = mockManager.ConfigureQueryMock <PostReply>();
            var  postReplyQueryObject = new PostReplyQueryObject(mapperMock.Object, queryMock.Object);

            var filter = new PostReplyFilterDto {
                PostId = filteredPostId
            };
            var temp = await postReplyQueryObject.ExecuteQuery(filter);

            Assert.AreEqual(expectedPredicate, mockManager.CapturedPredicate);
        }
        public async Task ApplyWhereClause_FilterByImageId_ReturnsCorrectSimplePredicate()
        {
            Guid filterImageId     = Guid.NewGuid();
            var  mockManager       = new QueryMockManager();
            var  expectedPredicate = new SimplePredicate(nameof(Vote.ImageId), ValueComparingOperator.Equal, filterImageId);
            var  mapperMock        = mockManager.ConfigureMapperMock <Vote, VoteDto, VoteFilterDto>();
            var  queryMock         = mockManager.ConfigureQueryMock <Vote>();
            var  voteQueryObject   = new VoteQueryObject(mapperMock.Object, queryMock.Object);

            var filter = new VoteFilterDto {
                ImageId = filterImageId
            };
            var temp = await voteQueryObject.ExecuteQuery(filter);

            Assert.AreEqual(expectedPredicate, mockManager.CapturedPredicate);
        }