com.dbobjekts.codegen.TableBuilder.kt Maven / Gradle / Ivy
package com.dbobjekts.codegen
import com.dbobjekts.api.PackageName
import com.dbobjekts.api.SchemaName
import com.dbobjekts.api.TableName
import com.dbobjekts.codegen.datatypemapper.ColumnMappingProperties
import com.dbobjekts.codegen.datatypemapper.ColumnTypeResolver
import com.dbobjekts.codegen.metadata.*
import org.slf4j.LoggerFactory
/**
* Yields a DBTableDefinition
*/
internal class TableBuilder(
val packageName: PackageName,
val schemaName: SchemaName,
val tableName: TableName,
val foreignKeyManager: ForeignKeyManager,
val columnTypeResolver: ColumnTypeResolver
) {
private val log = LoggerFactory.getLogger(TableBuilder::class.java)
private var alias: String = tableName.value
private val columns: MutableList = mutableListOf()
fun withAlias(alias: String): TableBuilder {
this.alias = alias
return this
}
fun build(): DBTableDefinition =
DBTableDefinition(packageName, schemaName, tableName, alias, columns, foreignKeyManager.findLinkedTables(schemaName, tableName))
private fun getForeignKeyDefinition(columnMetaData: ColumnMetaData): DBForeignKeyDefinition? =
foreignKeyManager.findForeignKey(schemaName, tableName, columnMetaData.columnName)?.let {
val foreignKey = columnTypeResolver.getForeignKeyColumnForType(schemaName, tableName, columnMetaData)
DBForeignKeyDefinition(
schema = schemaName,
table = tableName,
columnName = it.col,
columnType = foreignKey,
jdbcType = columnMetaData.columnType,
parentSchema = it.parentSchema,
parentTable = it.parentTable,
parentColumn = it.parentColumn,
partOfCompositePrimaryKey = columnMetaData.isPrimaryKey,
comment = columnMetaData.remarks
)
}
private fun findPrimaryKey(props: ColumnMetaData, hasCompositePK: Boolean): DBColumnDefinition? {
val columnMappingProperties = ColumnMappingProperties.fromMetaData(schemaName, tableName, props)
if (!props.isPrimaryKey) {
return null
}
if (props.isAutoIncrement) {
return DBAutoGeneratedKeyDefinition(
schema = schemaName,
table = tableName,
columnName = props.columnName,
columnType = columnTypeResolver.mapAutoIncrementColumn(columnMappingProperties),
jdbcType = props.columnType,
comment = props.remarks
)
}
val sequence = columnTypeResolver.findSequence(columnMappingProperties)
return if (sequence != null) {
val defaultType = columnTypeResolver.getDefaultMapping(columnMappingProperties)
DBSequenceKeyDefinition(
schema = schemaName,
table = tableName,
name = props.columnName,
columnType = columnTypeResolver.determineSequenceColumn(defaultType),
jdbcType = props.columnType,
sequence = sequence,
comment = props.remarks
)
} else if (props.isPrimaryKey && !hasCompositePK) {
columnTypeResolver.createColumnDefinition(schemaName, tableName, true, hasCompositePK, props)
} else
null
}
fun withColumns(
schema: SchemaName,
table: TableName,
columnMetaData: List
): TableBuilder {
val tableHasCompositePK: Boolean = columnMetaData.filter { it.isPrimaryKey }.count() > 1
columnMetaData.forEach { props ->
findPrimaryKey(props, tableHasCompositePK)?.let { pk ->
log.debug("Adding primary key for ${pk.tableName}.${pk.columnName}")
columns += pk
} ?: getForeignKeyDefinition(props)?.let { fk ->
log.debug("Adding foreign key for ${fk.tableName}.${fk.columnName}")
columns += fk
} ?: columnTypeResolver.createColumnDefinition(schema, table, false, tableHasCompositePK, props).also {
columns += it
}
}
return this
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy