Привет всем, я пытаюсь создать метод для применения условий SelectQuery, но я не знаю, как получить поля по имени или таблицу по имени, пример кода ...
Объект условия фильтра является массивом и выглядит следующим образом:
[{'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")));
}
}
}
но поле всегда обнуляется ...
как я могу это решить.