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

The following examples show how to use com.alibaba.fastsql.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: MysqlTableReplacer.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean visit(SQLExprTableSource x) {
    String schemaName = x.getSchema();
    String tableName = x.getTableName();
    if (schemaName != null) {
        schemaName = SQLUtils.forcedNormalize(schemaName, DbType.mysql);
    }
    if (tableName != null) {
        tableName = SQLUtils.forcedNormalize(tableName, DbType.mysql);
    }
    if (schemaName == null) {
        schemaName = this.schemaName;
    }
    Objects.requireNonNull(tableName);
    SchemaInfo mappingTable = getMappingTable(schemaName, tableName);
    if (mappingTable!=null){
        x.setExpr(new SQLPropertyExpr(mappingTable.getTargetSchema(), mappingTable.getTargetTable()));
    }
    return super.visit(x);
}
 
Example #2
Source File: InsertSQLHandler.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ExecuteCode onExplain(SQLRequest<MySqlInsertStatement> request, MycatDataContext dataContext, Response response) {
    response.setExplainMode(true);
    SQLExprTableSource tableSource = (SQLExprTableSource)request.getAst().getTableSource();
    updateHandler(request.getAst(), dataContext, (SQLExprTableSource) tableSource,response);
    return ExecuteCode.PERFORMED;
}
 
Example #3
Source File: TableCollector.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public static Map<String, Collection<String>> collect(String defaultSchema, String sql) {
    Map<String, Collection<String>> collectionMap = new HashMap<>();
    try {
        SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
        sqlStatement.accept(new MySqlASTVisitorAdapter() {
            @Override
            public boolean visit(SQLExprTableSource x) {
                String schema = x.getSchema();
                String tableName = x.getTableName();
                if (schema == null) {
                    schema = defaultSchema;
                }
                if (schema == null) {
                    throw new UnsupportedOperationException("please use schema");
                }
                schema = SQLUtils.normalize(schema);
                tableName = SQLUtils.normalize(tableName);
                Collection<String> strings = collectionMap.computeIfAbsent(schema, s -> new HashSet<>());
                strings.add(tableName);
                return super.visit(x);
            }
        });
    } catch (Throwable ignored) {

    }
    return collectionMap;
}
 
Example #4
Source File: GlobalRouter.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean handle(ParseContext parseContext) {
    List<SQLExprTableSource> leftTables = parseContext.startAndGetLeftTables();
    List<Set<String>> set = new ArrayList<>();
    HashMap<SQLExprTableSource, Set<DataNode>> mapping = new HashMap<>();
    for (SQLExprTableSource leftTable : leftTables) {
        Set<DataNode> globalRanges = parseContext.getGlobalRange(leftTable);
        if (globalRanges == null) {
            return false;
        }
        mapping.put(leftTable, globalRanges);
        set.add(globalRanges.stream().map(i->i.getTargetName()).collect(Collectors.toSet()));
    }
    //求交集
    Set<String> dataNodes = set.stream().reduce((dataNodes1, dataNodes2) -> {
        return Sets.intersection(dataNodes1, dataNodes2);
    }).orElse(Collections.emptySet());
    if (dataNodes.size() != 1) return false;
    String targetName = dataNodes.iterator().next();
    for (Map.Entry<SQLExprTableSource, Set<DataNode>> entry : mapping.entrySet()) {
        SQLExprTableSource key = entry.getKey();
        Set<DataNode> value = entry.getValue();
        boolean change = false;
        for (DataNode dataNode : value) {
           if( targetName.equals( dataNode.getTargetName())){
               change = true;
               parseContext.changeSchemaTable(key, dataNode);
               break;
           }
        }
        if (!change){
            return false;
        }
    }
    String sql = parseContext.getSqlStatement().toString();
    parseContext.plan(HBTBuilder.create()
            .from(targetName,sql)
            .build());
    return true;
}
 
Example #5
Source File: ParseHelper.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
default Set<DataNode> getGlobalRange(SQLExprTableSource leftTable ){
    TableHandler table = MetadataManager.INSTANCE.getTable(leftTable.getSchema(), leftTable.getTableName());
    if (table!=null){
        if( table  instanceof GlobalTableHandler){
           return  ((GlobalTableHandler) table).getDataNodeMap().values().stream().map(i->i).collect(Collectors.toSet());
        }
    }
    return null;
}
 
Example #6
Source File: ReplaceSQLHandler.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ExecuteCode onExplain(SQLRequest<SQLReplaceStatement> request, MycatDataContext dataContext, Response response) {
    response.setExplainMode(true);
    SQLExprTableSource tableSource = (SQLExprTableSource)request.getAst().getTableSource();
    updateHandler(request.getAst(), dataContext, (SQLExprTableSource) tableSource,response );
    return ExecuteCode.PERFORMED;
}
 
Example #7
Source File: DeleteSQLHandler.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ExecuteCode onExplain(SQLRequest<MySqlDeleteStatement> request, MycatDataContext dataContext, Response response) {
    response.setExplainMode(true);
    SQLExprTableSource tableSource = (SQLExprTableSource)request.getAst().getTableSource();
    updateHandler(request.getAst(), dataContext, (SQLExprTableSource) tableSource,response);
    return ExecuteCode.PERFORMED;
}
 
Example #8
Source File: GlobalTable.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Function<ParseContext, Iterator<TextUpdateInfo>> insertHandler() {
    return new Function<ParseContext, Iterator<TextUpdateInfo>>() {
        @Override
        public Iterator<TextUpdateInfo> apply(ParseContext s) {
            SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(s.getSql());
            MySqlInsertStatement sqlStatement1 = (MySqlInsertStatement) sqlStatement;
            SQLExprTableSource tableSource = sqlStatement1.getTableSource();
            return updateHandler(tableSource, sqlStatement1);
        }
    };
}
 
Example #9
Source File: TruncateSQLHandler.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ExecuteCode onExplain(SQLRequest<SQLTruncateStatement> request, MycatDataContext dataContext, Response response) {
    SQLExprTableSource tableSource = request.getAst().getTableSources().get(0);
    response.setExplainMode(true);
    updateHandler(request.getAst(), dataContext, (SQLExprTableSource) tableSource,response );
    return ExecuteCode.PERFORMED;
}
 
Example #10
Source File: GlobalTable.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Function<ParseContext, Iterator<TextUpdateInfo>> updateHandler() {
    return new Function<ParseContext, Iterator<TextUpdateInfo>>() {
        @Override
        public Iterator<TextUpdateInfo> apply(ParseContext s) {
            SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(s.getSql());
            MySqlUpdateStatement sqlStatement1 = (MySqlUpdateStatement) sqlStatement;
            SQLExprTableSource tableSource = (SQLExprTableSource)sqlStatement1.getTableSource();
            return updateHandler(tableSource, sqlStatement1);
        }
    };
}
 
Example #11
Source File: ConditionCollector.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(MySqlDeleteStatement x) {
    SQLExprTableSource tableSource = (SQLExprTableSource)x.getTableSource();
    QueryDataRange queryDataRange = new QueryDataRange(tableSource);
    if (root == null) {
        root = queryDataRange;
    }
    stack.push(queryDataRange);
    return super.visit(x);
}
 
Example #12
Source File: GlobalTable.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Function<ParseContext, Iterator<TextUpdateInfo>> deleteHandler() {
    return new Function<ParseContext, Iterator<TextUpdateInfo>>() {
        @Override
        public Iterator<TextUpdateInfo> apply(ParseContext parseContext) {
            SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(parseContext.getSql());
            MySqlDeleteStatement sqlStatement1 = (MySqlDeleteStatement) sqlStatement;
            SQLExprTableSource tableSource = (SQLExprTableSource)sqlStatement1.getTableSource();
            return updateHandler(tableSource, sqlStatement1);
        }
    };
}
 
Example #13
Source File: ConditionCollector.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(MySqlUpdateStatement x) {
    SQLExprTableSource tableSource = (SQLExprTableSource)x.getTableSource();
    QueryDataRange queryDataRange = new QueryDataRange(tableSource);
    if (root == null) {
        root = queryDataRange;
    }
    stack.push(queryDataRange);
    return super.visit(x);
}
 
Example #14
Source File: ContextExecuter.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(SQLExprTableSource x) {
    String schema = x.getSchema();
    if (schema == null) {
        x.setSchema(context.getDefaultSchema());
    }
    return super.visit(x);
}
 
Example #15
Source File: AnalyzeHanlder.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected ExecuteCode onExecute(SQLRequest<MySqlAnalyzeStatement> request, MycatDataContext dataContext, Response response) {
    MySqlAnalyzeStatement ast = request.getAst();
    List<SQLExprTableSource> tableSources = Optional.ofNullable(ast.getTableSources()).orElse(Collections.emptyList());
    if (tableSources.isEmpty()) {
        response.sendError(new MycatException("need tables"));
        return ExecuteCode.PERFORMED;
    } else {
        ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
        resultSetBuilder.addColumnInfo("Table", JDBCType.VARCHAR);
        resultSetBuilder.addColumnInfo("Op", JDBCType.VARCHAR);
        resultSetBuilder.addColumnInfo("Msg_type", JDBCType.VARCHAR);
        resultSetBuilder.addColumnInfo("Msg_Text", JDBCType.VARCHAR);

        for (SQLExprTableSource tableSource : tableSources) {
            String schemaName = SQLUtils.normalize(tableSource.getSchema());
            String tableName = SQLUtils.normalize(tableSource.getTableName());
            resultSetBuilder.addObjectRowPayload(Arrays.asList(
                    schemaName+"."+tableName,
                    "analyze",
                    "status",
                    "OK"
            ));
            TableHandler tableHandler = MetadataManager.INSTANCE.getTable(schemaName, tableName);
            if (tableHandler == null) {
                response.sendError(new MycatException(tableSource + "不存在"));
                return ExecuteCode.PERFORMED;
            }
            StatisticCenter.INSTANCE.computeTableRowCount(tableHandler);
        }
        response.sendResultSet(()->resultSetBuilder.build(),()->{throw new UnsupportedOperationException();});
        return ExecuteCode.PERFORMED;
    }


}
 
Example #16
Source File: QueryDataRange.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public QueryDataRange(SQLExprTableSource tableSource) {
this.tableSource =tableSource;
}
 
Example #17
Source File: SQLContextImpl.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean visit(SQLExprTableSource x) {
    someTables = x;
    return super.visit(x);
}
 
Example #18
Source File: ParseHelper.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
default public void changeSchemaTable(SQLExprTableSource tableSource, DataNode dataNode) {
    tableSource.setSchema(dataNode.getSchema());
    tableSource.setSimpleName(dataNode.getTable());
}
 
Example #19
Source File: UpdateSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ExecuteCode onExplain(SQLRequest<MySqlUpdateStatement> request, MycatDataContext dataContext, Response response) {
    response.setExplainMode(true);
    updateHandler(request.getAst(), dataContext, (SQLExprTableSource) request.getAst().getTableSource(), response);
    return ExecuteCode.PERFORMED;
}
 
Example #20
Source File: UpdateSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public static void updateHandler(SQLStatement sql, MycatDataContext dataContext, SQLExprTableSource tableSource, Response receiver) {
    MycatDBClientMediator mycatDBClientMediator = MycatDBs.createClient(dataContext);
    String schemaName = Optional.ofNullable(tableSource.getSchema() == null ? dataContext.getDefaultSchema() : tableSource.getSchema())
            .map(i-> SQLUtils.normalize(i)).orElse(null);
    String tableName = SQLUtils.normalize(tableSource.getTableName());
    SchemaHandler schemaHandler;
    Optional<Map<String, SchemaHandler>> handlerMapOptional = Optional.ofNullable(mycatDBClientMediator)
            .map(i -> i.config())
            .map(i -> i.getSchemaMap());
    Optional<String> targetNameOptional = Optional.ofNullable(RootHelper.INSTANCE)
            .map(i -> i.getConfigProvider())
            .map(i -> i.currentConfig())
            .map(i -> i.getMetadata())
            .map(i -> i.getPrototype())
            .map(i -> i.getTargetName());
    if (!handlerMapOptional.isPresent()) {
        if (targetNameOptional.isPresent()) {
            receiver.proxyUpdate(targetNameOptional.get(), Objects.toString(sql));
            return;
        } else {
            receiver.sendError(new MycatException("Unable to route:" + sql));
            return;
        }
    } else {
        Map<String, SchemaHandler> handlerMap = handlerMapOptional.get();
        schemaHandler = Optional.ofNullable(handlerMap.get(schemaName))
                .orElseGet(() -> {
                    if (mycatDBClientMediator.getSchema() == null) {
                        throw new MycatException("unknown schema:"+schemaName);//可能schemaName有值,但是值名不是配置的名字
                    }
                    return handlerMap.get(mycatDBClientMediator.getSchema());
                });
        if (schemaHandler == null) {
            receiver.sendError(new MycatException("Unable to route:" + sql));
            return;
        }
    }
    String defaultTargetName = schemaHandler.defaultTargetName();
    Map<String, TableHandler> tableMap = schemaHandler.logicTables();
    TableHandler tableHandler = tableMap.get(tableName);
    ///////////////////////////////common///////////////////////////////
    if (tableHandler == null) {
        receiver.proxyUpdate(defaultTargetName, sql.toString());
        return;
    }
    String string = sql.toString();
    if (sql instanceof MySqlInsertStatement) {
        switch (tableHandler.getType()) {
            case SHARDING:
                receiver.multiInsert(string, tableHandler.insertHandler().apply(new ParseContext(sql.toString())));
                break;
            case GLOBAL:
                receiver.multiGlobalInsert(string, tableHandler.insertHandler().apply(new ParseContext(sql.toString())));
                break;
        }

    } else if (sql instanceof com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) {
        switch (tableHandler.getType()) {
            case SHARDING:
                receiver.multiUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString())));
                break;
            case GLOBAL:
                receiver.multiGlobalUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString())));
                break;
        }

    } else if (sql instanceof com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) {
        switch (tableHandler.getType()) {
            case SHARDING:
                receiver.multiUpdate(string, tableHandler.updateHandler().apply(new ParseContext(sql.toString())));
                break;
            case GLOBAL:
                receiver.multiGlobalUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString())));
                break;
        }
    } else {
        throw new UnsupportedOperationException("unsupported statement:" + sql);
    }

}
 
Example #21
Source File: UpdateSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ExecuteCode onExecute(SQLRequest<MySqlUpdateStatement> request, MycatDataContext dataContext, Response response) {
    updateHandler(request.getAst(), dataContext, (SQLExprTableSource) request.getAst().getTableSource(), response);
    return ExecuteCode.PERFORMED;
}
 
Example #22
Source File: ReplaceSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ExecuteCode onExecute(SQLRequest<SQLReplaceStatement> request, MycatDataContext dataContext, Response response) {
    SQLExprTableSource tableSource = request.getAst().getTableSource();
    updateHandler(request.getAst(),dataContext,tableSource,response);
    return ExecuteCode.PERFORMED;
}
 
Example #23
Source File: QueryDataRange.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public SQLExprTableSource getTableSource() {
    return tableSource;
}
 
Example #24
Source File: DeleteSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ExecuteCode onExecute(SQLRequest<MySqlDeleteStatement> request, MycatDataContext dataContext, Response response) {
    SQLExprTableSource tableSource = (SQLExprTableSource)request.getAst().getTableSource();
    updateHandler(request.getAst(),dataContext,tableSource,response);
    return ExecuteCode.PERFORMED;
}
 
Example #25
Source File: InsertSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ExecuteCode onExecute(SQLRequest<MySqlInsertStatement> request, MycatDataContext dataContext, Response response) {
    SQLExprTableSource tableSource = request.getAst().getTableSource();
    updateHandler(request.getAst(),dataContext,tableSource,response);
    return ExecuteCode.PERFORMED;
}
 
Example #26
Source File: TruncateSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ExecuteCode onExecute(SQLRequest<SQLTruncateStatement> request, MycatDataContext dataContext, Response response) {
    SQLExprTableSource tableSource = request.getAst().getTableSources().get(0);
    updateHandler(request.getAst(), dataContext, tableSource, response);
    return ExecuteCode.PERFORMED;
}