use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class DbPlanManagerPersistorImpl method listPlan.
@Override
@SneakyThrows
public synchronized List<BaselinePlan> listPlan(long baseline) {
Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
if (!managerOptional.isPresent()) {
return Collections.emptyList();
}
JdbcConnectionManager jdbcConnectionManager = managerOptional.get();
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
List<Map<String, Object>> maps = JdbcUtils.executeQuery(connection.getRawConnection(), "select * FROM mycat.spm_plan where baseline_id = ?", Arrays.asList(baseline));
return maps.stream().map((Function<Map<String, Object>, BaselinePlan>) map -> {
return new BaselinePlan((String) map.get("sql"), (String) map.get("rel"), (Long) map.get("id"), (Long) map.get("baseline_id"), null);
}).collect(Collectors.toList());
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class DbPlanManagerPersistorImpl method savePlan.
@SneakyThrows
public synchronized void savePlan(BaselinePlan plan, boolean fix) {
Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
if (!managerOptional.isPresent()) {
return;
}
JdbcConnectionManager manager = managerOptional.get();
try (DefaultConnection connection = manager.getConnection(MetadataManager.getPrototype())) {
Connection rawConnection = connection.getRawConnection();
rawConnection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
rawConnection.setAutoCommit(false);
// List<Map<String, Object>> maps = JdbcUtils.executeQuery(rawConnection, "select * from mycat.spm_plan where `baseline_id` = ? and `rel` = ?",
// Arrays.asList(plan.getBaselineId(), plan.getRel()));
// if (maps.isEmpty()) {
JdbcUtils.execute(rawConnection, "replace mycat.spm_plan (id,`sql`,`baseline_id`,`rel`) values(?,?,?,?)", Arrays.asList(plan.getId(), plan.getSql(), plan.getBaselineId(), plan.rel));
// }
if (fix) {
JdbcUtils.execute(rawConnection, "update mycat.spm_baseline set fix_plan_id = ? where id = ?", Arrays.asList(plan.getBaselineId(), plan.getId()));
}
rawConnection.commit();
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class DbPlanManagerPersistorImpl method clear.
@SneakyThrows
public synchronized void clear() {
Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
if (!managerOptional.isPresent()) {
return;
}
JdbcConnectionManager jdbcConnectionManager = managerOptional.get();
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
JdbcUtils.execute(connection.getRawConnection(), "truncate mycat.spm_plan", Arrays.asList());
JdbcUtils.execute(connection.getRawConnection(), "truncate mycat.spm_baseline", Arrays.asList());
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class DbPlanManagerPersistorImpl method loadPlan.
@SneakyThrows
public synchronized Optional<BaselinePlan> loadPlan(long planId) {
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 = ?", Arrays.asList(planId));
if (maps.size() != 1) {
log.error("baseline is duplicate");
return Optional.empty();
}
Map<String, Object> map = maps.get(0);
return Optional.ofNullable(toBaselinePlan(map));
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class DbPlanManagerPersistorImpl method saveBaseline.
@Override
@SneakyThrows
public void saveBaseline(Collection<Baseline> baselines) {
Optional<JdbcConnectionManager> managerOptional = getManagerOptional();
if (!managerOptional.isPresent()) {
return;
}
JdbcConnectionManager jdbcConnectionManager = managerOptional.get();
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
Connection rawConnection = connection.getRawConnection();
rawConnection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
rawConnection.setAutoCommit(false);
try (PreparedStatement preparedStatement = rawConnection.prepareStatement("INSERT INTO mycat.spm_baseline (id,`constraint`,`extra_constraint`,`fix_plan_id`) values(?,?,?,?) " + "on duplicate key update id = values(id),`constraint` = values(`constraint`),`extra_constraint`=values(`extra_constraint`),`fix_plan_id`=values(`fix_plan_id`)")) {
for (Baseline baseline : baselines) {
long baselineId = baseline.getBaselineId();
String constraintText = JsonUtil.toJson(baseline.getConstraint());
String extraConstraintText = JsonUtil.toJson(baseline.getExtraConstraint());
preparedStatement.setObject(1, baselineId);
preparedStatement.setObject(2, constraintText);
preparedStatement.setObject(3, extraConstraintText);
preparedStatement.setObject(4, Optional.ofNullable(baseline.fixPlan).map(i -> i.getId()).orElse(null));
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
}
try (PreparedStatement preparedStatement = rawConnection.prepareStatement("INSERT INTO mycat.spm_plan (id,`sql`,`baseline_id`,`rel`) values(?,?,?,?) " + "on duplicate key update `id` = values(`id`),`sql` = values(`sql`),`baseline_id` = values(`baseline_id`),`rel` = values(`rel`)")) {
for (Baseline baseline : baselines) {
for (BaselinePlan baselinePlan : baseline.getPlanList()) {
preparedStatement.setObject(1, baselinePlan.getId());
preparedStatement.setObject(2, baselinePlan.getSql());
preparedStatement.setObject(3, baseline.getBaselineId());
preparedStatement.setObject(4, baselinePlan.getRel());
preparedStatement.addBatch();
}
}
preparedStatement.executeBatch();
}
rawConnection.commit();
}
}
Aggregations