Search in sources :

Example 1 with ExportParameterVisitor

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

the class MySqlOutputVisitor method visit.

public boolean visit(SQLCharExpr x) {
    if (this.parameterized && ParameterizedOutputVisitorUtils.checkParameterize(x)) {
        print('?');
        incrementReplaceCunt();
        if (this instanceof ExportParameterVisitor || this.parameters != null) {
            ExportParameterVisitorUtils.exportParameter(this.parameters, x);
        }
        return false;
    }
    print('\'');
    String text = x.getText();
    StringBuilder buf = new StringBuilder(text.length());
    for (int i = 0; i < text.length(); ++i) {
        char ch = text.charAt(i);
        if (ch == '\'') {
            buf.append('\'');
            buf.append('\'');
        } else if (ch == '\\') {
            buf.append('\\');
            buf.append('\\');
        } else if (ch == '\0') {
            buf.append('\\');
            buf.append('0');
        } else {
            buf.append(ch);
        }
    }
    if (buf.length() != text.length()) {
        text = buf.toString();
    }
    print0(text);
    print('\'');
    return false;
}
Also used : ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor) MySqlForceIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) MySqlUseIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint) MySqlIgnoreIndexHint(com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint)

Example 2 with ExportParameterVisitor

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

the class WallVisitorUtils method checkCondition.

private static void checkCondition(WallVisitor visitor, SQLExpr x) {
    if (x == null) {
        return;
    }
    if (visitor.getConfig().isMustParameterized()) {
        ExportParameterVisitor exportParameterVisitor = visitor.getProvider().createExportParameterVisitor();
        x.accept(exportParameterVisitor);
        if (exportParameterVisitor.getParameters().size() > 0) {
            addViolation(visitor, ErrorCode.NOT_PARAMETERIZED, "sql must parameterized", x);
        }
    }
}
Also used : ExportParameterVisitor(com.alibaba.druid.sql.visitor.ExportParameterVisitor)

Example 3 with ExportParameterVisitor

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

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

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

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