use of io.mycat.datasource.jdbc.datasource.DefaultConnection 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.DefaultConnection 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.DefaultConnection in project Mycat2 by MyCATApache.
the class StatisticCenterTest method testNormal.
@Test
@Disabled
@Ignore
public void testNormal() throws Exception {
DrdsSqlCompiler drds = getDrds();
String schemaName = "db1";
String tableName = "normal";
MetadataManager metadataManager = getMetadataManager();
TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
if (tableHandler == null) {
return;
}
tableHandler.createPhysicalTables();
NormalTable table = (NormalTable) tableHandler;
try (DefaultConnection connection = jdbcManager.getConnection("prototype")) {
deleteData(connection.getRawConnection(), "mycat", "analyze_table");
}
try (DefaultConnection connection = jdbcManager.getConnection(table.getDataNode().getTargetName())) {
deleteData(connection.getRawConnection(), schemaName, tableName);
JdbcUtils.execute(connection.getRawConnection(), "insert db1.normal(id,addressname) values(?,?)", Arrays.asList(1, "a"));
}
Double count = statisticCenter.computeTableRowCount(tableHandler);
Assert.assertTrue(count.equals(count));
statisticCenter.fetchTableRowCount(tableHandler);
try (DefaultConnection connection = jdbcManager.getConnection("prototype")) {
List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection.getRawConnection(), "select * from mycat.analyze_table ", Arrays.asList());
Assert.assertTrue(maps.toString().contains(tableName));
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class StatisticCenterTest method close.
@AfterClass
public static void close() throws Exception {
try (DefaultConnection defaultConnection = jdbcManager.getConnection("prototypeDs")) {
Connection rawConnection = defaultConnection.getRawConnection();
Statement statement = rawConnection.createStatement();
statement.execute("delete from mycat.analyze_table");
}
MetaClusterCurrent.wrapper(JdbcConnectionManager.class).close();
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class StatisticCenterTest method testGlobal.
@Test
@Disabled
@Ignore
public void testGlobal() throws Exception {
DrdsSqlCompiler drds = getDrds();
String schemaName = "db1";
String tableName = "global";
MetadataManager metadataManager = getMetadataManager();
TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
if (tableHandler == null) {
return;
}
tableHandler.createPhysicalTables();
GlobalTable table = (GlobalTable) tableHandler;
try (DefaultConnection connection = jdbcManager.getConnection("prototype")) {
deleteData(connection.getRawConnection(), schemaName, tableName);
JdbcUtils.execute(connection.getRawConnection(), "insert db1.global(id) values(?)", Arrays.asList(1));
}
Double count = statisticCenter.computeTableRowCount(tableHandler);
Assert.assertTrue(count.equals(count));
statisticCenter.fetchTableRowCount(tableHandler);
try (DefaultConnection connection = jdbcManager.getConnection("prototype")) {
List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection.getRawConnection(), "select * from mycat.analyze_table ", Arrays.asList());
Assert.assertTrue(maps.toString().contains(tableName));
}
}
Aggregations