Search in sources :

Example 26 with JdbcConnectionManager

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

Example 27 with JdbcConnectionManager

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);
            }
        }
    });
}
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 28 with JdbcConnectionManager

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

Example 29 with JdbcConnectionManager

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()));
}
Also used : List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 30 with JdbcConnectionManager

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();
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) ServerConfig(io.mycat.config.ServerConfig) DatasourceConfig(io.mycat.config.DatasourceConfig) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ClusterConfig(io.mycat.config.ClusterConfig)

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