All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dbobjekts.codegen.ValidateForeignKeyConstraints.kt Maven / Gradle / Ivy

There is a newer version: 0.6.0-RC2
Show newest version
package com.dbobjekts.codegen

import com.dbobjekts.api.SchemaName
import com.dbobjekts.api.TableName
import com.dbobjekts.codegen.metadata.DBCatalogDefinition

object ValidateForeignKeyConstraints {
    operator fun invoke(catalogDefinition: DBCatalogDefinition): Boolean {
        val excluded: Set> = getExcluded(catalogDefinition)
        val included = catalogDefinition.schemas.flatMap { it.tables }
        return included.all {
            if (it.foreignKeys().isEmpty()) true else it.foreignKeys()
                .all { !excluded.contains(Pair(it.parentSchema, it.parentTable)) }
        }
    }

    fun reportMissing(catalogDefinition: DBCatalogDefinition): List> {
        val excluded: Set> = getExcluded(catalogDefinition)
        val included = catalogDefinition.schemas.flatMap { it.tables }
        val fks = included.flatMap { it.foreignKeys() }
        return fks.filter {
            excluded.contains(Pair(it.parentSchema, it.parentTable))
        }.map {
            Pair(
                it.parentSchema.value + "." + it.tableName.value + "." + it.columnName.value,
                it.parentSchema.value + "." + it.parentTable + "." + it.parentColumn.value
            )
        }
    }

    private fun getExcluded(catalogDefinition: DBCatalogDefinition): Set> =
        catalogDefinition.schemas
            .flatMap { it.excludedTables }
            .map { Pair(it.schema, it.tableName) }
            .toSet()

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy