Java Code Examples for com.alibaba.druid.sql.ast.statement.SQLSelectItem#getExpr()

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLSelectItem#getExpr() . 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: dble   File: SelectHandler.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static boolean isSupportSelect(String stmt) {
    SQLStatementParser parser = new MySqlStatementParser(stmt);
    SQLStatement statement = parser.parseStatement();
    if (!(statement instanceof SQLSelectStatement)) {
        return false;
    }

    SQLSelectQuery sqlSelectQuery = ((SQLSelectStatement) statement).getSelect().getQuery();
    if (!(sqlSelectQuery instanceof MySqlSelectQueryBlock)) {
        return false;
    }
    MySqlSelectQueryBlock selectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
    SQLTableSource mysqlFrom = selectQueryBlock.getFrom();
    if (mysqlFrom != null) {
        return false;
    }
    for (SQLSelectItem item : selectQueryBlock.getSelectList()) {
        SQLExpr selectItem = item.getExpr();
        if (!isVariantRef(selectItem)) {
            return false;
        }
    }
    return true;
}
 
Example 2
@Override
public void parseFrom(SQLSelectQueryBlock queryBlock) {
    if (lexer.token() != Token.FROM) {
        return;
    }

    lexer.nextTokenIdent();

    if (lexer.token() == Token.UPDATE) { // taobao returning to urgly syntax
        updateStmt = this.parseUpdateStatment();
        List<SQLExpr> returnning = updateStmt.getReturning();
        for (SQLSelectItem item : queryBlock.getSelectList()) {
            SQLExpr itemExpr = item.getExpr();
            itemExpr.setParent(updateStmt);
            returnning.add(itemExpr);
        }
        returningFlag = true;
        return;
    }

    queryBlock.setFrom(parseTableSource());
}
 
Example 3
Source Project: Mycat2   File: JoinParser.java    License: GNU General Public License v3.0 5 votes vote down vote up
private String getFieldName(SQLSelectItem item){
	if (item.getExpr() instanceof SQLPropertyExpr) {			
		return item.getExpr().toString();//字段别名
	}
	else {
		return item.toString();
	}
}
 
Example 4
Source Project: Mycat2   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 5
Source Project: Mycat2   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 6
Source Project: baymax   File: MySqlSelectParser.java    License: Apache License 2.0 5 votes vote down vote up
protected 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 {
        return item.toString();
    }
}
 
Example 7
Source Project: baymax   File: MySqlSelectParser.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 解析聚合函数
 * @param result
 * @param plan
 * @param mysqlSelectQuery
 */
protected void parseAggregate(ParseResult result, ExecutePlan plan, MySqlSelectQueryBlock mysqlSelectQuery){
    // 要合并的列
    Map<String, MergeColumn.MergeType>      aggrColumns         = new HashMap<String, MergeColumn.MergeType>();
    Map<String/*field*/, String/*alias*/>   aliaColumns         = new HashMap<String, String>();
    // 查询的列
    List<SQLSelectItem>                     selectList          = mysqlSelectQuery.getSelectList();
    int                                     size                = selectList.size();
    //boolean                               isDistinct          = mysqlSelectQuery.getDistionOption() == 2;
    List<String>                            avgColumns          = null;

    for (int i = 0; i < size; i++){
        SQLSelectItem item = selectList.get(i);
        if (item.getExpr() instanceof SQLAggregateExpr) {
            SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr();
            String method = expr.getMethodName();
            // 只处理有别名的情况,无别名添加别名,否则某些数据库会得不到正确结果处理
            MergeColumn.MergeType mergeType = MergeColumn.buildMergeType(method);

            if (MergeColumn.MergeType.MERGE_UNSUPPORT == mergeType) {
                continue;
            }

            // 没有别名的 增加别名
            if (item.getAlias() == null || item.getAlias().length() <= 0) {
                item.setAlias(method + i);
            }

            // 保存合并列
            aggrColumns.put(item.getAlias(), mergeType);

            if (MergeColumn.MergeType.MERGE_AVG == mergeType) {
                if (avgColumns == null) {
                    avgColumns = new ArrayList<String>();
                }
                avgColumns.add(item.getAlias());

                //sum
                // 表达式
                SQLAggregateExpr sumExp = new SQLAggregateExpr("SUM");
                ReflectionUtils.copyProperties(expr, sumExp);
                sumExp.getArguments().addAll(expr.getArguments());
                sumExp.setMethodName("SUM");

                // item
                SQLSelectItem sum = new SQLSelectItem();
                String sumColName = item.getAlias() + "SUM";
                sum.setAlias(sumColName);
                sum.setExpr(sumExp);

                // 替换
                selectList.add(sum);
                aggrColumns.put(sumColName, MergeColumn.MergeType.MERGE_SUM);

                // count
                // 表达式
                SQLAggregateExpr countExp = new SQLAggregateExpr("COUNT");
                ReflectionUtils.copyProperties(expr, countExp);
                countExp.getArguments().addAll(expr.getArguments());
                countExp.setMethodName("COUNT");

                // item
                SQLSelectItem count = new SQLSelectItem();
                String countColName = item.getAlias() + "COUNT";
                count.setAlias(countColName);
                count.setExpr(countExp);

                // 替换
                selectList.add(count);
                aggrColumns.put(countColName, MergeColumn.MergeType.MERGE_COUNT);

                // 原始avg
                aggrColumns.put(item.getAlias(), MergeColumn.MergeType.MERGE_AVG);
            }
        }else
        // 所有select字段的别名
        if (item.getExpr() instanceof SQLAllColumnExpr){
            // select *
            result.setHasAllColumnExpr(true);
        }else if(item.getExpr() instanceof SQLIdentifierExpr){
            // 只有普通select list的字段才放到别名列表中,用于orderby groupby的取值比较
            String alia = item.getAlias();
            //String field = getFieldName(item);
            String field = item.getExpr().toString();
            alia = alia == null ? field : alia;
            aliaColumns.put(field, alia);
        }
    }
    plan.setMergeColumns(aggrColumns);
    result.setAliaColumns(aliaColumns);
}
 
Example 8
Source Project: Zebra   File: MySQLSelectASTVisitor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean visit(MySqlSelectQueryBlock x) {
	result.getMergeContext().increQueryCount();
	Map<String, SQLObjectImpl> selectItemMap = result.getMergeContext().getSelectItemMap();
	Map<String, String> columnNameAliasMapping = result.getMergeContext().getColumnNameAliasMapping();

	for (SQLSelectItem column : x.getSelectList()) {
		String name = null;
		if (column.getExpr() instanceof SQLAggregateExpr) {
			SQLAggregateExpr expr = (SQLAggregateExpr) column.getExpr();
			SQLExpr argument = expr.getArguments().get(0);
			if (argument instanceof SQLAllColumnExpr) {
				name = expr.getMethodName() + "(*)";
			} else if (argument instanceof SQLIntegerExpr) {
				name = expr.getMethodName() + "(1)";
			} else {
				name = expr.getMethodName() + "(" + argument.toString() + ")";
				if (column.getAlias() != null) {
					columnNameAliasMapping.put(name, column.getAlias());
				}
			}

			result.getMergeContext().setAggregate(true);
		} else if (column.getExpr() instanceof SQLIdentifierExpr || column.getExpr() instanceof SQLPropertyExpr) {
			name = ((SQLName) column.getExpr()).getSimpleName();

			if (column.getAlias() != null) {
				SQLName identifier = (SQLName) column.getExpr();
				columnNameAliasMapping.put(identifier.getSimpleName(), column.getAlias());
			}
		} else {
			// ignore SQLAllColumnExpr,SQLMethodInvokeExpr and etc.
		}

		selectItemMap.put(column.getAlias() == null ? name : column.getAlias(), column);
	}

	if (x.getDistionOption() == 2) {
		result.getMergeContext().setDistinct(true);
	}

	return true;
}