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

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock. 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: SelectParser.java    From dts with Apache License 2.0 6 votes vote down vote up
@Override
protected String getTableName(SQLSelectStatement parseSqlStatement) {
    SQLSelectQueryBlock selectQueryBlock = parseSqlStatement.getSelect().getQueryBlock();
    SQLTableSource tableSource = selectQueryBlock.getFrom();
    StringBuffer sb = new StringBuffer();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(sb) {

        @Override
        public boolean visit(SQLExprTableSource x) {
            printTableSourceExpr(x.getExpr());
            return false;
        }
    };
    visitor.visit((SQLExprTableSource)tableSource);
    return sb.toString();
}
 
Example #2
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private boolean sqlSelectQueryBlockEquals(SQLSelectQueryBlock obj1,SQLSelectQueryBlock obj2) {
    if (obj1 == obj2) return true;
    if (obj2 == null) return false;
    if (obj1.getClass() != obj2.getClass()) return false;
    if (obj1.isParenthesized() ^ obj2.isParenthesized()) return false;
    if (obj1.getDistionOption() != obj2.getDistionOption()) return false;
    if (obj1.getFrom() == null) {
        if (obj2.getFrom() != null) return false;
    } else if (!obj1.getFrom().equals(obj2.getFrom())) return false;
    if (obj1.getGroupBy() == null) {
        if (obj2.getGroupBy() != null) return false;
    } else if (!obj1.getGroupBy().equals(obj2.getGroupBy())) return false;
    if (obj1.getInto() == null) {
        if (obj2.getInto() != null) return false;
    } else if (!obj1.getInto().equals(obj2.getInto())) return false;
    if (obj1.getSelectList() == null) {
        if (obj2.getSelectList() != null) return false;
    } else if (!obj1.getSelectList().equals(obj2.getSelectList())) return false;
    if (obj1.getWhere() == null) {
        if (obj2.getWhere() != null) return false;
    } else if (!obj1.getWhere().equals(obj2.getWhere())) return false;
    return true;
}
 
Example #3
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 #4
Source File: ElasticSqlSelectParser.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: SelectParser.java    From dts with Apache License 2.0 5 votes vote down vote up
@Override
protected String getWhere(SQLSelectStatement parseSqlStatement) {
    SQLSelectQueryBlock selectQueryBlock = parseSqlStatement.getSelect().getQueryBlock();
    SQLExpr where = selectQueryBlock.getWhere();
    if (where == null) {
        return "";
    }
    return SQLUtils.toSQLString(where);

}
 
Example #6
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 #7
Source File: ElasticSearchDruidDataSource.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private void oracleValidationQueryCheck() {
    if (validationQuery == null) {
        return;
    }
    if (validationQuery.length() == 0) {
        return;
    }

    SQLStatementParser sqlStmtParser = SQLParserUtils.createSQLStatementParser(validationQuery, this.dbType);
    List<SQLStatement> stmtList = sqlStmtParser.parseStatementList();

    if (stmtList.size() != 1) {
        return;
    }

    SQLStatement stmt = stmtList.get(0);
    if (!(stmt instanceof SQLSelectStatement)) {
        return;
    }

    SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        if (((SQLSelectQueryBlock) query).getFrom() == null) {
            LOG.error("invalid oracle validationQuery. " + validationQuery + ", may should be : " + validationQuery
                    + " FROM DUAL");
        }
    }
}
 
Example #8
Source File: ElasticSearchDruidDataSource.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private void db2ValidationQueryCheck() {
    if (validationQuery == null) {
        return;
    }
    if (validationQuery.length() == 0) {
        return;
    }

    SQLStatementParser sqlStmtParser = SQLParserUtils.createSQLStatementParser(validationQuery, this.dbType);
    List<SQLStatement> stmtList = sqlStmtParser.parseStatementList();

    if (stmtList.size() != 1) {
        return;
    }

    SQLStatement stmt = stmtList.get(0);
    if (!(stmt instanceof SQLSelectStatement)) {
        return;
    }

    SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        if (((SQLSelectQueryBlock) query).getFrom() == null) {
            LOG.error("invalid db2 validationQuery. " + validationQuery + ", may should be : " + validationQuery
                    + " FROM SYSDUMMY");
        }
    }
}
 
Example #9
Source File: PaserExecutor.java    From dts with Apache License 2.0 4 votes vote down vote up
public static SQLType parse(StatementAdapter txcStatement) throws SQLException {
    long start = System.currentTimeMillis();
    SQLType sqlType = SQLType.SELECT;
    try {
        DbRuntimeContext txcRuntimeContext = txcStatement.getConnection().getConnectionRuntimeContext();
        String sql = txcStatement.getSql();
        SQLStatement sqlParseStatement = new MySqlStatementParser(sql).parseStatement();
        CommitInfo commitInfo = null;
        if (sqlParseStatement instanceof MySqlUpdateStatement) {
            commitInfo = UpdateParser.getInstance().parse(txcStatement);
            sqlType = SQLType.UPDATE;
            if (!Objects.isNull(commitInfo)&&!CollectionUtils.isEmpty(commitInfo.getOriginalValue().getLine())) {
                txcRuntimeContext.getInfo().add(commitInfo);
                fillDbMetaAndLockRow(txcStatement, commitInfo);
            }
        } else if (sqlParseStatement instanceof MySqlInsertStatement) {
            sqlType = SQLType.INSERT;
        } else if (sqlParseStatement instanceof MySqlDeleteStatement) {
            commitInfo = DeleteParser.getInstance().parse(txcStatement);
            sqlType = SQLType.DELETE;
            if (!Objects.isNull(commitInfo) && !CollectionUtils.isEmpty(commitInfo.getOriginalValue().getLine())) {
                txcRuntimeContext.getInfo().add(commitInfo);
                fillDbMetaAndLockRow(txcStatement, commitInfo);
            }
        } else if (sqlParseStatement instanceof SQLSelectStatement) {
            SQLSelectQueryBlock selectQueryBlock =
                ((SQLSelectStatement)sqlParseStatement).getSelect().getQueryBlock();
            if (selectQueryBlock.getFrom() != null) {
                SelectParser.getInstance().parse(txcStatement);
                sqlType = SQLType.SELECT;
            }
        }
    } catch (Exception e) {
        logger.error("parse sql error", e);
        if (e instanceof SQLException || e instanceof RuntimeException) {
            throw e;
        } else {
            throw new SQLException(e);
        }
    } finally {
        long cost = System.currentTimeMillis() - start;
        if (sqlType != SQLType.SELECT || cost > 50) {
            logger.debug("parser sql:{}, cost:{}ms", txcStatement.getSql(), cost);
        }
    }
    return sqlType;
}