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();
}
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();
}
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));
}
}
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));
}
}
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());
}
}
Aggregations