Commit a64cefa8 authored by npedot's avatar npedot
Browse files

fix schema constraint() and tables() if null condition

parent f02ef0ca
......@@ -12,9 +12,9 @@
<properties>
<main.class>unibz.cs.semint.kprime.Starter</main.class>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
<kotlin.version>1.3.30</kotlin.version>
<junit.version>4.12</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......
......@@ -172,10 +172,10 @@ class Constraint(): Labelled {
if (source.columns==null || source.columns.isEmpty()) return "no source columns"
if (target==null) return "no target"
if (target.columns==null || right().isEmpty()) return "no target columns"
var result = source.columns[0].toString()
var result = type +" " +source.table+":"+source.columns[0].toString()
for(col in source.columns.drop(1))
result += " , " + col.toString()
result +=" --> "
result += "," + col.toString()
result +=" --> " +target.table+":"
if (target.columns.size>0) {
result+= target.columns[0].toString()
for (col in target.columns.drop(1))
......
......@@ -21,8 +21,10 @@ class Schema () {
}
fun constraints(): MutableList<Constraint> {
if (constraints!=null) return constraints as MutableList<Constraint>
return ArrayList()
if (constraints==null) constraints = ArrayList()
return constraints as MutableList<Constraint>
// if (constraints!=null) return constraints as MutableList<Constraint>
// return ArrayList()
}
fun constraintsByType(type :Constraint.TYPE): List<Constraint> {
......@@ -32,8 +34,10 @@ class Schema () {
}
fun tables():ArrayList<Table> {
if (tables!=null) return tables as ArrayList<Table>
return ArrayList()
if (tables==null) tables = ArrayList()
return tables as ArrayList<Table>
// if (tables!=null) return tables as ArrayList<Table>
// return ArrayList()
}
fun constraint(name: String): Constraint? {
......@@ -72,10 +76,18 @@ class Schema () {
return primaryConstraint
}
fun addKey(commandArgs:String):Schema {
val tableName:String = commandArgs.split(":")[0]
val attributeNames = commandArgs.split(":")[1]
constraints().add(buildKey(tableName,Column.set(attributeNames)))
return this
}
fun buildKey(tableName: String, k: Set<Column>): Constraint {
val primaryConstraint = Constraint()
val primaryConstraint = Constraint.addKey {}
primaryConstraint.name = "pkey_$tableName"
primaryConstraint.source.table = "$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
......@@ -213,20 +225,6 @@ class Schema () {
return this
}
fun addKey(commandArgs:String):Schema {
val tableName:String = commandArgs.split(":")[0]
val attributeNames = commandArgs.split(":")[1]
val constraint = Constraint.addKey {}
constraint.id="ck_$tableName"
constraint.name = tableName+".primaryKey"
constraint.source.table=tableName
constraint.target.table=tableName
constraint.source.columns.addAll(Column.set(attributeNames))
constraint.target.columns.addAll(Column.set(attributeNames))
constraints().add(constraint)
return this
}
// FIXME Use SchemaCmdParser
fun addForeignKey(commandArgs:String):Schema {
val source:String = commandArgs.split("-->")[0]
......
......@@ -27,6 +27,9 @@ class ChangeSet() {
@JacksonXmlProperty(isAttribute = true)
var parent: String? = null
@JacksonXmlProperty(isAttribute = true)
var sqlCommands: List<String>? = null
@JacksonXmlElementWrapper(useWrapping=false)
var createView= ArrayList<CreateView>()
......@@ -77,6 +80,7 @@ class ChangeSet() {
}
infix fun plus(createConstraint: CreateConstraint)= apply{
println("plus $createConstraint")
this.createConstraint.add(createConstraint)
}
......
......@@ -112,7 +112,9 @@ class ApplyChangeSetUseCase(val serializer: SerializerServiceI) {
}
fun createConstraint(db:Database, createConstraint: CreateConstraint): Database {
println("apply create constraint ${createConstraint}")
db.schema.constraints().add(createConstraint)
println("apply constraint size ${db.schema.constraints().size}")
return db
}
......
......@@ -16,8 +16,8 @@ class SchemaTest {
val fd = Constraint.of("${time.name},${classroom.name}","${course.name}")
assertEquals(" Time , Classroom , Course"," $time , $classroom , $course")
assertEquals(" Time , Classroom --> Course ; "," $fd")
assertEquals("Time , Classroom --> Course ; ",fd.toString())
assertEquals(" :Time,Classroom --> :Course ; "," $fd")
assertEquals(" :Time,Classroom --> :Course ; ",fd.toString())
}
@Test
......@@ -68,7 +68,7 @@ class SchemaTest {
+ "C-->C;"
+ "C,D,E,F-->C,D,F")
val result= removeTrivial(constraints)
assertEquals("[A --> B ; ]",result.toString())
assertEquals("[ :A --> :B ; ]",result.toString())
}
@Test
......@@ -117,7 +117,7 @@ class SchemaTest {
var fds = Constraint.set("A-->B,C;B-->C;A-->B;A,B-->C")
fds = splitRight(fds)
val removed = removeUnnecessaryEntireFD(fds)
assertEquals("[B --> C ; , A --> B ; ]",removed.toString())
assertEquals("[ :A --> :B ; , :B --> :C ; ]",removed.toString())
}
@Test
......@@ -158,9 +158,10 @@ class SchemaTest {
fds = removeTrivial(fds)
// then
assertEquals(3,fds.size)
assertTrue(fds.contains(Constraint.of("A , B --> C")))
assertTrue(fds.contains(Constraint.of("B , C --> D , E")))
assertTrue(fds.contains(Constraint.of("A --> B, C")))
assertEquals("[ :B,C --> :E,D ; , :A --> :C,B ; , :A,B --> :C ; ]",fds.toString().trim())
assertTrue(fds.contains(Constraint.of("A,B --> C")))
assertTrue(fds.contains(Constraint.of("B,C --> E,D")))
assertTrue(fds.contains(Constraint.of("A --> C,B")))
}
@Test
......@@ -207,7 +208,6 @@ class SchemaTest {
assertEquals(1,schema.foreignKeys().size)
}
@Test
fun test_addDoubleInc() {
// given
......@@ -240,7 +240,7 @@ class SchemaTest {
schema.addForeignKey("person:id-->employee:id")
assertEquals(2,schema.constraints().size)
// when
schema.dropConstraint("person.primaryKey")
schema.dropConstraint("pkey_person")
schema.dropConstraint("person_employee.foreignKey1")
// then
assertEquals(0,schema.constraints().size)
......@@ -265,7 +265,7 @@ class SchemaTest {
// when
val check3NF = check3NF(attrs, fds)
// then
assertEquals("[C --> D ; ]",check3NF.toString())
assertEquals("[ :C --> :D ; ]",check3NF.toString())
assertEquals(1, check3NF.size)
}
......
......@@ -176,4 +176,82 @@ class ApplyChangeSetUseCaseTest {
vsplitChangeSet plus table1 plus table2 plus doubleInc plus person2Key
return vsplitChangeSet
}
private fun setUpPersonChangeSet2(): ChangeSet {
val changeSet = ChangeSet()
val schema = Schema()
val key = schema.buildKey("person",Column.set("name,surname"))
changeSet plus key
assertEquals(1,changeSet.createConstraint.size)
return changeSet
}
@Test
fun test_apply_changeset_to_person_db2() {
// given
val personDB = Database()
personDB.schema.addTable("person:name,surname,address")
val personCS = setUpPersonChangeSet2()
assertEquals(1, personCS.createConstraint.size)
val serializer = XMLSerializerJacksonAdapter()
val cs_xml = serializer.prettyChangeSet(personCS)
assertEquals("""
<changeSet id="">
<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>
</source>
<target name="" id="" table="person">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
</columns>
</target>
</createConstraint>
</changeSet>
""".trimIndent(),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>
<constraints>
<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>
</source>
<target name="" id="" table="person">
<columns>
<columns name="surname" id="" dbname="" nullable="false" dbtype=""/>
<columns name="name" id="" dbname="" nullable="false" dbtype=""/>
</columns>
</target>
</constraints>
</constraints>
</schema>
<mappings/>
</database>
""".trimIndent()
assertEquals(expectedDb,serializeNewDb)
}
}
\ 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