use of io.mycat.datasource.jdbc.datasource.DefaultConnection 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());
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection 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);
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection 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;
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection 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);
}
});
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection 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));
}
}
Aggregations