Commit 10c183bf authored by npedot's avatar npedot
Browse files

adds schema copy constraints from table to table

parent 8c71d795
......@@ -115,5 +115,4 @@ class Column (): Labelled {
return name.hashCode()
}
}
\ No newline at end of file
}
......@@ -191,5 +191,4 @@ class Constraint(): Labelled {
return this.toString().hashCode()
}
}
......@@ -52,7 +52,7 @@ class Schema () {
return constraints().filter { c -> c.source.table==name || c.target.table==name }
}
fun constraintsFromTableToTable(sourceTableName: String, targetTableName:String) {
fun moveConstraintsFromTableToTable(sourceTableName: String, targetTableName:String) {
val sourceTableConstraints = constraintsByTable(sourceTableName)
for (constr in sourceTableConstraints) {
if (constr.source.table==sourceTableName) {
......@@ -65,6 +65,22 @@ class Schema () {
}
}
fun copyConstraintsFromTableToTable(sourceTableName: String, targetTableName:String) {
val sourceTableConstraints = constraintsByTable(sourceTableName)
for (constr in sourceTableConstraints) {
val copyconstr = constr.clone()
if (constr.source.table==sourceTableName) {
copyconstr.source.table=targetTableName
}
if (constr.target.table==sourceTableName) {
copyconstr.target.table=targetTableName
}
if (constraints==null) constraints = ArrayList()
constraints?.add(copyconstr)
println("COPIED CONSTRAINT FROM $sourceTableName TO:"+constr.toString())
}
}
fun keys(tableName: String): List<Constraint> {
val first = constraints().filter { c ->
c.type == Constraint.TYPE.PRIMARY_KEY.name &&
......
......@@ -2,10 +2,10 @@ package unibz.cs.semint.kprime.domain.ddl
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement
import unibz.cs.semint.kprime.domain.ddl.Column
@JacksonXmlRootElement(localName = "source")
class Source () {
@JacksonXmlProperty(isAttribute = true)
var name: String =""
@JacksonXmlProperty(isAttribute = true)
......
......@@ -6,6 +6,7 @@ import unibz.cs.semint.kprime.domain.ddl.Column
@JacksonXmlRootElement(localName = "target")
class Target () {
@JacksonXmlProperty(isAttribute = true)
var name: String =""
@JacksonXmlProperty(isAttribute = true)
......
......@@ -2,10 +2,7 @@ package unibz.cs.semint.kprime.domain.ddl.schemalgo
import unibz.cs.semint.kprime.domain.ddl.Column
import unibz.cs.semint.kprime.domain.ddl.Schema
import unibz.cs.semint.kprime.domain.ddl.SchemaCmdParser
import unibz.cs.semint.kprime.domain.dml.AlterTable
import unibz.cs.semint.kprime.domain.dml.ChangeSet
import unibz.cs.semint.kprime.domain.dml.CreateTable
// FIXME in realtà non dovrebbe fermarsi al primo livello ma proseguire in modo ricorsivo e cominciare dalla coda.
// FIXME in realtà dovrebbe rimuovere tutti i constraint prima di operare le sostituzioni di colonna chiave per poi rimetterli.
......@@ -26,10 +23,12 @@ 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(",")
sqlCommands.add("CREATE TABLE SKEY$originTableName AS SELECT $sid,$keyCols FROM $originTableName")
schema.addTable("SKEY$originTableName:$sid,$keyCols")
val surrogateTableName = "SKEY$originTableName"
sqlCommands.add("CREATE TABLE $surrogateTableName AS SELECT $sid,$keyCols FROM $originTableName")
schema.addTable("$surrogateTableName:$sid,$keyCols")
//TODO changeSet.createTable.add(CreateTable() name "SKEY$originTableName" withCols Column.set("$sid,$keyCols"))
schema.copyConstraintsFromTableToTable(originTableName,surrogateTableName)
// search ftables with foreign keys od double-inc as pk
val rTables = schema.referencedTablesOf(originTableName)
println("==============REFERENCED:")
......@@ -41,11 +40,11 @@ fun oid(schema: Schema, originTableName: String): ChangeSet {
val notKeyCols = rTableColsToKeep.map { notKeyColName -> "${rTable.name}.$notKeyColName" }.joinToString(",")
// crea una nuova tabella con chiave surrogata + attributi non chiave
val rTableNewName = "${rTable.name}_1"
val newTableCommand = "CREATE TABLE ${rTableNewName} AS SELECT SKEY$originTableName.$sid,$notKeyCols FROM SKEY$originTableName JOIN ${rTable.name} ON SKEY$originTableName.${originTableKey.first()} = ${rTable.name}.${originTableKey.first()}"
schema.addTable("${rTableNewName}:SKEY$originTableName.$sid,$notKeyCols")
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.constraintsFromTableToTable(rTable.name,rTableNewName)
schema.moveConstraintsFromTableToTable(rTable.name,rTableNewName)
println(newTableCommand)
sqlCommands.add(newTableCommand)
// add constraint to new table (from memory)
......@@ -54,7 +53,7 @@ fun oid(schema: Schema, originTableName: String): ChangeSet {
// crea una double-inc tra tabella-chiave e tabella origine.
val index = (schema.constraints?.size?: 0) + changeSet.size()
schema.addDoubleInc("SKEY$originTableName:$sid<->${originTableName}:$sid")
schema.addDoubleInc("$surrogateTableName:$sid<->${originTableName}:$sid")
//TODO changeSet plus SchemaCmdParser.parseDoubleInclusion(index,"SKEY$originTableName:$sid<->${originTableName}:$sid")
// var ftables = schema.foreignsTable(originTableName)
......
......@@ -444,7 +444,7 @@ class SchemaTest {
schema.addTable("employee_1:name")
assertEquals(1,schema.constraintsByTable("employee").size)
// when
schema.constraintsFromTableToTable("employee","employee_1")
schema.moveConstraintsFromTableToTable("employee","employee_1")
// then
assertEquals(0,schema.constraintsByTable("employee").size)
assertEquals(1,schema.constraintsByTable("employee_1").size)
......
......@@ -5,6 +5,7 @@ import org.junit.Test
import unibz.cs.semint.kprime.domain.ddl.Schema
import unibz.cs.semint.kprime.domain.ddl.schemalgo.oid
import kotlin.test.assertEquals
class OidsTest {
@Test
......@@ -43,7 +44,6 @@ table8: DepName , DepAddress
table7_table1.INCLUSION13 INCLUSION SSN -> SSN
table7_table8.DOUBLE_INCLUSION14 DOUBLE_INCLUSION DepName -> DepName
*/
@Ignore
// TODO oid fun using changeset
fun test_oid_two_tables() {
// given
......@@ -67,24 +67,19 @@ table8: DepName , DepAddress
val sqlCommands = changeset.sqlCommands!!
// then
assertEquals(5,sqlCommands.size)
assertEquals(4,changeset.size())
assertEquals(3,changeset.createTable.size)
assertEquals("Table(name='SKEYtable3', id='', view='', condition='', parent=null, columns=[sidtable3, ssn], labels=null, catalog=null, schema=null, source=null)",changeset.createTable[0].toString())
assertEquals("Table(name='table1_1', id='', view='', condition='', parent=null, columns=[phone, sidtable3], labels=null, catalog=null, schema=null, source=null)",changeset.createTable[1].toString())
assertEquals("Table(name='table7_1', id='', view='', condition='', parent=null, columns=[sidtable3, depname], labels=null, catalog=null, schema=null, source=null)",changeset.createTable[2].toString())
assertEquals(1,changeset.createConstraint.size)
assertEquals("DOUBLE_INCLUSION SKEYtable3:sidtable3 --> table3:sidtable3 ; ",changeset.createConstraint[0].toString())
assertEquals(0,changeset.size())
val changeset2 = oid(schema, "table8")
val sqlCommands2 = changeset2.sqlCommands!!
// then
assertEquals(3,sqlCommands2.size)
assertEquals(2,changeset2.size())
assertEquals(1,changeset2.createTable.size)
assertEquals("Table(name='SKEYtable8', id='', view='', condition='', parent=null, columns=[sidtable8, depname], labels=null, catalog=null, schema=null, source=null)",changeset2.createTable[0].toString())
//assertEquals("Table(name='table7_1_1', id='', view='', condition='', parent=null, columns=[sidtable8, sidtable3], labels=null, catalog=null, schema=null, source=null)",changeset2.createTable[1].toString())
assertEquals(1,changeset2.createConstraint.size)
assertEquals("DOUBLE_INCLUSION SKEYtable8:sidtable8 --> table8:sidtable8 ; ",changeset2.createConstraint[0].toString())
assertEquals(4,sqlCommands2.size)
assertEquals(0,changeset2.size())
val constraintsTable3 = schema.constraintsByTable("SKEYtable3")
assertEquals(4, constraintsTable3.size)
assertEquals("PRIMARY_KEY SKEYtable3:ssn --> SKEYtable3:ssn ; ",constraintsTable3[0].toString())
assertEquals("DOUBLE_INCLUSION table1_1:ssn --> SKEYtable3:ssn ; ",constraintsTable3[1].toString())
assertEquals("INCLUSION table7_1_1:ssn --> SKEYtable3:ssn ; ",constraintsTable3[2].toString())
assertEquals("DOUBLE_INCLUSION SKEYtable3:sidtable3 --> table3:sidtable3 ; ",constraintsTable3[3].toString())
}
......
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