use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.
the class DropDatabaseSQLHandler method onPhysics.
protected void onPhysics(String name) {
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(metadataManager.getPrototype())) {
connection.executeUpdate(String.format("DROP DATABASE IF EXISTS %s;", name), false);
} catch (Throwable t) {
LOGGER.warn("", t);
}
}
use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager 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.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.
the class DropTableSQLHandler method onPhysics.
protected void onPhysics(String schema, String tableName) {
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(metadataManager.getPrototype())) {
connection.executeUpdate(String.format("DROP TABLE IF EXISTS %s;", schema + "." + tableName), false);
} catch (Throwable t) {
LOGGER.warn("", t);
}
}
use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.
the class GlobalTable method createPhysicalTables.
@Override
public void createPhysicalTables() {
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
List<Partition> partitions = (List) ImmutableList.builder().addAll(getGlobalDataNode()).build();
partitions.stream().parallel().forEach(node -> createPhysicalTable(jdbcConnectionManager, node, getCreateTableSQL()));
}
use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.
the class StatisticCenterTest method init.
@BeforeClass
public static void init() throws Exception {
HashMap<Class, Object> context = new HashMap<>();
context.put(Vertx.class, Vertx.vertx());
context.put(ServerConfig.class, new ServerConfig());
context.put(DrdsSqlCompiler.class, new DrdsSqlCompiler(new DrdsConst() {
@Override
public NameMap<SchemaHandler> schemas() {
return new NameMap<>();
}
}));
MetaClusterCurrent.register(context);
String customerDatasourceProvider = DruidDatasourceProvider.class.getName();
DatasourceConfig datasourceConfig = new DatasourceConfig();
datasourceConfig.setDbType("mysql");
datasourceConfig.setUser("root");
datasourceConfig.setPassword("123456");
datasourceConfig.setName("prototypeDs");
datasourceConfig.setUrl("jdbc:mysql://localhost:3306/mysql");
Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
ClusterConfig clusterConfig = new ClusterConfig();
clusterConfig.setName("prototype");
clusterConfig.setMasters(Arrays.asList("prototypeDs"));
Map<String, ClusterConfig> clusterConfigs = Maps.of("prototype", clusterConfig);
LinkedList<Runnable> runnables = new LinkedList<>();
ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(new ArrayList<>(clusterConfigs.values()), datasources, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
runnables.add(command);
return () -> {
};
});
context.put(ReplicaSelectorManager.class, manager);
context.put(JdbcConnectionManager.class, jdbcManager = new JdbcConnectionManager(DruidDatasourceProvider.class.getName(), datasources));
MetaClusterCurrent.register(context);
statisticCenter.init();
}
Aggregations