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