Search in sources :

Example 1 with MySqlExportParameterVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor in project druid by alibaba.

the class MySqlParameterizedOutputVisitorTest_16 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final String dbType = JdbcConstants.MYSQL;
    String sql = "/* 0bfacfa414829200086238910e/0.3// */" + "insert into `t1` (" + " `f0`, `f1`, `f2`, `f3`, `f4`, " + "`f5`, `f6`, `f7`, `f8`, `f9`, " + "`destination`, `start_standard`, `start_fee`, `add_standard`, `add_fee`, " + "`region_fee_standard`, `region_fee_add`, `cell_fee`, `way_day`, `version`)" + " values ( 1, 2, 2, 3, 0, -4, 1, null, '2016-12-28 18:13:28.825', '2016-12-28 18:13:28.825', 1, 1, 0, 1, 0, null, null, null, null, 0)\n";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("INSERT INTO `t1` (`f0`, `f1`, `f2`, `f3`, `f4`\n" + "\t, `f5`, `f6`, `f7`, `f8`, `f9`\n" + "\t, `destination`, `start_standard`, `start_fee`, `add_standard`, `add_fee`\n" + "\t, `region_fee_standard`, `region_fee_add`, `cell_fee`, `way_day`, `version`)\n" + "VALUES (?, ?, ?, ?, ?\n" + "\t\t, ?, ?, ?, ?, ?\n" + "\t\t, ?, ?, ?, ?, ?\n" + "\t\t, ?, ?, ?, ?, ?)", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    StringBuilder out = new StringBuilder();
    ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
    assertEquals(20, visitor.getParameters().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 2 with MySqlExportParameterVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor in project druid by alibaba.

the class MySqlParameterizedOutputVisitorTest_17 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    final String dbType = JdbcConstants.MYSQL;
    String sql = "replace into `mytable_0228` " + "( `user_id`, `c_level`, `l_level`, `t_level`, `v_level`, `tag`) " + "values ( 2272895716, 'C1', null, 'T1', 'V0', '0') ";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    assertEquals("REPLACE INTO mytable (`user_id`, `c_level`, `l_level`, `t_level`, `v_level`, `tag`)\n" + "VALUES (?, ?, ?, ?, ?\n" + "\t, ?)", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    StringBuilder out = new StringBuilder();
    ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
    assertEquals(6, visitor.getParameters().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 3 with MySqlExportParameterVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor in project druid by alibaba.

the class MySqlParameterizedOutputVisitorTest_8 method test_parameterize.

public void test_parameterize() throws Exception {
    final String dbType = JdbcConstants.MYSQL;
    String sql = "insert into test values(2,1) on duplicate key update ts=ts % 10000 +1";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    Assert.assertEquals("INSERT INTO test\n" + "VALUES (?, ?)\n" + "ON DUPLICATE KEY UPDATE ts = ts % ? + ?", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    StringBuilder out = new StringBuilder();
    ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
    Assert.assertEquals(4, visitor.getParameters().size());
    Assert.assertEquals(2, visitor.getParameters().get(0));
    Assert.assertEquals(1, visitor.getParameters().get(1));
    Assert.assertEquals(10000, visitor.getParameters().get(2));
    Assert.assertEquals(1, visitor.getParameters().get(3));
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 4 with MySqlExportParameterVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor in project druid by alibaba.

the class MySqlParameterizedOutputVisitorTest_9 method test_parameterize.

public void test_parameterize() throws Exception {
    final String dbType = JdbcConstants.MYSQL;
    String sql = "select * from t limit 3, 4";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    Assert.assertEquals("SELECT *\n" + "FROM t\n" + "LIMIT ?, ?", psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    StringBuilder out = new StringBuilder();
    ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
    Assert.assertEquals(2, visitor.getParameters().size());
    Assert.assertEquals(3, visitor.getParameters().get(0));
    Assert.assertEquals(4, visitor.getParameters().get(1));
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 5 with MySqlExportParameterVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor in project druid by alibaba.

the class MySqlParameterizedOutputVisitorTest_10 method test_for_parameterize.

public void test_for_parameterize() throws Exception {
    /*String instance = "100.81.152.9"+"_"+3314;
        int urlNum = Math.abs(instance.hashCode()) % 2;
        System.out.println(urlNum);*/
    /* String formattedSql = SQLUtils.format("select * from ? where id = ?", JdbcConstants.MYSQL,
                Arrays.<Object> asList("abc,a"));
        System.out.println(formattedSql);*/
    final String dbType = JdbcConstants.MYSQL;
    String sql = "SELECT `SURVEY_ANSWER`.`TIME_UPDATED`, `SURVEY_ANSWER`.`ANSWER_VALUE` FROM `S_ANSWER_P0115` `SURVEY_ANSWER` WHERE `SURVEY_ANSWER`.`SURVEY_ID` = 11 AND `SURVEY_ANSWER`.`QUESTION_CODE` = 'qq' ORDER BY `SURVEY_ANSWER`.`TIME_UPDATED` DESC LIMIT 1, 2";
    String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
    /* Assert.assertEquals("SELECT *\n" +
                "FROM t\n" +
                "LIMIT ?, ?", psql);*/
    System.out.println(psql);
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    StringBuilder out = new StringBuilder();
    ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
    System.out.println(visitor.getParameters());
    stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    visitor = new MySqlExportParameterVisitor();
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
    System.out.println(visitor.getParameters());
/*  Assert.assertEquals(2, visitor.getParameters().size());
        Assert.assertEquals(3, visitor.getParameters().get(0));
        Assert.assertEquals(4, visitor.getParameters().get(1));*/
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)6 ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)6 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)5