com.alibaba.druid.sql.ast.statement.SQLSelect Java Examples

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLSelect. 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: PreparedStatement.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private static String[] getColumns(String sql) {
    String[] columnNames;
    try {
        SQLStatementParser sqlStatementParser = SQLParserUtils.createSQLStatementParser(sql, JdbcUtils.MYSQL);
        SQLStatement statement = sqlStatementParser.parseStatement();
        if (statement instanceof SQLSelectStatement) {
            SQLSelect select = ((SQLSelectStatement) statement).getSelect();
            com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock query = (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) select.getQuery();
            int size = query.getSelectList().size();
            if (size == 1){
                if("*".equalsIgnoreCase(   query.getSelectList().get(0).toString())){
                    throw new Exception("unsupport * in select items:"+sql);
                }
            } {
                columnNames = new String[size];
                for (int i = 0; i < size; i++) {
                    columnNames[i] = query.getSelectList().get(i).toString();
                }
                return columnNames;
            }

        }
    }catch (Exception e){
        LOGGER.error("can not get column count",e);
    }
    return new String[]{};
}
 
Example #2
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private void addSubQuerys(SQLSelect sqlselect){
		/* 多个 sqlselect 之间  , equals 和 hashcode 是相同的.去重时 都被过滤掉了. */
		if(subQuerys.isEmpty()){
			subQuerys.add(sqlselect);
			return;
		}
        boolean exists = false;
		Iterator<SQLSelect> iter = subQuerys.iterator();
		while(iter.hasNext()){
			SQLSelect ss = iter.next();
			if(ss.getQuery() instanceof SQLSelectQueryBlock
					&&sqlselect.getQuery() instanceof SQLSelectQueryBlock){
				SQLSelectQueryBlock current = (SQLSelectQueryBlock)sqlselect.getQuery();
				SQLSelectQueryBlock ssqb = (SQLSelectQueryBlock)ss.getQuery();
//                  if(!sqlSelectQueryBlockEquals(ssqb,current)){
//                    subQuerys.add(sqlselect);
//                  }
                /**
                 * 修正判定逻辑,应改为全不在subQuerys中才加入<br/>
                 * 
                 * @author SvenAugustus
                 */
                if(sqlSelectQueryBlockEquals(current,ssqb)){
                   exists = true;
                   break;
                }
				}
			}
        if(!exists) {
          subQuerys.add(sqlselect);
		}
	}
 
Example #3
Source File: DruidMycatRouteStrategy.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取子查询执行结果后,改写原始sql 继续执行.
 * @param statement
 * @param sqlselect
 * @param param
 * @return
 */
private String buildSql(SQLStatement statement,SQLSelect sqlselect,List param){

	SQLObject parent = sqlselect.getParent();
	RouteMiddlerReaultHandler handler = middlerResultHandler.get(parent.getClass());
	if(handler==null){
		throw new UnsupportedOperationException(parent.getClass()+" current is not supported ");
	}
	return handler.dohandler(statement, sqlselect, parent, param);
}
 
Example #4
Source File: ItemInSubQuery.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLExpr expr = leftOperand.toExpression();
    SQLSelect sqlSelect = new SQLSelect(query);
    SQLInSubQueryExpr inSub = new SQLInSubQueryExpr(sqlSelect);
    inSub.setExpr(expr);
    inSub.setNot(isNeg);
    return inSub;
}
 
Example #5
Source File: ItemAllAnySubQuery.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLSelect sqlSelect = new SQLSelect(query);
    if (isAll) {
        return new SQLAllExpr(sqlSelect);
    } else {
        return new SQLAnyExpr(sqlSelect);
    }
}
 
Example #6
Source File: ShardPreparedStatement.java    From Zebra with Apache License 2.0 5 votes vote down vote up
private void replaceLimitParams(SQLParsedResult parseResult) {
	if (parseResult != null) {
		SQLStatement sqlStatement = parseResult.getStmt();
		if (parseResult.getStmt() != null && sqlStatement instanceof SQLSelectStatement) {
			SQLSelect sqlSelect = ((SQLSelectStatement) sqlStatement).getSelect();
			if (sqlSelect != null) {
				SQLSelectQuery sqlSelectQuery = sqlSelect.getQuery();
				if (sqlSelectQuery != null && sqlSelectQuery instanceof MySqlSelectQueryBlock) {
					MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
					MySqlSelectQueryBlock.Limit limitExpr = sqlSelectQueryBlock.getLimit();
					if (limitExpr != null) {
						int offsetRefIndex = -1;
						int countRefIndex = -1;
						if (limitExpr.getOffset() instanceof SQLVariantRefExpr
						      && limitExpr.getRowCount() instanceof SQLVariantRefExpr) {
							SQLVariantRefExpr offsetExpr = (SQLVariantRefExpr) limitExpr.getOffset();
							SQLVariantRefExpr countExpr = (SQLVariantRefExpr) limitExpr.getRowCount();

							offsetRefIndex = offsetExpr.getIndex();
							countRefIndex = countExpr.getIndex();

							if (offsetRefIndex > countRefIndex && offsetRefIndex != -1 && countRefIndex != -1) {
								offsetExpr.setIndex(countRefIndex);
								countExpr.setIndex(offsetRefIndex);
							}
						}
					}
				}
			}
		}
	}
}
 
Example #7
Source File: ShardColumnValueUtil.java    From Zebra with Apache License 2.0 5 votes vote down vote up
private static SQLExpr getWhere(SQLParsedResult parseResult) {
	SQLExpr expr = null;
	SQLStatement stmt = parseResult.getStmt();

	if (parseResult.getType() == SqlType.SELECT || parseResult.getType() == SqlType.SELECT_FOR_UPDATE) {
		MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) (((SQLSelectStatement) stmt).getSelect()).getQuery();
		expr = query.getWhere();
	} else if (parseResult.getType() == SqlType.UPDATE) {
		expr = ((MySqlUpdateStatement) stmt).getWhere();
	} else if (parseResult.getType() == SqlType.DELETE) {
		expr = ((MySqlDeleteStatement) stmt).getWhere();
	} else if (parseResult.getType() == SqlType.REPLACE) { // add for replace
		MySqlReplaceStatement replaceStatement = (MySqlReplaceStatement) stmt;
		SQLQueryExpr queryExpr = replaceStatement.getQuery();
		if (queryExpr != null) {
			SQLSelect sqlSelect = queryExpr.getSubQuery();
			sqlSelect.getQuery();
			if (sqlSelect != null) {
				MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) sqlSelect.getQuery();
				if (queryBlock != null) {
					expr = queryBlock.getWhere();
				}
			}
		}
	}

	return expr;
}
 
Example #8
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public Queue<SQLSelect> getSubQuerys() {
	return subQuerys;
}
 
Example #9
Source File: ItemScalarSubQuery.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    SQLSelect sqlSelect = new SQLSelect(query);
    return new SQLQueryExpr(sqlSelect);
}
 
Example #10
Source File: ItemExistsSubQuery.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLExpr toExpression() {
    return new SQLExistsExpr(new SQLSelect(query), isNot);
}
 
Example #11
Source File: RouteMiddlerReaultHandler.java    From Mycat2 with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 处理中间结果
 * @param statement
 * @param sqlselect
 * @param param
 * @return
 */
String dohandler(SQLStatement statement,SQLSelect sqlselect,SQLObject parent,List param);