com.alibaba.druid.sql.ast.expr.SQLCaseExpr Java Examples

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLCaseExpr. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ItemFuncCase.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLCaseExpr caseExpr = new SQLCaseExpr();
    List<SQLExpr> exprList = toExpressionList(args);
    for (int index = 0; index < ncases; ) {
        SQLExpr exprCond = exprList.get(index++);
        SQLExpr exprValue = exprList.get(index++);

        SQLCaseExpr.Item item = new SQLCaseExpr.Item(exprCond, exprValue);
        caseExpr.addItem(item);
    }
    if (firstExprNum > 0) {
        caseExpr.setValueExpr(exprList.get(firstExprNum));
    }
    if (elseExprNum > 0) {
        caseExpr.setElseExpr(exprList.get(elseExprNum));
    }
    return caseExpr;
}
 
Example #2
Source File: CaseWhenParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public String parse() throws SqlParseException {
    List<String> result = new ArrayList<String>();

    for (SQLCaseExpr.Item item : caseExpr.getItems()) {
        SQLExpr conditionExpr = item.getConditionExpr();

        WhereParser parser = new WhereParser(new SqlParser(), conditionExpr);
        /*
        zhongshu-comment 将case when的各种条件判断转换为script的if-else判断,举例如下
        case when:
            CASE
      WHEN platform_id = 'PC' AND os NOT IN ('全部') THEN 'unknown'
      ELSE os

   script的if-else:
      将上文case when例子中的WHEN platform_id = 'PC' AND os NOT IN ('全部') THEN 'unknown' 解析成如下的script:
      (doc['platform_id'].value=='PC') && (doc['os'].value != '全部' )
         */
        String scriptCode = explain(parser.findWhere());
        if (scriptCode.startsWith(" &&")) {
            scriptCode = scriptCode.substring(3);
        }
        if (result.size() == 0) {
            result.add("if(" + scriptCode + ")" + "{" + Util.getScriptValueWithQuote(item.getValueExpr(), "'") + "}");
        } else {
            result.add("else if(" + scriptCode + ")" + "{" + Util.getScriptValueWithQuote(item.getValueExpr(), "'") + "}");
        }

    }
    SQLExpr elseExpr = caseExpr.getElseExpr();
    if (elseExpr == null) {
        result.add("else { null }");
    } else {
        result.add("else {" + Util.getScriptValueWithQuote(elseExpr, "'") + "}");
    }

    return Joiner.on(" ").join(result);
}
 
Example #3
Source File: SqlParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private ScriptSortBuilder.ScriptSortType judgeIsStringSort(SQLExpr expr) {
    if (expr instanceof SQLCaseExpr) {
        List<SQLCaseExpr.Item> itemList = ((SQLCaseExpr) expr).getItems();
        for (SQLCaseExpr.Item item : itemList) {
            if (item.getValueExpr() instanceof SQLCharExpr) {
                return ScriptSortBuilder.ScriptSortType.STRING;
            }
        }
    }
    return ScriptSortBuilder.ScriptSortType.NUMBER;
}
 
Example #4
Source File: CaseWhenParser.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public CaseWhenParser(SQLCaseExpr caseExpr, String alias, String tableAlias) {
    this.alias = alias;
    this.tableAlias = tableAlias;
    this.caseExpr = caseExpr;

}
 
Example #5
Source File: CaseWhenParser.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
/**
 * zhongshu-comment
 * 1、该方法的作用:将在where子句中的case when解析为es script
 * 2、该种情况的es script和select、group by、order by等子句中的case when的es script不一样,
 *      因为在where子句中script的返回值是布尔类型,所以script中需要有个布尔判断,
 *      而其他情况的script返回值就是转换后的值,该值一般是字符串、数值
 * @author zhongshu
 * @return
 * @throws SqlParseException
 */
public String parseCaseWhenInWhere(Object[] valueArr) throws SqlParseException {
    List<String> result = new ArrayList<String>();
    String TMP = "tmp";
    result.add("String " + TMP + " = '';");

    for (SQLCaseExpr.Item item : caseExpr.getItems()) {
        SQLExpr conditionExpr = item.getConditionExpr();

        WhereParser parser = new WhereParser(new SqlParser(), conditionExpr);

        String scriptCode = explain(parser.findWhere());
        if (scriptCode.startsWith(" &&")) {
            scriptCode = scriptCode.substring(3);
        }
        if (result.size() == 1) { //zhongshu-comment 在for循环之前就已经先add了一个元素
            result.add("if(" + scriptCode + ")" + "{" + TMP + "=" + Util.getScriptValueWithQuote(item.getValueExpr(), "'") + "}");
        } else {
            result.add("else if(" + scriptCode + ")" + "{" + TMP + "=" + Util.getScriptValueWithQuote(item.getValueExpr(), "'") + "}");
        }

    }
    SQLExpr elseExpr = caseExpr.getElseExpr();
    if (elseExpr == null) {
        result.add("else { null }");
    } else {
        result.add("else {" + TMP + "=" + Util.getScriptValueWithQuote(elseExpr, "'") + "}");
    }

    /*
    zhongshu-comment
    1、第一种情况in
    field in (a, b, c)     --> field == a || field == b || field == c

    2、第二种情况not in
    field not in (a, b, c) --> field != a && field != b && field != c
                     等价于 --> !(field == a || field == b || field == c) 即对第一种情况取反,
                                (field == a || field == b || field == c)里的a、b、c要全部为false,!(field == a || field == b || field == c)才为true

    3、这里只拼接第一种情况,不拼接第一种情况,
        如果要需要第二种情况,那就调用该方法得到返回值后自行拼上取反符号和括号: !(${该方法的返回值})
     */
    String judgeStatement = parseInNotInJudge(valueArr, TMP, "==", "||", true);
    result.add("return " + judgeStatement +  ";");
    return Joiner.on(" ").join(result);
}