use of com.alibaba.druid.sql.visitor.SQLASTVisitor in project druid by alibaba.
the class ClearSchema_0 method test_insert_0.
public void test_insert_0() throws Exception {
String sql = "INSERT INTO testdb.Websites (name, country)\n" + "SELECT app_name, country FROM testdb.apps;";
SQLStatement stmt = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL).get(0);
SQLASTVisitor v = new SQLASTVisitorAdapter() {
@Override
public boolean visit(SQLPropertyExpr x) {
if (SQLUtils.replaceInParent(x, new SQLIdentifierExpr(x.getName()))) {
return false;
}
return super.visit(x);
}
};
stmt.accept(v);
assertEquals("INSERT INTO Websites (name, country)\n" + "SELECT app_name, country\n" + "FROM apps;", stmt.toString());
}
use of com.alibaba.druid.sql.visitor.SQLASTVisitor in project druid by alibaba.
the class Issue2038 method test_for_demo.
public void test_for_demo() throws Exception {
String sql = "select * from (select * from t union all select * from t1 union all select * from t3) xx";
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, DbType.db2);
SQLASTVisitor visitor = new DB2ASTVisitorAdapter() {
public boolean visit(SQLUnionQuery x) {
System.out.println("union");
return true;
}
public boolean visit(DB2SelectQueryBlock x) {
System.out.println("select");
return true;
}
};
for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}
}
use of com.alibaba.druid.sql.visitor.SQLASTVisitor in project druid by alibaba.
the class Issue1912 method test_for_issue.
public void test_for_issue() throws Exception {
String sql = "select a from t";
SQLStatementParser parser = new SQLStatementParser(sql);
SQLStatement stmt = parser.parseStatement();
final Map<String, String> columnMapping = new HashMap<String, String>();
columnMapping.put("a", "b");
SQLASTVisitor visitor = new SQLASTVisitorAdapter() {
public boolean visit(SQLIdentifierExpr x) {
String destColumn = columnMapping.get(x.getName());
if (destColumn != null) {
x.setName(destColumn);
}
return super.visit(x);
}
};
stmt.accept(visitor);
assertEquals("SELECT b\n" + "FROM t", SQLUtils.toSQLString(stmt));
}
use of com.alibaba.druid.sql.visitor.SQLASTVisitor in project druid by alibaba.
the class MySqlSelectTest_226 method test_0.
public void test_0() throws Exception {
String sql = "SELECT sum(`price`) AS aggregate, `aa` \n" + "FROM `na_orders`\n" + "WHERE `updated_at` > '2018-12-03 00:00:00'\n" + " AND `pay_status` = 1\n" + " AND 0 = 1\n" + "ORDER BY 1";
// System.out.println(sql);
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());
SQLStatement stmt = statementList.get(0);
SQLASTVisitor v = new MySqlASTVisitorAdapter() {
public boolean visit(SQLIdentifierExpr x) {
System.out.println(x.getName());
return true;
}
};
stmt.accept(v);
// assertEquals("SELECT userid, order, unionid\n" +
// "FROM t", stmt.toString());
//
// assertEquals("select userid, order, unionid\n" +
// "from t", stmt.clone().toLowerCaseString());
}
Aggregations