JOOQ动态条件查询

时间:2018-04-25 00:13:18

标签: java sql spring jooq

大家好我试着制作一个方法来应用条件一个SelectQuery,但是我不知道hot是按名称获取字段还是按名称输入表格,代码示例......

过滤条件的对象是一个数组,如下所示:

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

查询构建器方法如下所示:

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);
}

addQueryConditions看起来像:

@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")));
        }
    }
}

但该字段始终为空...

我怎么能解决这个问题。

2 个答案:

答案 0 :(得分:1)

各种TableLike.field(String)方法区分大小写。您的JSON结构发送此输入

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

然而,您的专栏已重命名为小写id

branch.ID.as("id")

答案 1 :(得分:1)

我用这个解决了......

function isp_gallery_item( $atts ) {
    // Attributes
    $a = shortcode_atts( array(
        'id' => '',
    ), $atts );

    return sprintf( '<h3>%s</h3>', get_the_title( $a['id'] ) );
}
add_shortcode( 'ispimg', 'isp_gallery_item' );

元素过滤器是一个ubjects数组..

    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)));
            }
        }
    }
}

在字段中我放置了表格和字段[{"field":"person.name","condition":"like","value":"Carlos"}]