Commit a7bf72cb authored by npedot's avatar npedot
Browse files

adds schema moveConstraintsFromColsToCol

parent 10c183bf
......@@ -65,6 +65,19 @@ class Schema () {
}
}
fun moveConstraintsFromColsToCol(originTableName: String, keyCols: String, sid: String) {
val colNames = keyCols.split(",")
val constraints = constraintsByTable(originTableName)
for (constr in constraints) {
for (col in constr.source.columns) {
if (keyCols.contains(col.name)) col.name = sid
}
for (col in constr.target.columns) {
if (keyCols.contains(col.name)) col.name = sid
}
}
}
fun copyConstraintsFromTableToTable(sourceTableName: String, targetTableName:String) {
val sourceTableConstraints = constraintsByTable(sourceTableName)
for (constr in sourceTableConstraints) {
......
......@@ -23,6 +23,7 @@ fun oid(schema: Schema, originTableName: String): ChangeSet {
// create a key-table with projection of oid and pk
val keyCols = originTableKey.map { c -> c.name }.joinToString(",")
val surrogateTableName = "SKEY$originTableName"
sqlCommands.add("CREATE TABLE $surrogateTableName AS SELECT $sid,$keyCols FROM $originTableName")
schema.addTable("$surrogateTableName:$sid,$keyCols")
......@@ -31,25 +32,24 @@ fun oid(schema: Schema, originTableName: String): ChangeSet {
schema.copyConstraintsFromTableToTable(originTableName,surrogateTableName)
// search ftables with foreign keys od double-inc as pk
val rTables = schema.referencedTablesOf(originTableName)
println("==============REFERENCED:")
for (rTable in rTables) {
// remove constraint from old table (and remember)
println(rTable.name)
val rTableColsToKeep = schema.table(rTable.name)!!.columns.toMutableSet()
rTableColsToKeep.removeAll(originTable!!.columns)
val notKeyCols = rTableColsToKeep.map { notKeyColName -> "${rTable.name}.$notKeyColName" }.joinToString(",")
// crea una nuova tabella con chiave surrogata + attributi non chiave
val rTableNewName = "${rTable.name}_1"
// FIXME funziona solo per chiavi a colonna singola
val newTableCommand = "CREATE TABLE ${rTableNewName} AS SELECT $surrogateTableName.$sid,$notKeyCols FROM SKEY$originTableName JOIN ${rTable.name} ON SKEY$originTableName.${originTableKey.first()} = ${rTable.name}.${originTableKey.first()}"
schema.addTable("${rTableNewName}:$surrogateTableName.$sid,$notKeyCols")
//TODO changeSet.createTable.add(CreateTable() name rTableNewName withCols Column.set("SKEY$originTableName.$sid,$notKeyCols"))
schema.moveConstraintsFromTableToTable(rTable.name,rTableNewName)
println(newTableCommand)
val rTableKeys = schema.key(rTable.name).map { col -> col.name }.joinToString(",")
schema.moveConstraintsFromColsToCol(rTableNewName,rTableKeys,sid)
sqlCommands.add(newTableCommand)
// add constraint to new table (from memory)
}
println("________________________")
// crea una double-inc tra tabella-chiave e tabella origine.
val index = (schema.constraints?.size?: 0) + changeSet.size()
......@@ -63,6 +63,9 @@ fun oid(schema: Schema, originTableName: String): ChangeSet {
// remove ex-pk columns from origin-table
sqlCommands.add("ALTER TABLE $originTableName DROP COLUMN $keyCols")
schema.table(originTableName)!!.columns.removeAll(originTableKey)
schema.moveConstraintsFromColsToCol(originTableName,keyCols,sid)
//TODO changeset remove columns
changeSet.sqlCommands = sqlCommands
return changeSet
......
......@@ -450,4 +450,18 @@ class SchemaTest {
assertEquals(1,schema.constraintsByTable("employee_1").size)
assertEquals("[DOUBLE_INCLUSION person:name --> employee_1:name ; ]",schema.constraintsByTable("employee_1").toString())
}
@Test
fun test_move_constraint_from_cols_to_col() {
// given
val schema = Schema()
schema.addTable("Person:name,surname")
schema.addKey("Person:name")
// when
schema.moveConstraintsFromColsToCol("Person","name","surname")
//then
val key = schema.key("Person")
assertEquals("surname",key.first().name)
assertEquals(1,key.size)
}
}
......@@ -23,6 +23,7 @@ class OidsTest {
assertEquals(4,sqlCommands.size)
assertEquals("ALTER TABLE person ADD COLUMN sidperson int NOT NULL auto_increment UNIQUE",sqlCommands[0])
assertEquals("CREATE TABLE SKEYperson AS SELECT sidperson,surname,name FROM person",sqlCommands[1])
// FIXME manca join name, ha preso solo surname
assertEquals("CREATE TABLE teacher_1 AS SELECT SKEYperson.sidperson,teacher.course FROM SKEYperson JOIN teacher ON SKEYperson.surname = teacher.surname",sqlCommands[2])
assertEquals("ALTER TABLE person DROP COLUMN surname,name",sqlCommands[3])
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment