use of io.mycat.config.MycatRouterConfigOps in project Mycat2 by MyCATApache.
the class CreateViewSQLHandler method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<SQLCreateViewStatement> request, MycatDataContext dataContext, Response response) {
LockService lockService = MetaClusterCurrent.wrapper(LockService.class);
SQLCreateViewStatement ast = request.getAst();
resolveSQLExprTableSource(ast.getTableSource(), dataContext);
return lockService.lock(DDL_LOCK, new Supplier<Future<Void>>() {
@Override
public Future<Void> get() {
String schemaName = Optional.ofNullable(ast.getSchema()).orElse(dataContext.getDefaultSchema());
schemaName = SQLUtils.normalize(schemaName);
String viewName = SQLUtils.normalize(ast.getName().getSimpleName());
SQLSelect subQuery = ast.getSubQuery();
SQLSelectStatement sqlSelectStatement = new SQLSelectStatement();
sqlSelectStatement.setSelect(subQuery);
List<String> aliasList = Optional.ofNullable(ast.getColumns()).orElse(Collections.emptyList()).stream().map(i -> SQLUtils.normalize(i.toString())).collect(Collectors.toList());
HackRouter hackRouter = new HackRouter(sqlSelectStatement, dataContext);
boolean distSql = !hackRouter.analyse();
try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
if (!distSql) {
Pair<String, String> plan = hackRouter.getPlan();
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(plan.getKey())) {
Connection rawConnection = connection.getRawConnection();
Statement statement = rawConnection.createStatement();
statement.setMaxRows(0);
MycatRowMetaData metaData = connection.executeQuery(plan.getValue()).getMetaData();
if (!aliasList.isEmpty()) {
metaData = RenameMycatRowMetaData.of(metaData, aliasList);
}
String createTableSql = PrototypeService.generateSql(schemaName, viewName, metaData.metaData());
ops.putNormalTable(schemaName, viewName, (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(createTableSql));
statement.close();
try {
SQLSelectStatement phySQLSelectStatement = (SQLSelectStatement) SQLUtils.parseSingleMysqlStatement(plan.getValue());
ast.setSubQuery(phySQLSelectStatement.getSelect());
// 建立物理视图
JdbcUtils.execute(rawConnection, ast.toString());
} catch (Throwable throwable) {
LOGGER.error("build phy view fail", throwable);
}
}
} else {
ops.addView(schemaName, viewName, ast.toString());
}
ops.commit();
return response.sendOk();
} catch (Throwable throwable) {
return Future.failedFuture(throwable);
}
}
});
}
use of io.mycat.config.MycatRouterConfigOps in project Mycat2 by MyCATApache.
the class CreateTableSQLHandler method createTable.
public synchronized void createTable(Map hint, String schemaName, String tableName, MySqlCreateTableStatement createTableSql) throws Exception {
if (createTableSql == null && hint != null) {
Object sql = hint.get("createTableSql");
if (sql instanceof MySqlCreateTableStatement) {
createTableSql = (MySqlCreateTableStatement) sql;
} else {
createTableSql = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(Objects.toString(sql));
}
}
Objects.requireNonNull(createTableSql);
try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
if (schemaName == null || tableName == null) {
MySqlCreateTableStatement ast = (MySqlCreateTableStatement) createTableSql;
schemaName = SQLUtils.normalize(ast.getSchema());
tableName = SQLUtils.normalize(ast.getTableName());
}
if (hint == null || (hint != null && hint.isEmpty())) {
if (createTableSql.isBroadCast()) {
ops.putGlobalTable(schemaName, tableName, createTableSql);
} else if (createTableSql.getDbPartitionBy() == null && createTableSql.getTablePartitionBy() == null) {
ops.putNormalTable(schemaName, tableName, createTableSql);
} else {
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
int defaultStoreNodeNum = metadataManager.getDefaultStoreNodeNum();
if (defaultStoreNodeNum == 0) {
ops.getOriginal().getClusters().stream().filter(i -> "prototype".equals(i.getName())).findFirst().ifPresent(clusterConfig -> {
ClusterConfig config = JsonUtil.from(JsonUtil.toJson(clusterConfig), ClusterConfig.class);
ops.putReplica(config);
});
}
ops.putHashTable(schemaName, tableName, createTableSql, getAutoHashProperties(createTableSql));
}
} else {
Map<String, Object> infos = hint;
switch(Objects.requireNonNull(infos.get("type")).toString()) {
case "normal":
{
String targetName = (String) infos.get("targetName");
ops.putNormalTable(schemaName, tableName, createTableSql, targetName);
break;
}
case "global":
{
ops.putGlobalTable(schemaName, tableName, createTableSql);
break;
}
case "range":
{
ops.putRangeTable(schemaName, tableName, createTableSql, infos);
break;
}
case "hash":
{
ops.putHashTable(schemaName, tableName, createTableSql, infos);
break;
}
}
}
ops.commit();
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
TableHandler table = metadataManager.getTable(schemaName, tableName);
if (table == null) {
throw new MycatException("create table fail:" + schemaName + "." + tableName);
}
}
}
use of io.mycat.config.MycatRouterConfigOps in project Mycat2 by MyCATApache.
the class DropViewSQLHandler method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<SQLDropViewStatement> request, MycatDataContext dataContext, Response response) {
LockService lockService = MetaClusterCurrent.wrapper(LockService.class);
SQLDropViewStatement ast = request.getAst();
SQLExprTableSource sqlExprTableSource = ast.getTableSources().get(0);
resolveSQLExprTableSource(sqlExprTableSource, dataContext);
return lockService.lock(DDL_LOCK, new Supplier<Future<Void>>() {
@Override
public Future<Void> get() {
String schemaName = Optional.ofNullable(sqlExprTableSource.getSchema()).orElse(dataContext.getDefaultSchema());
schemaName = SQLUtils.normalize(schemaName);
String viewName = SQLUtils.normalize(sqlExprTableSource.getTableName());
try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
ops.removeView(schemaName, viewName);
ops.commit();
return response.sendOk();
} catch (Throwable throwable) {
return Future.failedFuture(throwable);
}
}
});
}
use of io.mycat.config.MycatRouterConfigOps 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();
}
use of io.mycat.config.MycatRouterConfigOps in project Mycat2 by MyCATApache.
the class SQLDropProcedureHandler method onExecute.
@Override
@SneakyThrows
protected Future<Void> onExecute(SQLRequest<SQLDropProcedureStatement> request, MycatDataContext dataContext, Response response) {
SQLDropProcedureStatement 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());
try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
ops.removeProcedure(schemaName, pName);
ops.commit();
}
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
connection.executeUpdate(ast.toString(), false);
}
return response.sendOk();
}
Aggregations