Search in sources :

Example 1 with OdpsQueryAliasStatement

use of com.alibaba.druid.sql.dialect.odps.ast.OdpsQueryAliasStatement in project druid by alibaba.

the class SqlTypeUDF method evaluate.

public String evaluate(String sql, String dbTypeName, boolean throwError) {
    if (sql == null || sql.isEmpty()) {
        return null;
    }
    DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
    SQLType sqlType = null;
    try {
        List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
        if (stmtList.size() == 0) {
            return SQLType.EMPTY.name();
        }
        int setCnt = 0, notSetCnt = 0;
        SQLStatement notSetStmt = null;
        for (SQLStatement stmt : stmtList) {
            if (stmt instanceof SQLSetStatement) {
                setCnt++;
            } else {
                notSetStmt = stmt;
                notSetCnt++;
            }
        }
        if (setCnt == stmtList.size()) {
            Lexer lexer = SQLParserUtils.createLexer(sql, dbType);
            sqlType = lexer.scanSQLTypeV2();
            if (sqlType != null) {
                return sqlType.name();
            }
            return SQLType.SET.name();
        }
        if (notSetCnt == 1) {
            Lexer lexer = SQLParserUtils.createLexer(sql, dbType);
            sqlType = lexer.scanSQLTypeV2();
            sqlType = SqlTypeUDF.getStmtSqlType(notSetStmt, sqlType);
        } else {
            for (SQLStatement stmt : stmtList) {
                if (stmt instanceof OdpsQueryAliasStatement || stmt instanceof OdpsDeclareVariableStatement) {
                    return SQLType.SCRIPT.name();
                }
            }
            Set<String> typeNameSet = new TreeSet<>();
            for (SQLStatement stmt : stmtList) {
                if (stmt instanceof SQLSetStatement) {
                    continue;
                }
                SQLType type = SqlTypeUDF.getStmtSqlType(stmt, null);
                if (type != null) {
                    typeNameSet.add(type.name());
                }
            }
            if (typeNameSet.size() == 1) {
                return typeNameSet.stream().findFirst().get();
            }
            if (typeNameSet.size() > 0) {
                StringBuffer buf = new StringBuffer();
                for (String s : typeNameSet) {
                    if (buf.length() != 0) {
                        buf.append(',');
                    }
                    buf.append(s);
                }
                return "MULTI:" + buf.toString();
            }
            sqlType = SQLType.MULTI;
        }
    } catch (ParserException ex) {
        sql = sql.trim();
        int semiIndex = sql.indexOf(';');
        if (semiIndex == sql.length() - 1 || semiIndex == -1 && sql.indexOf('\n') == -1) {
            String singleLineSqlType = getSqlTypeForSingleLineSql(sql, dbType);
            if (singleLineSqlType != null) {
                return singleLineSqlType;
            }
        } else {
            if (sql.lastIndexOf('\n', semiIndex) == -1) {
                String firstSql = sql.substring(0, semiIndex).trim().toLowerCase();
                if (firstSql.startsWith("set ")) {
                    String restSql = sql.substring(semiIndex + 1);
                    return evaluate(restSql, dbTypeName, throwError);
                }
            }
        }
        try {
            Lexer lexer = SQLParserUtils.createLexer(sql, dbType);
            int semiCnt = 0;
            for_: for (Token token = null; ; ) {
                lexer.nextToken();
                if (token == Token.VARIANT && lexer.token() == Token.COLONEQ) {
                    return SQLType.SCRIPT.name();
                }
                token = lexer.token();
                switch(token) {
                    case EOF:
                    case ERROR:
                        break for_;
                    case SEMI:
                        semiCnt++;
                        break;
                    default:
                        break;
                }
            }
        } catch (ParserException ignored) {
        }
        sqlType = SQLType.ERROR;
    } catch (Throwable ex) {
        if (throwError) {
            throw new IllegalArgumentException("error sql : \n" + sql, ex);
        }
        sqlType = SQLType.ERROR;
    }
    if (sqlType != null) {
        return sqlType.name();
    }
    return SQLType.UNKNOWN.name();
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) OdpsDeclareVariableStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsDeclareVariableStatement) TreeSet(java.util.TreeSet) OdpsQueryAliasStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsQueryAliasStatement)

Aggregations

DbType (com.alibaba.druid.DbType)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 OdpsDeclareVariableStatement (com.alibaba.druid.sql.dialect.odps.ast.OdpsDeclareVariableStatement)1 OdpsQueryAliasStatement (com.alibaba.druid.sql.dialect.odps.ast.OdpsQueryAliasStatement)1 TreeSet (java.util.TreeSet)1