Search in sources :

Example 46 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_26 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT `xxx_reverse_od`.`id` from xxx_reverse_od_0446 `xxx_reverse_od` where `xxx_reverse_od`.name=1 limit 1;";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT `xxx_reverse_od`.`id`\n" + "FROM xxx_reverse_od `xxx_reverse_od`\n" + "WHERE `xxx_reverse_od`.name = ?\n" + "LIMIT ?;", 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`.`id`\n" + "FROM xxx_reverse_od_0446 `xxx_reverse_od`\n" + "WHERE `xxx_reverse_od`.name = ?\n" + "LIMIT ?;", 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 47 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_20 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select `a_entry_2664`.`id` from `a_entry_2664`";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT a_entry.`id`\n" + "FROM a_entry", 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("a_entry", "a_entry_2664");
    visitor1.setParameters(visitor.getParameters());
    stmt.accept(visitor1);
    assertEquals("SELECT a_entry_2664.`id`\n" + "FROM a_entry_2664", 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 48 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_23 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "select `wmc_xxx_s`.fid from `wmc_xxx_s_0479` as `wmc_xxx_s` where `wmc_xxx_s`.fid = 3";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT `wmc_xxx_s`.fid\n" + "FROM wmc_xxx_s `wmc_xxx_s`\n" + "WHERE `wmc_xxx_s`.fid = ?", 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("wmc_xxx_s", "wmc_xxx_s_0479");
    visitor1.setParameters(visitor.getParameters());
    stmt.accept(visitor1);
    assertEquals("SELECT `wmc_xxx_s`.fid\n" + "FROM wmc_xxx_s_0479 `wmc_xxx_s`\n" + "WHERE `wmc_xxx_s`.fid = ?", 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 49 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_25 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final DbType dbType = JdbcConstants.MYSQL;
    String sql = "SELECT `xxx_reverse_order`.`id` from xxx_reverse_order_0446 XXX_REVERSE_ORDER limit 1;";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT `xxx_reverse_order`.`id`\n" + "FROM xxx_reverse_order XXX_REVERSE_ORDER\n" + "LIMIT ?;", 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_order", "xxx_reverse_order_0446");
    visitor1.setParameters(visitor.getParameters());
    stmt.accept(visitor1);
    assertEquals("SELECT `xxx_reverse_order`.`id`\n" + "FROM xxx_reverse_order_0446 XXX_REVERSE_ORDER\n" + "LIMIT ?;", 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 50 with SQLASTOutputVisitor

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

the class MySqlParameterizedOutputVisitorTest_27 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` where `xxx_reverse_od`.name=1 group by `xxx_reverse_od`.dept order by `xxx_reverse_od`.dept";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("SELECT `xxx_reverse_od`.dept, count(*)\n" + "FROM xxx_reverse_od `xxx_reverse_od`\n" + "WHERE `xxx_reverse_od`.name = ?\n" + "GROUP BY `xxx_reverse_od`.dept\n" + "ORDER BY `xxx_reverse_od`.dept", 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" + "WHERE `xxx_reverse_od`.name = ?\n" + "GROUP BY `xxx_reverse_od`.dept\n" + "ORDER BY `xxx_reverse_od`.dept", 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)

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