com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor Java Examples

The following examples show how to use com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SelectParser.java    From dts with Apache License 2.0 6 votes vote down vote up
@Override
protected String getTableName(SQLSelectStatement parseSqlStatement) {
    SQLSelectQueryBlock selectQueryBlock = parseSqlStatement.getSelect().getQueryBlock();
    SQLTableSource tableSource = selectQueryBlock.getFrom();
    StringBuffer sb = new StringBuffer();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(sb) {

        @Override
        public boolean visit(SQLExprTableSource x) {
            printTableSourceExpr(x.getExpr());
            return false;
        }
    };
    visitor.visit((SQLExprTableSource)tableSource);
    return sb.toString();
}
 
Example #2
Source File: MysqlVisitor.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
void buildTableName(TableNode tableNode, StringBuilder sb) {
    String tableName = "`" + tableNode.getPureName() + "`";
    String fullName = "`" + tableNode.getPureSchema() + "`." + tableName;
    mapTableToSimple.put(fullName, tableName);
    sb.append(" ").append(fullName);
    String alias = tableNode.getAlias();
    if (alias != null) {
        sb.append(" `").append(alias).append("`");
    }
    List<SQLHint> hintList = tableNode.getHintList();
    if (hintList != null && !hintList.isEmpty()) {
        sb.append(' ');
        boolean isFirst = true;
        for (SQLHint hint : hintList) {
            if (isFirst)
                isFirst = false;
            else
                sb.append(" ");
            MySqlOutputVisitor ov = new MySqlOutputVisitor(sb);
            ov.setShardingSupport(false);
            hint.accept(ov);
        }
    }
}
 
Example #3
Source File: MySqlSqlParser.java    From baymax with Apache License 2.0 6 votes vote down vote up
@Override
public void changeSql(ParseResult result, ExecutePlan plan) {
    if (ExecuteType.NO == plan.getExecuteType()){
    }else {
        for (TrargetSqlEntity sql : plan.getSqlList()){
            ReplaceTableNameVisitor replaceVisitor = new ReplaceTableNameVisitor(sql.getLogicTableName(), sql.getTargetTableName());
            StringBuilder out = new StringBuilder();
            MySqlOutputVisitor outPutVisitor = new MySqlOutputVisitor(out);
            // 替换表名
            statement.accept(replaceVisitor);
            // 输出sql
            statement.accept(outPutVisitor);
            sql.setOriginalSql(result.getSql());
            sql.setTargetSql(out.toString());
            // 输出sql后要还原statement以便下次替换表名
            replaceVisitor.reset();
        }
    }
}
 
Example #4
Source File: ReplaceTableNameVisitorTest.java    From baymax with Apache License 2.0 6 votes vote down vote up
public void test(String sql, String logicName, String targetName){

        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement statement = parser.parseStatement();

        ReplaceTableNameVisitor replaceVisitor = new ReplaceTableNameVisitor(logicName, targetName);
        StringBuilder out = new StringBuilder();
        MySqlOutputVisitor outPutVisitor = new MySqlOutputVisitor(out);
        // 替换表名
        statement.accept(replaceVisitor);
        // 输出sql
        statement.accept(outPutVisitor);

        System.out.println();
        System.out.println(sql);
        System.out.println(out.toString());
        // 输出sql后要还原statement以便下次替换表名
        replaceVisitor.reset();
    }
 
Example #5
Source File: MySQLItemVisitor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
private void initName(SQLExpr expr) {
    StringBuilder sb = new StringBuilder();
    MySqlOutputVisitor ov = new MySqlOutputVisitor(sb);
    ov.setShardingSupport(false);
    expr.accept(ov);
    item.setItemName(sb.toString());
}
 
Example #6
Source File: Item.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
public final String getItemName() {
    if (itemName == null || itemName.length() == 0) {
        SQLExpr expr = toExpression();
        StringBuilder sb = new StringBuilder();
        MySqlOutputVisitor ov = new MySqlOutputVisitor(sb);
        ov.setShardingSupport(false);
        expr.accept(ov);
        itemName = sb.toString();
    }
    return itemName;
}
 
Example #7
Source File: DefaultDruidParser.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
String statementToString(SQLStatement statement) {
    StringBuffer buf = new StringBuffer();
    MySqlOutputVisitor visitor = new MySqlOutputVisitor(buf);
    visitor.setShardingSupport(false);
    statement.accept(visitor);
    return buf.toString();
}
 
Example #8
Source File: MySqlSelectAggParserTest.java    From baymax with Apache License 2.0 5 votes vote down vote up
/**
 * //agg
 * getMergeColumns
 * getAliaColumns
 *
 * //group by
 * setGroupbyColumns
 *
 * @param sql
 */
public void test(String sql){
    ParseResult result = new ParseResult();
    ExecutePlan plan = new ExecutePlan();

    parser.init(sql, null);
    parser.parse(result);

    plan.setExecuteType(ExecuteType.PARTITION);

    SQLSelectStatement stmt = (SQLSelectStatement) parser.statement;

    parser.parseMysqlQueary(result, plan, (MySqlSelectQueryBlock) stmt.getSelect().getQuery());

    StringBuilder out = new StringBuilder();
    MySqlOutputVisitor outPutVisitor = new MySqlOutputVisitor(out);
    stmt.accept(outPutVisitor);

    System.out.println();
    System.out.println("/***********************agg*************************/");
    System.out.println(plan.getMergeColumns());
    System.out.println("setHasAllColumnExpr:" + result.isHasAllColumnExpr());
    System.out.println(out.toString());
    PrintUtil.printFildAlisMap(result.getAliaColumns());
    System.out.println("/*********************group by**********************/");
    System.out.println(plan.getGroupbyColumns());
    System.out.println("/*********************order by**********************/");
    System.out.println(plan.getOrderbyColumns());

}