Search in sources :

Example 36 with DbType

use of com.alibaba.druid.DbType 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)

Example 37 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SqlParams method evaluate.

public String evaluate(String sql, String dbTypeName, boolean throwError) {
    try {
        DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
        List<Object> outParameters = new ArrayList<Object>();
        ParameterizedOutputVisitorUtils.parameterize(sql, dbType, outParameters);
        return JSONUtils.toJSONString(outParameters);
    } catch (ParserException ex) {
        if (throwError) {
            throw new IllegalArgumentException("error sql : \n" + sql, ex);
        }
        return null;
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) ArrayList(java.util.ArrayList) DbType(com.alibaba.druid.DbType)

Example 38 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SqlSyntaxCheck method evaluate.

public Boolean evaluate(String sql, String dbTypeName, boolean throwError) {
    if (sql == null || sql.length() == 0) {
        return null;
    }
    DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
    try {
        SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
        List<SQLStatement> statementList = parser.parseStatementList();
        return true;
    } catch (ParserException ex) {
        if (throwError) {
            throw new IllegalArgumentException("error sql : \n" + sql, ex);
        }
        return false;
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 39 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class ExportConditionColumns method evaluate.

public String evaluate(String sql, String dbTypeName) {
    DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
    try {
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor);
        }
        StringBuffer buf = new StringBuffer();
        for (TableStat.Column column : visitor.getColumns()) {
            if ((!column.isWhere()) && !column.isJoin()) {
                continue;
            }
            if (buf.length() != 0) {
                buf.append(',');
            }
            buf.append(column.toString());
        }
        return buf.toString();
    } catch (Exception ex) {
        System.err.println("error sql : " + sql);
        ex.printStackTrace();
        return null;
    }
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 40 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SqlFormat method evaluate.

public String evaluate(String sql, String dbTypeName, boolean throwError) {
    DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
    try {
        SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
        List<SQLStatement> statementList = parser.parseStatementList();
        return SQLUtils.toSQLString(statementList, dbType);
    } catch (Exception ex) {
        if (throwError) {
            throw new IllegalArgumentException("error sql : \n" + sql, ex);
        }
        return sql;
    }
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) ParserException(com.alibaba.druid.sql.parser.ParserException)

Aggregations

DbType (com.alibaba.druid.DbType)114 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)76 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)70 ArrayList (java.util.ArrayList)50 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)45 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)21 TableStat (com.alibaba.druid.stat.TableStat)10 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)5 ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)5 ParserException (com.alibaba.druid.sql.parser.ParserException)4 Map (java.util.Map)4 JdbcParameter (com.alibaba.druid.proxy.jdbc.JdbcParameter)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)2 OracleSQLObject (com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject)2