diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java index ecbb1c5ab1a90..a7a19df939a6b 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLBuilder.java @@ -105,14 +105,16 @@ public SQLUnit toSQL(final Map logicAndActualTables) { */ public SQLUnit toSQL() { StringBuilder result = new StringBuilder(); - List insertParameters = new LinkedList<>(); + List insertParameters = Collections.emptyList(); for (Object each : segments) { if (each instanceof Alterable) { result.append(((Alterable) each).toString(null, Collections.emptyMap())); } else { result.append(each); } - insertParameters.addAll(getInsertParameters(each, null)); + if (each instanceof InsertSetPlaceholder || each instanceof InsertValuesPlaceholder) { + insertParameters = getInsertParameters(each); + } } return insertParameters.isEmpty() ? new SQLUnit(result.toString(), new ArrayList<>(parameters)) : new SQLUnit(result.toString(), insertParameters); } @@ -126,7 +128,7 @@ public SQLUnit toSQL() { */ public SQLUnit toSQL(final RoutingUnit routingUnit, final Map logicAndActualTables) { StringBuilder result = new StringBuilder(); - List insertParameters = new LinkedList<>(); + List insertParameters = Collections.emptyList(); for (Object each : segments) { if (each instanceof Alterable) { result.append(((Alterable) each).toString(routingUnit, logicAndActualTables)); @@ -134,33 +136,33 @@ public SQLUnit toSQL(final RoutingUnit routingUnit, final Map lo result.append(each); } if (each instanceof InsertSetPlaceholder || each instanceof InsertValuesPlaceholder) { - insertParameters.addAll(getInsertParameters(each, routingUnit)); + insertParameters = getInsertParameters(each, routingUnit); } } return insertParameters.isEmpty() ? new SQLUnit(result.toString(), new ArrayList<>(parameters)) : new SQLUnit(result.toString(), insertParameters); } - private List getInsertParameters(final Object target, final RoutingUnit routingUnit) { + private List getInsertParameters(final Object segment) { List result = new LinkedList<>(); - List units = target instanceof InsertSetPlaceholder ? ((InsertSetPlaceholder) target).getUnits() : ((InsertValuesPlaceholder) target).getUnits(); - result.addAll(getInsertParameters(routingUnit, units)); + List units = segment instanceof InsertSetPlaceholder ? ((InsertSetPlaceholder) segment).getUnits() : ((InsertValuesPlaceholder) segment).getUnits(); + for (InsertOptimizeResultUnit each : units) { + result.addAll(Arrays.asList(each.getParameters())); + } return result; } - private List getInsertParameters(final RoutingUnit routingUnit, final List units) { + private List getInsertParameters(final Object segment, final RoutingUnit routingUnit) { List result = new LinkedList<>(); + List units = segment instanceof InsertSetPlaceholder ? ((InsertSetPlaceholder) segment).getUnits() : ((InsertValuesPlaceholder) segment).getUnits(); for (InsertOptimizeResultUnit each : units) { - if (isToAppendInsertOptimizeResult(routingUnit, each)) { + if (isAppendInsertParameter(each, routingUnit)) { result.addAll(Arrays.asList(each.getParameters())); } } return result; } - private boolean isToAppendInsertOptimizeResult(final RoutingUnit routingUnit, final InsertOptimizeResultUnit unit) { - if (null == routingUnit || unit.getDataNodes().isEmpty()) { - return true; - } + private boolean isAppendInsertParameter(final InsertOptimizeResultUnit unit, final RoutingUnit routingUnit) { for (DataNode each : unit.getDataNodes()) { if (routingUnit.getTableUnit(each.getDataSourceName(), each.getTableName()).isPresent()) { return true;