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

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLUnionQuery. 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: Util.java    From elasticsearch-sql with 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
Source File: ESActionFactory.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
/**
 * Create the compatible Query object
 * based on the SQL query.
 * @param sql The SQL query.
 * @return Query object.
 */
   public static QueryAction create(Client client, String sql) throws SqlParseException, SQLFeatureNotSupportedException {
       sql = sql.replaceAll("\n", " ").trim();
       String firstWord = sql.substring(0, sql.indexOf(' '));
       switch (firstWord.toUpperCase()) {
		case "SELECT":
		    //zhongshu-comment 将sql字符串解析成AST,即SQLQueryExpr sqlExpr就是AST了,下面的代码就开始访问AST、从中获取token
			SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql);
               if(isMulti(sqlExpr)){//zhongshu-comment 判断是不是union查询,union查询两个select语句,btw:子查询也有多个select语句,至少2个
                   MultiQuerySelect multiSelect = new SqlParser().parseMultiSelect((SQLUnionQuery) sqlExpr.getSubQuery().getQuery());
                   handleSubQueries(client,multiSelect.getFirstSelect());
                   handleSubQueries(client,multiSelect.getSecondSelect());
                   return new MultiQueryAction(client, multiSelect);
               }
               else if(isJoin(sqlExpr,sql)){//zhongshu-comment join连接查询
                   JoinSelect joinSelect = new SqlParser().parseJoinSelect(sqlExpr);
                   handleSubQueries(client, joinSelect.getFirstTable());
                   handleSubQueries(client, joinSelect.getSecondTable());
                   return ESJoinQueryActionFactory.createJoinAction(client, joinSelect);
               }
               else {
                   //zhongshu-comment 大部分查询都是走这个分支,先看懂这个分支
                   Select select = new SqlParser().parseSelect(sqlExpr);
                   //todo 看不懂,测试了好几个常见的sql,都没有进去handleSubQueries该方法,那就先不理了,看别的
                   handleSubQueries(client, select);
                   return handleSelect(client, select);
               }
		case "DELETE":
               SQLStatementParser parser = createSqlStatementParser(sql);
			SQLDeleteStatement deleteStatement = parser.parseDeleteStatement();
			Delete delete = new SqlParser().parseDelete(deleteStatement);
			return new DeleteQueryAction(client, delete);
           case "SHOW":
               return new ShowQueryAction(client,sql);
		default:
			throw new SQLFeatureNotSupportedException(String.format("Unsupported query: %s", sql));
	}
}
 
Example #3
Source File: ElasticSqlSelectParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Override
public SQLUnionQuery unionRest(SQLUnionQuery union) {
    if (lexer.token() == Token.LIMIT) {
        union.setLimit(this.exprParser.parseLimit());
    }
    return super.unionRest(union);
}
 
Example #4
Source File: MySQLSelectASTVisitor.java    From Zebra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean visit(SQLUnionQuery x) {
	result.getMergeContext().increUnionCount();
	return super.visit(x);
}
 
Example #5
Source File: ESActionFactory.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private static boolean isMulti(SQLQueryExpr sqlExpr) {
    return sqlExpr.getSubQuery().getQuery() instanceof SQLUnionQuery;
}
 
Example #6
Source File: SqlParser.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public MultiQuerySelect parseMultiSelect(SQLUnionQuery query) throws SqlParseException {
    Select firstTableSelect = this.parseSelect((MySqlSelectQueryBlock) query.getLeft());
    Select secondTableSelect = this.parseSelect((MySqlSelectQueryBlock) query.getRight());
    return new MultiQuerySelect(query.getOperator(),firstTableSelect,secondTableSelect);
}