Commit d2388b07 authored by npedot's avatar npedot
Browse files

adds info for cadidate keys and binary relations

parent 336eae6a
...@@ -122,9 +122,17 @@ class Schema () { ...@@ -122,9 +122,17 @@ class Schema () {
}.firstOrNull() }.firstOrNull()
} }
fun keyPrimary(tableName: String): Constraint? {
return constraints().filter { c ->
c.type == Constraint.TYPE.PRIMARY_KEY.name &&
c.source.table == "${tableName}"
}.firstOrNull()
}
fun keysAll(tableName: String): List<Constraint> { fun keysAll(tableName: String): List<Constraint> {
val first = constraints().filter { c -> val first = constraints().filter { c ->
(c.type == Constraint.TYPE.PRIMARY_KEY.name || (c.type == Constraint.TYPE.PRIMARY_KEY.name ||
c.type == Constraint.TYPE.CANDIDATE_KEY.name||
c.type == Constraint.TYPE.SURROGATE_KEY.name) && c.type == Constraint.TYPE.SURROGATE_KEY.name) &&
c.source.table == "${tableName}" c.source.table == "${tableName}"
}.toList() }.toList()
...@@ -148,6 +156,7 @@ class Schema () { ...@@ -148,6 +156,7 @@ class Schema () {
} }
fun addKey(tableName:String, k:Set<Column>): Constraint { fun addKey(tableName:String, k:Set<Column>): Constraint {
keyPrimary(tableName).apply { this?.type = Constraint.TYPE.CANDIDATE_KEY.name }
val primaryConstraint = buildKey(tableName, k, Constraint.TYPE.PRIMARY_KEY.name) val primaryConstraint = buildKey(tableName, k, Constraint.TYPE.PRIMARY_KEY.name)
constraints().add(primaryConstraint) constraints().add(primaryConstraint)
return primaryConstraint return primaryConstraint
...@@ -156,7 +165,7 @@ class Schema () { ...@@ -156,7 +165,7 @@ class Schema () {
fun addKey(commandArgs:String):Schema { fun addKey(commandArgs:String):Schema {
val tableName:String = commandArgs.split(":")[0] val tableName:String = commandArgs.split(":")[0]
val attributeNames = commandArgs.split(":")[1] val attributeNames = commandArgs.split(":")[1]
constraints().add(buildKey(tableName, Column.set(attributeNames), Constraint.TYPE.PRIMARY_KEY.name)) addKey(tableName,Column.set(attributeNames))
return this return this
} }
...@@ -169,7 +178,7 @@ class Schema () { ...@@ -169,7 +178,7 @@ class Schema () {
fun buildKey(tableName: String, k: Set<Column>, keyType: String): Constraint { fun buildKey(tableName: String, k: Set<Column>, keyType: String): Constraint {
val keyConstraint = Constraint.addKey() val keyConstraint = Constraint.addKey()
keyConstraint.name = "pkey_$tableName" keyConstraint.name = "pkey_$tableName"+"_"+k.joinToString("_")
keyConstraint.source.table = tableName keyConstraint.source.table = tableName
keyConstraint.target.table = tableName keyConstraint.target.table = tableName
keyConstraint.source.columns.addAll(k) keyConstraint.source.columns.addAll(k)
...@@ -189,6 +198,7 @@ class Schema () { ...@@ -189,6 +198,7 @@ class Schema () {
fun keysAll(): List<Constraint> { fun keysAll(): List<Constraint> {
return constraints().filter { return constraints().filter {
c -> c.type.equals(Constraint.TYPE.PRIMARY_KEY.name) c -> c.type.equals(Constraint.TYPE.PRIMARY_KEY.name)
|| c.type.equals(Constraint.TYPE.CANDIDATE_KEY.name)
|| c.type.equals(Constraint.TYPE.SURROGATE_KEY.name) } || c.type.equals(Constraint.TYPE.SURROGATE_KEY.name) }
} }
...@@ -526,4 +536,9 @@ class Schema () { ...@@ -526,4 +536,9 @@ class Schema () {
return oid(this,tableName) return oid(this,tableName)
} }
fun isBinaryRelation(tableName:String):Boolean {
val table = table(tableName)?:return false
return table.columns.size==2
&& foreignsWithTarget(tableName).size==2
}
} }
...@@ -239,7 +239,7 @@ class SchemaTest { ...@@ -239,7 +239,7 @@ class SchemaTest {
schema.addForeignKey("person:id-->employee:id") schema.addForeignKey("person:id-->employee:id")
assertEquals(2,schema.constraints().size) assertEquals(2,schema.constraints().size)
// when // when
schema.dropConstraint("pkey_person") schema.dropConstraint("pkey_person_id")
schema.dropConstraint("person_employee.foreignKey1") schema.dropConstraint("person_employee.foreignKey1")
// then // then
assertEquals(0,schema.constraints().size) assertEquals(0,schema.constraints().size)
...@@ -464,4 +464,15 @@ class SchemaTest { ...@@ -464,4 +464,15 @@ class SchemaTest {
assertEquals("surname",key.first().name) assertEquals("surname",key.first().name)
assertEquals(1,key.size) assertEquals(1,key.size)
} }
@Test
fun test_is_binary_relation() {
// given
val schema = Schema()
schema.addTable("Person:name,surname")
schema.addForeignKey("X:a-->Person:name")
schema.addForeignKey("Y:b-->Person:surname")
// then
assertTrue(schema.isBinaryRelation("Person"))
}
} }
...@@ -88,7 +88,7 @@ table8: DepName , DepAddress ...@@ -88,7 +88,7 @@ table8: DepName , DepAddress
assertEquals(3, constraintsSsnPhone1.size) 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_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("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("pkey_ssn_phone_1_sidssn_name: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 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") val constraintsSsnDepname11 = schema.constraintsByTable("ssn_depname_1_1")
...@@ -100,7 +100,7 @@ table8: DepName , DepAddress ...@@ -100,7 +100,7 @@ table8: DepName , DepAddress
assertEquals("ssn_depname_ssn_name.inclusion1_1:INCLUSION ssn_depname_1_1:ssn --> SKEYssn_name:ssn ; ",constraintsSsnDepname11[2].toStringWithName())//TO FIX 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("pkey_ssn_depname_1_1_sidssn_depaddress: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 assertEquals("ssn_depaddress_ssn_depname.doubleInc2_1:DOUBLE_INCLUSION SKEYssn_depaddress:depname --> ssn_depname_1_1:depname ; ",constraintsSsnDepname11[4].toStringWithName())//TO FIX
} }
......
...@@ -197,7 +197,7 @@ class ApplyChangeSetUseCaseTest { ...@@ -197,7 +197,7 @@ class ApplyChangeSetUseCaseTest {
val cs_xml = serializer.prettyChangeSet(personCS) val cs_xml = serializer.prettyChangeSet(personCS)
assertEquals(""" assertEquals("""
<changeSet id=""> <changeSet id="">
<createConstraint name="pkey_person" id="" type="PRIMARY_KEY"> <createConstraint name="pkey_person_surname_name" id="" type="PRIMARY_KEY">
<source name="" id="" table="person"> <source name="" id="" table="person">
<columns> <columns>
<columns name="surname" id="" nullable="false" dbtype="" dbtable=""/> <columns name="surname" id="" nullable="false" dbtype="" dbtable=""/>
...@@ -232,7 +232,7 @@ class ApplyChangeSetUseCaseTest { ...@@ -232,7 +232,7 @@ class ApplyChangeSetUseCaseTest {
</tables> </tables>
</tables> </tables>
<constraints> <constraints>
<constraints name="pkey_person" id="" type="PRIMARY_KEY"> <constraints name="pkey_person_surname_name" id="" type="PRIMARY_KEY">
<source name="" id="" table="person"> <source name="" id="" table="person">
<columns> <columns>
<columns name="surname" id="" nullable="false" dbtype="" dbtable=""/> <columns name="surname" id="" nullable="false" dbtype="" dbtable=""/>
......
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