Commit e3aa00a9 authored by npedot's avatar npedot
Browse files

adds sqlize liquibase add table, first minimal edition

parent 7a795a04
compile:
mvn clean compile -Dorg.slf4j.simpleLogger.defaultLogLevel=warn
test:
mvn clean test -Dorg.slf4j.simpleLogger.defaultLogLevel=warn
install:
mvn clean install -Dorg.slf4j.simpleLogger.defaultLogLevel=warn
package unibz.cs.semint.kprime.adapter.service
import liquibase.database.core.H2Database
import liquibase.sqlgenerator.SqlGenerator
import liquibase.sqlgenerator.SqlGeneratorChain
import liquibase.sqlgenerator.core.AddColumnGenerator
import liquibase.statement.NotNullConstraint
import liquibase.statement.SqlStatement
import liquibase.statement.core.AddColumnStatement
import unibz.cs.semint.kprime.adapter.service.liquibase.sqlizeAddColumn
import unibz.cs.semint.kprime.domain.dml.ChangeSet
import unibz.cs.semint.kprime.domain.dml.CreateColumn
import unibz.cs.semint.kprime.usecase.service.SQLizeServiceI
import java.util.*
......@@ -20,23 +16,4 @@ class LiquibaseSQLizeAdapter: SQLizeServiceI {
return change.createColumn.flatMap { sqlizeAddColumn(it) }
}
fun sqlizeAddColumn(createColumns: CreateColumn): List<String> {
val statements = createColumns.columns.map {
AddColumnStatement(
createColumns.catalog,
createColumns.schema,
it.dbtable,
it.dbname,
it.dbtype,
null,
NotNullConstraint()
)
}
val addStatements = AddColumnStatement(statements)
val generateSql = AddColumnGenerator().generateSql(
addStatements,
H2Database(),
MockSqlGeneratorChain())
return generateSql.map { it.toSql() }
}
}
\ No newline at end of file
package unibz.cs.semint.kprime.adapter.service.liquibase
import liquibase.database.core.H2Database
import liquibase.sqlgenerator.core.AddColumnGenerator
import liquibase.statement.NotNullConstraint
import liquibase.statement.core.AddColumnStatement
import unibz.cs.semint.kprime.adapter.service.MockSqlGeneratorChain
import unibz.cs.semint.kprime.domain.dml.CreateColumn
fun sqlizeAddColumn(createColumns: CreateColumn): List<String> {
val statements = createColumns.columns.map {
AddColumnStatement(
createColumns.catalog,
createColumns.schema,
it.dbtable,
it.dbname,
it.dbtype,
null,
NotNullConstraint()
)
}
val addStatements = AddColumnStatement(statements)
val generateSql = AddColumnGenerator().generateSql(
addStatements,
H2Database(),
MockSqlGeneratorChain())
return generateSql.map { it.toSql() }
}
package unibz.cs.semint.kprime.adapter.service.liquibase
import liquibase.database.Database
import liquibase.database.core.H2Database
import liquibase.database.core.PostgresDatabase
import liquibase.database.core.UnsupportedDatabase
import liquibase.datatype.LiquibaseDataType
import liquibase.datatype.core.*
import liquibase.sqlgenerator.core.CreateTableGenerator
import liquibase.statement.core.CreateTableStatement
import unibz.cs.semint.kprime.adapter.service.MockSqlGeneratorChain
import unibz.cs.semint.kprime.domain.ddl.DataType
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.CreateTable
fun sqlizeAddTable(dbTrademark: DatabaseTrademark, createTable: CreateTable): List<String> {
val createTableStatement = CreateTableStatement(createTable.catalog,createTable.schema,createTable.name)
createTable.columns.map { createTableStatement.addColumn(it.dbname,liquibaseDataType(DataType.fromString(it.dbtype))) }
return CreateTableGenerator().generateSql(
createTableStatement,
liquibaseDbByTrademark(dbTrademark),
MockSqlGeneratorChain())
.map { it.toSql() }
}
fun liquibaseDataType(dbDataType: DataType): LiquibaseDataType? {
return when(dbDataType) {
DataType.int -> IntType()
DataType.varchar -> NVarcharType()
DataType.timestamp -> TimestampType()
DataType.date -> DateType()
DataType.time -> TimeType()
DataType.currency -> CurrencyType()
DataType.boolean -> BooleanType()
DataType.bigint -> BigIntType()
DataType.unknown -> UnknownType()
}
}
fun liquibaseDbByTrademark(dbTrademark: DatabaseTrademark): Database? {
return when(dbTrademark) {
DatabaseTrademark.PSQL -> PostgresDatabase()
DatabaseTrademark.H2 -> H2Database()
DatabaseTrademark.unknown -> UnsupportedDatabase()
}
}
\ No newline at end of file
package unibz.cs.semint.kprime.domain.ddl
enum class DataType {
int,timestamp,date,time,bigint,varchar,boolean,currency,unknown;
companion object {
fun fromString(value: String?): DataType {
return when (value?.toLowerCase()) {
"int" -> int
"time" -> time
"timestamp" -> timestamp
"date" -> date
"currency" -> currency
"boolean" -> boolean
"bigint" -> bigint
"varchar" -> varchar
else -> unknown
}
}
}
}
\ No newline at end of file
package unibz.cs.semint.kprime.domain.ddl
enum class DatabaseTrademark {
H2,PSQL,unknown
}
\ No newline at end of file
package unibz.cs.semint.kprime.adapter.liquibase
import org.junit.Test
import unibz.cs.semint.kprime.adapter.service.liquibase.sqlizeAddTable
import unibz.cs.semint.kprime.domain.ddl.Column
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.CreateTable
import kotlin.test.assertEquals
class SqlizeAddTableTest {
@Test
fun test_add_table_liquibase_sqlize() {
// given
val createTable = CreateTable()
createTable.name("tab1")
val col1 = Column("colName1", "colId1", "col1DbName")
col1.dbtype = "int"
col1.dbtable = createTable.name
createTable.columns.add(col1)
// when
val sqlizeAddTableLines = sqlizeAddTable(DatabaseTrademark.H2, createTable)
// then
assertEquals("""
CREATE TABLE tab1 (col1DbName INT)
""".trimIndent(),sqlizeAddTableLines.joinToString(System.lineSeparator()))
}
}
\ 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