Search in sources :

Example 21 with DefaultConnection

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);
            }
        }
    });
}
Also used : SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLCreateViewStatement(com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) Statement(java.sql.Statement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) MycatRouterConfigOps(io.mycat.config.MycatRouterConfigOps) HackRouter(io.mycat.prototypeserver.mysql.HackRouter) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) SQLCreateViewStatement(com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement) Future(io.vertx.core.Future) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) List(java.util.List) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) Pair(io.mycat.util.Pair) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) RenameMycatRowMetaData(io.mycat.beans.mycat.RenameMycatRowMetaData)

Example 22 with DefaultConnection

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);
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 23 with DefaultConnection

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));
    }
}
Also used : NormalTable(io.mycat.calcite.table.NormalTable) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) NameMap(io.mycat.util.NameMap) DrdsTest(io.mycat.drdsrunner.DrdsTest) Disabled(org.junit.jupiter.api.Disabled)

Example 24 with DefaultConnection

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();
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Statement(java.sql.Statement) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 25 with DefaultConnection

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));
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) GlobalTable(io.mycat.calcite.table.GlobalTable) NameMap(io.mycat.util.NameMap) DrdsTest(io.mycat.drdsrunner.DrdsTest) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)53 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)39 SneakyThrows (lombok.SneakyThrows)23 Connection (java.sql.Connection)20 MetadataManager (io.mycat.MetadataManager)6 java.util (java.util)6 SQLException (java.sql.SQLException)5 Statement (java.sql.Statement)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Collectors (java.util.stream.Collectors)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)4 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)4 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)4 NameMap (io.mycat.util.NameMap)4 NotNull (org.jetbrains.annotations.NotNull)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)3