use of io.mycat.sqlhandler.SQLRequest in project Mycat2 by MyCATApache.
the class CreateTableSQLHandler method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<MySqlCreateTableStatement> request, MycatDataContext dataContext, Response response) {
LockService lockService = MetaClusterCurrent.wrapper(LockService.class);
return lockService.lock(DDL_LOCK, () -> {
try {
Map hint = Optional.ofNullable(request.getAst().getHeadHintsDirect()).map(i -> i.get(0)).map(i -> i.getText()).filter(i -> {
i = i.replaceAll(" ", "");
return i.contains("+mycat:createTable{");
}).map(i -> i.substring(i.indexOf("{"))).map(i -> JsonUtil.from(i, Map.class)).orElse(null);
MySqlCreateTableStatement ast = request.getAst();
String schemaName = ast.getSchema() == null ? dataContext.getDefaultSchema() : SQLUtils.normalize(ast.getSchema());
String tableName = ast.getTableName() == null ? null : SQLUtils.normalize(ast.getTableName());
if (ast.getSchema() == null) {
ast.setSchema(schemaName);
}
if (tableName == null) {
return response.sendError(new MycatException("CreateTableSQL need tableName"));
}
if (schemaName == null) {
return response.sendError("No database selected", 1046);
}
createTable(hint, schemaName, tableName, ast);
return response.sendOk();
} catch (Throwable throwable) {
return Future.failedFuture(throwable);
}
});
}
use of io.mycat.sqlhandler.SQLRequest 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