You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/04/24 09:32:31 UTC
[shardingsphere] branch master updated: Rewrite table name with
alter table for fk (#10178)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 3025f0e Rewrite table name with alter table for fk (#10178)
3025f0e is described below
commit 3025f0e5872017408faf8b71706dbf0d1027b6d7
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Apr 24 17:32:01 2021 +0800
Rewrite table name with alter table for fk (#10178)
---
.../token/generator/impl/TableTokenGenerator.java | 10 ++++-----
.../sharding/rewrite/token/pojo/TableToken.java | 8 +++++--
.../src/test/resources/sharding/alter.xml | 25 ++++++++++++++++++++++
3 files changed, 36 insertions(+), 7 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/TableTokenGenerator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/TableTokenGenerator.java
index b47085d..a493ebe 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/TableTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/TableTokenGenerator.java
@@ -18,13 +18,13 @@
package org.apache.shardingsphere.sharding.rewrite.token.generator.impl;
import lombok.Setter;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.type.TableAvailable;
+import org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
+import org.apache.shardingsphere.sharding.rewrite.token.pojo.TableToken;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.aware.ShardingRuleAware;
-import org.apache.shardingsphere.sharding.rewrite.token.pojo.TableToken;
-import org.apache.shardingsphere.infra.binder.type.TableAvailable;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
import java.util.Collection;
import java.util.Collections;
@@ -36,7 +36,7 @@ import java.util.LinkedList;
@Setter
public final class TableTokenGenerator implements CollectionSQLTokenGenerator, ShardingRuleAware {
- private ShardingRule shardingRule;
+ private ShardingRule shardingRule;
@Override
public boolean isGenerateSQLToken(final SQLStatementContext sqlStatementContext) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TableToken.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TableToken.java
index bb41805..0ec8d45 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TableToken.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TableToken.java
@@ -19,19 +19,21 @@ package org.apache.shardingsphere.sharding.rewrite.token.pojo;
import com.google.common.base.Joiner;
import lombok.Getter;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.type.TableAvailable;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.RouteUnitAware;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.Substitutable;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.stream.Collectors;
/**
* Table token.
@@ -70,7 +72,9 @@ public final class TableToken extends SQLToken implements Substitutable, RouteUn
}
private Map<String, String> getLogicAndActualTables(final RouteUnit routeUnit) {
- Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames();
+ Collection<String> tableNames = sqlStatementContext instanceof TableAvailable
+ ? ((TableAvailable) sqlStatementContext).getAllTables().stream().map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toList())
+ : sqlStatementContext.getTablesContext().getTableNames();
Map<String, String> result = new HashMap<>(tableNames.size(), 1);
for (RouteMapper each : routeUnit.getTableMappers()) {
result.put(each.getLogicName().toLowerCase(), each.getActualName());
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/alter.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/alter.xml
new file mode 100644
index 0000000..d5e7dab
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/alter.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
+ <rewrite-assertion id="alter_table_with_add_foreign_constraint_for_postgresql" db-type="PostgreSQL">
+ <input sql="ALTER TABLE t_account add constraint t_account_fk foreign key (account_id) references t_account_detail (account_id)" />
+ <output sql="ALTER TABLE t_account_0 add constraint t_account_fk foreign key (account_id) references t_account_detail_0 (account_id)" />
+ <output sql="ALTER TABLE t_account_1 add constraint t_account_fk foreign key (account_id) references t_account_detail_1 (account_id)" />
+ </rewrite-assertion>
+</rewrite-assertions>