Search in sources :

Example 1 with ModeValue

use of com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue 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)

Example 2 with ModeValue

use of com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue in project otter by alibaba.

the class ConfigHelper method parseMode.

/**
     * 解析DataMedia中的namespace和name,支持offer[1-128]分库的定义
     */
public static ModeValue parseMode(String value) {
    PatternMatcher matcher = new Perl5Matcher();
    if (matcher.matches(value, patterns.get(MODE_PATTERN))) {
        MatchResult matchResult = matcher.getMatch();
        String prefix = matchResult.group(1);
        String startStr = matchResult.group(3);
        String ednStr = matchResult.group(4);
        int start = Integer.valueOf(startStr);
        int end = Integer.valueOf(ednStr);
        String postfix = matchResult.group(5);
        List<String> values = new ArrayList<String>();
        for (int i = start; i <= end; i++) {
            StringBuilder builder = new StringBuilder(value.length());
            String str = String.valueOf(i);
            // 处理0001类型
            if (startStr.length() == ednStr.length() && startStr.startsWith("0")) {
                str = StringUtils.leftPad(String.valueOf(i), startStr.length(), '0');
            }
            builder.append(prefix).append(str).append(postfix);
            values.add(builder.toString());
        }
        return new ModeValue(Mode.MULTI, values);
    } else if (isWildCard(value)) {
        // 通配符支持
        return new ModeValue(Mode.WILDCARD, Arrays.asList(value));
    } else {
        return new ModeValue(Mode.SINGLE, Arrays.asList(value));
    }
}
Also used : ModeValue(com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 3 with ModeValue

use of com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue in project otter by alibaba.

the class ConfigHelperTest method testParse.

@Test
public void testParse() {
    String v1 = "offer[001-128]";
    ModeValue m1 = ConfigHelper.parseMode(v1);
    want.bool(m1.getMode().isMulti()).is(true);
    want.collection(m1.getMultiValue()).sizeEq(128);
    String v2 = "offer[1-128]test";
    ModeValue m2 = ConfigHelper.parseMode(v2);
    want.bool(m2.getMode().isMulti()).is(true);
    want.collection(m2.getMultiValue()).sizeEq(128);
    String v3 = "[1-128]test";
    ModeValue m3 = ConfigHelper.parseMode(v3);
    want.bool(m3.getMode().isMulti()).is(true);
    want.collection(m3.getMultiValue()).sizeEq(128);
    String v4 = "offer[1-128";
    ModeValue m4 = ConfigHelper.parseMode(v4);
    want.bool(m4.getMode().isWildCard()).is(true);
    String v5 = "offer1-128]";
    ModeValue m5 = ConfigHelper.parseMode(v5);
    want.bool(m5.getMode().isWildCard()).is(true);
    String v6 = "offer1128";
    ModeValue m6 = ConfigHelper.parseMode(v6);
    want.bool(m6.getMode().isSingle()).is(true);
}
Also used : ModeValue(com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.shared.common.BaseOtterTest)

Example 4 with ModeValue

use of com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue in project otter by alibaba.

the class DataSourceChecker method checkMap.

public String checkMap(String namespace, String name, Long dataSourceId) {
    Connection conn = null;
    Statement stmt = null;
    DataMediaSource source = dataMediaSourceService.findById(dataSourceId);
    DataSource dataSource = null;
    try {
        DbMediaSource dbMediaSource = (DbMediaSource) source;
        dataSource = dataSourceCreator.createDataSource(dbMediaSource);
        // conn = dataSource.getConnection();
        // if (null == conn) {
        // return DATABASE_FAIL;
        // }
        ModeValue namespaceValue = ConfigHelper.parseMode(namespace);
        ModeValue nameValue = ConfigHelper.parseMode(name);
        String tempNamespace = namespaceValue.getSingleValue();
        String tempName = nameValue.getSingleValue();
        try {
            Table table = DdlUtils.findTable(new JdbcTemplate(dataSource), tempNamespace, tempNamespace, tempName);
            if (table == null) {
                return SELECT_FAIL;
            }
        } catch (SQLException se) {
            logger.error("check error!", se);
            return SELECT_FAIL;
        } catch (Exception e) {
            logger.error("check error!", e);
            return SELECT_FAIL;
        }
    // String selectSql = "SELECT * from " + tempNamespace + "." +
    // tempName + " where 1 = 0";
    // String insertSql = "INSERT INTO " + tempNamespace + "." +
    // tempName + " select * from ";
    // insertSql += "( SELECT * from " + tempNamespace + "." + tempName
    // + ") table2 where 1 = 0";
    // String deleteSql = "DELETE from " + tempNamespace + "." +
    // tempName + " where 1 = 0";
    //
    // stmt = conn.createStatement();
    //
    // try {
    // stmt.executeQuery(selectSql);
    // } catch (SQLException se) {
    // return SELECT_FAIL;
    // }
    //
    // try {
    // stmt.execute(insertSql);
    // } catch (SQLException se) {
    // return INSERT_FAIL;
    // }
    //
    // try {
    // stmt.execute(deleteSql);
    // } catch (SQLException se) {
    // return DELETE_FAIL;
    // }
    } finally {
        closeConnection(conn, stmt);
        dataSourceCreator.destroyDataSource(dataSource);
    }
    return TABLE_SUCCESS;
}
Also used : Table(org.apache.ddlutils.model.Table) ModeValue(com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) DataMediaSource(com.alibaba.otter.shared.common.model.config.data.DataMediaSource) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Aggregations

ModeValue (com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue)4 DataMediaSource (com.alibaba.otter.shared.common.model.config.data.DataMediaSource)2 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 DataSource (javax.sql.DataSource)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2 BaseOtterTest (com.alibaba.otter.shared.common.BaseOtterTest)1 ModeValueFilter (com.alibaba.otter.shared.common.model.config.ModeValueFilter)1 DdlSchemaFilter (com.alibaba.otter.shared.common.utils.meta.DdlSchemaFilter)1 DdlTableNameFilter (com.alibaba.otter.shared.common.utils.meta.DdlTableNameFilter)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 Table (org.apache.ddlutils.model.Table)1 MatchResult (org.apache.oro.text.regex.MatchResult)1 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)1 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)1 Test (org.testng.annotations.Test)1