示例#1
0
        /// <summary>
        /// 生成组合筛选
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="queryFilter">查询过滤器实例</param>
        /// <param name="parameters">查询参数</param>
        /// <returns></returns>
        private string GenerateCombinedFilter <TEntity>(CombinedQueryFilter queryFilter, DynamicParameters parameters)
        {
            string spliter = queryFilter.Clause == BooleanClause.Or ? " OR " : " AND ";
            string left    = GenerateFilter <TEntity>(queryFilter.Filter1, parameters);
            string right   = GenerateFilter <TEntity>(queryFilter.Filter2, parameters);

            if (!right.IsNullOrWhiteSpace())
            {
                if (left.IsNullOrWhiteSpace())
                {
                    return(right);
                }
                return($"({left}) {spliter} ({right})");
            }
            return(left.IfNullOrWhiteSpace(string.Empty));
        }
示例#2
0
        public void CountFilterAsunc()
        {
            ClearDapperCompisiteKeyEntity();
            try
            {
                CreateDapperCompisiteKeyEntity(1, 1, "2", "2", "5", "5");
                CreateDapperCompisiteKeyEntity(1, 2, "2", "4", "5", "2");
                CreateDapperCompisiteKeyEntity(2, 2, "3", "4", "5", "2");
                CreateDapperCompisiteKeyEntity(3, 3, "3", "4", "5", "2");
                CreateDapperCompisiteKeyEntity(4, 3, "3", "4", "5", "2");

                var ins = _dapper;
                var qf  = new SingleQueryFilter()
                          .AddEqual(nameof(DapperCompisiteKeyEntity.products_id), 1)
                          .AddEqual(nameof(DapperCompisiteKeyEntity.language_id), 1);
                int count = ins.CountAsync(qf).GetAwaiter().GetResult();
                Assert.Equal(1, count);

                var qf2 = new SingleQueryFilter()
                          .AddEqual(nameof(DapperCompisiteKeyEntity.products_id), 1)
                          .AddEqual(nameof(DapperCompisiteKeyEntity.language_id), 2);

                var c1 = new CombinedQueryFilter(qf, qf2, BooleanClause.Or);
                count = ins.CountAsync(c1).GetAwaiter().GetResult();
                Assert.Equal(2, count);

                var qf3 = new SingleQueryFilter().AddEqual(nameof(DapperCompisiteKeyEntity.products_description), 3);
                count = ins.CountAsync(qf3).GetAwaiter().GetResult();
                Assert.Equal(3, count);


                var qf4 = new SingleQueryFilter().AddEqual(nameof(DapperCompisiteKeyEntity.products_name), 4);
                count = ins.CountAsync(qf4).GetAwaiter().GetResult();
                Assert.Equal(4, count);

                var qf5 = new SingleQueryFilter().AddEqual(nameof(DapperCompisiteKeyEntity.products_short_description), 5);
                count = ins.CountAsync(qf5).GetAwaiter().GetResult();
                Assert.Equal(5, count);
            }
            finally
            {
                ClearDapperCompisiteKeyEntity();
            }
        }
        private void CombinedFilterTest()
        {
            DapperSqlGenerator generator = this.MockGenerator();
            SingleQueryFilter  filter    = new SingleQueryFilter();

            filter.AddEqual("name", "evalue");
            filter.AddGreaterOrEqual("gte", 1);
            filter.AddGreater("gt", 0);
            filter.AddLess("lt", 10000);
            filter.AddLessOrEqual("lte", 999);
            filter.AddNotEqual("null", null);



            SingleQueryFilter filter2 = new SingleQueryFilter();

            filter2.AddGreaterOrEqual("gte2", 1);
            filter2.AddEqual("name2", "evalue");
            filter2.AddGreater("gt2", 0);
            filter2.AddLess("lt2", 10000);
            filter2.AddLessOrEqual("lte2", 999);
            filter2.AddNotEqual("null2", null);

            SingleQueryFilter filter3 = new SingleQueryFilter();

            filter3.AddEqual("name23", "evalue");
            filter3.AddGreaterOrEqual("gte3", 1);
            filter3.AddGreater("gt3", 0);
            filter3.AddLess("lt3", 10000);
            filter3.AddLessOrEqual("lte3", 999);
            filter3.AddEqual("null3", null);

            CombinedQueryFilter cf1 = new CombinedQueryFilter(filter2, filter3, BooleanClause.And);

            CombinedQueryFilter cf2 = new CombinedQueryFilter(filter, cf1, BooleanClause.Or);

            DynamicParameters parameters = new DynamicParameters();

            generator.GenerateFilter <DapperEntityWithNoBool>(cf2, parameters);

            Assert.Equal(15, parameters.ParameterNames.Count()); // null 不作为参数。
        }
示例#4
0
        private void Compare(CombinedQueryFilter s1, CombinedQueryFilter s2)
        {
            Assert.Equal(s1.Clause, s2.Clause);
            Assert.Equal(s1.Filter1 is SingleQueryFilter, s2.Filter1 is SingleQueryFilter);
            Assert.Equal(s1.Filter2 is SingleQueryFilter, s2.Filter2 is SingleQueryFilter);

            if (s1.Filter1 is SingleQueryFilter)
            {
                Compare((SingleQueryFilter)(s1.Filter1), (SingleQueryFilter)(s2.Filter1));
            }
            if (s1.Filter2 is SingleQueryFilter)
            {
                Compare((SingleQueryFilter)(s1.Filter2), (SingleQueryFilter)(s2.Filter2));
            }
            if (s1.Filter1 is CombinedQueryFilter)
            {
                Compare((CombinedQueryFilter)(s1.Filter1), (CombinedQueryFilter)(s2.Filter1));
            }
            if (s1.Filter2 is CombinedQueryFilter)
            {
                Compare((CombinedQueryFilter)(s1.Filter2), (CombinedQueryFilter)(s2.Filter2));
            }
        }
示例#5
0
        public void TestParse()
        {
            Expression <Func <User, bool> > lamda1 = u => u.Id == 1;
            Expression <Func <User, bool> > lamda2 = u => u.Id == 3 || u.Name == "3";
            Expression <Func <User, bool> > lamda3 = u => u.Id == 3 && u.Name == "3" && u.Id == 0;
            Expression <Func <User, bool> > lamda4 = u => (u.Id == 3 && u.Name == "3") || u.Id == 0;
            Expression <Func <User, bool> > lamda5 = u => (u.Id == 3 && u.Name == "3") || (u.Id == 0 && u.Name == "4");

            #region lamda1
            var query1           = LamdaQueryParser.Where <User>(lamda1) as SingleQueryFilter;
            SingleQueryFilter qf = new SingleQueryFilter();
            qf.AddEqual(nameof(User.Id), 1);
            Compare(query1, qf);
            #endregion

            #region lamda2
            var query2 = LamdaQueryParser.Where <User>(lamda2) as CombinedQueryFilter;

            SingleQueryFilter qf1 = new SingleQueryFilter();
            qf1.AddEqual(nameof(User.Id), 3);
            SingleQueryFilter qf2 = new SingleQueryFilter();
            qf2.AddEqual(nameof(User.Name), "3");


            CombinedQueryFilter cf1 = new CombinedQueryFilter(qf1, qf2, BooleanClause.Or);

            Compare(query2, cf1);
            #endregion

            #region lamda3
            var query3 = LamdaQueryParser.Where <User>(lamda3) as CombinedQueryFilter;

            SingleQueryFilter qf3 = new SingleQueryFilter();
            qf3.AddEqual(nameof(User.Id), 3);
            SingleQueryFilter qf4 = new SingleQueryFilter();
            qf4.AddEqual(nameof(User.Name), "3");

            CombinedQueryFilter cf2 = new CombinedQueryFilter(qf3, qf4, BooleanClause.And);

            SingleQueryFilter qf5 = new SingleQueryFilter();
            qf5.AddEqual(nameof(User.Id), 0);

            CombinedQueryFilter cf3 = new CombinedQueryFilter(cf2, qf5, BooleanClause.And);

            Compare(query3, cf3);

            #endregion

            #region lamda4
            var query4 = LamdaQueryParser.Where <User>(lamda4) as CombinedQueryFilter;

            SingleQueryFilter qf6 = new SingleQueryFilter();
            qf6.AddEqual(nameof(User.Id), 3);
            SingleQueryFilter qf7 = new SingleQueryFilter();
            qf7.AddEqual(nameof(User.Name), "3");

            CombinedQueryFilter cf4 = new CombinedQueryFilter(qf6, qf7, BooleanClause.And);

            SingleQueryFilter qf8 = new SingleQueryFilter();
            qf8.AddEqual(nameof(User.Id), 0);
            CombinedQueryFilter cf5 = new CombinedQueryFilter(cf2, qf5, BooleanClause.Or);
            Compare(query4, cf5);

            #endregion

            #region lamba5
            var query5 = LamdaQueryParser.Where <User>(lamda5) as CombinedQueryFilter;

            SingleQueryFilter sf1 = new SingleQueryFilter();
            sf1.AddEqual(nameof(User.Id), 3);
            SingleQueryFilter sf2 = new SingleQueryFilter();
            sf2.AddEqual(nameof(User.Name), "3");

            CombinedQueryFilter c1 = new CombinedQueryFilter(sf1, sf2, BooleanClause.And);

            SingleQueryFilter sf3 = new SingleQueryFilter();
            sf3.AddEqual(nameof(User.Id), 0);
            SingleQueryFilter sf4 = new SingleQueryFilter();
            sf4.AddEqual(nameof(User.Name), "4");

            CombinedQueryFilter c2 = new CombinedQueryFilter(sf3, sf4, BooleanClause.And);

            CombinedQueryFilter c3 = new CombinedQueryFilter(c1, c2, BooleanClause.Or);

            Compare(query5, c3);
            #endregion
        }