com.alibaba.druid.sql.ast.statement.SQLExprTableSource Java Examples

The following examples show how to use com.alibaba.druid.sql.ast.statement.SQLExprTableSource. 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: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public boolean visit(MySqlDeleteStatement x) {
    setAliasMap();

    setMode(x, Mode.Delete);

    accept(x.getFrom());
    accept(x.getUsing());
    x.getTableSource().accept(this);

    if (x.getTableSource() instanceof SQLExprTableSource) {
        SQLName tableName = (SQLName) ((SQLExprTableSource) x.getTableSource()).getExpr();
        String ident = tableName.toString();
        setCurrentTable(x, ident);

        TableStat stat = this.getTableStat(ident,ident);
        stat.incrementDeleteCount();
    }

    accept(x.getWhere());

    accept(x.getOrderBy());
    accept(x.getLimit());

    return false;
}
 
Example #3
Source File: ExplainHandler.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isInsertSeq(ServerConnection c, String stmt, SchemaConfig schema) throws SQLException {
    SQLStatementParser parser = new MySqlStatementParser(stmt);
    MySqlInsertStatement statement = (MySqlInsertStatement) parser.parseStatement();
    String schemaName = schema == null ? null : schema.getName();
    SQLExprTableSource tableSource = statement.getTableSource();
    SchemaUtil.SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(c.getUser(), schemaName, tableSource);
    String tableName = schemaInfo.getTable();
    schema = schemaInfo.getSchemaConfig();
    TableConfig tableConfig = schema.getTables().get(tableName);
    if (tableConfig == null) {
        return false;
    } else if (tableConfig.isAutoIncrement()) {
        return true;
    }
    return false;
}
 
Example #4
Source File: DruidCreateIndexParser.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt,
                                 ServerSchemaStatVisitor visitor, ServerConnection sc, boolean isExplain) throws SQLException {
    rrs.setOnline(true);
    SQLCreateIndexStatement createStmt = (SQLCreateIndexStatement) stmt;
    SQLTableSource tableSource = createStmt.getTable();
    if (tableSource instanceof SQLExprTableSource) {
        String schemaName = schema == null ? null : schema.getName();
        SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, (SQLExprTableSource) tableSource);
        String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
        rrs.setStatement(statement);
        String noShardingNode = RouterUtil.isNoShardingDDL(schemaInfo.getSchemaConfig(), schemaInfo.getTable());
        if (noShardingNode != null) {
            RouterUtil.routeToSingleDDLNode(schemaInfo, rrs, noShardingNode);
            return schemaInfo.getSchemaConfig();
        }
        RouterUtil.routeToDDLNode(schemaInfo, rrs);
        return schemaInfo.getSchemaConfig();
    } else {
        String msg = "The DDL is not supported, sql:" + stmt;
        throw new SQLNonTransientException(msg);
    }
}
 
Example #5
Source File: SqlVisitor.java    From baymax with Apache License 2.0 6 votes vote down vote up
@Override
public boolean visit(MySqlDeleteStatement x) {
    setAliasMap();

    setMode(x, Mode.Delete);

    accept(x.getFrom());
    accept(x.getUsing());
    x.getTableSource().accept(this);

    if (x.getTableSource() instanceof SQLExprTableSource) {
        SQLName tableName = (SQLName) ((SQLExprTableSource) x.getTableSource()).getExpr();
        String ident = tableName.toString();
        setCurrentTable(x, ident);
        // 和父类只有这行不同
        TableStat stat = this.getTableStat(ident,ident);
        stat.incrementDeleteCount();
    }

    accept(x.getWhere());

    accept(x.getOrderBy());
    accept(x.getLimit());

    return false;
}
 
Example #6
Source File: ElasticSqlSelectParser.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Override
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
    if (lexer.identifierEquals(FnvHash.Constants.USING)) {
        return tableSource;
    }

    parseIndexHintList(tableSource);

    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        accept(Token.LPAREN);
        this.exprParser.names(((SQLExprTableSource) tableSource).getPartitions(), tableSource);
        accept(Token.RPAREN);
    }

    return super.parseTableSourceRest(tableSource);
}
 
Example #7
Source File: GlobalTableUtil.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private static SQLExprTableSource getDDLTableSource(SQLStatement statement) {
	SQLExprTableSource source = null;
	if (statement instanceof SQLAlterTableStatement) {
		source = ((SQLAlterTableStatement)statement).getTableSource();
		
	} else if (isCreate(statement)) {
		source = ((SQLCreateTableStatement)statement).getTableSource();
	}
	return source;
}
 
Example #8
Source File: DruidMycatRouteStrategy.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private SQLExprTableSource getDisTable(SQLTableSource tableSource,RouteResultsetNode node) throws SQLSyntaxErrorException{
	if(node.getSubTableName()==null){
		String msg = " sub table not exists for " + node.getName() + " on " + tableSource;
		LOGGER.error("DruidMycatRouteStrategyError " + msg);
		throw new SQLSyntaxErrorException(msg);
	}

	SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
	sqlIdentifierExpr.setParent(tableSource.getParent());
	sqlIdentifierExpr.setName(node.getSubTableName());
	SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
	return from2;
}
 
Example #9
Source File: ReplaceTableNameVisitor.java    From baymax with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(SQLExprTableSource astNode) {
    if (StringUtil.removeBackquote(astNode.toString()).equals(originalName)){
        if (isReplase){
            throw new BayMaxException("分区表名在一个Sql中只能出现一次:" + originalName + "," +newName);
        }else {
            node = (SQLIdentifierExpr) astNode.getExpr();
            node.setName(newName);
            isReplase = true;
        }
    }
    return true;
}
 
Example #10
Source File: DefaultSQLRewrite.java    From Zebra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(SQLExprTableSource x) {
	SQLName name = (SQLName) x.getExpr();
	String simpleName = name.getSimpleName();
	boolean hasQuote = simpleName.charAt(0) == '`';
	String tableName = hasQuote ? parseTableName(simpleName) : simpleName;
	String finalTable = tableMapping.get(tableName);

	if (finalTable != null) {
		if (hasQuote) {
			print0("`" + finalTable + "`");
		} else {
			print0(finalTable);
		}
	} else {
		x.getExpr().accept(this);
	}

	if (x.getAlias() != null) {
		print(' ');
		print0(x.getAlias());
	}

	for (int i = 0; i < x.getHintsSize(); ++i) {
		print(' ');
		x.getHints().get(i).accept(this);
	}

	return false;
}
 
Example #11
Source File: AbstractMySQLASTVisitor.java    From Zebra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(SQLExprTableSource x) {
	SQLName table = (SQLName) x.getExpr();
	String simpleName = table.getSimpleName();
	String tableName = simpleName.startsWith("`") ? parseTableName(simpleName) : simpleName;

	result.getRouterContext().getTableSet().add(tableName);

	return true;
}
 
Example #12
Source File: ElasticSqlSelectParser.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
protected SQLTableSource primaryTableSourceRest(SQLTableSource tableSource) {
    parseIndexHintList(tableSource);

    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        accept(Token.LPAREN);
        this.exprParser.names(((SQLExprTableSource) tableSource).getPartitions(), tableSource);
        accept(Token.RPAREN);
    }

    return tableSource;
}
 
Example #13
Source File: SQLUtils.java    From das with Apache License 2.0 4 votes vote down vote up
static boolean replaceTableName(SQLExprTableSource x, String tableShardId) {
    String baseName = x.getName().getSimpleName();
    x.setExpr(baseName + "_" + tableShardId);
    return true;
}
 
Example #14
Source File: MycatSchemaStatVisitor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
/**
     * 从between语句中获取字段所属的表名。
     * 对于容易出现ambiguous的(字段不知道到底属于哪个表),实际应用中必须使用别名来避免歧义
     * @param betweenExpr
     * @param column
     * @return
     */
    private String getOwnerTableName(SQLBetweenExpr betweenExpr,String column) {
        if(tableStats.size() == 1) {//只有一个表,直接返回这一个表名
            return tableStats.keySet().iterator().next().getName();
        } else if(tableStats.size() == 0) {//一个表都没有,返回空串
            return "";
        } else {//多个表名
            for (Column col : columns.keySet())
            {
                if(col.getName().equals(column)) {
                    return col.getTable();
                }
            }
//            for(Column col : columns) {//从columns中找表名
//                if(col.getName().equals(column)) {
//                    return col.getTable();
//                }
//            }

            //前面没找到表名的,自己从parent中解析

            SQLObject parent = betweenExpr.getParent();
            if(parent instanceof SQLBinaryOpExpr)
            {
                parent=parent.getParent();
            }

            if(parent instanceof MySqlSelectQueryBlock) {
                MySqlSelectQueryBlock select = (MySqlSelectQueryBlock) parent;
                if(select.getFrom() instanceof SQLJoinTableSource) {//多表连接
                    SQLJoinTableSource joinTableSource = (SQLJoinTableSource)select.getFrom();
                    return joinTableSource.getLeft().toString();//将left作为主表,此处有不严谨处,但也是实在没有办法,如果要准确,字段前带表名或者表的别名即可
                } else if(select.getFrom() instanceof SQLExprTableSource) {//单表
                    return select.getFrom().toString();
                }
            }
            else if(parent instanceof SQLUpdateStatement) {
                SQLUpdateStatement update = (SQLUpdateStatement) parent;
                return update.getTableName().getSimpleName();
            } else if(parent instanceof SQLDeleteStatement) {
                SQLDeleteStatement delete = (SQLDeleteStatement) parent;
                return delete.getTableName().getSimpleName();
            } else {
                
            }
        }
        return "";
    }