Commit 162d9fb1 authored by npedot's avatar npedot
Browse files

adds column name with dbtable x.y

parent ded2a996
......@@ -27,6 +27,8 @@ class Column (): Labelled {
var labels: String? = null
@JacksonXmlProperty(isAttribute = true)
var default: Any? = null
@JacksonXmlProperty(isAttribute = true)
var dbtable: String? = null
private var labeller = Labeller()
......@@ -57,11 +59,24 @@ class Column (): Labelled {
fun of(name:String):Column {
val c = Column()
c.name = name.trim()
val (colname,tableName) = splitName(name)
println(colname)
println(tableName)
c.name = colname
c.dbtable = tableName
return c
}
private fun splitName(name:String):Pair<String,String> {
val tokens = name.trim().split(".")
println(tokens)
return if (tokens.size==1) {
Pair(tokens[0],"")
} else {
Pair(tokens[1],tokens[0])
}
}
}
override fun resetLabels(labelsAsString: String): String {
labels = labeller.resetLabels(labelsAsString)
......
......@@ -46,9 +46,22 @@ class Schema () {
return constraints().filter { c -> c.id==id }.firstOrNull()
}
fun constraintByTable(name: String): Constraint? {
if (constraints().isEmpty()) return null
return constraints().filter { c -> c.source.table==name || c.target.table==name }.firstOrNull()
fun constraintsByTable(name: String): List<Constraint> {
if (constraints().isEmpty()) return emptyList()
return constraints().filter { c -> c.source.table==name || c.target.table==name }
}
fun constraintsFromTableToTable(sourceTableName: String, targetTableName:String) {
val sourceTableConstraints = constraintsByTable(sourceTableName)
for (constr in sourceTableConstraints) {
if (constr.source.table==sourceTableName) {
constr.source.table=targetTableName
}
if (constr.target.table==sourceTableName) {
constr.target.table=targetTableName
}
println("MOVED CONSTRAINT FROM $sourceTableName TO:"+constr.toString())
}
}
fun keys(tableName: String): List<Constraint> {
......@@ -421,7 +434,7 @@ class Schema () {
*/
fun oidForTable(tableName:String):List<String> {
return oid(this,tableName)
return oid(this,tableName).sqlCommands?: emptyList()
}
}
......@@ -71,8 +71,8 @@ class Table (): Labelled {
}
infix fun withColumn(name: String ) = apply {
val col = Column()
col.name = name
val col = Column.of(name)
//col.name = name
this.columns.add(col)
}
......
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.dml.AlterTable
import unibz.cs.semint.kprime.domain.dml.ChangeSet
import unibz.cs.semint.kprime.domain.dml.CreateTable
fun oid(schema: Schema, originTableName: String): List<String> {
fun oid(schema: Schema, originTableName: String): ChangeSet {
val changeSet = ChangeSet()
var sqlCommands = mutableListOf<String>()
// given a origin-table with primary key (pk)
val originTable = schema.table(originTableName)
......@@ -10,9 +15,13 @@ fun oid(schema: Schema, originTableName: String): List<String> {
val sid = "sid$originTableName"
// adds one column autoincrement
sqlCommands.add("ALTER TABLE $originTableName ADD COLUMN $sid int NOT NULL auto_increment UNIQUE")
schema.table(originTableName)!!.columns.add(Column.of(sid))
//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(",")
sqlCommands.add("CREATE TABLE SKEY$originTableName AS SELECT $sid,$keyCols FROM $originTableName")
schema.addTable("SKEY$originTableName:$sid,$keyCols")
//changeSet.createTable.add(CreateTable() name "SKEY$originTableName" withCols Column.set("$sid,$keyCols"))
// 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.
......@@ -25,7 +34,11 @@ fun oid(schema: Schema, originTableName: String): List<String> {
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()}"
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")
changeSet.createTable.add(CreateTable() name rTableNewName withCols Column.set("SKEY$originTableName.$sid,$notKeyCols"))
schema.constraintsFromTableToTable(rTable.name,rTableNewName)
println(newTableCommand)
sqlCommands.add(newTableCommand)
// add constraint to new table (from memory)
......@@ -39,6 +52,8 @@ fun oid(schema: Schema, originTableName: String): List<String> {
// 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)
// remove ex-pk columns from fk tables
return sqlCommands
changeSet.sqlCommands = sqlCommands
return changeSet
}
\ No newline at end of file
......@@ -128,7 +128,7 @@ class ApplyChangeSetUseCase(val serializer: SerializerServiceI) {
fun dropConstraint(db:Database, dropConstraint: DropConstraint):Database {
if (db.schema.constraints().isEmpty()) return db
if (dropConstraint.tableName.isNotEmpty()) {
db.schema.constraintByTable(dropConstraint.tableName)
db.schema.constraintsByTable(dropConstraint.tableName).firstOrNull()
.let { it -> if (it != null) db.schema.constraints().remove(it) }
} else {
db.schema.constraint(dropConstraint.constraintName)
......
......@@ -433,4 +433,21 @@ class SchemaTest {
assertTrue(notkey.contains(Column.of("age")))
assertTrue(notkey.contains(Column.of("address")))
}
@Test
fun test_move_constraints_from_table_to_table() {
// given
val schema = Schema()
schema.addTable("person:name")
schema.addTable("employee:name")
schema.addDoubleInc("person:name<->employee:name")
schema.addTable("employee_1:name")
assertEquals(1,schema.constraintsByTable("employee").size)
// when
schema.constraintsFromTableToTable("employee","employee_1")
// then
assertEquals(0,schema.constraintsByTable("employee").size)
assertEquals(1,schema.constraintsByTable("employee_1").size)
assertEquals("[DOUBLE_INCLUSION person:name --> employee_1:name ; ]",schema.constraintsByTable("employee_1").toString())
}
}
package unibz.cs.semint.kprime.domain.ddl
import org.junit.Test
import kotlin.test.assertEquals
class ColumnTest {
@Test
fun test_column_composed_name_split() {
// given
val col = Column.of("tab1.colA")
// then
assertEquals("tab1",col.dbtable)
assertEquals("colA",col.name)
}
@Test
fun test_column_simple_name_split() {
// given
val col = Column.of("colA")
// then
assertEquals("",col.dbtable)
assertEquals("colA",col.name)
}
}
\ No newline at end of file
......@@ -16,7 +16,7 @@ class OidsTest {
schema.addDoubleInc("person:name,surname<->teacher:name,surname")
val originTableName = "person"
// when
val sqlCommands = oid(schema, originTableName)
val sqlCommands = oid(schema, originTableName).sqlCommands!!
// then
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])
......@@ -58,7 +58,7 @@ table8: DepName , DepAddress
schema.addTable("table8:depname,depaddress")
schema.addDoubleInc("table8:depname<->table7:depname")
// when
val sqlCommands = oid(schema,"table3")
val sqlCommands = oid(schema,"table3").sqlCommands!!
// then
assertEquals(5,sqlCommands.size)
}
......
......@@ -106,15 +106,15 @@ class ApplyChangeSetUseCaseTest {
<tables>
<tables name="person1" id="" view="" condition="">
<columns>
<columns name="K" id="" dbname="" nullable="false" dbtype=""/>
<columns name="T" id="" dbname="" nullable="false" dbtype=""/>
<columns name="S" id="" dbname="" nullable="false" dbtype=""/>
<columns name="K" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="T" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="S" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</tables>
<tables name="person2" id="" view="" condition="">
<columns>
<columns name="T" id="" dbname="" nullable="false" dbtype=""/>
<columns name="S" id="" dbname="" nullable="false" dbtype=""/>
<columns name="T" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="S" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</tables>
</tables>
......@@ -200,14 +200,14 @@ class ApplyChangeSetUseCaseTest {
<createConstraint name="pkey_person" id="" type="PRIMARY_KEY">
<source name="" id="" table="person">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</source>
<target name="" id="" table="person">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</target>
</createConstraint>
......@@ -225,9 +225,9 @@ class ApplyChangeSetUseCaseTest {
<tables>
<tables name="person" id="t1" view="" condition="">
<columns>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="address" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="address" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</tables>
</tables>
......@@ -235,14 +235,14 @@ class ApplyChangeSetUseCaseTest {
<constraints name="pkey_person" id="" type="PRIMARY_KEY">
<source name="" id="" table="person">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</source>
<target name="" id="" table="person">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</target>
</constraints>
......@@ -281,14 +281,14 @@ class ApplyChangeSetUseCaseTest {
<createConstraint name="person_employee.doubleInc1" id="cdi1" type="DOUBLE_INCLUSION">
<source name="" id="" table="person">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</source>
<target name="" id="" table="employee">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</target>
</createConstraint>
......@@ -304,16 +304,16 @@ class ApplyChangeSetUseCaseTest {
<tables>
<tables name="person" id="t1" view="" condition="">
<columns>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="address" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="address" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</tables>
<tables name="employee" id="t2" view="" condition="">
<columns>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="salary" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="salary" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</tables>
</tables>
......@@ -321,14 +321,14 @@ class ApplyChangeSetUseCaseTest {
<constraints name="person_employee.doubleInc1" id="cdi1" type="DOUBLE_INCLUSION">
<source name="" id="" table="person">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</source>
<target name="" id="" table="employee">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
<columns name="surname" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype="" dbtable=""/>
</columns>
</target>
</constraints>
......
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