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

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLSelectItem#getAlias() . 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: JoinParser.java    From Mycat2 with 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 2
Source File: MysqlSystemSchemaHandler.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
private static FieldPacket[] generateFieldPacket(List<SQLSelectItem> selectList) {
    FieldPacket[] fields = new FieldPacket[selectList.size()];
    for (int i = 0; i < selectList.size(); i++) {
        String columnName;
        SQLSelectItem selectItem = selectList.get(i);
        if (selectItem.getAlias() != null) {
            columnName = StringUtil.removeBackQuote(selectList.get(i).getAlias());
        } else {
            columnName = StringUtil.removeBackQuote(selectItem.toString());
        }
        fields[i] = PacketUtil.getField(columnName, Fields.FIELD_TYPE_VAR_STRING);
    }
    return fields;
}
 
Example 3
Source File: MySqlSelectParser.java    From baymax with 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 4
Source File: MySQLSelectASTVisitor.java    From Zebra with 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;
}