influxdb-java icon indicating copy to clipboard operation
influxdb-java copied to clipboard

How to use andNested () to generate SQL composed of multiple brackets

Open changechenhao opened this issue 5 years ago • 1 comments

I want to get a statement like this:

SELECT * FROM flow WHERE ((ipValue <= 100 AND ipValue >= 200) OR (ipValue <= 300 AND ipValue >= 400)) AND value < 200

But I found that the api can only be written like this:

WhereQueryImpl<SelectQueryImpl> query = select()
                .from("flow", getFullyMeasurement(param.getRp(), param.getTableName()))
                .where();

        query.andNested().and(lte("ipValue", 100))
                .and(gte("ipValue", 200)).close();
        query.orNested().and(lte("ipValue", 300))
                .and(gte("ipValue", 400)).close();
        query.and(lt("value", 200));

result

SELECT * FROM flow WHERE (ipValue <= 100 AND ipValue >= 200) OR (ipValue <= 300 AND ipValue >= 400) AND value < 200

How can I generate this form of sql:

((ipValue <= 100 AND ipValue >= 200) OR (ipValue <= 300 AND ipValue >= 400))

changechenhao avatar Jun 09 '20 03:06 changechenhao

Hi @changechenhao,

You could do it by using https://github.com/influxdata/influxdb-java/blob/01979bfa19d6ee2c05445ca2fbb201eda74593e7/src/main/java/org/influxdb/querybuilder/clauses/NestedClause.java#L7

Check this code:

Query select =
    select()
            .from(DATABASE, "flow")
            .where()
            .andNested()
            .or(new NestedClause(Lists.newArrayList(new AndConjunction(lte("ipValue", 100)), new AndConjunction(gte("ipValue", 200)))))
            .or(new NestedClause(Lists.newArrayList(new AndConjunction(lte("ipValue", 300)), new AndConjunction(gte("ipValue", 400)))))
            .close()
            .and(lt("value", 200));

System.out.println("select.getCommand() = " + select.getCommand());

Regards

bednar avatar Jul 01 '20 07:07 bednar