Search in sources :

Example 56 with JdbcConnectionManager

use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.

the class CreateDatabaseSQLHandler method onPhysics.

protected void onPhysics(String sql) {
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(metadataManager.getPrototype())) {
        connection.executeUpdate(sql, false);
    } catch (Throwable t) {
        LOGGER.warn("", t);
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 57 with JdbcConnectionManager

use of io.mycat.datasource.jdbc.datasource.JdbcConnectionManager in project Mycat2 by MyCATApache.

the class DropIndexSQLHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<SQLDropIndexStatement> request, MycatDataContext dataContext, Response response) {
    LockService lockService = MetaClusterCurrent.wrapper(LockService.class);
    return lockService.lock(DDL_LOCK, () -> {
        try {
            SQLDropIndexStatement sqlDropIndexStatement = request.getAst();
            sqlDropIndexStatement.setIfExists(true);
            String indexName = SQLUtils.normalize(sqlDropIndexStatement.getIndexName().toString());
            resolveSQLExprTableSource(sqlDropIndexStatement.getTableName(), dataContext);
            SQLExprTableSource tableSource = sqlDropIndexStatement.getTableName();
            String schema = SQLUtils.normalize(tableSource.getSchema());
            String tableName = SQLUtils.normalize(tableSource.getTableName());
            MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
            JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
            TableHandler table = metadataManager.getTable(schema, tableName);
            MySqlCreateTableStatement sqlStatement = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(table.getCreateTableSQL());
            boolean updateShardingTable = false;
            updateShardingTable = isUpdateShardingTable(indexName, sqlStatement, updateShardingTable);
            if (!updateShardingTable) {
                Set<Partition> partitions = getDataNodes(table);
                try {
                    executeOnDataNodes(sqlDropIndexStatement, jdbcConnectionManager, partitions, tableSource);
                } catch (Throwable e) {
                    LOGGER.error("", e);
                }
            }
            // List<MySqlTableIndex> mysqlIndexes = sqlStatement.getMysqlIndexes();
            // mysqlIndexes.stream().filter(i->SQLUtils.nameEquals(sqlDropIndexStatement.getIndexName(),i.getName())).findFirst()
            // .ifPresent(c->mysqlIndexes.remove(c));
            sqlStatement.apply(sqlDropIndexStatement);
            CreateTableSQLHandler.INSTANCE.createTable(ImmutableMap.of(), schema, tableName, sqlStatement);
            return response.sendOk();
        } catch (Throwable throwable) {
            return Future.failedFuture(throwable);
        }
    });
}
Also used : SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) SQLDropIndexStatement(com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement)

Aggregations

JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)57 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)38 SneakyThrows (lombok.SneakyThrows)21 Connection (java.sql.Connection)16 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)8 java.util (java.util)8 NotNull (org.jetbrains.annotations.NotNull)8 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)7 DatasourceConfig (io.mycat.config.DatasourceConfig)7 JdbcDataSource (io.mycat.datasource.jdbc.datasource.JdbcDataSource)7 Collectors (java.util.stream.Collectors)7 MetadataManager (io.mycat.MetadataManager)6 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)6 NameMap (io.mycat.util.NameMap)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 MycatRowMetaData (io.mycat.beans.mycat.MycatRowMetaData)5 SQLException (java.sql.SQLException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Logger (org.slf4j.Logger)5