Using PathBuilder and Boolean Builder to create dynamic queries

73 views
Skip to first unread message

Frances Tso

unread,
Jun 8, 2016, 6:23:29 AM6/8/16
to Querydsl
Hi, 
I'm having some trouble understanding how the PathBuilder and Boolean Builder works. 

The problem I'm trying to solve is I'm given a hashmap<String, String> filter and a list sort. Filter contains keys which are the column names in a PostgreSQL and the values are what the entry should be equal to. Sort is a list of column names to sort in ascending order. I am trying to create a sql query so that it returns a list of entries where the filter's keys are equal to its values sorted by the columns in the list sort. 

My attempt is below based on what I have found on stack overflow and in the documents:

void getOut(HashMap<String, String> filter, List<String> sort)
{
    Set filterSet = filter.entrySet();
    Iterator filterI = filterSet.iterator();
    Set sortSet = sort.entrySet();
    Iterator sortI = sortSet.iterator();

    SQLTemplates templates = new PostgreSQLTemplates();
    Configuration config = new Configuration(templates);

    PathBuilder<Object> bnrPath = new PathBuilder<Object>(Object.class, "bnr");
    BooleanBuilder where = new BooleanBuilder();
    BooleanBuilder order = new BooleanBuilder(); 

    while (filterI.hasNext())
    {
        Map.Entry entry = (Map.Entry)filterI.next();
        StringPath key = bnrPath.getString((String)entry.getKey());
        where.and(key.eq((String)entry.getValue()));
    }

    while (sortI.hasNext())
    {
        StringPath key = bnrPath.getString(sortI.next());
        order.and(key.asc);
    }

    SQLQuery sql = new SQLQuery(config).from(bnrPath).where(where).orderBy(order);
}

Any help is appreciated. Thank you very much.
    
Reply all
Reply to author
Forward
0 new messages