Search in sources :

Example 1 with DdlSchemaFilter

use of com.alibaba.otter.shared.common.utils.meta.DdlSchemaFilter in project otter by alibaba.

the class DataSourceChecker method checkNamespaceTables.

public String checkNamespaceTables(final String namespace, final String name, final Long dataSourceId) {
    DataSource dataSource = null;
    try {
        DataMediaSource source = dataMediaSourceService.findById(dataSourceId);
        DbMediaSource dbMediaSource = (DbMediaSource) source;
        dataSource = dataSourceCreator.createDataSource(dbMediaSource);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List<String> schemaList;
        {
            ModeValue mode = ConfigHelper.parseMode(namespace);
            String schemaPattern = ConfigHelper.makeSQLPattern(mode, namespace);
            final ModeValueFilter modeValueFilter = ConfigHelper.makeModeValueFilter(mode, namespace);
            if (source.getType().isOracle()) {
                schemaList = Arrays.asList(namespace);
            } else {
                schemaList = DdlUtils.findSchemas(jdbcTemplate, schemaPattern, new DdlSchemaFilter() {

                    @Override
                    public boolean accept(String schemaName) {
                        return modeValueFilter.accept(schemaName);
                    }
                });
            }
        }
        final List<String> matchSchemaTables = new ArrayList<String>();
        matchSchemaTables.add("Find schema and tables:");
        if (schemaList != null) {
            ModeValue mode = ConfigHelper.parseMode(name);
            String tableNamePattern = ConfigHelper.makeSQLPattern(mode, name);
            final ModeValueFilter modeValueFilter = ConfigHelper.makeModeValueFilter(mode, name);
            for (String schema : schemaList) {
                DdlUtils.findTables(jdbcTemplate, schema, schema, tableNamePattern, null, new DdlTableNameFilter() {

                    @Override
                    public boolean accept(String catalogName, String schemaName, String tableName) {
                        if (modeValueFilter.accept(tableName)) {
                            matchSchemaTables.add(schemaName + "." + tableName);
                        }
                        return false;
                    }
                });
            }
        }
        if (matchSchemaTables.size() == 1) {
            return TABLE_FAIL;
        }
        return StringUtils.join(matchSchemaTables, "<br>\n");
    } catch (Exception e) {
        logger.error("check error!", e);
        return TABLE_FAIL;
    } finally {
        dataSourceCreator.destroyDataSource(dataSource);
    }
}
Also used : ModeValue(com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue) ModeValueFilter(com.alibaba.otter.shared.common.model.config.ModeValueFilter) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) DdlTableNameFilter(com.alibaba.otter.shared.common.utils.meta.DdlTableNameFilter) DataMediaSource(com.alibaba.otter.shared.common.model.config.data.DataMediaSource) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) DdlSchemaFilter(com.alibaba.otter.shared.common.utils.meta.DdlSchemaFilter)

Aggregations

ModeValueFilter (com.alibaba.otter.shared.common.model.config.ModeValueFilter)1 ModeValue (com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue)1 DataMediaSource (com.alibaba.otter.shared.common.model.config.data.DataMediaSource)1 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)1 DdlSchemaFilter (com.alibaba.otter.shared.common.utils.meta.DdlSchemaFilter)1 DdlTableNameFilter (com.alibaba.otter.shared.common.utils.meta.DdlTableNameFilter)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1