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;
}
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);
}
}
}
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());
}
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());
}
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));
}
Aggregations