Search in sources :

Example 6 with JdbcConnectionManager

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

the class DbPlanManagerPersistorImpl method deleteBaselineByExtraConstraint.

@Override
@SneakyThrows
public void deleteBaselineByExtraConstraint(List<String> infos) {
    if (infos.isEmpty()) {
        return;
    }
    Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
    if (!managerOptional.isPresent()) {
        return;
    }
    JdbcConnectionManager manager = managerOptional.get();
    try (DefaultConnection connection = manager.getConnection(MetadataManager.getPrototype())) {
        Connection rawConnection = connection.getRawConnection();
        List<String> list = new ArrayList<>();
        for (String info : infos) {
            list.add(" extra_constraint like \"%" + info + "%\" ");
        }
        JdbcUtils.executeQuery(rawConnection, "delete  from mycat.spm_baseline where " + String.join(" or ", list), Collections.emptyList());
    }
}
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 7 with JdbcConnectionManager

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

the class DbPlanManagerPersistorImpl method loadBaseline.

@Override
@SneakyThrows
public synchronized Optional<Baseline> loadBaseline(long baselineId) {
    Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
    if (!managerOptional.isPresent()) {
        return Optional.empty();
    }
    JdbcConnectionManager jdbcConnectionManager = managerOptional.get();
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
        List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection.getRawConnection(), "SELECT * FROM mycat.spm_baseline where id = ?", Arrays.asList(baselineId));
        if (maps.isEmpty()) {
            return Optional.empty();
        }
        if (maps.size() != 1) {
            log.error("baseline is duplicate");
            return Optional.empty();
        }
        Map<String, Object> map = maps.get(0);
        Baseline baseline = toBaseline(map);
        return Optional.of(baseline);
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Example 8 with JdbcConnectionManager

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

the class DbPlanManagerPersistorImpl method loadAllBaseline.

@Override
@SneakyThrows
public synchronized Map<Constraint, Baseline> loadAllBaseline() {
    Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
    if (!managerOptional.isPresent()) {
        return Collections.emptyMap();
    }
    JdbcConnectionManager jdbcConnectionManager = managerOptional.get();
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
        Connection rawConnection = connection.getRawConnection();
        Map<Constraint, Baseline> baselineList = JdbcUtils.executeQuery(rawConnection, "SELECT * FROM mycat.spm_baseline", Collections.emptyList()).stream().map(b -> toBaseline(b)).distinct().collect(Collectors.toMap(k -> k.getConstraint(), v -> v));
        if (baselineList.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<Long, List<BaselinePlan>> baselinePlanMap = JdbcUtils.executeQuery(connection.getRawConnection(), "SELECT * FROM mycat.spm_plan", Collections.emptyList()).stream().parallel().map(m -> toBaselinePlan(m)).collect(Collectors.groupingBy(k -> k.getBaselineId()));
        for (Baseline baseline : baselineList.values()) {
            List<BaselinePlan> baselinePlans = baselinePlanMap.getOrDefault(baseline.baselineId, Collections.emptyList());
            baseline.getPlanList().addAll(baselinePlans);
        }
        return baselineList;
    }
}
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) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Example 9 with JdbcConnectionManager

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

the class DbPlanManagerPersistorImpl method checkStore.

/**
 * SHOW CREATE TABLE mycat.`spm_baseline`;
 * SHOW CREATE TABLE mycat.`spm_plan`;
 */
@Override
@SneakyThrows
public synchronized void checkStore() {
    String prototype = MetadataManager.getPrototype();
    Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
    managerOptional.ifPresent(manager -> {
        try (DefaultConnection connection = manager.getConnection(prototype)) {
            JdbcUtils.execute(connection.getRawConnection(), "CREATE DATABASE  IF  NOT EXISTS mycat");
            JdbcUtils.execute(connection.getRawConnection(), "CREATE TABLE IF  NOT EXISTS mycat.`spm_baseline` (\n" + "  `id` bigint(22) NOT NULL AUTO_INCREMENT,\n" + "  `fix_plan_id` bigint(22) DEFAULT NULL,\n" + "  `constraint` longtext CHARACTER SET utf8mb4 NOT NULL,\n" + "  `extra_constraint` longtext,\n" + "  PRIMARY KEY (`id`),\n" + "  UNIQUE KEY `constraint_index` (`constraint`(22)),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ");
            JdbcUtils.execute(connection.getRawConnection(), "CREATE TABLE IF  NOT EXISTS mycat.`spm_plan` (\n" + "  `id` bigint(22) NOT NULL AUTO_INCREMENT,\n" + "  `sql` longtext,\n" + "  `rel` longtext,\n" + "  `baseline_id` bigint(22) DEFAULT NULL,\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
        } catch (Exception e) {
            log.error("", e);
        }
    });
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SQLException(java.sql.SQLException) SneakyThrows(lombok.SneakyThrows)

Example 10 with JdbcConnectionManager

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

the class DbPlanManagerPersistorImpl method loadBaselineByBaseLineSql.

@Override
@SneakyThrows
public synchronized Optional<Baseline> loadBaselineByBaseLineSql(String baseLineSql, Constraint constraint) {
    Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
    if (!managerOptional.isPresent()) {
        return Optional.empty();
    }
    JdbcConnectionManager manager = managerOptional.get();
    try (DefaultConnection connection = manager.getConnection(MetadataManager.getPrototype())) {
        Connection rawConnection = connection.getRawConnection();
        List<Map<String, Object>> maps = JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_baseline where `constraint` = ?", Arrays.asList(JsonUtil.toJson(constraint)));
        if (maps.isEmpty()) {
            return Optional.empty();
        }
        Map<String, Object> map = maps.get(0);
        return Optional.ofNullable(toBaseline(map));
    }
}
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)

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