Search in sources :

Example 1 with SQLASTVisitorAdapter

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));
// 
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) ArrayList(java.util.ArrayList) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HiveCreateTableStatement(com.alibaba.druid.sql.dialect.hive.stmt.HiveCreateTableStatement) SQLAdhocTableSource(com.alibaba.druid.sql.ast.SQLAdhocTableSource) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Example 2 with SQLASTVisitorAdapter

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());
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLASTVisitor(com.alibaba.druid.sql.visitor.SQLASTVisitor) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 3 with SQLASTVisitorAdapter

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));
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLASTVisitor(com.alibaba.druid.sql.visitor.SQLASTVisitor) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) HashMap(java.util.HashMap) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 4 with SQLASTVisitorAdapter

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);
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLAlterTableDropIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLAllExpr(com.alibaba.druid.sql.ast.expr.SQLAllExpr) SQLReleaseSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement) SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) SQLCreateDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement) SQLDropViewStatement(com.alibaba.druid.sql.ast.statement.SQLDropViewStatement) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAlterTableDisableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint) SQLSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLSavePointStatement) SQLAlterTableDropConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLCallStatement(com.alibaba.druid.sql.ast.statement.SQLCallStatement) SQLSomeExpr(com.alibaba.druid.sql.ast.expr.SQLSomeExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLExprHint(com.alibaba.druid.sql.ast.statement.SQLExprHint) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLCurrentOfCursorExpr(com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr) SQLCommentStatement(com.alibaba.druid.sql.ast.statement.SQLCommentStatement) SQLColumnCheck(com.alibaba.druid.sql.ast.statement.SQLColumnCheck) SQLAnyExpr(com.alibaba.druid.sql.ast.expr.SQLAnyExpr) SQLAlterTableAlterColumn(com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLDefaultExpr(com.alibaba.druid.sql.ast.expr.SQLDefaultExpr) SQLAlterTableEnableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint)

Example 5 with SQLASTVisitorAdapter

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;
        }
    };
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SchemaRepository(com.alibaba.druid.sql.repository.SchemaRepository)

Aggregations

SQLASTVisitorAdapter (com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)3 SQLAdhocTableSource (com.alibaba.druid.sql.ast.SQLAdhocTableSource)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLOver (com.alibaba.druid.sql.ast.SQLOver)1 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)1 SQLAllExpr (com.alibaba.druid.sql.ast.expr.SQLAllExpr)1 SQLAnyExpr (com.alibaba.druid.sql.ast.expr.SQLAnyExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLCurrentOfCursorExpr (com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr)1 SQLDefaultExpr (com.alibaba.druid.sql.ast.expr.SQLDefaultExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1