Search in sources :

Example 11 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_50_orderby method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select * from t order by 1";
    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.MYSQL);
    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();
    System.out.println(psql);
    assertEquals("SELECT *\n" + "FROM t\n" + "ORDER BY 1", psql);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 12 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_28 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT `xxx_reverse_od`.dept, count(*) from xxx_reverse_od_0446 `xxx_reverse_od` join abc_xx_abc `aa` on `xxx_reverse_od`.id = `aa`.id";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT `xxx_reverse_od`.dept, count(*)\n" + "FROM xxx_reverse_od `xxx_reverse_od`\n" + "\tJOIN abc_xx_abc `aa` ON `xxx_reverse_od`.id = `aa`.id", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    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);
    visitor.setExportTables(true);
    /*visitor.setPrettyFormat(false);*/
    SQLStatement stmt = stmtList.get(0);
    stmt.accept(visitor);
    // System.out.println(parameters);
    assertEquals(0, parameters.size());
    StringBuilder buf = new StringBuilder();
    SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
    visitor1.addTableMapping("xxx_reverse_od", "xxx_reverse_od_0446");
    visitor1.setParameters(visitor.getParameters());
    stmt.accept(visitor1);
    assertEquals("SELECT `xxx_reverse_od`.dept, count(*)\n" + "FROM xxx_reverse_od_0446 `xxx_reverse_od`\n" + "\tJOIN abc_xx_abc `aa` ON `xxx_reverse_od`.id = `aa`.id", buf.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 13 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_33 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select * from t where id = 1 or id = 2";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id = ?", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    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);
    visitor.setExportTables(true);
    /*visitor.setPrettyFormat(false);*/
    SQLStatement stmt = stmtList.get(0);
    stmt.accept(visitor);
    // System.out.println(parameters);
    assertEquals(1, parameters.size());
    // SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(psql, dbType);
    // List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement pstmt = SQLUtils.parseStatements(psql, dbType).get(0);
    StringBuilder buf = new StringBuilder();
    SQLASTOutputVisitor visitor1 = SQLUtils.createOutputVisitor(buf, dbType);
    visitor1.addTableMapping("udata", "udata_0888");
    visitor1.setInputParameters(visitor.getParameters());
    pstmt.accept(visitor1);
    assertEquals("SELECT *\n" + "FROM t\n" + "WHERE id IN (1, 2)", buf.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 14 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_37 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "insert into fc_sms_0011_201704 (c1, c2, c3) values (1, 'a', 'b')";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement statement = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    // List<Object> parameters = new ArrayList<Object>();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.MYSQL);
    visitor.setParameterized(true);
    visitor.setParameterizedMergeInList(true);
    // visitor.setParameters(parameters);
    visitor.setExportTables(true);
    visitor.setPrettyFormat(false);
    statement.accept(visitor);
    assertEquals("INSERT INTO fc_sms (c1, c2, c3) VALUES (?, ?, ?)", out.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType)

Example 15 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_43 method restore.

public static String restore(String sql, String table, String params) /*JSONArray paramsArray, JSONArray destArray*/
{
    JSONArray destArray = JSON.parseArray(table.replaceAll("''", "'"));
    params = StringUtils.replace(params.replaceAll("''", "'"), "\\\"", "\"");
    JSONArray paramsArray = JSON.parseArray(params);
    DbType dbType = JdbcConstants.MYSQL;
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, dbType);
    List<Object> paramsList = new ArrayList<Object>(paramsArray);
    visitor.setParameters(paramsList);
    SchemaStatVisitor schemaStatVisitor = new MySqlSchemaStatVisitor();
    stmt.accept(schemaStatVisitor);
    JSONArray srcArray = new JSONArray();
    for (Map.Entry<TableStat.Name, TableStat> entry : schemaStatVisitor.getTables().entrySet()) {
        System.out.println(entry.getKey().getName());
        srcArray.add(entry.getKey().getName());
    }
    for (int i = 0; i < srcArray.size(); i++) {
        visitor.addTableMapping(srcArray.getString(i), destArray.getString(i));
    }
    stmt.accept(visitor);
    return out.toString();
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) Map(java.util.Map) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

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