use of com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter in project druid by alibaba.
the class MySqlSelectTest_203_dla method test_0.
public void test_0() throws Exception {
String sql = "SELECT $1, $2 FROM\n" + "TABLE temp_1\n" + "(\n" + " $1 int,\n" + " $2 string\n" + ")\n" + "TBLPROPERTIES (\n" + " CATALOG='oss',\n" + " LOCATION='oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + " SCHEMA='jinluo_test0810'\n" + ")\n" + "META LIFECYCLE 1";
// System.out.println(sql);
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals(1, statementList.size());
assertEquals("SELECT $1, $2\n" + "FROM TABLE temp_1 (\n" + "\t$1 int,\n" + "\t$2 string\n" + ")\n" + "TBLPROPERTIES (\n" + "\t'CATALOG' = 'oss',\n" + "\t'LOCATION' = 'oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + "\t'SCHEMA' = 'jinluo_test0810'\n" + ")\n" + "META LIFECYCLE 1", stmt.toString());
assertEquals("select $1, $2\n" + "from table temp_1 (\n" + "\t$1 int,\n" + "\t$2 string\n" + ")\n" + "tblproperties (\n" + "\t'CATALOG' = 'oss',\n" + "\t'LOCATION' = 'oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + "\t'SCHEMA' = 'jinluo_test0810'\n" + ")\n" + "meta lifecycle 1", stmt.toLowerCaseString());
final TempTableNameGen tempTableNameGen = new TempTableNameGen() {
@Override
public String generateName() {
return "__temp_table_1";
}
};
final List<SQLCreateTableStatement> createTableStatementList = new ArrayList<SQLCreateTableStatement>();
SQLASTVisitorAdapter v = new MySqlASTVisitorAdapter() {
public boolean visit(SQLAdhocTableSource x) {
final String tableName = tempTableNameGen.generateName();
HiveCreateTableStatement createStmt = (HiveCreateTableStatement) x.getDefinition();
createStmt.setParent(null);
createStmt.setTableName(tableName);
createStmt.setExternal(true);
SQLUtils.replaceInParent(x, new SQLExprTableSource(tableName));
createTableStatementList.add(createStmt);
return false;
}
public boolean visit(SQLVariantRefExpr x) {
String name = x.getName();
if (name != null && name.startsWith("$")) {
SQLUtils.replaceInParent(x, new SQLIdentifierExpr(name));
}
return false;
}
};
stmt.accept(v);
for (SQLCreateTableStatement createStmt : createTableStatementList) {
System.out.println(createStmt.toString(VisitorFeature.OutputNameQuote));
}
System.out.println();
System.out.println(stmt.toString(VisitorFeature.OutputNameQuote));
//
}
use of com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter 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.SQLASTVisitorAdapter 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.SQLASTVisitorAdapter in project druid by alibaba.
the class SQLASTVisitorAdapterTest method test_adapter.
public void test_adapter() throws Exception {
SQLASTVisitorAdapter adapter = new SQLASTVisitorAdapter();
new SQLBinaryOpExpr().accept(adapter);
new SQLInListExpr().accept(adapter);
new SQLSelectQueryBlock().accept(adapter);
new SQLDropTableStatement().accept(adapter);
new SQLCreateTableStatement().accept(adapter);
new SQLDeleteStatement().accept(adapter);
new SQLCurrentOfCursorExpr().accept(adapter);
new SQLInsertStatement().accept(adapter);
new SQLUpdateStatement().accept(adapter);
new SQLNotNullConstraint().accept(adapter);
new SQLMethodInvokeExpr().accept(adapter);
new SQLCallStatement().accept(adapter);
new SQLSomeExpr().accept(adapter);
new SQLAnyExpr().accept(adapter);
new SQLAllExpr().accept(adapter);
new SQLDefaultExpr().accept(adapter);
new SQLCommentStatement().accept(adapter);
new SQLDropViewStatement().accept(adapter);
new SQLSavePointStatement().accept(adapter);
new SQLReleaseSavePointStatement().accept(adapter);
new SQLCreateDatabaseStatement().accept(adapter);
new SQLAlterTableDropIndex().accept(adapter);
new SQLOver().accept(adapter);
new SQLWithSubqueryClause().accept(adapter);
new SQLAlterTableAlterColumn().accept(adapter);
new SQLAlterTableStatement().accept(adapter);
new SQLAlterTableDisableConstraint().accept(adapter);
new SQLAlterTableEnableConstraint().accept(adapter);
new SQLColumnCheck().accept(adapter);
new SQLExprHint().accept(adapter);
new SQLAlterTableDropConstraint().accept(adapter);
}
use of com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter in project druid by alibaba.
the class Resolve_AllColumn_Test method test_resolve.
public void test_resolve() throws Exception {
SchemaRepository repository = new SchemaRepository(DbType.mysql);
repository.acceptDDL("create table t_emp(emp_id bigint, name varchar(20));");
SQLStatement stmt = SQLUtils.parseSingleMysqlStatement("select * from t_emp");
repository.resolve(stmt, SchemaResolveVisitor.Option.ResolveAllColumn);
assertEquals("SELECT emp_id, name\n" + "FROM t_emp", stmt.toString());
SQLSelectQueryBlock queryBlock = ((SQLSelectStatement) stmt).getSelect().getQueryBlock();
SQLIdentifierExpr expr = (SQLIdentifierExpr) queryBlock.getSelectList().get(0).getExpr();
assertNotNull(expr.getResolvedColumn());
new SQLASTVisitorAdapter() {
public boolean visit(SQLSelectQueryBlock queryBlock) {
final List<SQLSelectItem> selectList = queryBlock.getSelectList();
for (int i = 0; i < selectList.size(); i++) {
final SQLSelectItem selectItem = selectList.get(i);
final SQLExpr expr = selectItem.getExpr();
if (expr instanceof SQLAllColumnExpr) {
} else if (expr instanceof SQLPropertyExpr && ((SQLPropertyExpr) expr).getName().equals("*")) {
}
}
return true;
}
};
}
Aggregations