Search in sources :

Example 6 with ExportParameterVisitor

use of com.alibaba.druid.sql.visitor.ExportParameterVisitor 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 7 with ExportParameterVisitor

use of com.alibaba.druid.sql.visitor.ExportParameterVisitor 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)

Example 8 with ExportParameterVisitor

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

the class ExportParameters method test_export_parameters.

public void test_export_parameters() throws Exception {
    String sql = "select * from t where id = 3 and name = 'abc'";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    StringBuilder out = new StringBuilder();
    ExportParameterVisitor visitor = new MySqlExportParameterVisitor(out);
    for (SQLStatement stmt : stmtList) {
        stmt.accept(visitor);
    }
    String paramteredSql = out.toString();
    System.out.println(paramteredSql);
    // [3, "abc"]
    List<Object> paramters = visitor.getParameters();
    for (Object param : paramters) {
        System.out.println(param);
    }
}
Also used : 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 9 with ExportParameterVisitor

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

the class ExportAndParameterizedVisitorTestCase method testParameterizedVisitor.

public void testParameterizedVisitor() {
    // final String sql =
    // "insert  into tab01(a,b,c) values('a1','bXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1',5)";
    Object[][] sqlAndExpectedCases = { { "insert  into tab01(a,b,c) values('a1','b1',5)", 3, "a1" }, { "select * from tab01 where a=1 and b='b1'", 2, 1 }, { "update tab01 set d='d1' where a=1 and b='b1'", 3, "d1" }, { "delete from tab01 where a=1 and b='b1'", 2, 1.0 } };
    String[] dbTypes = { "mysql", "oracle", "db2", JdbcConstants.POSTGRESQL, JdbcUtils.JTDS, "not-found" };
    // String[]  dbTypes = { JdbcUtils.JTDS};
    for (String dbType : dbTypes) {
        System.out.println("dbType:" + dbType);
        for (Object[] arr : sqlAndExpectedCases) {
            final String sql = (String) arr[0];
            StringBuilder out = new StringBuilder();
            final SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
            final ParameterizedVisitor pVisitor = (ParameterizedVisitor) ExportParameterVisitorUtils.createExportParameterVisitor(out, dbType);
            final SQLStatement parseStatement = parser.parseStatement();
            parseStatement.accept(pVisitor);
            // final ExportParameterVisitor vistor2 = new MySqlExportParameterVisitor();
            // parseStatement.accept(vistor2);
            final ExportParameterVisitor vistor2 = (ExportParameterVisitor) pVisitor;
            System.out.println("before:" + sql);
            System.out.println("after:" + out);
            System.out.println("size:" + vistor2.getParameters());
            final int expectedSize = (Integer) arr[1];
            Assert.assertEquals(expectedSize, vistor2.getParameters().size());
        }
    }
}
Also used : ParameterizedVisitor(com.alibaba.druid.sql.visitor.ParameterizedVisitor) ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)9 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)7 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)6 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)5 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 MySqlForceIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint)1 MySqlIgnoreIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)1 MySqlUseIndexHint (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint)1 ParameterizedVisitor (com.alibaba.druid.sql.visitor.ParameterizedVisitor)1