Skip to content

Commit

Permalink
Added script creation and execution for the split of an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
suarezgpablo committed Oct 3, 2024
1 parent f751cf0 commit 92dfe0d
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 30 deletions.
6 changes: 6 additions & 0 deletions modevo-script/dat/bmk/testMindsV3SplitColumn-script.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FOR $1,$2 = SELECT parent_guid, id FROM comments WHERE parent_guid>'f' ALLOW FILTERING
FOR $3,$4 = SELECT parent_guid, id FROM comments WHERE parent_guid>'p' ALLOW FILTERING
FOR $5,$6 = SELECT parent_guid, id FROM comments WHERE parent_guid<'p' ALLOW FILTERING
INSERT INTO comments(parent_guid_c1, id) VALUES ($1, $2);
INSERT INTO comments(parent_guid_c2, id) VALUES ($3, $4);
INSERT INTO comments(parent_guid_c3, id) VALUES ($5, $6);
4 changes: 4 additions & 0 deletions modevo-script/dat/inp/testMindsV3SplitColumn-initDB.cql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TRUNCATE "testMindsV3SplitColumn".comments;
Insert into "testMindsV3SplitColumn".comments (id, parent_guid) values ('1', '100');
Insert into "testMindsV3SplitColumn".comments (id, parent_guid) values ('2', '200');
Insert into "testMindsV3SplitColumn".comments (id, parent_guid) values ('3', '300');
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ public class Insert {
private String nameNewTable; //Only used when a new table is required when adding a new column
public Insert() {
columnValue = new ArrayList<>();
nameNewTable = "";
}
public Insert(Table table) {
this();

this.table = table;
}
public Insert(Table table, For insideFor) {
Expand All @@ -31,13 +33,14 @@ public Insert(Table table, For insideFor) {
* @param key If there is no key, value is null
* @param target
*/
public void addColumnValue(Column columnSelect, Select s, String[] key, Column target) {
public ColumnValue addColumnValue(Column columnSelect, Select s, String[] key, Column target) {
ColumnValue cv = new ColumnValue ();
cv.setColumn(target);
cv.setSelectOrigin(s);
cv.setKey(key);
cv.setColumnSelectOrigin(columnSelect);
this.getColumnValue().add(cv);
this.getColumnValue().add(cv);
return cv;
}
public String getNameNewTable() {
return nameNewTable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import giis.modevo.model.schema.Column;
import giis.modevo.model.schema.Schema;
import giis.modevo.model.schema.Table;
import giis.modevo.model.schemaevolution.CriteriaSplit;
import giis.modevo.model.schemaevolution.SchemaChange;
import giis.modevo.model.schemaevolution.SchemaEvolution;
import giis.modevo.model.schemaevolution.SplitColumn;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -33,11 +36,13 @@ public class Script {
private List<Insert> inserts;
private ScriptText scriptText;
private boolean executable;
private List<For> forsSplit;
public Script () {
setExecutable(true);//default value
selects = new ArrayList<>();
fors = new ArrayList<>();
inserts = new ArrayList<>();
forsSplit = new ArrayList<>();
setForsHigherLevel(new ArrayList<>());
scriptText = new ScriptText();
}
Expand All @@ -62,6 +67,9 @@ else if (mt.migrationFromOneTableNewTable(schema)) {
else if (mt.migrationNewColumn(schema) && !mt.migrationFromRemovePK(se, mt)) {
scripts.add(migrationNewColumn (schema, se, mt));
}
else if (mt.migrationSplitColumn(se)) {
scripts.add(migrationSplitColumn (schema, se, mt));
}
else {
return new ArrayList<>(); //Scenarios not implemented
}
Expand All @@ -70,6 +78,42 @@ else if (mt.migrationNewColumn(schema) && !mt.migrationFromRemovePK(se, mt)) {
return scripts;
}

private Script migrationSplitColumn(Schema schema, SchemaEvolution se, MigrationTable mt) {
log.info("Split Column Script. Target table: %s", mt.getName());
Script script = new Script ();
for (SchemaChange sc : se.getChanges()) {
if (sc instanceof SplitColumn spc) {
List<CriteriaSplit> critSplits = spc.getCs();
String oldColumn = spc.getOldColumn();
Table t = schema.getTable(mt.getName());
Column oldColumnObject = t.getColumn(oldColumn);
for (CriteriaSplit critSplit : critSplits) {
For forSplit = new For ();
Select s = new Select (forSplit);
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);
insert.addColumnValue (copyOldColumnObject, s, null, critSplit.getColumn());
for (Column c: t.getKey()) {
ColumnValue cv=insert.addColumnValue (c, s, null, c);
Column copyTarget = new Column (c);
cv.getSelectOrigin().getSearch().add(copyTarget);
cv.setColumn(copyTarget);

}
script.getForsSplit().add(forSplit);
script.addForSelectInsert(forSplit, s, insert);
}
}
}
return script;
}
private void checkIfExecutable(List<Script> scripts) {
for (Script s: scripts) {
for (Insert i: s.getInserts()) {
Expand Down Expand Up @@ -261,7 +305,7 @@ private Select insertSelect(Column c, Table from, String[] keyColumnFrom, Schema

public boolean inList(Column c, List<ColumnValue> columnValues) {
for (ColumnValue cv : columnValues) {
if (cv.getColumn().equals(c)) {
if (cv.getColumn().equalsValues(c)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ public String writeScript(Script s) {
return "Not executable script because there are key columns that cannot be inserted";
}
StringBuilder sb = new StringBuilder();
For forNested = s.getFors().get(0);
while (forNested != null) {
writeSyntaxFor (sb, forNested, s);
forNested = forNested.getNestedFor();
if (s.getForsSplit().isEmpty()) {
For forNested = s.getFors().get(0);
while (forNested != null) {
writeSyntaxFor (sb, forNested, s);
forNested = forNested.getNestedFor();
}
}
else { //When there is a split
List<For> forsSplit = s.getForsSplit();
for (For forSplit:forsSplit) {
writeSyntaxFor (sb, forSplit, s);
}
}
writeSyntaxInsert (sb, s);
log.info("Script screated");
Expand Down Expand Up @@ -53,8 +61,16 @@ private void writeSyntaxInsert(StringBuilder sb, Script s) {
if (cv.getVariableName()==null) {
cv.setVariableName(cv.getColumn().getVariableName());
}
if (cv.getVariableName()==null) {
cv.setVariableName(cv.getColumnSelectOrigin().getVariableName());
}
String nameColumn = cv.getColumn().getName();
String nameVariable=cv.getSelectOrigin().findNameVariable (cv.getColumn().getNameAttribute(), cv.getColumn().getNameEntity());
if (nameVariable == null) {
Column columnOrigin = cv.getSelectOrigin().getSplitColumn();
nameVariable=columnOrigin.getVariableName();

}
namesColumns.append(nameColumn);
insertPlaceholders.append(nameVariable);
}
Expand Down Expand Up @@ -99,6 +115,9 @@ private String buildSelect(Select s, StringBuilder sb, Script se) {
counterVariables++;
}
columns.append(" FROM ").append(s.getTable().getName());
if (!se.getForsSplit().isEmpty()) {
textCriteriaSplit (s, columns);
}
for (int j=0; j<s.getWhere().size();j++) {
if (j==0) {
columns.append(" WHERE ");
Expand All @@ -121,6 +140,31 @@ private String buildSelect(Select s, StringBuilder sb, Script se) {

}

private void textCriteriaSplit(Select s, StringBuilder columns) {
columns.append(" WHERE ");
String nameColumn = s.getSplitColumn().getName();
columns.append(nameColumn);
String criteriaOperator = s.getCriteriaOperator();
switch (criteriaOperator) {
case "eq":
columns.append("=");
break;
case "g":
columns.append(">");
break;
case "l":
columns.append("<");
break;
case "ge":
columns.append(">=");
break;
case "le":
columns.append("<=");
break;
}
columns.append("'"+s.getCriteriaValue()+"'");
columns.append(" ALLOW FILTERING");
}
private String getNameVariableColumn(Column ce, Script s) {
String nameAttribute = ce.getNameAttribute();
String nameEntity = ce.getNameEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class Select {
private List<ColumnValue> whereValue;
private List<Column> where;
private String selectStatement;
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,28 @@ public void execute(Script script, CassandraConnection c, String nameKeyspace) {
return;
}
List<For> highLevelFors = script.getForsHigherLevel();
List<For> splitFors = script.getForsSplit();
for (For highFor : highLevelFors) {
List<Select> highLevelSelects = highFor.getSelectsFor();
Select s = highLevelSelects.get(0); //For now there is only one select at most, change into a list when there will be more.
String nameTable = s.getTable().getName();
String selectStatementWithKeyspace = s.getSelectStatement().replace(FROM+nameTable,FROM+ "\""+nameKeyspace+"\"."+nameTable);
ResultSet rs = c.executeStatement(selectStatementWithKeyspace);
Iterator<Row> resultIt = rs.iterator();
while (resultIt.hasNext()) {
Row rw = resultIt.next();
List<ColumnValue> cvs = getColumnValuesSearchRow(rw, s);
List<Select> selectsInside = highFor.getSelectsInsideFor();
List<List<ColumnValue>> cvsFromWhere = executeSelects (cvs, selectsInside, c, nameKeyspace);
executeInserts (script, cvs, cvsFromWhere, highFor, c, nameKeyspace);
}
executionFor (highFor, script, c, nameKeyspace);
}
for (For splitFor : splitFors) {
executionFor (splitFor, script, c, nameKeyspace);
}

}
private void executionFor (For forToExecute, Script script, CassandraConnection c, String nameKeyspace) {
List<Select> highLevelSelects = forToExecute.getSelectsFor();
Select s = highLevelSelects.get(0); //For now there is only one select at most, change into a list when there will be more.
String nameTable = s.getTable().getName();
String selectStatementWithKeyspace = s.getSelectStatement().replace(FROM+nameTable,FROM+ "\""+nameKeyspace+"\"."+nameTable);
ResultSet rs = c.executeStatement(selectStatementWithKeyspace);
Iterator<Row> resultIt = rs.iterator();
while (resultIt.hasNext()) {
Row rw = resultIt.next();
List<ColumnValue> cvs = getColumnValuesSearchRow(rw, s);
List<Select> selectsInside = forToExecute.getSelectsInsideFor();
List<List<ColumnValue>> cvsFromWhere = executeSelects (cvs, selectsInside, c, nameKeyspace);
executeInserts (script, cvs, cvsFromWhere, forToExecute, c, nameKeyspace);
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public void testThingsBoardV13NewColumnKeyInTable() {
testScript(name.getMethodName(), connection);
}
@Test
public void testMindsV3SplitColumn () {
testScript(name.getMethodName(), connection);
}
@Test
public void testMindsV9NewTableMigrationFromOneTable () {
testScript(name.getMethodName(), connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
xmi:id="cs1"
column="c1"
value="f"
operator="gt"/>
operator="g"/>
<CriteriaScplit
xmi:id="cs2"
column="c2"
value="p"
operator="gt"/>
operator="g"/>
<CriteriaScplit
xmi:id="cs3"
column="c3"
value="p"
operator="lt"/>
operator="l"/>
</xmi:XMI>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import giis.modevo.model.schemaevolution.RemovePK;
import giis.modevo.model.schemaevolution.SchemaChange;
import giis.modevo.model.schemaevolution.SchemaEvolution;
import giis.modevo.model.schemaevolution.SplitColumn;
import lombok.Getter;
import lombok.Setter;

Expand Down Expand Up @@ -125,6 +126,17 @@ public boolean migrationFromRemovePK(SchemaEvolution se, MigrationTable mt) {
}
return false;
}
/**
* Checks if the migration script contains a split of a column
*/
public boolean migrationSplitColumn(SchemaEvolution se) {
for (SchemaChange sc : se.getChanges()) {
if (sc instanceof SplitColumn) {
return true;
}
}
return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ public Column(Column c) {
this.ck = c.ck;
this.setNameAttribute(c.nameAttribute);
this.table = c.table;
this.dataType = c.dataType;
this.nameEntity = c.nameEntity;
this.variableName = c.variableName;

}

public Column(String nameTable) {
this.name = nameTable;
}

public boolean equalsValues (Column c) {
if (c.getName().equals(this.getName()) && c.getTable().equals(this.getTable()) && c.getNameAttribute().equals(this.getNameAttribute()) && c.getNameEntity().equals(this.getNameEntity())) {
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public Table(String name, List<Column> columns) {
super();
this.setName(name);
this.columns = columns;
for (Column c: columns) {
c.setTable(this);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void storeInfo(SchemaEvolution se, NodeList list, Element element) {
if (column == null) {
throw new DocumentException(messageIdMissing(c));
}
Column columnObject = super.columnFromModelToObject(column);
Column columnObject = super.columnFromModelToObject(column, t);
t.getColumns().add(columnObject);
AddColumn ac = new AddColumn(columnObject, t);
se.getChanges().add(ac);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void storeInfo(SchemaEvolution se, NodeList list, Node node) {
if (elementColumnSource == null) {
throw new DocumentException(messageIdMissing(idTargetColumn));
}
Column c = columnFromModelToObject(elementColumnSource);
Column c = columnFromModelToObject(elementColumnSource, source);
String criteriaString = elementCopy.getAttribute("criteria");
JoinColumn jt = new JoinColumn(c, source, criteriaString);
String[] columnsIdArray = idsSourceColumns.split(" ");
Expand All @@ -50,7 +50,7 @@ protected void storeInfo(SchemaEvolution se, NodeList list, Node node) {
if (column == null) {
throw new DocumentException(messageIdMissing(id));
}
Column columnSource = columnFromModelToObject(column);
Column columnSource = columnFromModelToObject(column, source);
jt.getSourceColumns().add(columnSource);
}
se.getChanges().add(jt);
Expand Down
Loading

0 comments on commit 92dfe0d

Please sign in to comment.