Search in sources :

Example 1 with NormalBackEndTableInfoConfig

use of io.mycat.config.NormalBackEndTableInfoConfig in project Mycat2 by MyCATApache.

the class PrototypeService method getDefaultNormalTable.

public Map<String, NormalTableConfig> getDefaultNormalTable(String targetName, String schemaName, Predicate<String> tableFilter) {
    Set<String> tables = new HashSet<>();
    Optional<JdbcConnectionManager> jdbcConnectionManagerOptional = getPrototypeConnectionManager();
    if (!jdbcConnectionManagerOptional.isPresent()) {
        return Collections.emptyMap();
    }
    JdbcConnectionManager jdbcConnectionManager = jdbcConnectionManagerOptional.get();
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
        RowBaseIterator tableIterator = connection.executeQuery("show tables from " + schemaName);
        while (tableIterator.next()) {
            tables.add(tableIterator.getString(0));
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        return Collections.emptyMap();
    }
    Map<String, NormalTableConfig> res = new ConcurrentHashMap<>();
    tables.stream().filter(tableFilter).parallel().forEach(tableName -> {
        NormalBackEndTableInfoConfig normalBackEndTableInfoConfig = new NormalBackEndTableInfoConfig(targetName, schemaName, tableName);
        try {
            String createTableSQLByJDBC = getCreateTableSQLByJDBC(schemaName, tableName, Collections.singletonList(new BackendTableInfo(targetName, schemaName, tableName))).orElse(null);
            if (createTableSQLByJDBC != null) {
                res.put(tableName, new NormalTableConfig(createTableSQLByJDBC, normalBackEndTableInfoConfig));
            } else {
            // exception
            }
        } catch (Throwable e) {
            LOGGER.warn("", e);
        }
    });
    return res;
}
Also used : NormalTableConfig(io.mycat.config.NormalTableConfig) NormalBackEndTableInfoConfig(io.mycat.config.NormalBackEndTableInfoConfig) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

RowBaseIterator (io.mycat.api.collector.RowBaseIterator)1 NormalBackEndTableInfoConfig (io.mycat.config.NormalBackEndTableInfoConfig)1 NormalTableConfig (io.mycat.config.NormalTableConfig)1 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)1 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1