Skip to content

Commit

Permalink
for #2275, refactor SQLBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 16, 2019
1 parent 5cfdfb1 commit 5a12da6
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ public SQLUnit toSQL(final Map<String, String> logicAndActualTables) {
*/
public SQLUnit toSQL() {
StringBuilder result = new StringBuilder();
List<Object> insertParameters = new LinkedList<>();
List<Object> insertParameters = Collections.emptyList();
for (Object each : segments) {
if (each instanceof Alterable) {
result.append(((Alterable) each).toString(null, Collections.<String, String>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);
}
Expand All @@ -126,41 +128,41 @@ public SQLUnit toSQL() {
*/
public SQLUnit toSQL(final RoutingUnit routingUnit, final Map<String, String> logicAndActualTables) {
StringBuilder result = new StringBuilder();
List<Object> insertParameters = new LinkedList<>();
List<Object> insertParameters = Collections.emptyList();
for (Object each : segments) {
if (each instanceof Alterable) {
result.append(((Alterable) each).toString(routingUnit, logicAndActualTables));
} else {
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<Object> getInsertParameters(final Object target, final RoutingUnit routingUnit) {
private List<Object> getInsertParameters(final Object segment) {
List<Object> result = new LinkedList<>();
List<InsertOptimizeResultUnit> units = target instanceof InsertSetPlaceholder ? ((InsertSetPlaceholder) target).getUnits() : ((InsertValuesPlaceholder) target).getUnits();
result.addAll(getInsertParameters(routingUnit, units));
List<InsertOptimizeResultUnit> units = segment instanceof InsertSetPlaceholder ? ((InsertSetPlaceholder) segment).getUnits() : ((InsertValuesPlaceholder) segment).getUnits();
for (InsertOptimizeResultUnit each : units) {
result.addAll(Arrays.asList(each.getParameters()));
}
return result;
}

private List<Object> getInsertParameters(final RoutingUnit routingUnit, final List<InsertOptimizeResultUnit> units) {
private List<Object> getInsertParameters(final Object segment, final RoutingUnit routingUnit) {
List<Object> result = new LinkedList<>();
List<InsertOptimizeResultUnit> 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;
Expand Down

0 comments on commit 5a12da6

Please sign in to comment.