use of io.mycat.config.NormalProcedureConfig in project Mycat2 by MyCATApache.
the class SQLCallStatementHandler method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<SQLCallStatement> request, MycatDataContext dataContext, Response response) {
SQLCallStatement ast = request.getAst();
SQLName procedureExpr = ast.getProcedureName();
if (procedureExpr instanceof SQLIdentifierExpr && dataContext.getDefaultSchema() != null) {
String name = ((SQLIdentifierExpr) procedureExpr).getName();
procedureExpr = new SQLPropertyExpr(new SQLIdentifierExpr(dataContext.getDefaultSchema()), name);
}
SQLPropertyExpr sqlPropertyExpr = (SQLPropertyExpr) procedureExpr;
String schemaName = SQLUtils.normalize(sqlPropertyExpr.getOwnerName(), true);
String pName = SQLUtils.normalize(sqlPropertyExpr.getName(), true);
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
Optional<ProcedureHandler> procedureHandlerOptional = metadataManager.getProcedure(schemaName, pName);
if (procedureHandlerOptional.isPresent()) {
ProcedureHandler procedureHandler = procedureHandlerOptional.get();
switch(procedureHandler.getType()) {
case NORMAL:
NormalProcedureHandler normalProcedureHandler = (NormalProcedureHandler) procedureHandler;
NormalProcedureConfig config = normalProcedureHandler.getConfig();
NormalBackEndProcedureInfoConfig locality = config.getLocality();
String targetName = dataContext.resolveDatasourceTargetName(locality.getTargetName(), true);
return response.proxyProcedure(ast.toString(), targetName);
}
LOGGER.error("unknown call {}", ast);
}
return response.proxyProcedure(ast.toString(), MetadataManager.getPrototype());
}
use of io.mycat.config.NormalProcedureConfig in project Mycat2 by MyCATApache.
the class SQLCreateProcedureHandler method onExecute.
@Override
@SneakyThrows
protected Future<Void> onExecute(SQLRequest<SQLCreateProcedureStatement> request, MycatDataContext dataContext, Response response) {
SQLCreateProcedureStatement ast = request.getAst();
if (ast.getName() instanceof SQLIdentifierExpr) {
String defaultSchema = dataContext.getDefaultSchema();
if (defaultSchema != null) {
ast.setName(new SQLPropertyExpr(defaultSchema, ((SQLIdentifierExpr) ast.getName()).getName()));
}
}
if (!(ast.getName() instanceof SQLPropertyExpr)) {
throw new IllegalArgumentException("unknown schema:");
}
SQLPropertyExpr pNameExpr = (SQLPropertyExpr) ast.getName();
String schemaName = SQLUtils.normalize(pNameExpr.getOwnerName().toLowerCase());
String pName = SQLUtils.normalize(pNameExpr.getName().toLowerCase());
List<SQLParameter> sqlParameters = Optional.ofNullable(ast.getParameters()).orElse(Collections.emptyList());
Map<SQLParameter.ParameterType, List<SQLParameter>> parameterTypeListMap = sqlParameters.stream().collect(Collectors.groupingBy(k -> k.getParamType()));
SQLBlockStatement block = (SQLBlockStatement) ast.getBlock();
if (dataContext.getDefaultSchema() != null) {
block.accept(new MySqlASTVisitorAdapter() {
@Override
public void endVisit(SQLExprTableSource x) {
resolveSQLExprTableSource(x, dataContext);
}
});
}
Map<String, Collection<String>> collect = TableCollector.collect(dataContext.getDefaultSchema(), block);
int resultSetCount = getResultSetCount(block);
List<TableHandler> tableHandlers = getTableHandlers(block);
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
NormalProcedureConfig normalProcedureConfig = new NormalProcedureConfig();
normalProcedureConfig.setCreateProcedureSQL(ast.toString());
NormalBackEndProcedureInfoConfig normalBackEndProcedureInfoConfig = new NormalBackEndProcedureInfoConfig();
normalBackEndProcedureInfoConfig.setProcedureName(pName);
normalBackEndProcedureInfoConfig.setSchemaName(schemaName);
normalBackEndProcedureInfoConfig.setTargetName(MetadataManager.getPrototype());
normalProcedureConfig.setLocality(normalBackEndProcedureInfoConfig);
try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
ops.addProcedure(schemaName, pName, normalProcedureConfig);
ops.commit();
}
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
connection.executeUpdate(ast.toString(), false);
}
return response.sendOk();
}
Aggregations