Java Code Examples for com.alibaba.druid.sql.ast.expr.SQLNullExpr

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLNullExpr. These examples are extracted from open source projects. 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 Project: Mycat2   Source File: JoinParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
private Object getExpValue(SQLExpr expr){
	if (expr instanceof SQLIntegerExpr){
		return ((SQLIntegerExpr)expr).getNumber().longValue();
	}
	if (expr instanceof SQLNumberExpr){
		return ((SQLNumberExpr)expr).getNumber().doubleValue();
	}		
	if (expr instanceof SQLCharExpr){
		String va=((SQLCharExpr)expr).toString();
		return va;//remove(va,'\'');
	}
	if (expr instanceof SQLBooleanExpr){			
		return ((SQLBooleanExpr)expr).getValue();
	}			
	if (expr instanceof SQLNullExpr){
		return null;
	}

	return expr;		
}
 
Example 2
Source Project: elasticsearch-sql   Source File: Util.java    License: Apache License 2.0 6 votes vote down vote up
public static Object getScriptValueWithQuote(SQLExpr expr, String quote) throws SqlParseException {
    if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) {
        return "doc['" + expr.toString() + "'].value";
    }  else if (expr instanceof SQLCharExpr) {
        return quote + ((SQLCharExpr) expr).getValue() + quote;
    } else if (expr instanceof SQLIntegerExpr) {
        return ((SQLIntegerExpr) expr).getValue();
    } else if (expr instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) expr).getNumber();
    } else if (expr instanceof SQLNullExpr) {
        return ((SQLNullExpr) expr).toString().toLowerCase();
    } else if (expr instanceof  SQLBinaryOpExpr) {
        //zhongshu-comment 该分支由忠树添加
        String left = "doc['" + ((SQLBinaryOpExpr) expr).getLeft().toString() + "'].value";
        String operator = ((SQLBinaryOpExpr) expr).getOperator().getName();
        String right = "doc['" + ((SQLBinaryOpExpr) expr).getRight().toString() + "'].value";
        return left + operator + right;
    }
    throw new SqlParseException("could not parse sqlBinaryOpExpr need to be identifier/valuable got " + expr.getClass().toString() + " with value:" + expr.toString());
}
 
Example 3
Source Project: dble   Source File: ShardingValuesHandler.java    License: GNU General Public License v2.0 5 votes vote down vote up
private Integer handleShardingColumn(DumpFileContext context, List<SQLExpr> values) throws SQLNonTransientException {
    AbstractPartitionAlgorithm algorithm = context.getTableConfig().getRule().getRuleAlgorithm();
    SQLExpr expr = values.get(context.getPartitionColumnIndex());
    String shardingValue = null;
    if (expr instanceof SQLIntegerExpr) {
        SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
        shardingValue = intExpr.getNumber() + "";
    } else if (expr instanceof SQLCharExpr) {
        SQLCharExpr charExpr = (SQLCharExpr) expr;
        shardingValue = charExpr.getText();
    }

    if (shardingValue == null && !(expr instanceof SQLNullExpr)) {
        throw new SQLNonTransientException("Not Supported of Sharding Value EXPR :" + values.toString());
    }

    Integer nodeIndex;
    try {
        nodeIndex = algorithm.calculate(shardingValue);
        // null means can't find any valid index
        if (nodeIndex == null || nodeIndex >= context.getTableConfig().getDataNodes().size()) {
            throw new SQLNonTransientException("can't find any valid datanode shardingValue" + values.toString());
        }
    } catch (Exception e) {
        throw new SQLNonTransientException("can't calculate valid datanode shardingValue" + values.toString() + ",due to " + e.getMessage());
    }
    return nodeIndex;
}
 
Example 4
Source Project: dble   Source File: ColumnRoute.java    License: GNU General Public License v2.0 5 votes vote down vote up
public ColumnRoute(IsValue value) {
    String stringValue = null;
    if (value.getValue() != null) {
        if (value.getValue() instanceof SQLNullExpr) {
            stringValue = "null";
        }
    }
    this.colValue = stringValue;
}
 
Example 5
static String shardingValueToSting(SQLExpr valueExpr) throws SQLNonTransientException {
    String shardingValue = null;
    if (valueExpr instanceof SQLIntegerExpr) {
        SQLIntegerExpr intExpr = (SQLIntegerExpr) valueExpr;
        shardingValue = intExpr.getNumber() + "";
    } else if (valueExpr instanceof SQLCharExpr) {
        SQLCharExpr charExpr = (SQLCharExpr) valueExpr;
        shardingValue = charExpr.getText();
    }

    if (shardingValue == null && !(valueExpr instanceof SQLNullExpr)) {
        throw new SQLNonTransientException("Not Supported of Sharding Value EXPR :" + valueExpr.toString());
    }
    return shardingValue;
}
 
Example 6
Source Project: elasticsearch-sql   Source File: CaseWhenParser.java    License: Apache License 2.0 5 votes vote down vote up
private void explainWhere(List<String> codes, Where where) throws SqlParseException {
    if (where instanceof Condition) {
        Condition condition = (Condition) where;

        if (condition.getValue() instanceof ScriptFilter) {
            codes.add(String.format("Function.identity().compose((o)->{%s}).apply(null)", ((ScriptFilter) condition.getValue()).getScript()));
        } else if (condition.getOpear() == OPEAR.BETWEEN) {
            Object[] objs = (Object[]) condition.getValue();
            codes.add("(" + "doc['" + condition.getName() + "'].value >= " + objs[0] + " && doc['"
                    + condition.getName() + "'].value <=" + objs[1] + ")");
        } else if (condition.getOpear() == OPEAR.IN) {// in
            //zhongshu-comment 增加该分支,可以解析case when判断语句中的in、not in判断语句
            codes.add(parseInNotInJudge(condition, "==", "||", false));
        } else if (condition.getOpear() == OPEAR.NIN) { // not in
            codes.add(parseInNotInJudge(condition, "!=", "&&", false));
        } else {
            SQLExpr nameExpr = condition.getNameExpr();
            SQLExpr valueExpr = condition.getValueExpr();
            if(valueExpr instanceof SQLNullExpr) {
                //zhongshu-comment 空值查询的意思吗?例如:查a字段没有值的那些文档,是这个意思吗
                codes.add("(" + "doc['" + nameExpr.toString() + "']" + ".empty)");
            } else {
                //zhongshu-comment 该分支示例:(doc['c'].value==1)
                codes.add("(" + Util.getScriptValueWithQuote(nameExpr, "'") + condition.getOpertatorSymbol() + Util.getScriptValueWithQuote(valueExpr, "'") + ")");
            }
        }
    } else {
        for (Where subWhere : where.getWheres()) {
            List<String> subCodes = new ArrayList<String>();
            explainWhere(subCodes, subWhere);
            String relation = subWhere.getConn().name().equals("AND") ? "&&" : "||";
            codes.add(Joiner.on(relation).join(subCodes));
        }
    }
}
 
Example 7
Source Project: elasticsearch-sql   Source File: WhereParser.java    License: Apache License 2.0 5 votes vote down vote up
private Object parseValue(SQLExpr expr) throws SqlParseException {
    if (expr instanceof SQLNumericLiteralExpr) {
        Number number = ((SQLNumericLiteralExpr) expr).getNumber();
        if(number instanceof BigDecimal){
            return number.doubleValue();
        }
        if(number instanceof BigInteger){
            return number.longValue();
        }
        return ((SQLNumericLiteralExpr) expr).getNumber();
    } else if (expr instanceof SQLCharExpr) {
        return ((SQLCharExpr) expr).getText();
    } else if (expr instanceof SQLMethodInvokeExpr) {
        return expr;
    } else if (expr instanceof SQLNullExpr) {
        return null;
    } else if (expr instanceof SQLIdentifierExpr) {
        return expr;
    } else if (expr instanceof SQLPropertyExpr) {
        return expr;
    } else {
        /*
        zhongshu-comment 解析where子查询时会抛出这样的异常:
        Failed to parse SqlExpression of type class com.alibaba.druid.sql.ast.expr.SQLQueryExpr. expression value: [email protected]
         */
        throw new SqlParseException(
                String.format("Failed to parse SqlExpression of type %s. expression value: %s", expr.getClass(), expr)
        );
    }
}
 
Example 8
Source Project: dble   Source File: ItemNull.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    return new SQLNullExpr();
}