Skip to content

Commit

Permalink
Removed code smell For-Select cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
suarezgpablo committed Oct 4, 2024
1 parent 6ddff27 commit dd86a16
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
17 changes: 13 additions & 4 deletions modevo-script/src/main/java/giis/modevo/migration/script/For.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ public For () {
*/
public void newForSelect (Select s) {
this.getSelectsFor().add(s);
For oldFor = s.getInsideFor();
oldFor.setNestedFor(this); //might change to a list
s.setLoopFor(this);
s.setInsideFor(null);
}
public boolean getSelect(Select select) {
for (Select s: selectsFor) {
if (select.equals(s)) {
return true;
}
}
for (Select s: selectsInsideFor) {
if (select.equals(s)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ private Script migrationSplitColumn(Schema schema, SchemaEvolution se, Migration
Column oldColumnObject = t.getColumn(oldColumn);
for (CriteriaSplit critSplit : critSplits) {
For forSplit = new For ();
Select s = new Select (forSplit);
Select s = new Select ();
forSplit.getSelectsFor().add(s);
s.setTable(t);
Column copyOldColumnObject = new Column (oldColumnObject);

s.getSearch().add(copyOldColumnObject);
s.setSplitColumn(copyOldColumnObject);
forSplit.getSelectsFor().add(s);
s.setCriteriaOperator(critSplit.getOperator());
s.setCriteriaValue(critSplit.getValue());
Insert insert = new Insert(schema.getTable(mt.getName()), forSplit);
Expand Down Expand Up @@ -176,7 +176,7 @@ private Script migrationNewTableAll(Schema schema, SchemaEvolution se, Migration
ModelUtilities mu = new ModelUtilities ();
//Initilization of first For statement, Select looped by the for and the Insert inside the loop
For firstFor = new For ();
Select s = new Select (firstFor);
Select s = new Select ();
firstFor.getSelectsFor().add(s);
Table to = mu.findTable (schema, se, mt.getName());
Insert insert = new Insert( to, firstFor);
Expand Down Expand Up @@ -227,15 +227,16 @@ private Script migrationNewColumn(Schema schema, SchemaEvolution se, MigrationTa
log.info("New SELECT to table %s", targetTable);
existed = false;
forSourceKey = new For ();
selectTargetKey = new Select (forSourceKey, targetTable);
selectTargetKey = new Select (targetTable);
forSourceKey.getSelectsFor().add(selectTargetKey);
script.getSelects().add(selectTargetKey);
script.getForsHigherLevel().add(forSourceKey);
script.getFors().add(forSourceKey);
fors.add(forSourceKey);
}
else {
log.info("Using existing SELECT to table %s", targetTable);
forSourceKey = selectTargetKey.getLoopFor();
forSourceKey = findFor(selectTargetKey);
}
Insert insertTarget = new Insert(targetTable, forSourceKey);
insertTarget.setNameNewTable(mt.getNewTableName());
Expand Down Expand Up @@ -267,6 +268,16 @@ private Script migrationNewColumn(Schema schema, SchemaEvolution se, MigrationTa
}
return script;
}
/**
* Finds the For statement where the select statement is executed
*/
private For findFor(Select select) {
for (For forCurrent:fors) {
if (forCurrent.getSelect (select))
return forCurrent;
}
return null;
}
private void addForSelectInsert(For firstFor, Select s, Insert insert) {
this.getFors().add(firstFor);
this.getSelects().add(s);
Expand All @@ -288,14 +299,14 @@ private Select insertSelect(Column c, Table from, String[] keyColumnFrom, Schema
s.addWhere (schema, keyColumnFrom);
Select sourceValues = s.getSelectSourceValueWhere(s.getWhere(), selects);
if (sourceValues != null) {
if (sourceValues.getLoopFor() == null) {
For forSelect = findFor(sourceValues);
if (forSelect == null) {
For newFor = new For();
newFor.newForSelect(sourceValues);
this.getFors().add(newFor);
}
else {
s.setInsideFor(sourceValues.getLoopFor());
sourceValues.getLoopFor().getSelectsInsideFor().add(s);
forSelect.getSelectsInsideFor().add(s);
}
}
this.getSelects().add(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,21 @@ public class Select {
private String criteriaOperator;
private String criteriaValue;
private Column splitColumn;
private For insideFor; //when the SELECT is inside a FOR
private For loopFor; //when the FOR iterates over the SELECT results


public Select () {
where = new ArrayList<>();
search = new ArrayList<>();
whereValue = new ArrayList<>();
}

public Select(For firstFor) {
this();
this.setLoopFor(firstFor);
}

public Select(Table table, Column c) {
this();
this.table = table;
search.add(c);
}

public Select(For loopFor, Table table) {
public Select(Table table) {
this ();
this.loopFor = loopFor;
this.table = table;
}
public boolean includesAllColumns(List<Column> where2) {
Expand Down

0 comments on commit dd86a16

Please sign in to comment.