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

com.dbobjekts.codegen.metadata.DBSchemaDefinition.kt Maven / Gradle / Ivy

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

import com.dbobjekts.api.PackageName
import com.dbobjekts.api.SchemaName
import com.dbobjekts.metadata.Schema

data class DBSchemaDefinition(
    override val packageName: PackageName,
    val schemaName: SchemaName,
    val tables: List,
    val excludedTables: List
) : DBObjectDefinition {
    override fun toString(): String = schemaName.value

    val asPackage: String = schemaName.asPackage()

    fun prettyPrint(): String =
        """
           |Schema ${packageName.toString()}.${schemaName.value} has ${tables.size} tables.
           |${tables.map { it.prettyPrint() }.joinToString("\n")}"""

    /**
     * Searches the current schema for a table by name.
     */
    fun findTable(table: String): DBTableDefinition? =
        tables.firstOrNull { it.tableName.value.equals(table, true) }

    fun diff(schema: Schema): List {
        val diffs = mutableListOf()
        if (tables.size != schema.tables.size) {
            val inDb = tables.map { it.tableName.value }.joinToString(",")
            val inCatalog = schema.tables.map { it.tableName.value }.joinToString(",")
            diffs += ("DB schema $schema has ${tables.size} tables ($inDb), but catalog has ${schema.tables.size} ($inCatalog)}")
        }
        diffs += tables.flatMap { dbTableDef: DBTableDefinition ->
            val match = schema.tables.find { it.tableName.value.equals(dbTableDef.tableName.value, true) }
            if (match == null) listOf("DB table ${dbTableDef.schema.value}.${dbTableDef.tableName.value} not found in catalog") else dbTableDef.diff(
                match
            )
        }
        return diffs
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy