Couchbase N1qlQuery DSL:构建表达式时要使用的空但智能的表达式?

Couchbase N1qlQuery DSL:构建表达式时要使用的空但智能的表达式?,couchbase,querydsl,couchbase-java-api,Couchbase,Querydsl,Couchbase Java Api,我们从前端获取参数: 最终字符串货币-如果为NULL或空,或“全部”表示用户选择的其他所有货币。何处不需要列表 final Boolean isSFlag-如果为null,则如果列为true或flase,则这不会是where else的一部分 表示如果货币为空且isSFlag为空,则select语句将为: select col1, col2 from 'collecitonA' 如果当前值为“INR”,则 select col1, col2 from 'collecitonA' where c

我们从前端获取参数:

最终字符串货币-如果为NULL或空,或“全部”表示用户选择的其他所有货币。何处不需要列表

final Boolean isSFlag-如果为null,则如果列为true或flase,则这不会是where else的一部分

表示如果货币为空且isSFlag为空,则select语句将为:

select col1, col2 from 'collecitonA'
如果当前值为“INR”,则

select col1, col2 from 'collecitonA' where currenncy = 'INR'
如果“INRI和flag”为false,则:

select col1, col2 from 'collecitonA' where currenncy = 'INR' and flagS = false
构建此select的好方法是什么?我现在所拥有的:

    /** Create statement per params
 * ignore currency if null or ALL.
 * ignore isSFlag if null */
private Statement crt(final String currency, final Boolean isSFlag) {
    Statement statement = null;
    try {
        System.out.println("Cur :\"" + currency + "\", isSh " + isSFlag);
        Expression whr = createWhere(currency, isSFlag);
        //FromPath pp = select(path(i("c1"), "*"))
         //       .from(i("MutalFundData"));//.as("S") .where(whr);//can I add the where clause only if its non empty ?

        Expression slct = path(i("c1", "c2"));
        final String frm = "MutualFundData";
        if(whr == null){
            statement = select(slct).from(i(frm));
        }else{
            statement = select(slct).from(i(frm)).where(whr);
        }
        System.out.println(statement);

    } catch (Exception e) {
        System.out.println("err  " + e + ", isSh " + isSFlag);
    }
    return statement;
}

private Expression createWhere(final String currency, final Boolean isSFlag) {
    String currencyUpper = currency == null ? null : currency.toUpperCase();
    Expression where = x("1=1");//can i initialize this to some sort of empty expression o
    // Use apache fn for checking if empty
    //if its all or empty string ignore it - dont add to where clause
    if (currency != null && currency.length() > 0 && (!currency.equals("ALL"))) {
        where = where.and(x("CURRENCY")).eq(i(currencyUpper));
    }
    //for SFlag need 3 value from front end - whether all rows, or only where SFlag false or where true, use NULL or an int/ String to denote
    if (isSFlag != null) {
        where = where.and("isSFlag").eq(isSFlag);
    }
    return where;
}

 //not yet using. 
private Expression createWhere2(final String currency, final Boolean isSFlag) {
    String currencyUpper = currency == null ? null : currency.toUpperCase();
    Expression where = null;//can i initialize this to some sort of empty expression o
    // Use apache fn for checking if empty
    //if its all or empty string ignore it - dont add to where clause
    if (currency != null && currency.length() > 0 && (!currency.equals("ALL"))) {
        //where = chckAnd(where, x("CURRENCY")).eq(i(currencyUpper));
        where = x("CURRENCY").eq(i(currencyUpper);
    }
    //for SFlag need 3 value from front end - whether all rows, or only where SFlag false or where true, use NULL or an int/ String to denote
    if (isSFlag != null) {
        where = chckAnd(where, x("isSFlag").eq(isSFlag));
    }
    return where;
}

private Expression chckAnd(Expression a, Expression b){
    if(a == null)return b;
    return a.and(b);
}
这是回报

Cur:,isSh null 从
MutalFundData
中选择
c1
*,其中1=1

当前:“美元”,isSh为空 从
MutalFundData
中选择
c1
*,其中1=1,货币=
USD

Cur:“USD”,是真的吗 从
MutalFundData
中选择
c1
*,其中1=1,币种=
USD
和isSFlag=TRUE

有没有办法:

  • 仅在需要时追加到位置
  • 初始化where,但使其为空,如果不需要,则不写入和?需要一个旗子吗?这里使用函数chckAnd,但若第一个逻辑运算符是OR呢?我需要另一个助手。Couchbase库可以改进吗

  • 如果您有改进API的想法。但是,DSL API尚未迁移到Couchbase Java SDK的版本3——建议您自己构建N1QL字符串。但是,DSL API尚未迁移到Couchbase Java SDK的第3版——建议您自己构建N1QL字符串。