Commit ded2a996 authored by npedot's avatar npedot
Browse files

adds schema methods for demo1

parent 3f558465
......@@ -66,6 +66,14 @@ class Schema () {
return keys[0].source.columns.toSet()
}
fun notkey(tableName: String): Set<Column> {
val keyCols = key(tableName)
val table = table(tableName) ?: return emptySet()
val cols = table.columns.toMutableSet()
cols.removeAll(keyCols)
return cols
}
fun addKey(tableName:String, k:Set<Column>): Constraint {
val primaryConstraint = buildKey(tableName, k)
constraints().add(primaryConstraint)
......@@ -98,28 +106,56 @@ class Schema () {
return constraints().filter { c -> c.type.equals(Constraint.TYPE.FOREIGN_KEY.name) }
}
fun inclusions(): List<Constraint> {
return constraints().filter { c -> c.type.equals(Constraint.TYPE.INCLUSION.name) }
}
fun foreignsWithSource(tableName: String): List<Constraint> {
return foreignKeys().filter { f -> f.source.table.equals(tableName) }
}
fun referencedTablesOf(tableName: String): List<Table> {
fun foreignsWithTarget(tableName: String): List<Constraint> {
return foreignKeys().filter { f -> f.target.table.equals(tableName) }
}
fun inclusionsWithSource(tableName: String): List<Constraint> {
return inclusions().filter { f -> f.source.table.equals(tableName) }
}
fun inclusionsWithTarget(tableName: String): List<Constraint> {
return inclusions().filter { f -> f.target.table.equals(tableName) }
}
fun referencedTablesOf(tableName: String): Set<Table> {
var rTables = foreignTablesOf(tableName)
val diTables = doubleIncTablesOf(tableName)
val iTables = inclusionTablesOf(tableName)
println(diTables)
rTables.addAll(diTables)
return rTables
rTables.addAll(iTables)
return rTables.toSet()
}
private fun foreignTablesOf(tableName: String): ArrayList<Table> {
var rTables = ArrayList<Table>()
var foreignConstr = foreignsWithSource(tableName)
var foreignConstr = foreignsWithTarget(tableName)
for (foreign in foreignConstr) {
var t = table(foreign.target.table)
var t = table(foreign.source.table)
if (t != null) rTables.add(t)
}
return rTables
}
private fun inclusionTablesOf(tableName: String): ArrayList<Table> {
var iTables = ArrayList<Table>()
var inclusionConstraints = inclusionsWithTarget(tableName)
for (inclusion in inclusionConstraints) {
var t = table(inclusion.source.table)
if (t != null) iTables.add(t)
}
return iTables
}
private fun doubleIncTablesOf(tableName: String): ArrayList<Table> {
var rTables = ArrayList<Table>()
val doubleTargets = doubleIncs().filter { di -> di.source.table.equals(tableName)}
......@@ -140,10 +176,6 @@ class Schema () {
return rTables
}
fun foreignsWithTarget(tableName: String): List<Constraint> {
return foreignKeys().filter { f -> f.target.table.equals(tableName) }
}
fun doubleIncs(): List<Constraint> {
return constraints().filter { c -> c.type.equals(Constraint.TYPE.DOUBLE_INCLUSION.name) }
}
......
......@@ -36,6 +36,17 @@ object SchemaCmdParser {
return constraintsToAdd
}
// table1:a-->table2:b
fun parseForeignKey(index: Int, constraintExpression: String): Constraint {
val split = constraintExpression.split("-->")
val tableSourceString = split[0]
val tableTargetString = split[1]
val tableSource = parseTable(tableSourceString)
val tableTarget = parseTable(tableTargetString)
val type = Constraint.TYPE.FOREIGN_KEY.name
return buildConstraint(tableSource, tableTarget, type, index)
}
// table1:a-->table2:b
fun parseInclusion(index: Int, constraintExpression: String): Constraint {
val split = constraintExpression.split("-->")
......
......@@ -7,16 +7,32 @@ fun oid(schema: Schema, originTableName: String): List<String> {
// given a origin-table with primary key (pk)
val originTable = schema.table(originTableName)
val originTableKey = schema.key(originTableName)
val sid = "sid$originTableName"
// adds one column autoincrement
sqlCommands.add("ALTER TABLE $originTableName ADD COLUMN sid int NOT NULL auto_increment UNIQUE")
sqlCommands.add("ALTER TABLE $originTableName 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(",")
sqlCommands.add("CREATE TABLE SKEY$originTableName AS SELECT sid,$keyCols FROM $originTableName")
sqlCommands.add("CREATE TABLE SKEY$originTableName AS SELECT $sid,$keyCols FROM $originTableName")
// search ftables with foreign keys od double-inc as pk
// 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.
// FIXME dovrebbe anche mantenere aggiorato lo schema logico.
val rTables = schema.referencedTablesOf(originTableName)
println("==============REFERENCED:")
rTables.forEach{t -> println(t.name)}
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(",")
val newTableCommand = "CREATE TABLE ${rTable.name}_1 AS SELECT SKEY$originTableName.$sid,$notKeyCols FROM SKEY$originTableName JOIN ${rTable.name} ON SKEY$originTableName.${originTableKey.first()} = ${rTable.name}.${originTableKey.first()}"
println(newTableCommand)
sqlCommands.add(newTableCommand)
// add constraint to new table (from memory)
}
println("________________________")
// var ftables = schema.foreignsTable(originTableName)
// for each ftable adds one column oid with join with corresponding to fkey values
// replace pk origin-table
......
......@@ -420,4 +420,17 @@ class SchemaTest {
assertTrue(lostBCNFConstraints.equals(Constraint.set("A,B --> C")))
}
@Test
fun test_not_key_col_finder() {
// given
val schema = Schema()
schema.addTable("person:name,surname,age,address")
schema.addKey("person:name,surname")
// when
val notkey = schema.notkey("person")
// then
assertEquals(2,notkey.size)
assertTrue(notkey.contains(Column.of("age")))
assertTrue(notkey.contains(Column.of("address")))
}
}
......@@ -4,7 +4,6 @@ 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
......@@ -19,8 +18,49 @@ class OidsTest {
// when
val sqlCommands = oid(schema, originTableName)
// then
assertEquals("ALTER TABLE person ADD COLUMN sid int NOT NULL auto_increment UNIQUE",sqlCommands[0])
assertEquals("CREATE TABLE SKEYperson AS SELECT sid,surname,name FROM person",sqlCommands[1])
assertEquals("ALTER TABLE person DROP COLUMN surname,name",sqlCommands[2])
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])
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])
}
@Test
/*
Tables:
table1: SSN , Phone
table3: SSN , Name
table7: SSN , DepName
table8: DepName , DepAddress
pkey_table1 PRIMARY_KEY SSN , Phone -> SSN , Phone
pkey_table3 PRIMARY_KEY SSN -> SSN
table1_table3.DOUBLE_INCLUSION7 DOUBLE_INCLUSION SSN -> SSN
pkey_table7 PRIMARY_KEY SSN -> SSN
pkey_table8 PRIMARY_KEY DepName -> DepName
table7_table3.INCLUSION12 INCLUSION SSN -> SSN
table7_table1.INCLUSION13 INCLUSION SSN -> SSN
table7_table8.DOUBLE_INCLUSION14 DOUBLE_INCLUSION DepName -> DepName
*/
fun test_oid_two_tables() {
// given
val schema = Schema()
// table1: SSN , Phone
schema.addTable("table1:ssn,phone")
// table3: SSN , Name ->double-inc table1
schema.addTable("table3:ssn,name")
schema.addKey("table3:ssn")
schema.addDoubleInc("table1:ssn<->table3: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.addDoubleInc("table8:depname<->table7:depname")
// when
val sqlCommands = oid(schema,"table3")
// then
assertEquals(5,sqlCommands.size)
}
}
\ No newline at end of file
}
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