Commit 5ef54f58 authored by npedot's avatar npedot
Browse files

changes for demo2 oid step

parent a7bf72cb
......@@ -58,8 +58,6 @@ class Column (): Labelled {
fun of(name:String):Column {
val c = Column()
val (colname,tableName) = splitName(name)
println(colname)
println(tableName)
c.name = colname
c.dbtable = tableName
return c
......@@ -68,7 +66,6 @@ class Column (): Labelled {
private fun splitName(name:String):Pair<String,String> {
val tokens = name.trim().split(".")
println(tokens)
return if (tokens.size==1) {
Pair(tokens[0],"")
} else {
......
......@@ -165,6 +165,9 @@ class Constraint(): Labelled {
return labels?: ""
}
fun toStringWithName(): String {
return "${this.name}:${toString()}"
}
override fun toString(): String {
if (source==null) return "no source"
if (source.columns==null || source.columns.isEmpty()) return "no source columns"
......
......@@ -61,12 +61,11 @@ class Schema () {
if (constr.target.table==sourceTableName) {
constr.target.table=targetTableName
}
println("MOVED CONSTRAINT FROM $sourceTableName TO:"+constr.toString())
println("MOVED CONSTRAINT FROM $sourceTableName TO ${constr.name}:"+constr.toString())
}
}
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) {
......@@ -78,10 +77,23 @@ class Schema () {
}
}
fun removeKeyConstraint(originTableName: String) {
val constraints = constraintsByTable(originTableName)
var toRemove : Constraint? = null
for (constr in constraints) {
if (constr.type == Constraint.TYPE.PRIMARY_KEY.name)
toRemove = constr
}
if (toRemove!=null) this.constraints?.remove(toRemove)
}
fun copyConstraintsFromTableToTable(sourceTableName: String, targetTableName:String) {
val sourceTableConstraints = constraintsByTable(sourceTableName)
var constrToRemove = mutableListOf<Constraint>()
for (constr in sourceTableConstraints) {
val copyconstr = constr.clone()
constrToRemove.add(constr)
copyconstr.name = copyconstr.name+"_1"
if (constr.source.table==sourceTableName) {
copyconstr.source.table=targetTableName
}
......@@ -92,6 +104,7 @@ class Schema () {
constraints?.add(copyconstr)
println("COPIED CONSTRAINT FROM $sourceTableName TO:"+constr.toString())
}
this.constraints?.removeAll(constrToRemove)
}
fun keys(tableName: String): List<Constraint> {
......@@ -102,7 +115,24 @@ class Schema () {
return first
}
fun key(tableName: String): Set<Column> {
fun keySurrogate(tableName: String): Constraint? {
return constraints().filter { c ->
c.type == Constraint.TYPE.SURROGATE_KEY.name &&
c.source.table == "${tableName}"
}.firstOrNull()
}
fun keysAll(tableName: String): List<Constraint> {
val first = constraints().filter { c ->
(c.type == Constraint.TYPE.PRIMARY_KEY.name ||
c.type == Constraint.TYPE.SURROGATE_KEY.name) &&
c.source.table == "${tableName}"
}.toList()
return first
}
fun keyCols(tableName: String): Set<Column> {
var resultCols = mutableSetOf<Column>()
val keys = keys(tableName)
if (keys.isEmpty()) return mutableSetOf()
......@@ -110,7 +140,7 @@ class Schema () {
}
fun notkey(tableName: String): Set<Column> {
val keyCols = key(tableName)
val keyCols = keyCols(tableName)
val table = table(tableName) ?: return emptySet()
val cols = table.columns.toMutableSet()
cols.removeAll(keyCols)
......@@ -118,7 +148,7 @@ class Schema () {
}
fun addKey(tableName:String, k:Set<Column>): Constraint {
val primaryConstraint = buildKey(tableName, k)
val primaryConstraint = buildKey(tableName, k, Constraint.TYPE.PRIMARY_KEY.name)
constraints().add(primaryConstraint)
return primaryConstraint
}
......@@ -126,25 +156,42 @@ class Schema () {
fun addKey(commandArgs:String):Schema {
val tableName:String = commandArgs.split(":")[0]
val attributeNames = commandArgs.split(":")[1]
constraints().add(buildKey(tableName,Column.set(attributeNames)))
constraints().add(buildKey(tableName, Column.set(attributeNames), Constraint.TYPE.PRIMARY_KEY.name))
return this
}
fun buildKey(tableName: String, k: Set<Column>): Constraint {
val primaryConstraint = Constraint.addKey {}
primaryConstraint.name = "pkey_$tableName"
primaryConstraint.source.table = tableName
primaryConstraint.target.table = tableName
primaryConstraint.source.columns.addAll(k)
primaryConstraint.target.columns.addAll(k)
primaryConstraint.type = Constraint.TYPE.PRIMARY_KEY.name
return primaryConstraint
fun addSurrogateKey(commandArgs:String):Schema {
val tableName:String = commandArgs.split(":")[0]
val attributeNames = commandArgs.split(":")[1]
constraints().add(buildKey(tableName, Column.set(attributeNames), Constraint.TYPE.SURROGATE_KEY.name))
return this
}
fun buildKey(tableName: String, k: Set<Column>, keyType: String): Constraint {
val keyConstraint = Constraint.addKey {}
keyConstraint.name = "pkey_$tableName"
keyConstraint.source.table = tableName
keyConstraint.target.table = tableName
keyConstraint.source.columns.addAll(k)
keyConstraint.target.columns.addAll(k)
keyConstraint.type = keyType
return keyConstraint
}
fun keys(): List<Constraint> {
return constraints().filter { c -> c.type.equals(Constraint.TYPE.PRIMARY_KEY.name) }
}
fun keysSurrogate(): List<Constraint> {
return constraints().filter { c -> c.type.equals(Constraint.TYPE.SURROGATE_KEY.name) }
}
fun keysAll(): List<Constraint> {
return constraints().filter {
c -> c.type.equals(Constraint.TYPE.PRIMARY_KEY.name)
|| c.type.equals(Constraint.TYPE.SURROGATE_KEY.name) }
}
fun foreignKeys(): List<Constraint> {
return constraints().filter { c -> c.type.equals(Constraint.TYPE.FOREIGN_KEY.name) }
}
......@@ -177,7 +224,6 @@ class Schema () {
var rTables = foreignTablesOf(tableName)
val diTables = doubleIncTablesOf(tableName)
val iTables = inclusionTablesOf(tableName)
println(diTables)
rTables.addAll(diTables)
rTables.addAll(iTables)
return rTables.toSet()
......@@ -206,17 +252,14 @@ class Schema () {
private fun doubleIncTablesOf(tableName: String): ArrayList<Table> {
var rTables = ArrayList<Table>()
val doubleTargets = doubleIncs().filter { di -> di.source.table.equals(tableName)}
println(doubleTargets)
for (double in doubleTargets) {
val name1 = double.target.table
println(" target $name1")
var t = table(name1)
if (t != null) rTables.add(t)
}
val doubleSources = doubleIncs().filter { di -> di.target.table.equals(tableName)}
for (double in doubleSources) {
val name1 = double.source.table
println(" source $name1")
var t = table(name1)
if (t != null) rTables.add(t)
}
......
......@@ -2,6 +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.Table
import unibz.cs.semint.kprime.domain.dml.ChangeSet
// FIXME in realtà non dovrebbe fermarsi al primo livello ma proseguire in modo ricorsivo e cominciare dalla coda.
......@@ -13,60 +14,65 @@ fun oid(schema: Schema, originTableName: String): ChangeSet {
// given a origin-table with primary key (pk)
val originTable = schema.table(originTableName)
val originTableKey = schema.key(originTableName)
val originTableKey = schema.keyCols(originTableName)
// adds one column autoincrement to origin table
val sid = "sid$originTableName"
sqlCommands.add("ALTER TABLE $originTableName ADD COLUMN $sid int NOT NULL auto_increment UNIQUE")
schema.table(originTableName)!!.columns.add(Column.of(sid))
schema.addSurrogateKey("$originTable:$sid")
//TODO changeSet.alterTable!!.add(AlterTable() onTable originTableName withStatement "ADD COLUMN $sid int NOT NULL auto_increment UNIQUE")
// 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")
schema.addSurrogateKey("$surrogateTableName:$sid")
//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)
for (rTable in rTables) {
// remove constraint from old table (and remember)
updateReferencedTables(schema, originTableName, originTable, surrogateTableName, sid, originTableKey, sqlCommands)
schema.copyConstraintsFromTableToTable(originTableName,surrogateTableName)
// crea una double-inc tra tabella-chiave e tabella origine.
//TODO val index = (schema.constraints?.size?: 0) + changeSet.size()
//TODO changeSet plus SchemaCmdParser.parseDoubleInclusion(index,"SKEY$originTableName:$sid<->${originTableName}:$sid")
schema.addDoubleInc("$surrogateTableName:$sid<->${originTableName}:$sid")
schema.moveConstraintsFromColsToCol(originTableName,keyCols,sid)
sqlCommands.add("ALTER TABLE $originTableName DROP COLUMN $keyCols")
schema.table(originTableName)!!.columns.removeAll(originTableKey)
//TODO changeset remove columns
changeSet.sqlCommands = sqlCommands
return changeSet
}
private fun updateReferencedTables(schema: Schema, originTableName: String, originTable: Table?, surrogateTableName: String, sid: String, originTableKey: Set<Column>, sqlCommands: MutableList<String>) {
for (rTable in schema.referencedTablesOf(originTableName)) {
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"
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()}"
// TODO changeSet.createTable.add(CreateTable() name rTableNewName withCols Column.set("SKEY$originTableName.$sid,$notKeyCols"))
schema.addTable("${rTableNewName}:$surrogateTableName.$sid,$notKeyCols")
//TODO changeSet.createTable.add(CreateTable() name rTableNewName withCols Column.set("SKEY$originTableName.$sid,$notKeyCols"))
schema.addSurrogateKey("$rTableNewName:$sid")
schema.moveConstraintsFromTableToTable(rTable.name,rTableNewName)
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)
}
schema.moveConstraintsFromTableToTable(rTable.name, rTableNewName)
// crea una double-inc tra tabella-chiave e tabella origine.
val index = (schema.constraints?.size?: 0) + changeSet.size()
schema.addDoubleInc("$surrogateTableName:$sid<->${originTableName}:$sid")
//TODO changeSet plus SchemaCmdParser.parseDoubleInclusion(index,"SKEY$originTableName:$sid<->${originTableName}:$sid")
val rTableKeys = schema.keyCols(rTableNewName).map { col -> col.name }.joinToString(",")
// println(schema.constraints)
// println(schema.constraintsByTable(rTableNewName))
// println(schema.keys(rTableNewName))
println("OID moveConstraintsFromColsToCol ${rTableNewName}:$rTableKeys to $rTableNewName:$sid")
schema.moveConstraintsFromColsToCol(rTableNewName, rTableKeys, sid)
schema.removeKeyConstraint(rTableNewName)
// var ftables = schema.foreignsTable(originTableName)
// for each ftable adds one column oid with join with corresponding to fkey values
// replace pk origin-table
// replace fks to new surrogate id column
// 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
sqlCommands.add(newTableCommand)
}
}
\ No newline at end of file
......@@ -18,7 +18,7 @@ class VSplitUseCase {
if (tableWithFunctionalName.isEmpty()) return changeSet
// compute K
val keyCols = metadataDatabase.schema.key(tableWithFunctionalName)
val keyCols = metadataDatabase.schema.keyCols(tableWithFunctionalName)
var key = keyCols.map { x -> x.name }.toSet()
println("key $key")
......
......@@ -460,7 +460,7 @@ class SchemaTest {
// when
schema.moveConstraintsFromColsToCol("Person","name","surname")
//then
val key = schema.key("Person")
val key = schema.keyCols("Person")
assertEquals("surname",key.first().name)
assertEquals(1,key.size)
}
......
package unibz.cs.semint.kprime.domain.schemalgo
import org.junit.Ignore
import org.junit.Test
import unibz.cs.semint.kprime.domain.ddl.Schema
import unibz.cs.semint.kprime.domain.ddl.schemalgo.oid
......@@ -46,42 +45,61 @@ table8: DepName , DepAddress
table7_table8.DOUBLE_INCLUSION14 DOUBLE_INCLUSION DepName -> DepName
*/
// TODO oid fun using changeset
fun test_oid_two_tables() {
fun test_two_oid_four_tables() {
// given
val schema = Schema()
// table1: SSN , Phone
schema.addTable("table1:ssn,phone")
schema.addTable("ssn_phone:ssn,phone")
schema.addKey("ssn_phone:ssn,phone")
// table3: SSN , Name ->double-inc table1
schema.addTable("table3:ssn,name")
schema.addKey("table3:ssn")
schema.addDoubleInc("table1:ssn<->table3:ssn")
schema.addTable("ssn_name:ssn,name")
schema.addKey("ssn_name:ssn")
schema.addDoubleInc("ssn_phone:ssn<->ssn_name:ssn")
// table7: SSN , DepName
schema.addTable("table7:ssn,depname")
schema.addInclusion("table7:ssn-->table3:ssn")
schema.addInclusion("table7:ssn-->table1:ssn")
// table8: DepName , DepAddress -> double-inc table7
schema.addTable("table8:depname,depaddress")
schema.addKey("table8:depname")
schema.addDoubleInc("table8:depname<->table7:depname")
schema.addTable("ssn_depname:ssn,depname")
schema.addInclusion("ssn_depname:ssn-->ssn_name:ssn")
schema.addInclusion("ssn_depname:ssn-->ssn_phone:ssn")
// table8: DepName , DepAddress -> double-inc ssn_depname
schema.addTable("ssn_depaddress:depname,depaddress")
schema.addKey("ssn_depaddress:depname")
schema.addDoubleInc("ssn_depaddress:depname<->ssn_depname:depname")
// when
val changeset = oid(schema, "table3")
val changeset = oid(schema, "ssn_name")
val sqlCommands = changeset.sqlCommands!!
// then
assertEquals(5,sqlCommands.size)
assertEquals(0,changeset.size())
val changeset2 = oid(schema, "table8")
val changeset2 = oid(schema, "ssn_depaddress")
val sqlCommands2 = changeset2.sqlCommands!!
// then
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())
assertEquals(
"[ssn_phone, ssn_name, ssn_depname, ssn_depaddress, SKEYssn_name, ssn_phone_1, ssn_depname_1, SKEYssn_depaddress, ssn_depname_1_1]",
schema.tables?.map { it.name }.toString())
val constraintsSsnPhone1 = schema.constraintsByTable("ssn_phone_1")
assertEquals(3, constraintsSsnPhone1.size)
// assertEquals("ssn_phone_ssn_name.doubleInc1:DOUBLE_INCLUSION ssn_phone_1:sidssn_name --> ssn_name:sidssn_name ; ",constraintsSsnPhone1[0].toStringWithName())
assertEquals("ssn_depname_ssn_phone.inclusion2:INCLUSION ssn_depname_1_1:sidssn_name --> ssn_phone_1:sidssn_name ; ",constraintsSsnPhone1[0].toStringWithName())
assertEquals("pkey_ssn_phone_1:SURROGATE_KEY ssn_phone_1:sidssn_name --> ssn_phone_1:sidssn_name ; ",constraintsSsnPhone1[1].toStringWithName())
assertEquals("ssn_phone_ssn_name.doubleInc1_1:DOUBLE_INCLUSION ssn_phone_1:sidssn_name --> SKEYssn_name:sidssn_name ; ",constraintsSsnPhone1[2].toStringWithName()) //TO FIX
val constraintsSsnDepname11 = schema.constraintsByTable("ssn_depname_1_1")
assertEquals(5, constraintsSsnDepname11.size)
// assertEquals("INCLUSION ssn_depname_1_1:sidssn_name --> ssn_name:sidssn_name ; ",constraintsSsnDepname11[0].toString())
assertEquals("INCLUSION ssn_depname_1_1:sidssn_name --> ssn_phone_1:sidssn_name ; ",constraintsSsnDepname11[0].toString())
// assertEquals("DOUBLE_INCLUSION ssn_depaddress:sidssn_depaddress --> ssn_depname_1_1:sidssn_depaddress ; ",constraintsSsnDepname11[1].toString())
assertEquals("SURROGATE_KEY ssn_depname_1_1:sidssn_name --> ssn_depname_1_1:sidssn_name ; ",constraintsSsnDepname11[1].toString())
assertEquals("ssn_depname_ssn_name.inclusion1_1:INCLUSION ssn_depname_1_1:ssn --> SKEYssn_name:ssn ; ",constraintsSsnDepname11[2].toStringWithName())//TO FIX
assertEquals("pkey_ssn_depname_1_1:SURROGATE_KEY ssn_depname_1_1:sidssn_depaddress --> ssn_depname_1_1:sidssn_depaddress ; ",constraintsSsnDepname11[3].toStringWithName())
assertEquals("ssn_depaddress_ssn_depname.doubleInc2_1:DOUBLE_INCLUSION SKEYssn_depaddress:depname --> ssn_depname_1_1:depname ; ",constraintsSsnDepname11[4].toStringWithName())//TO FIX
}
}
......
......@@ -180,7 +180,7 @@ class ApplyChangeSetUseCaseTest {
private fun setUpPersonChangeSetAddKey(): ChangeSet {
val changeSet = ChangeSet()
val schema = Schema()
val key = schema.buildKey("person",Column.set("name,surname"))
val key = schema.buildKey("person", Column.set("name,surname"), Constraint.TYPE.PRIMARY_KEY.name)
changeSet plus key
assertEquals(1,changeSet.createConstraint.size)
return changeSet
......
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