1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.sharding.rewrite.token.pojo;
19
20 import org.apache.shardingsphere.infra.datanode.DataNode;
21 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.RouteUnitAware;
22 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.InsertValue;
23 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.InsertValuesToken;
24 import org.apache.shardingsphere.infra.route.context.RouteUnit;
25
26
27
28
29 public final class ShardingInsertValuesToken extends InsertValuesToken implements RouteUnitAware {
30
31 public ShardingInsertValuesToken(final int startIndex, final int stopIndex) {
32 super(startIndex, stopIndex);
33 }
34
35 @Override
36 public String toString(final RouteUnit routeUnit) {
37 StringBuilder result = new StringBuilder();
38 appendInsertValue(routeUnit, result);
39 result.delete(result.length() - 2, result.length());
40 return result.toString();
41 }
42
43 @Override
44 public String toString() {
45 return toString(null);
46 }
47
48 private void appendInsertValue(final RouteUnit routeUnit, final StringBuilder stringBuilder) {
49 for (InsertValue each : getInsertValues()) {
50 if (isAppend(routeUnit, (ShardingInsertValue) each)) {
51 stringBuilder.append(each).append(", ");
52 }
53 }
54 }
55
56 private boolean isAppend(final RouteUnit routeUnit, final ShardingInsertValue insertValueToken) {
57 if (insertValueToken.getDataNodes().isEmpty() || null == routeUnit) {
58 return true;
59 }
60 for (DataNode each : insertValueToken.getDataNodes()) {
61 if (routeUnit.findTableMapper(each.getDataSourceName(), each.getTableName()).isPresent()) {
62 return true;
63 }
64 }
65 return false;
66 }
67 }