Skip to content
Snippets Groups Projects
Commit 3f558465 authored by npedot's avatar npedot
Browse files

adds cs apply test on add double inc constraint

parent efb419d2
No related branches found
No related tags found
No related merge requests found
......@@ -99,17 +99,49 @@ class Schema () {
}
fun foreignsWithSource(tableName: String): List<Constraint> {
return foreignKeys().filter { f -> f.source.name.equals(tableName) }
return foreignKeys().filter { f -> f.source.table.equals(tableName) }
}
// fun referencesTable(tableName: String): List<Constraint> {
// var rTables = foreignKeys().filter { f -> f.source.name.equals(tableName) }.toMutableList()
// rTables.addAll()
// return rTables
// }
fun referencedTablesOf(tableName: String): List<Table> {
var rTables = foreignTablesOf(tableName)
val diTables = doubleIncTablesOf(tableName)
println(diTables)
rTables.addAll(diTables)
return rTables
}
private fun foreignTablesOf(tableName: String): ArrayList<Table> {
var rTables = ArrayList<Table>()
var foreignConstr = foreignsWithSource(tableName)
for (foreign in foreignConstr) {
var t = table(foreign.target.table)
if (t != null) rTables.add(t)
}
return rTables
}
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)
}
return rTables
}
fun foreignsWithTarget(tableName: String): List<Constraint> {
return foreignKeys().filter { f -> f.target.name.equals(tableName) }
return foreignKeys().filter { f -> f.target.table.equals(tableName) }
}
fun doubleIncs(): List<Constraint> {
......@@ -227,7 +259,6 @@ class Schema () {
return this
}
// FIXME Use SchemaCmdParser
fun addForeignKey(commandArgs:String):Schema {
val source:String = commandArgs.split("-->")[0]
val target:String = commandArgs.split("-->")[1]
......@@ -250,7 +281,6 @@ class Schema () {
return this
}
// FIXME Use SchemaCmdParser
fun addDoubleInc(commandArgs:String):Schema {
val source:String = commandArgs.split("<->")[0]
val target:String = commandArgs.split("<->")[1]
......@@ -261,16 +291,21 @@ class Schema () {
val targetTableName:String = target.split(":")[0]
val targetAttributeNames = target.split(":")[1]
val constraintPos = constraintsByType(Constraint.TYPE.DOUBLE_INCLUSION).size+1
val constraint = buildDoubleInc(sourceTableName, targetTableName, sourceAttributeNames, targetAttributeNames)
constraints().add(constraint)
return this
}
internal fun buildDoubleInc(sourceTableName: String, targetTableName: String, sourceAttributeNames: String, targetAttributeNames: String): Constraint {
val constraintPos = constraintsByType(Constraint.TYPE.DOUBLE_INCLUSION).size + 1
val constraint = Constraint.doubleInclusion {}
constraint.id="cdi$constraintPos"
constraint.id = "cdi$constraintPos"
constraint.name = "${sourceTableName}_${targetTableName}.doubleInc$constraintPos"
constraint.source.table=sourceTableName
constraint.target.table=targetTableName
constraint.source.table = sourceTableName
constraint.target.table = targetTableName
constraint.source.columns.addAll(Column.set(sourceAttributeNames))
constraint.target.columns.addAll(Column.set(targetAttributeNames))
constraints().add(constraint)
return this
return constraint
}
fun addInclusion(commandArgs:String):Schema {
......@@ -283,16 +318,21 @@ class Schema () {
val targetTableName:String = target.split(":")[0]
val targetAttributeNames = target.split(":")[1]
val constraintPos = constraintsByType(Constraint.TYPE.INCLUSION).size+1
val constraint = buildInclusion(sourceTableName, targetTableName, sourceAttributeNames, targetAttributeNames)
constraints().add(constraint)
return this
}
internal fun buildInclusion(sourceTableName: String, targetTableName: String, sourceAttributeNames: String, targetAttributeNames: String): Constraint {
val constraintPos = constraintsByType(Constraint.TYPE.INCLUSION).size + 1
val constraint = Constraint.inclusion {}
constraint.id="ci$constraintPos"
constraint.id = "ci$constraintPos"
constraint.name = "${sourceTableName}_${targetTableName}.inclusion$constraintPos"
constraint.source.table=sourceTableName
constraint.target.table=targetTableName
constraint.source.table = sourceTableName
constraint.target.table = targetTableName
constraint.source.columns.addAll(Column.set(sourceAttributeNames))
constraint.target.columns.addAll(Column.set(targetAttributeNames))
constraints().add(constraint)
return this
return constraint
}
fun decomposeBCNF(): Set<Relation> {
......
......@@ -13,6 +13,10 @@ fun oid(schema: Schema, originTableName: String): List<String> {
val keyCols = originTableKey.map { c -> c.name }.joinToString(",")
sqlCommands.add("CREATE TABLE SKEY$originTableName AS SELECT sid,$keyCols FROM $originTableName")
// search ftables with foreign keys od double-inc as pk
val rTables = schema.referencedTablesOf(originTableName)
println("==============REFERENCED:")
rTables.forEach{t -> println(t.name)}
println("________________________")
// var ftables = schema.foreignsTable(originTableName)
// for each ftable adds one column oid with join with corresponding to fkey values
// replace pk origin-table
......
......@@ -12,7 +12,9 @@ class OidsTest {
// given
val schema = Schema()
schema.addTable("person:name,surname,address")
schema.addTable("teacher:name,surname,course")
schema.addKey("person:name,surname")
schema.addDoubleInc("person:name,surname<->teacher:name,surname")
val originTableName = "person"
// when
val sqlCommands = oid(schema, originTableName)
......
......@@ -93,7 +93,7 @@ class ApplyChangeSetUseCaseTest {
fun test_apply_changeset_to_person_db() {
//given
val db = setUpPersonDb()
val changeset = setUpPersonChangeSet()
val changeset = setUpPersonChangeSetSplitTable()
val serializer = XMLSerializerJacksonAdapter()
// when
val newdb = ApplyChangeSetUseCase(serializer).apply(db, changeset)
......@@ -164,7 +164,7 @@ class ApplyChangeSetUseCaseTest {
}
private fun setUpPersonChangeSet(): ChangeSet {
private fun setUpPersonChangeSetSplitTable(): ChangeSet {
val dropPersonTable = DropTable() name "person"
val dropPrimaryKeyConstraint = DropConstraint() name "person.primaryKey"
val vsplitChangeSet = initChangeSet {} withId "234"
......@@ -177,7 +177,7 @@ class ApplyChangeSetUseCaseTest {
return vsplitChangeSet
}
private fun setUpPersonChangeSet2(): ChangeSet {
private fun setUpPersonChangeSetAddKey(): ChangeSet {
val changeSet = ChangeSet()
val schema = Schema()
val key = schema.buildKey("person",Column.set("name,surname"))
......@@ -191,7 +191,7 @@ class ApplyChangeSetUseCaseTest {
// given
val personDB = Database()
personDB.schema.addTable("person:name,surname,address")
val personCS = setUpPersonChangeSet2()
val personCS = setUpPersonChangeSetAddKey()
assertEquals(1, personCS.createConstraint.size)
val serializer = XMLSerializerJacksonAdapter()
val cs_xml = serializer.prettyChangeSet(personCS)
......@@ -254,4 +254,91 @@ class ApplyChangeSetUseCaseTest {
assertEquals(expectedDb,serializeNewDb)
}
private fun setUpPersonChangeSetAddDoubleInc(): ChangeSet {
val changeSet = ChangeSet()
val schema = Schema()
val doubleInc = schema.buildDoubleInc("person",
"employee","name,surname","name,surname")
changeSet plus doubleInc
return changeSet
}
// TODO Test Person with inclusion constraint changeset.
@Test
fun test_apply_changeset_to_person_double_inc() {
// given
val personDB = Database()
personDB.schema.addTable("person:name,surname,address")
personDB.schema.addTable("employee:name,surname,salary")
val personCS = setUpPersonChangeSetAddDoubleInc()
assertEquals(1, personCS.createConstraint.size)
val serializer = XMLSerializerJacksonAdapter()
val cs_xml = serializer.prettyChangeSet(personCS)
assertEquals("""<changeSet id="">
<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>
</source>
<target name="" id="" table="employee">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
</columns>
</target>
</createConstraint>
</changeSet>""",cs_xml)
// when
val newDB = ApplyChangeSetUseCase(serializer).apply(personDB, personCS)
//then
assertEquals(1,newDB.schema.constraints?.size)
// checks identity
val serializeNewDb = serializer.prettyDatabase(newDB)
val expectedDb ="""<database name="" id="" source="">
<schema name="" id="">
<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>
</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>
</tables>
</tables>
<constraints>
<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>
</source>
<target name="" id="" table="employee">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
</columns>
</target>
</constraints>
</constraints>
</schema>
<mappings/>
</database>"""
assertEquals(expectedDb,serializeNewDb)
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment