Search in sources :

Example 1 with JdbcConnectionManager

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

the class PrototypeService method getCreateTableSQLByJDBC.

public Optional<String> getCreateTableSQLByJDBC(String schemaName, String tableName, List<Partition> backends) {
    Optional<JdbcConnectionManager> jdbcConnectionManagerOptional = getPrototypeConnectionManager();
    if (!jdbcConnectionManagerOptional.isPresent()) {
        return Optional.empty();
    }
    JdbcConnectionManager jdbcConnectionManager = jdbcConnectionManagerOptional.get();
    backends = new ArrayList<>(backends);
    backends.add(new BackendTableInfo(PROTOTYPE, schemaName, tableName));
    if (backends == null || backends.isEmpty()) {
        return Optional.empty();
    }
    for (Partition backend : backends) {
        try {
            Partition backendTableInfo = backend;
            String targetName = backendTableInfo.getTargetName();
            String targetSchemaTable = backendTableInfo.getTargetSchemaTable();
            try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
                String sql = "SHOW CREATE TABLE " + targetSchemaTable;
                SQLStatement sqlStatement = null;
                try (RowBaseIterator rowBaseIterator = connection.executeQuery(sql)) {
                    rowBaseIterator.next();
                    String string = rowBaseIterator.getString(1);
                    sqlStatement = SQLUtils.parseSingleMysqlStatement(string);
                    if (sqlStatement instanceof MySqlCreateTableStatement) {
                        MySqlCreateTableStatement sqlStatement1 = (MySqlCreateTableStatement) sqlStatement;
                        sqlStatement1.setTableName(SQLUtils.normalize(tableName));
                        // 顺序不能颠倒
                        sqlStatement1.setSchema(SQLUtils.normalize(schemaName));
                        return Optional.of(sqlStatement1.toString());
                    }
                } catch (Exception e) {
                    LOGGER.error("", e);
                }
                if (sqlStatement == null || !(sqlStatement instanceof SQLCreateTableStatement)) {
                    try (RowBaseIterator rowBaseIterator = connection.executeQuery("select * from " + targetSchemaTable + " where 0 limit 0")) {
                        MycatRowMetaData metaData = rowBaseIterator.getMetaData();
                        String createTableSql = generateSql(schemaName, tableName, metaData.metaData());
                        return Optional.of(createTableSql);
                    }
                }
                continue;
            } catch (Exception e) {
                LOGGER.error("", e);
            }
            continue;
        } catch (Throwable e) {
            LOGGER.error("can not get create table sql from:" + backend.getTargetName() + backend.getTargetSchemaTable(), e);
            continue;
        }
    }
    return Optional.empty();
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData)

Example 2 with JdbcConnectionManager

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

the class PrototypeService method getMycatRowMetaDataPrepareSQLByTargetName.

private Optional<MycatRowMetaData> getMycatRowMetaDataPrepareSQLByTargetName(String targetName, String sql) {
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
        Connection rawConnection = connection.getRawConnection();
        PreparedStatement preparedStatement = rawConnection.prepareStatement(sql);
        ResultSetMetaData metaData = preparedStatement.getMetaData();
        if (metaData != null) {
            return Optional.of(new CopyMycatRowMetaData(new JdbcRowMetaData(metaData)));
        }
    } catch (Exception e) {
        LOGGER.warn("can not get meta from {} {}", targetName, e);
        return Optional.empty();
    }
    return Optional.empty();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) PreparedStatement(java.sql.PreparedStatement) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData)

Example 3 with JdbcConnectionManager

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

the class DbPlanManagerPersistorImpl method deletePlan.

@Override
@SneakyThrows
public synchronized void deletePlan(long planId) {
    Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
    if (!managerOptional.isPresent()) {
        return;
    }
    JdbcConnectionManager manager = managerOptional.get();
    try (DefaultConnection connection = manager.getConnection(MetadataManager.getPrototype())) {
        Connection rawConnection = connection.getRawConnection();
        JdbcUtils.execute(rawConnection, "delete from mycat.spm_plan where id = ?", Arrays.asList(planId));
        JdbcUtils.execute(rawConnection, "update  mycat.spm_baseline set  fix_plan_id = null where fix_plan_id = ?", Arrays.asList(planId));
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Example 4 with JdbcConnectionManager

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

the class DbPlanManagerPersistorImpl method loadFixPlan.

@SneakyThrows
public synchronized Optional<BaselinePlan> loadFixPlan(long planId, long baselineId) {
    Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
    if (!managerOptional.isPresent()) {
        return Optional.empty();
    }
    JdbcConnectionManager manager = managerOptional.get();
    try (DefaultConnection connection = manager.getConnection(MetadataManager.getPrototype())) {
        List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection.getRawConnection(), "SELECT * FROM mycat.spm_plan where id = ? and baseline_id = ?", Arrays.asList(planId, baselineId));
        if (maps.size() != 1) {
            log.error("baseline is duplicate");
            return Optional.empty();
        }
        Map<String, Object> map = maps.get(0);
        return Optional.ofNullable(toBaselinePlan(map));
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Example 5 with JdbcConnectionManager

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

the class DbPlanManagerPersistorImpl method loadPlanByBaselineId.

@SneakyThrows
public synchronized List<BaselinePlan> loadPlanByBaselineId(long baselineId) {
    Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
    if (!managerOptional.isPresent()) {
        return Collections.emptyList();
    }
    JdbcConnectionManager manager = managerOptional.get();
    try (DefaultConnection connection = manager.getConnection(MetadataManager.getPrototype())) {
        List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection.getRawConnection(), "SELECT * FROM mycat.spm_plan where baseline_id = ?", Arrays.asList(baselineId));
        return maps.stream().map(i -> toBaselinePlan(i)).collect(Collectors.toList());
    }
}
Also used : MetadataManager(io.mycat.MetadataManager) java.util(java.util) Logger(org.slf4j.Logger) Connection(java.sql.Connection) MetaClusterCurrent(io.mycat.MetaClusterCurrent) SneakyThrows(lombok.SneakyThrows) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) PreparedStatement(java.sql.PreparedStatement) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) SQLException(java.sql.SQLException) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) JdbcUtils(com.alibaba.druid.util.JdbcUtils) JsonUtil(io.mycat.util.JsonUtil) NotNull(org.jetbrains.annotations.NotNull) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

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