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

The following examples show how to use com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr. 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: DruidInsertParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
private String getShardingValue(SQLExpr expr) throws SQLNonTransientException {
	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();
	} else if (expr instanceof SQLMethodInvokeExpr) {
		SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr)expr;
		try {
			shardingValue = tryInvokeSQLMethod(methodInvokeExpr);
		}catch (Exception e){
			LOGGER.error("",e);
		}
		if (shardingValue == null){
			shardingValue = expr.toString();
		}
	} else {
		shardingValue = expr.toString();
	}
	return shardingValue;
}
 
Example 2
private SQLExpr doInvoke(SQLMethodInvokeExpr expr) throws SQLNonTransientException {
    String methodName = expr.getMethodName().toUpperCase();
    switch (methodName) {
        case "NOW":
        case "SYSDATE":
        case "CURRENT_TIMESTAMP":
            return invokeNow();
        case "ADDDATE":
        case "DATE_ADD":
            return invokeAddDate(expr, false);
        case "SUBDATE":
        case "DATE_SUB":
            return invokeAddDate(expr, true);
    }
    return null;
}
 
Example 3
Source Project: dble   Source File: SchemaUtil.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static boolean isNoSharding(ServerConnection source, SQLSelectQuery sqlSelectQuery, SQLStatement selectStmt, SQLStatement childSelectStmt, String contextSchema, Set<String> schemas, StringPtr dataNode)
        throws SQLException {
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mySqlSelectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
        //CHECK IF THE SELECT LIST HAS INNER_FUNC IN,WITCH SHOULD BE DEAL BY DBLE
        for (SQLSelectItem item : mySqlSelectQueryBlock.getSelectList()) {
            if (item.getExpr() instanceof SQLMethodInvokeExpr) {
                if (ItemCreate.getInstance().isInnerFunc(((SQLMethodInvokeExpr) item.getExpr()).getMethodName())) {
                    return false;
                }
            }
        }
        return isNoSharding(source, mySqlSelectQueryBlock.getFrom(), selectStmt, childSelectStmt, contextSchema, schemas, dataNode);
    } else if (sqlSelectQuery instanceof SQLUnionQuery) {
        return isNoSharding(source, (SQLUnionQuery) sqlSelectQuery, selectStmt, contextSchema, schemas, dataNode);
    } else {
        return false;
    }
}
 
Example 4
Source Project: dble   Source File: MySQLPlanNodeVisitor.java    License: GNU General Public License v2.0 6 votes vote down vote up
private String createInnerFuncSelectSQL(List<SQLSelectItem> items) {
    StringBuffer sb = new StringBuffer("SELECT ");
    if (items != null) {
        for (SQLSelectItem si : items) {
            if (si.getExpr() instanceof SQLMethodInvokeExpr &&
                    ItemCreate.getInstance().isInnerFunc(((SQLMethodInvokeExpr) si.getExpr()).getMethodName())) {
                sb.append(si.getExpr().toString() + ",");
            }
        }
        if (sb.length() > 7) {
            sb.setLength(sb.length() - 1);
            return sb.toString();
        }
    }
    return null;
}
 
Example 5
Source Project: dble   Source File: ItemDecimalTypeConvert.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    if (precision >= 0 || dec > 0) {
        SQLMethodInvokeExpr dataType = new SQLMethodInvokeExpr();
        dataType.setMethodName("DECIMAL");
        if (precision >= 0) {
            dataType.addParameter(new SQLIntegerExpr(precision));
        }
        if (dec > 0) {
            dataType.addParameter(new SQLIntegerExpr(dec));
        }
        method.addParameter(dataType);
    } else {
        method.addParameter(new SQLIdentifierExpr("DECIMAL"));
    }
    return method;
}
 
Example 6
Source Project: dble   Source File: ItemFuncTrim.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    if (mTrimMode == TrimTypeEnum.LTRIM) {
        method.setMethodName("LTRIM");
        method.addParameter(args.get(0).toExpression());

    } else if (mTrimMode == TrimTypeEnum.RTRIM) {
        method.setMethodName("RTRIM");
        method.addParameter(args.get(0).toExpression());

    } else {
        method.setMethodName("TRIM");
        method.addParameter(args.get(0).toExpression());
        if (this.getArgCount() > 1) {
            method.setFrom(args.get(1).toExpression());
        }
        if (mTrimMode != TrimTypeEnum.DEFAULT) {
            method.setTrimOption(mTrimMode.toString());
        }
    }
    return method;
}
 
Example 7
Source Project: elasticsearch-sql   Source File: Maker.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 构建过滤条件
 * 
 * @param cond
 * @return
 * @throws SqlParseException
 */
protected ToXContent make(Condition cond) throws SqlParseException {

       String name = cond.getName();
       Object value = cond.getValue();

       ToXContent x = null;

       if (value instanceof SQLMethodInvokeExpr) {
           x = make(cond, name, (SQLMethodInvokeExpr) value);
       }
       else if (value instanceof SubQueryExpression){
           x = make(cond,name,((SubQueryExpression)value).getValues());
       } else {
		x = make(cond, name, value);
	}


	return x;
}
 
Example 8
Source Project: elasticsearch-sql   Source File: WhereParser.java    License: Apache License 2.0 6 votes vote down vote up
private boolean explanSpecialCondWithBothSidesAreLiterals(SQLBinaryOpExpr bExpr, Where where) throws SqlParseException {
    if ((bExpr.getLeft() instanceof SQLNumericLiteralExpr || bExpr.getLeft() instanceof SQLCharExpr) &&
            (bExpr.getRight() instanceof SQLNumericLiteralExpr || bExpr.getRight() instanceof SQLCharExpr)
            ) {
        SQLMethodInvokeExpr sqlMethodInvokeExpr = new SQLMethodInvokeExpr("script", null);
        String operator = bExpr.getOperator().getName();
        if (operator.equals("=")) {
            operator = "==";
        }
        sqlMethodInvokeExpr.addParameter(
                new SQLCharExpr(Util.expr2Object(bExpr.getLeft(), "'") +
                        " " + operator + " " +
                        Util.expr2Object(bExpr.getRight(), "'"))
        );

        explanCond("AND", sqlMethodInvokeExpr, where);
        return true;
    }
    return false;
}
 
Example 9
Source Project: elasticsearch-sql   Source File: ElasticSqlExprParser.java    License: Apache License 2.0 6 votes vote down vote up
protected SQLExpr bracketRest(SQLExpr expr) {
    Number index;

    if (lexer.token() == Token.LITERAL_INT) {
        index = lexer.integerValue();
        lexer.nextToken();
    } else {
        throw new ParserException("error : " + lexer.stringVal());
    }

    if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
        methodInvokeExpr.getParameters().add(new SQLIntegerExpr(index));
    }
    lexer.nextToken();
    expr = primaryRest(expr);
    return expr;
}
 
Example 10
Source Project: elasticsearch-sql   Source File: FieldMaker.java    License: Apache License 2.0 6 votes vote down vote up
private static Field makeFilterMethodField(SQLMethodInvokeExpr filterMethod, String alias) throws SqlParseException {
    List<SQLExpr> parameters = filterMethod.getParameters();
    int parametersSize = parameters.size();
    if (parametersSize != 1 && parametersSize != 2) {
        throw new SqlParseException("filter group by field should only have one or 2 parameters filter(Expr) or filter(name,Expr)");
    }
    String filterAlias = filterMethod.getMethodName();
    SQLExpr exprToCheck = null;
    if (parametersSize == 1) {
        exprToCheck = parameters.get(0);
        filterAlias = "filter(" + exprToCheck.toString().replaceAll("\n", " ") + ")";
    }
    if (parametersSize == 2) {
        filterAlias = Util.extendedToString(parameters.get(0));
        exprToCheck = parameters.get(1);
    }
    Where where = Where.newInstance();
    new WhereParser(new SqlParser()).parseWhere(exprToCheck, where);
    if (where.getWheres().size() == 0)
        throw new SqlParseException("unable to parse filter where.");
    List<KVValue> methodParameters = new ArrayList<>();
    methodParameters.add(new KVValue("where", where));
    methodParameters.add(new KVValue("alias", filterAlias + "@FILTER"));
    return new MethodField("filter", methodParameters, null, alias);
}
 
Example 11
Source Project: Mycat2   Source File: JoinParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
private String getMethodInvokeFieldName(SQLSelectItem item){
	SQLMethodInvokeExpr invoke = (SQLMethodInvokeExpr)item.getExpr();
	List<SQLExpr> itemExprs = invoke.getParameters();
	for(SQLExpr itemExpr:itemExprs){
		if (itemExpr instanceof SQLPropertyExpr) {
			return itemExpr.toString();//字段别名
		}
	}
	return item.toString();
}
 
Example 12
Source Project: Mycat2   Source File: JoinParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void parserFields(List<SQLSelectItem> mysqlSelectList){
		//显示的字段
		String key="";
		String value ="";
		String exprfield = "";
		for(SQLSelectItem item : mysqlSelectList) {
			if (item.getExpr() instanceof SQLAllColumnExpr) {
				//*解析
				setField(item.toString(), item.toString());
			}
			else {
				if (item.getExpr() instanceof SQLAggregateExpr) {
					SQLAggregateExpr expr =(SQLAggregateExpr)item.getExpr();
					 key = getExprFieldName(expr);
					 setField(key, value);
				}else if(item.getExpr() instanceof SQLMethodInvokeExpr){
					key = getMethodInvokeFieldName(item);
					exprfield=getFieldName(item);
//					value=item.getAlias();
					setField(key, value,exprfield);
				}else {					
					key=getFieldName(item);
					value=item.getAlias();
					setField(key, value);
				}			
				
			}
		}			
	}
 
Example 13
Source Project: Mycat2   Source File: DruidSelectParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
private String getFieldName(SQLSelectItem item){
	if ((item.getExpr() instanceof SQLPropertyExpr)||(item.getExpr() instanceof SQLMethodInvokeExpr)
			|| (item.getExpr() instanceof SQLIdentifierExpr) || item.getExpr() instanceof SQLBinaryOpExpr) {
		return item.getExpr().toString();//字段别名
	}
	else if (!StringUtil.isEmpty(item.getAlias())) { // add by hehuang 20181205 如果SelectItem存在别名,则认为表达式为字段名,sql语法支持常量作为字段
		return item.getExpr().toString();
	} else {
		return item.toString();
	}
}
 
Example 14
@Override
public String invoke(SQLMethodInvokeExpr expr) throws SQLNonTransientException {
    SQLExpr ret = doInvoke(expr);
    if (ret != null) {
        return ret.toString();
    }
    throw new SQLNonTransientException("unsupported mysql function expression: " + expr.toString());
}
 
Example 15
Source Project: dble   Source File: MySQLPlanNodeVisitor.java    License: GNU General Public License v2.0 5 votes vote down vote up
private List<SQLSelectItem> selectInnerFuncList(List<SQLSelectItem> items) {
    List<SQLSelectItem> result = new ArrayList<>();
    for (SQLSelectItem si : items) {
        if (si.getExpr() instanceof SQLMethodInvokeExpr &&
                ItemCreate.getInstance().isInnerFunc(((SQLMethodInvokeExpr) si.getExpr()).getMethodName())) {
            result.add(si);
        }
    }
    return result;
}
 
Example 16
Source Project: dble   Source File: ItemNCharTypeConvert.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    if (castLength >= 0) {
        SQLMethodInvokeExpr dataType = new SQLMethodInvokeExpr();
        dataType.setMethodName("NCHAR");
        dataType.addParameter(new SQLIntegerExpr(castLength));
        method.addParameter(dataType);
    } else {
        method.addParameter(new SQLIdentifierExpr("NCHAR"));
    }
    return method;
}
 
Example 17
Source Project: dble   Source File: ItemFuncSignedConvert.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    method.addParameter(new SQLIdentifierExpr("SIGNED"));
    return method;
}
 
Example 18
Source Project: dble   Source File: ItemTimeTypeConvert.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    if (decimals != NOT_FIXED_DEC) {
        SQLMethodInvokeExpr dataType = new SQLMethodInvokeExpr();
        dataType.setMethodName("TIME");
        dataType.addParameter(new SQLIntegerExpr(decimals));
        method.addParameter(dataType);
    } else {
        method.addParameter(new SQLIdentifierExpr("TIME"));
    }
    return method;
}
 
Example 19
Source Project: dble   Source File: ItemFuncBinaryConvert.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    if (decimals != NOT_FIXED_DEC) {
        SQLMethodInvokeExpr dataType = new SQLMethodInvokeExpr();
        dataType.setMethodName("BINARY");
        dataType.addParameter(new SQLIntegerExpr(decimals));
        method.addParameter(dataType);
    } else {
        method.addParameter(new SQLIdentifierExpr("BINARY"));
    }
    return method;
}
 
Example 20
Source Project: dble   Source File: ItemCharTypeConvert.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    if (castLength >= 0) {
        SQLMethodInvokeExpr dataType = new SQLMethodInvokeExpr();
        dataType.setMethodName("CHAR");
        dataType.addParameter(new SQLIntegerExpr(castLength));
        method.addParameter(dataType);
    } else {
        method.addParameter(new SQLIdentifierExpr("CHAR"));
    }
    return method;
}
 
Example 21
Source Project: dble   Source File: ItemDatetimeTypeConvert.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    if (decimals != NOT_FIXED_DEC) {
        SQLMethodInvokeExpr dataType = new SQLMethodInvokeExpr();
        dataType.setMethodName("DATETIME");
        dataType.addParameter(new SQLIntegerExpr(decimals));
        method.addParameter(dataType);
    } else {
        method.addParameter(new SQLIdentifierExpr("DATETIME"));
    }
    return method;
}
 
Example 22
Source Project: dble   Source File: ItemDateTypeConvert.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    method.addParameter(new SQLIdentifierExpr("DATE"));
    return method;
}
 
Example 23
Source Project: dble   Source File: ItemFuncUnsignedConvert.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr();
    method.setMethodName("CONVERT");
    method.addParameter(args.get(0).toExpression());
    method.addParameter(new SQLIdentifierExpr("UNSIGNED"));
    return method;
}
 
Example 24
Source Project: dble   Source File: ItemFuncIf.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr(funcName());
    for (Item arg : args) {
        method.addParameter(arg.toExpression());
    }
    return method;
}
 
Example 25
Source Project: dble   Source File: ItemFunc.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    if (ItemCreate.getInstance().isNativeFunc(this.funcName())) {
        SQLMethodInvokeExpr nativeFe = new SQLMethodInvokeExpr(this.funcName().toUpperCase());
        for (Item item : args) {
            nativeFe.addParameter(item.toExpression());
        }
        return nativeFe;
    } else {
        throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "unexpected function:" + funcName());
    }
}
 
Example 26
Source Project: dble   Source File: ItemFuncUnknown.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr(funcName);
    for (Item arg : args) {
        method.addParameter(arg.toExpression());
    }
    return method;
}
 
Example 27
Source Project: dble   Source File: ItemFuncChar.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr(funcName());
    for (Item arg : args) {
        method.addParameter(arg.toExpression());
    }
    return method;
}
 
Example 28
Source Project: dble   Source File: ItemFuncOrd.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr(funcName());
    for (Item arg : args) {
        method.addParameter(arg.toExpression());
    }
    return method;
}
 
Example 29
Source Project: dble   Source File: ItemSumVariance.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr(funcName());
    for (Item arg : args) {
        method.addParameter(arg.toExpression());
    }
    return method;
}
 
Example 30
Source Project: dble   Source File: ItemSumXor.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLMethodInvokeExpr method = new SQLMethodInvokeExpr(funcName());
    for (Item arg : args) {
        method.addParameter(arg.toExpression());
    }
    return method;
}