简体   繁体   中英

Parse where clause query from Rest Get call

I am receiving a where clause from Rest API Get and I should convert properties, So I need to convert this string to java object with logical and .... for example :

String where = "prop = 1 and prop2 = 'ssdf' or date > 20121204";

Is there any java library that converts where clause to java object with separate condition and operator ?

Finally I found this library : https://github.com/JSQLParser/JSqlParser

Seems like very good for query parsing

Using JSqlParser one could use an already implemented utility method to parse conditions like in your where clause:

String where = "prop = 1 and prop2 = 'ssdf' or date > 20121204";
Expression expr = CCJSqlParserUtil.parseCondExpression(where);
System.out.println(expr);

This works with JSqlParser V0.9.x ( https://github.com/JSQLParser/JSqlParser )

Then you have an object tree with separated operators, ands, ors, values. This one you could traverse using the ExpressionVisitorAdapter, eg

ExpressionVisitorAdapter visitor = new ExpressionVisitorAdapter() {
    @Override
    public void visit(Column column) {
        System.out.println(column);
    }
};

expr.accept(visitor);

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