Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat():canal-adapter同步到rdb时,mirrorDb模式下支持改库名 #5218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ private void initMappingConfig(String key, MappingConfig baseConfigMap, MirrorDb
mappingConfig.setDbMapping(dbMapping);
dbMapping.setDatabase(dml.getDatabase());
dbMapping.setTable(dml.getTable());
dbMapping.setTargetDb(dml.getDatabase());
// 获取 targetDb,如果没有设置则使用 dml.getDatabase()
String targetDb = baseConfigMap.getDbMapping().getTargetDb();
if (StringUtils.isEmpty(targetDb)) {
targetDb = dml.getDatabase();
}
dbMapping.setTargetDb(targetDb);
dbMapping.setTargetTable(dml.getTable());
dbMapping.setMapAll(true);
List<String> pkNames = dml.getPkNames();
Expand All @@ -151,8 +156,16 @@ private void initMappingConfig(String key, MappingConfig baseConfigMap, MirrorDb
*/
private void executeDdl(MirrorDbConfig mirrorDbConfig, Dml ddl) {
try (Connection conn = dataSource.getConnection(); Statement statement = conn.createStatement()) {
// 替换反引号

String sql = ddl.getSql();
// 从配置中获取 targetDb,如果有则进行替换
String targetDb = mirrorDbConfig.getMappingConfig().getDbMapping().getTargetDb();
if (!StringUtils.isEmpty(targetDb)) {
String originalDbName = ddl.getDatabase();
String regex = String.format("\\b%s\\b([.`])", originalDbName);
sql = sql.replaceAll(regex, targetDb + "$1");
}
// 替换反引号
String backtick = SyncUtil.getBacktickByDbType(dataSource.getDbType());
if (!"`".equals(backtick)) {
sql = sql.replaceAll("`", backtick);
Expand Down