java—如何对数据集上的查询建模?

mcvgt66p  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(231)

我在想一种在数据集上建立查询模型的方法。我使用lombok来减少样板代码。
我代表的第一个元素是运算符:

@Getter
@RequiredArgsConstructor
public enum Operator {
    EQUAL("="),
    NOT_EQUAL("!="),
    BELONGING("∈"),
    NOT_BELONGING("∉"),
    GREATER_THAN(">"),
    GREATER_THAN_EQUAL("≥"),
    LESS_THAN("<"),
    LESS_THAN_EQUAL("≤");

    private final String value;
}

下一个元素是 predicate ,表示我们在某个列上查找的值:

@Data
@AllArgsConstructor
public class Predicate {
    private String key;
    private Operator operator;
    private Object value;
}

到目前为止,我能够建模的查询对象只允许我使用and、or条件执行查询:

@Data
public class Query {
    private Map<String, PredicateDTO> predicates = new HashMap<>();

    public Query(Predicate... predicates) {
        for (Predicate predicate : predicates) {
            this.predicates.put(predicate.getKey(), predicate);
        }
    }
}

e、 g.考虑对汽车数据集的查询:

Predicate makePredicate = new Predicate("make", Operator.EQUAL, "Ford");
Predicate yearPredicate = new Predicate("year", Operator.GREATER_THAN_EQUAL, 2018);
Predicate pricePredicate = new Predicate("price", Operator.LESS_THAN_EQUAL, 10000);

QueryDTO query = new Query(makePredicate, yearPredicate, pricePredicate);

我现在能够表示的唯一查询(互斥)是:
SELECT * FROM cars WHERE cars.make="ford" AND cars.year>=2018 AND cars.price<=10000; SELECT * FROM cars WHERE cars.make="ford" OR cars.year>=2018 OR cars.price<=10000; 我所缺少的和正在寻找的是一种允许用 Query 类以及可能的其他 predicate 组合:
SELECT * FROM cars WHERE (cars.make="ford" AND cars.year>=2018) OR cars.price<=10000; SELECT * FROM cars WHERE cars.make="ford" OR (cars.year>=2018 AND cars.price<=10000); 如何对查询类建模以表示此类查询?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题