Search in sources :

Example 31 with SQLASTOutputVisitor

use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.

the class Oracle_param_2 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT TO_DATE('2013-02-11', 'YYYY-MM-DD') FROM dual;";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement statement = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.ORACLE);
    List<Object> parameters = new ArrayList<Object>();
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setParameters(parameters);
    /*visitor.setPrettyFormat(false);*/
    statement.accept(visitor);
    /* JSONArray array = new JSONArray();
        for(String table : visitor.getTables()){
            array.add(table.replaceAll("`",""));
        }*/
    String psql = out.toString();
    assertEquals("SELECT TO_DATE(?, 'YYYY-MM-DD')\n" + "FROM dual;", psql);
    String params_json = JSONArray.toJSONString(parameters, SerializerFeature.WriteClassName);
    System.out.println(params_json);
    JSONArray jsonArray = JSON.parseArray(params_json);
    String json = JSONArray.toJSONString(jsonArray, SerializerFeature.WriteClassName);
    assertEquals("[\"2013-02-11\"]", json);
    String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
    assertEquals("SELECT TO_DATE('2013-02-11', 'YYYY-MM-DD')\n" + "FROM dual;", rsql);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 32 with SQLASTOutputVisitor

use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.

the class SQLUtils method toSQLString.

public static String toSQLString(SQLObject obj) {
    if (obj instanceof SQLStatement) {
        SQLStatement stmt = (SQLStatement) obj;
        return toSQLString(stmt, stmt.getDbType());
    }
    if (obj instanceof MySqlObject) {
        return toMySqlString(obj);
    }
    StringBuilder out = new StringBuilder();
    obj.accept(new SQLASTOutputVisitor(out));
    String sql = out.toString();
    return sql;
}
Also used : MySqlObject(com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)

Example 33 with SQLASTOutputVisitor

use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.

the class SQLUtils method toSQLString.

public static String toSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters, FormatOption option, Map<String, String> tableMapping) {
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = createFormatOutputVisitor(out, statementList, dbType);
    if (parameters != null) {
        visitor.setInputParameters(parameters);
    }
    if (option == null) {
        option = DEFAULT_FORMAT_OPTION;
    }
    visitor.setFeatures(option.features);
    if (tableMapping != null) {
        visitor.setTableMapping(tableMapping);
    }
    boolean printStmtSeperator;
    if (DbType.sqlserver == dbType) {
        printStmtSeperator = false;
    } else {
        printStmtSeperator = DbType.oracle != dbType;
    }
    for (int i = 0, size = statementList.size(); i < size; i++) {
        SQLStatement stmt = statementList.get(i);
        if (i > 0) {
            SQLStatement preStmt = statementList.get(i - 1);
            if (printStmtSeperator && !preStmt.isAfterSemi()) {
                visitor.print(";");
            }
            List<String> comments = preStmt.getAfterCommentsDirect();
            if (comments != null) {
                for (int j = 0; j < comments.size(); ++j) {
                    String comment = comments.get(j);
                    if (j != 0) {
                        visitor.println();
                    }
                    visitor.printComment(comment);
                }
            }
            if (printStmtSeperator) {
                visitor.println();
            }
            if (!(stmt instanceof SQLSetStatement)) {
                visitor.println();
            }
        }
        // {
        // List<String> comments = stmt.getBeforeCommentsDirect();
        // if (comments != null){
        // for(String comment : comments) {
        // visitor.printComment(comment);
        // visitor.println();
        // }
        // }
        // }
        stmt.accept(visitor);
        if (i == size - 1) {
            List<String> comments = stmt.getAfterCommentsDirect();
            if (comments != null) {
                for (int j = 0; j < comments.size(); ++j) {
                    String comment = comments.get(j);
                    if (j != 0) {
                        visitor.println();
                    }
                    visitor.printComment(comment);
                }
            }
        }
    }
    return out.toString();
}
Also used : SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)

Example 34 with SQLASTOutputVisitor

use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.

the class SqlHolder method getParams.

public String getParams() {
    ensureParsed();
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
    List<Object> parameters = new ArrayList<Object>();
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    visitor.setParameters(parameters);
    ast.accept(visitor);
    String params = JSONArray.toJSONString(parameters, SerializerFeature.WriteClassName);
    params = StringUtils.replace(params, "\"", "\\\"");
    return params;
}
Also used : SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLObject(com.alibaba.druid.sql.ast.SQLObject)

Example 35 with SQLASTOutputVisitor

use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.

the class Templates method parameterize.

public static String parameterize(SQLStatement ast, Set<String> physicalNames, List<Object> params) {
    List<Object> parameters = null;
    Appendable out = new StringBuilder();
    SQLASTOutputVisitor visitor = new MySqlOutputVisitor(out);
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    if (physicalNames != null) {
        visitor.setExportTables(true);
    }
    if (params != null) {
        parameters = new ArrayList<Object>();
        visitor.setParameters(parameters);
    }
    visitor.setPrettyFormat(false);
    ast.accept(visitor);
    if (physicalNames != null) {
        Set<String> tableSet = visitor.getTables();
        if (tableSet != null) {
            physicalNames.addAll(tableSet);
        }
    }
    if (params != null) {
        if (!parameters.isEmpty()) {
            params.addAll(parameters);
        }
    }
    String sqlTemplate = out.toString();
    return sqlTemplate;
}
Also used : MySqlOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)

Aggregations

SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)67 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)56 ArrayList (java.util.ArrayList)48 DbType (com.alibaba.druid.DbType)45 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)44 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)3 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)2 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)2 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLSelectGroupByClause (com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause)2 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)2 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)2 DB2OutputVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2OutputVisitor)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)2 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)2