package com.ejlchina.searcher;

import com.ejlchina.searcher.util.MapUtils;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestCase1 {

    public static class SearchBean {
        private long id;
        private String name;
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    @Test
    public void test1() {
        SqlExecutor sqlExecutor = new SqlExecutor() {
            @Override
            public <T> SqlResult<T> execute(SearchSql<T> searchSql) {
                Assert.assertEquals("select name c_1, id c_0 from search_bean limit ?, ?", searchSql.getListSqlString());
                List<Object> listParams = searchSql.getListSqlParams();
                Assert.assertEquals(2, listParams.size());
                Assert.assertEquals(0L, listParams.get(0));
                Assert.assertEquals(15, listParams.get(1));
                Assert.assertEquals("select count(*) s_count from search_bean", searchSql.getClusterSqlString());
                Assert.assertEquals(0, searchSql.getClusterSqlParams().size());
                Assert.assertEquals("s_count", searchSql.getCountAlias());
                Assert.assertEquals(0, searchSql.getSummaryAliases().size());
                return new SqlResult<>(searchSql);
            }
        };

        MapSearcher mapSearcher = SearcherBuilder.mapSearcher().sqlExecutor(sqlExecutor).build();
        mapSearcher.search(SearchBean.class, new HashMap<>());
        mapSearcher.search(SearchBean.class, null);

        BeanSearcher beanSearcher = SearcherBuilder.beanSearcher().sqlExecutor(sqlExecutor).build();
        beanSearcher.search(SearchBean.class, new HashMap<>());
        beanSearcher.search(SearchBean.class, null);
    }

    @Test
    public void test2() {
        SqlExecutor sqlExecutor = new SqlExecutor() {
            @Override
            public <T> SqlResult<T> execute(SearchSql<T> searchSql) {
                System.out.println(searchSql.getListSqlString());
                System.out.println(searchSql.getListSqlParams());

                Assert.assertEquals("select name c_1, id c_0 from search_bean where (id = ?) limit ?, ?", searchSql.getListSqlString());
                List<Object> listParams = searchSql.getListSqlParams();
                Assert.assertEquals(3, listParams.size());
                Assert.assertEquals(1, listParams.get(0));
                Assert.assertEquals(0L, listParams.get(1));
                Assert.assertEquals(15, listParams.get(2));

                System.out.println(searchSql.getClusterSqlString());
                Assert.assertEquals("select count(*) s_count from search_bean where (id = ?)", searchSql.getClusterSqlString());

                List<Object> clusterSqlParams = searchSql.getClusterSqlParams();
                System.out.println(clusterSqlParams);
                Assert.assertEquals(1, clusterSqlParams.size());
                Assert.assertEquals(1, clusterSqlParams.get(0));

                System.out.println(searchSql.getCountAlias());

                Assert.assertEquals("s_count", searchSql.getCountAlias());

                System.out.println(searchSql.getSummaryAliases());

                Assert.assertEquals(0, searchSql.getSummaryAliases().size());
                return new SqlResult<>(searchSql);
            }
        };
        MapSearcher mapSearcher = SearcherBuilder.mapSearcher().sqlExecutor(sqlExecutor).build();
        BeanSearcher beanSearcher = SearcherBuilder.beanSearcher().sqlExecutor(sqlExecutor).build();

        Map<String, Object> params1 = new HashMap<>();
        params1.put("id", 1);
        Map<String, Object> params2 = MapUtils.builder().field(SearchBean::getId, 1).build();

        mapSearcher.search(SearchBean.class, params1);
        mapSearcher.search(SearchBean.class, params2);

        beanSearcher.search(SearchBean.class, params1);
        beanSearcher.search(SearchBean.class, params2);
    }


}

@@ -4221,10 +4221,1653 @@
     %7D%0A%0A%0A
+    @Test%0A    public void test3() %7B%0A        SqlExecutor sqlExecutor = new SqlExecutor() %7B%0A            @Override%0A            public %3CT%3E SqlResult%3CT%3E execute(SearchSql%3CT%3E searchSql) %7B%0A                System.out.println(searchSql.getListSqlString());%0A                Assert.assertEquals(%22select name c_1, id c_0 from search_bean order by c_1 asc%22, searchSql.getListSqlString());%0A                List%3CObject%3E listParams = searchSql.getListSqlParams();%0A                Assert.assertEquals(0, listParams.size());%0A                Assert.assertNull(searchSql.getClusterSqlString());%0A                List%3CObject%3E clusterSqlParams = searchSql.getClusterSqlParams();%0A                Assert.assertEquals(0, clusterSqlParams.size());%0A                Assert.assertNull(searchSql.getCountAlias());%0A                Assert.assertEquals(0, searchSql.getSummaryAliases().size());%0A                return new SqlResult%3C%3E(searchSql);%0A            %7D%0A        %7D;%0A        MapSearcher mapSearcher = SearcherBuilder.mapSearcher().sqlExecutor(sqlExecutor).build();%0A        BeanSearcher beanSearcher = SearcherBuilder.beanSearcher().sqlExecutor(sqlExecutor).build();%0A%0A        Map%3CString, Object%3E params1 = new HashMap%3C%3E();%0A        params1.put(%22sort%22, %22name%22);%0A        params1.put(%22order%22, %22asc%22);%0A        Map%3CString, Object%3E params2 = MapUtils.builder()%0A                .orderBy(SearchBean::getName, %22asc%22)%0A                .build();%0A        mapSearcher.searchAll(SearchBean.class, params1);%0A        mapSearcher.searchAll(SearchBean.class, params2);%0A        beanSearcher.searchAll(SearchBean.class, params1);%0A        beanSearcher.searchAll(SearchBean.class, params2);%0A    %7D%0A%0A
 %7D%0A

add test case