Java Code Examples for com.alibaba.druid.sql.ast.SQLObject

The following examples show how to use com.alibaba.druid.sql.ast.SQLObject. 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: elasticsearch-sql   Source File: Util.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean isFromJoinOrUnionTable(SQLExpr expr) {
    SQLObject temp = expr;
    AtomicInteger counter = new AtomicInteger(10);
    while (temp != null &&
            !(expr instanceof SQLSelectQueryBlock) &&
            !(expr instanceof SQLJoinTableSource) && !(expr instanceof SQLUnionQuery) && counter.get() > 0) {
        counter.decrementAndGet();
        temp = temp.getParent();
        if (temp instanceof SQLSelectQueryBlock) {
            SQLTableSource from = ((SQLSelectQueryBlock) temp).getFrom();
            if (from instanceof SQLJoinTableSource || from instanceof SQLUnionQuery) {
                return true;
            }
        }
        if (temp instanceof SQLJoinTableSource || temp instanceof SQLUnionQuery) {
            return true;
        }
    }
    return false;
}
 
Example 2
/**
 * 获取子查询执行结果后,改写原始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 3
Source Project: dble   Source File: SqlStringUtil.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static String toSQLString(SQLObject sqlObject) {
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = new DbleOutputVisitor(out);
    SQLUtils.FormatOption option = new SQLUtils.FormatOption(true, true);
    visitor.setUppCase(option.isUppCase());
    visitor.setPrettyFormat(option.isPrettyFormat());
    visitor.setParameterized(option.isParameterized());
    sqlObject.accept(visitor);
    String sql = out.toString();
    return sql;
}
 
Example 4
Source Project: dble   Source File: ServerSchemaStatVisitor.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * get table name of field in between expr
 *
 */
private String getOwnerTableName(SQLBetweenExpr betweenExpr, String column) {
    if (aliasMap.size() == 1) { //only has 1 table
        return aliasMap.keySet().iterator().next();
    } else if (aliasMap.size() == 0) { //no table
        return "";
    } else { // multi tables
        for (Column col : columns.values()) {
            if (col.getName().equals(column)) {
                return col.getTable();
            }
        }

        //parser from parent
        SQLObject parent = betweenExpr.getParent();
        if (parent instanceof SQLBinaryOpExpr) {
            parent = parent.getParent();
        }

        if (parent instanceof MySqlSelectQueryBlock) {
            MySqlSelectQueryBlock select = (MySqlSelectQueryBlock) parent;
            if (select.getFrom() instanceof SQLJoinTableSource) {
                SQLJoinTableSource joinTableSource = (SQLJoinTableSource) select.getFrom();
                //FIXME :left as driven table
                return joinTableSource.getLeft().toString();
            } else if (select.getFrom() instanceof SQLExprTableSource) {
                return select.getFrom().toString();
            }
        } else if (parent instanceof SQLUpdateStatement) {
            SQLUpdateStatement update = (SQLUpdateStatement) parent;
            return update.getTableName().getSimpleName();
        } else if (parent instanceof SQLDeleteStatement) {
            SQLDeleteStatement delete = (SQLDeleteStatement) parent;
            return delete.getTableName().getSimpleName();
        }
    }
    return "";
}
 
Example 5
/**
     * 从between语句中获取字段所属的表名。
     * 对于容易出现ambiguous的(字段不知道到底属于哪个表),实际应用中必须使用别名来避免歧义
     * @param betweenExpr
     * @param column
     * @return
     */
    private String getOwnerTableName(SQLBetweenExpr betweenExpr,String column) {
        if(tableStats.size() == 1) {//只有一个表,直接返回这一个表名
            return tableStats.keySet().iterator().next().getName();
        } else if(tableStats.size() == 0) {//一个表都没有,返回空串
            return "";
        } else {//多个表名
            for (Column col : columns.keySet())
            {
                if(col.getName().equals(column)) {
                    return col.getTable();
                }
            }
//            for(Column col : columns) {//从columns中找表名
//                if(col.getName().equals(column)) {
//                    return col.getTable();
//                }
//            }

            //前面没找到表名的,自己从parent中解析

            SQLObject parent = betweenExpr.getParent();
            if(parent instanceof SQLBinaryOpExpr)
            {
                parent=parent.getParent();
            }

            if(parent instanceof MySqlSelectQueryBlock) {
                MySqlSelectQueryBlock select = (MySqlSelectQueryBlock) parent;
                if(select.getFrom() instanceof SQLJoinTableSource) {//多表连接
                    SQLJoinTableSource joinTableSource = (SQLJoinTableSource)select.getFrom();
                    return joinTableSource.getLeft().toString();//将left作为主表,此处有不严谨处,但也是实在没有办法,如果要准确,字段前带表名或者表的别名即可
                } else if(select.getFrom() instanceof SQLExprTableSource) {//单表
                    return select.getFrom().toString();
                }
            }
            else if(parent instanceof SQLUpdateStatement) {
                SQLUpdateStatement update = (SQLUpdateStatement) parent;
                return update.getTableName().getSimpleName();
            } else if(parent instanceof SQLDeleteStatement) {
                SQLDeleteStatement delete = (SQLDeleteStatement) parent;
                return delete.getTableName().getSimpleName();
            } else {
                
            }
        }
        return "";
    }
 
Example 6
Source Project: baymax   Source File: OrVisitor.java    License: Apache License 2.0 4 votes vote down vote up
public OrEntity(OrVisitor orVisitor, SQLObject x){
    this.orVisitor = orVisitor;
    this.x = x;
}
 
Example 7
/**
 * 处理中间结果
 * @param statement
 * @param sqlselect
 * @param param
 * @return
 */
String dohandler(SQLStatement statement,SQLSelect sqlselect,SQLObject parent,List param);