Search in sources :

Example 36 with DefaultConnection

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());
    }
}
Also used : BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Example 37 with DefaultConnection

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();
    }
}
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 38 with DefaultConnection

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

Example 39 with DefaultConnection

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

Example 40 with DefaultConnection

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();
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) PreparedStatement(java.sql.PreparedStatement) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Aggregations

DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)53 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)39 SneakyThrows (lombok.SneakyThrows)23 Connection (java.sql.Connection)20 MetadataManager (io.mycat.MetadataManager)6 java.util (java.util)6 SQLException (java.sql.SQLException)5 Statement (java.sql.Statement)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Collectors (java.util.stream.Collectors)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)4 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)4 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)4 NameMap (io.mycat.util.NameMap)4 NotNull (org.jetbrains.annotations.NotNull)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)3