简体   繁体   中英

JOOQ dynamic conditions query

Hi everybody i try to make a method to apply conditions a SelectQuery, but i don't know hot get the fields by name or the table by name, the code example...

the filter condition's object is an array and looks like:

[{'field': 'ID', 'condition' : 'in', 'value' : ['f4019bc3-d06b-4673-87e7-75bab23ac85a','c13beeef-2893-4a04-9ebf-ecdc851475bc']}]

the query builder method looks like:

public List<Branch> find(String order, JSONArray filters, Integer limit, Integer pageNumber) {
    SelectQuery<Record6<String,Integer,String,String,String,String>> selectSentence = dsl.select(
            branch.ID.as("id"),
            branch.CODE.as("code"), 
            branch.NAME.as("name"), 
            enterprises.ID.as("enterprise.id"), 
            enterprises.NAME.as("enterprise.name"), 
            enterprises.NIT.as("enterprise.nit"))
            .from(branch)
            .leftOuterJoin(enterprises)
            .on(branch.ENTERPRISE.eq(enterprises.ID)).getQuery();
    if(order != null && order.startsWith("-")) {
            selectSentence.addOrderBy(Tables.BRANCH.field(order.substring(1)).desc());
    }else {
            selectSentence.addOrderBy(Tables.BRANCH.field(order).asc());
    }
    addQueryConditions(filters, selectSentence);
    selectSentence.addLimit(limit);
    selectSentence.addOffset((pageNumber - 1) * limit);
    return selectSentence.fetchInto(Branch.class);
}

And the addQueryConditions looks like:

@SuppressWarnings("unchecked")
private void addQueryConditions(JSONArray filters, SelectQuery<?> query) {
    for (int i = 0, size = filters.length(); i < size; i++){
        JSONObject filter = filters.getJSONObject(i);
        String filterCondition = filter.optString("condition");
        if(Comparator.EQUALS.toSQL().equals(filterCondition)) {
            Field<Object> field = (Field<Object>) query.field(filter.optString("field"));
            Comparator comparator = Comparator.EQUALS;
            query.addConditions(field.compare(comparator, filter.optString("value")));
        }else if(Comparator.GREATER.toSQL().equals(filterCondition)) {
            Field<Object> field = (Field<Object>) query.field(filter.optString("field"));
            Comparator comparator = Comparator.GREATER;
            query.addConditions(field.compare(comparator, filter.optString("value")));
        }else if(Comparator.GREATER_OR_EQUAL.toSQL().equals(filterCondition)) {
            Field<Object> field = (Field<Object>) query.field(filter.optString("field"));
            Comparator comparator = Comparator.GREATER_OR_EQUAL;
            query.addConditions(field.compare(comparator, filter.optString("value")));
        }else if(Comparator.IN.toSQL().equals(filterCondition)) {
            Field<Object> field = (Field<Object>) query.field(filter.optString("field"));
            Comparator comparator = Comparator.IN;
            query.addConditions(field.compare(comparator, filter.optString("value")));
        }else if(Comparator.LESS.toSQL().equals(filterCondition)) {
            Field<Object> field = (Field<Object>) query.field(filter.optString("field"));
            Comparator comparator = Comparator.LESS;
            query.addConditions(field.compare(comparator, filter.optString("value")));
        }else if(Comparator.LESS_OR_EQUAL.toSQL().equals(filterCondition)) {
            Field<Object> field = (Field<Object>) query.field(filter.optString("field"));
            Comparator comparator = Comparator.LESS_OR_EQUAL;
            query.addConditions(field.compare(comparator, filter.optString("value")));
        }else if(Comparator.LIKE.toSQL().equals(filterCondition)) {
            Field<Object> field = (Field<Object>) query.field(filter.optString("field"));
            Comparator comparator = Comparator.LIKE;
            query.addConditions(field.compare(comparator, filter.optString("value")));
        }
    }
}

but the field always come null...

how i can solve this.

The various TableLike.field(String) methods are case sensitive. Your JSON structure sends this input

[{'field': 'ID', ...}]

Yet your column is renamed to lower-case id :

branch.ID.as("id")

I solve with this...

    public static void addQueryConditions(String filter, SelectQuery<?> query) {
    JSONArray filterArray = new JSONArray(filter);
    for (int i = 0, size = filterArray.length(); i < size; i++){
        JSONObject filterObject = filterArray.getJSONObject(i);
        String filterCondition = filterObject.optString("condition");
        Field<Object> field = DSL.field(filterObject.optString("field"));
        if(field != null) {
            String fieldValue = filterObject.optString("value");
            if(Comparator.EQUALS.toSQL().equals(filterCondition)) {
                query.addConditions(field.eq(fieldValue));
            }else if(Comparator.GREATER.toSQL().equals(filterCondition)) {
                query.addConditions(field.gt(fieldValue));
            }else if(Comparator.GREATER_OR_EQUAL.toSQL().equals(filterCondition)) {
                query.addConditions(field.ge(fieldValue));
            }else if(Comparator.IN.toSQL().equals(filterCondition)) {
                List<String> listValues = new ArrayList<>();
                if(!Constants.EMPTY_STRING.equals(fieldValue)) {
                    JSONArray valuesJson = new JSONArray(fieldValue);
                    for(int pos = 0; pos < valuesJson.length(); pos++){
                        listValues.add(valuesJson.optString(pos));
                    }
                }
                query.addConditions(field.in(listValues));
            }else if(Comparator.LESS.toSQL().equals(filterCondition)) {
                query.addConditions(field.lt(fieldValue));
            }else if(Comparator.LESS_OR_EQUAL.toSQL().equals(filterCondition)) {
                query.addConditions(field.le(fieldValue));
            }else if(Comparator.LIKE.toSQL().equals(filterCondition)) {
                query.addConditions(field.like(Constants.PERCENT_SYMBOL.concat(fieldValue).concat(Constants.PERCENT_SYMBOL)));
            }
        }
    }
}

And the element filter is an array of ubjects..

[{"field":"person.name","condition":"like","value":"Carlos"}]

in the field i put the table and field person.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM