use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class Oracle_param_2 method test_for_parameterize.
public void test_for_parameterize() throws Exception {
final DbType dbType = JdbcConstants.MYSQL;
String sql = "SELECT TO_DATE('2013-02-11', 'YYYY-MM-DD') FROM dual;";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement statement = stmtList.get(0);
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, JdbcConstants.ORACLE);
List<Object> parameters = new ArrayList<Object>();
visitor.setParameterized(true);
visitor.setParameterizedMergeInList(true);
visitor.setParameters(parameters);
/*visitor.setPrettyFormat(false);*/
statement.accept(visitor);
/* JSONArray array = new JSONArray();
for(String table : visitor.getTables()){
array.add(table.replaceAll("`",""));
}*/
String psql = out.toString();
assertEquals("SELECT TO_DATE(?, 'YYYY-MM-DD')\n" + "FROM dual;", psql);
String params_json = JSONArray.toJSONString(parameters, SerializerFeature.WriteClassName);
System.out.println(params_json);
JSONArray jsonArray = JSON.parseArray(params_json);
String json = JSONArray.toJSONString(jsonArray, SerializerFeature.WriteClassName);
assertEquals("[\"2013-02-11\"]", json);
String rsql = SQLUtils.toSQLString(SQLUtils.parseStatements(psql, dbType), dbType, jsonArray);
assertEquals("SELECT TO_DATE('2013-02-11', 'YYYY-MM-DD')\n" + "FROM dual;", rsql);
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class SQLUtils method toSQLString.
public static String toSQLString(SQLObject obj) {
if (obj instanceof SQLStatement) {
SQLStatement stmt = (SQLStatement) obj;
return toSQLString(stmt, stmt.getDbType());
}
if (obj instanceof MySqlObject) {
return toMySqlString(obj);
}
StringBuilder out = new StringBuilder();
obj.accept(new SQLASTOutputVisitor(out));
String sql = out.toString();
return sql;
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class SQLUtils method toSQLString.
public static String toSQLString(List<SQLStatement> statementList, DbType dbType, List<Object> parameters, FormatOption option, Map<String, String> tableMapping) {
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = createFormatOutputVisitor(out, statementList, dbType);
if (parameters != null) {
visitor.setInputParameters(parameters);
}
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setFeatures(option.features);
if (tableMapping != null) {
visitor.setTableMapping(tableMapping);
}
boolean printStmtSeperator;
if (DbType.sqlserver == dbType) {
printStmtSeperator = false;
} else {
printStmtSeperator = DbType.oracle != dbType;
}
for (int i = 0, size = statementList.size(); i < size; i++) {
SQLStatement stmt = statementList.get(i);
if (i > 0) {
SQLStatement preStmt = statementList.get(i - 1);
if (printStmtSeperator && !preStmt.isAfterSemi()) {
visitor.print(";");
}
List<String> comments = preStmt.getAfterCommentsDirect();
if (comments != null) {
for (int j = 0; j < comments.size(); ++j) {
String comment = comments.get(j);
if (j != 0) {
visitor.println();
}
visitor.printComment(comment);
}
}
if (printStmtSeperator) {
visitor.println();
}
if (!(stmt instanceof SQLSetStatement)) {
visitor.println();
}
}
// {
// List<String> comments = stmt.getBeforeCommentsDirect();
// if (comments != null){
// for(String comment : comments) {
// visitor.printComment(comment);
// visitor.println();
// }
// }
// }
stmt.accept(visitor);
if (i == size - 1) {
List<String> comments = stmt.getAfterCommentsDirect();
if (comments != null) {
for (int j = 0; j < comments.size(); ++j) {
String comment = comments.get(j);
if (j != 0) {
visitor.println();
}
visitor.printComment(comment);
}
}
}
}
return out.toString();
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class SqlHolder method getParams.
public String getParams() {
ensureParsed();
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);
ast.accept(visitor);
String params = JSONArray.toJSONString(parameters, SerializerFeature.WriteClassName);
params = StringUtils.replace(params, "\"", "\\\"");
return params;
}
use of com.alibaba.druid.sql.visitor.SQLASTOutputVisitor in project druid by alibaba.
the class Templates method parameterize.
public static String parameterize(SQLStatement ast, Set<String> physicalNames, List<Object> params) {
List<Object> parameters = null;
Appendable out = new StringBuilder();
SQLASTOutputVisitor visitor = new MySqlOutputVisitor(out);
visitor.setParameterized(true);
visitor.setParameterizedMergeInList(true);
if (physicalNames != null) {
visitor.setExportTables(true);
}
if (params != null) {
parameters = new ArrayList<Object>();
visitor.setParameters(parameters);
}
visitor.setPrettyFormat(false);
ast.accept(visitor);
if (physicalNames != null) {
Set<String> tableSet = visitor.getTables();
if (tableSet != null) {
physicalNames.addAll(tableSet);
}
}
if (params != null) {
if (!parameters.isEmpty()) {
params.addAll(parameters);
}
}
String sqlTemplate = out.toString();
return sqlTemplate;
}
Aggregations