Java Code Examples for com.alibaba.druid.sql.parser.SQLParserUtils

The following examples show how to use com.alibaba.druid.sql.parser.SQLParserUtils. 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: Mycat2   Source File: PreparedStatement.java    License: 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
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 3
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 4
Source Project: DataLink   Source File: DBUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static void sqlValid(String sql, DataBaseType dataBaseType){
    SQLStatementParser statementParser = SQLParserUtils.createSQLStatementParser(sql,dataBaseType.getTypeName());
    statementParser.parseStatementList();
}
 
Example 5
Source Project: Mycat2   Source File: TableStatAnalyzer.java    License: GNU General Public License v3.0 4 votes vote down vote up
private SQLStatement parseStmt(String sql) {
	SQLStatementParser statParser = SQLParserUtils.createSQLStatementParser(sql, "mysql");
	SQLStatement stmt = statParser.parseStatement();
	return stmt;		
}
 
Example 6
Source Project: dble   Source File: TableStatAnalyzer.java    License: GNU General Public License v2.0 4 votes vote down vote up
private SQLStatement parseStmt(String sql) {
    SQLStatementParser statParser = SQLParserUtils.createSQLStatementParser(sql, "mysql");
    SQLStatement stmt = statParser.parseStatement();
    return stmt;
}