Commit 44c3bc23 authored by npedot's avatar npedot
Browse files

adds sqlize liquibase drop columns, drop tables

parent e3aa00a9
......@@ -4,6 +4,8 @@ import liquibase.sqlgenerator.SqlGenerator
import liquibase.sqlgenerator.SqlGeneratorChain
import liquibase.statement.SqlStatement
import unibz.cs.semint.kprime.adapter.service.liquibase.sqlizeAddColumn
import unibz.cs.semint.kprime.adapter.service.liquibase.sqlizeAddTable
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.ChangeSet
import unibz.cs.semint.kprime.usecase.service.SQLizeServiceI
import java.util.*
......@@ -12,8 +14,11 @@ class MockSqlGeneratorChain: SqlGeneratorChain<SqlStatement>(TreeSet<SqlGenerato
class LiquibaseSQLizeAdapter: SQLizeServiceI {
override fun sqlize(change: ChangeSet): List<String> {
return change.createColumn.flatMap { sqlizeAddColumn(it) }
override fun sqlize(dbTrademark: DatabaseTrademark, change: ChangeSet): List<String> {
val result = mutableListOf<String>()
result.addAll(change.createColumn.flatMap { sqlizeAddColumn(dbTrademark, it) })
result.addAll(change.createTable.flatMap { sqlizeAddTable(dbTrademark, it) })
return result
}
}
\ No newline at end of file
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 unibz.cs.semint.kprime.domain.ddl.DataType
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
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.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.ddl.DataType
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.CreateColumn
fun sqlizeAddColumn(createColumns: CreateColumn): List<String> {
val statements = createColumns.columns.map {
AddColumnStatement(
fun sqlizeAddColumn(dbTrademark: DatabaseTrademark, createColumns: CreateColumn): List<String> {
val statements = createColumns.columns.map { AddColumnStatement(
createColumns.catalog,
createColumns.schema,
it.dbtable,
it.dbname,
it.dbtype,
liquibaseDataType(DataType.fromString(it.dbtype)).toString(),
null,
NotNullConstraint()
)
......@@ -22,7 +22,7 @@ fun sqlizeAddColumn(createColumns: CreateColumn): List<String> {
val addStatements = AddColumnStatement(statements)
val generateSql = AddColumnGenerator().generateSql(
addStatements,
H2Database(),
liquibaseDbByTrademark(dbTrademark),
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
......@@ -15,7 +9,12 @@ 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))) }
createTable.columns.map {
createTableStatement.addColumn(
it.dbname,
liquibaseDataType(DataType.fromString(it.dbtype))
)
}
return CreateTableGenerator().generateSql(
createTableStatement,
liquibaseDbByTrademark(dbTrademark),
......@@ -23,24 +22,3 @@ fun sqlizeAddTable(dbTrademark: DatabaseTrademark, createTable: CreateTable): Li
.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.adapter.service.liquibase
import liquibase.pro.packaged.it
import liquibase.sqlgenerator.core.DropColumnGenerator
import liquibase.statement.NotNullConstraint
import liquibase.statement.core.AddColumnStatement
import liquibase.statement.core.DropColumnStatement
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.DropColumn
fun sqlizeDropColumn(dbTrademark: DatabaseTrademark, dropColumns: List<DropColumn>): List<String> {
val statements = dropColumns.map { DropColumnStatement(
it.catalog,
it.schema,
it.tableName,
it.name
)}
val dropStatements = DropColumnStatement(statements)
val generateSql = DropColumnGenerator().generateSql(
dropStatements,
liquibaseDbByTrademark(dbTrademark),
MockSqlGeneratorChain())
return generateSql.map { it.toSql() }
}
package unibz.cs.semint.kprime.adapter.service.liquibase
import liquibase.sqlgenerator.core.DropTableGenerator
import liquibase.statement.core.DropTableStatement
import unibz.cs.semint.kprime.adapter.service.MockSqlGeneratorChain
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.DropTable
fun sqlizeDropTable(dbTrademark: DatabaseTrademark, dropTables: List<DropTable>): List<String> {
val dropStatements = dropTables.map { DropTableStatement(
it.catalog,
it.schemaName,
it.tableName,
it.cascadeConstraints?:false
)
}
val sqls =
dropStatements.flatMap {
DropTableGenerator().generateSql(
it,
liquibaseDbByTrademark(dbTrademark),
MockSqlGeneratorChain()).toList()
}
return sqls.map { it.toSql() }
}
......@@ -5,6 +5,13 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty
class DropColumn {
@JacksonXmlProperty(isAttribute = true)
var catalog: String? = null
@JacksonXmlProperty(isAttribute = true)
var schema: String? = null
@JacksonXmlProperty(isAttribute = true)
var tableName :String = ""
......
......@@ -7,10 +7,18 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText
@JacksonXmlRootElement(localName = "droptable")
class DropTable() {
@JacksonXmlProperty(isAttribute = true)
var cascadeConstraints: Boolean? = null
@JacksonXmlProperty(isAttribute = true)
var catalog: String? = null
@JacksonXmlProperty(isAttribute = true)
var path: String = ""
@JacksonXmlProperty(isAttribute = true)
var schemaName: String = ""
@JacksonXmlProperty(isAttribute = true)
var tableName: String = ""
......
package unibz.cs.semint.kprime.usecase.service
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.ChangeSet
interface SQLizeServiceI {
fun sqlize(change: ChangeSet): List<String>
fun sqlize(dbTrademark: DatabaseTrademark,change: ChangeSet): List<String>
}
\ No newline at end of file
......@@ -3,33 +3,50 @@ package unibz.cs.semint.kprime.adapter
import org.junit.Test
import unibz.cs.semint.kprime.adapter.service.LiquibaseSQLizeAdapter
import unibz.cs.semint.kprime.domain.ddl.Column
import unibz.cs.semint.kprime.domain.ddl.DataType
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.ChangeSet
import unibz.cs.semint.kprime.domain.dml.CreateColumn
import unibz.cs.semint.kprime.domain.dml.CreateTable
import kotlin.test.assertEquals
class LiquibaseSQLizeAdapterTest {
@Test
fun test_add_column_change() {
fun test_add_column_and_table_change_liquibase_sqlize() {
// given
val change = ChangeSet()
val createColumn = CreateColumn()
createColumn.catalog = "cat1"
createColumn.schema = "schema1"
val col1 = Column("colName","colId")
col1.dbname = "colDbName"
col1.dbtype = "colDbType"
col1.dbtype = DataType.varchar.name
col1.dbtable = "tableDbName"
createColumn.columns.add(col1)
change.createColumn.add(createColumn)
val createTable = CreateTable()
createTable.catalog = "cat1"
createTable.schema = "schema1"
createTable.name = "tab1"
val col11 = Column("colName11","colId11")
col11.dbname = "colDbName11"
col11.dbtype = DataType.int.name
col11.dbtable = "tableDbName11"
createTable.columns.add(col11)
change.createTable.add(createTable)
// when
val sqlize = LiquibaseSQLizeAdapter().sqlize(change)
val sqlize = LiquibaseSQLizeAdapter().sqlize(DatabaseTrademark.H2, change)
// then
val sqlizeresult = sqlize.joinToString(System.lineSeparator())
assertEquals("""
ALTER TABLE schema1.tableDbName ADD colDbName COLDBTYPE NOT NULL
ALTER TABLE schema1.tableDbName ADD colDbName NVARCHAR NOT NULL
CREATE TABLE schema1.tab1 (colDbName11 INT)
""".trimIndent(),sqlizeresult)
}
}
\ 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.sqlizeAddColumn
import unibz.cs.semint.kprime.domain.ddl.Column
import unibz.cs.semint.kprime.domain.ddl.DataType
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.ChangeSet
import unibz.cs.semint.kprime.domain.dml.CreateColumn
import kotlin.test.assertEquals
class SqlizeAddColumnTest {
@Test
fun test_add_column_liquibase_sqlize() {
// given
val createColumn = CreateColumn()
createColumn.catalog = "cat1"
createColumn.schema = "schema1"
val col1 = Column("colName","colId")
col1.dbname = "colDbName"
col1.dbtype = DataType.varchar.name
col1.dbtable = "tableDbName"
createColumn.columns.add(col1)
// when
val sqlize = sqlizeAddColumn(DatabaseTrademark.H2, createColumn)
// then
val sqlizeresult = sqlize.joinToString(System.lineSeparator())
assertEquals("""
ALTER TABLE schema1.tableDbName ADD colDbName NVARCHAR NOT NULL
""".trimIndent(),sqlizeresult)
}
}
\ 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.sqlizeDropColumn
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.DropColumn
import kotlin.test.assertEquals
class SqlizeDropColumnTest {
@Test
fun test_drop_column_liquibase_sqlize() {
// given
val dropColumn1 = DropColumn()
dropColumn1.catalog = "cat1"
dropColumn1.schema = "schema1"
dropColumn1.name = "colDbName"
dropColumn1.tableName = "tableDbName"
val dropColumn2 = DropColumn()
dropColumn2.catalog = "cat1"
dropColumn2.schema = "schema1"
dropColumn2.name = "colDbName2"
dropColumn2.tableName = "tableDbName"
// when
val sqlize = sqlizeDropColumn(DatabaseTrademark.H2, listOf(dropColumn1,dropColumn2))
// then
val sqlizeresult = sqlize.joinToString(System.lineSeparator())
assertEquals("""
ALTER TABLE schema1.tableDbName DROP COLUMN colDbName
ALTER TABLE schema1.tableDbName DROP COLUMN colDbName2
""".trimIndent(),sqlizeresult)
}
}
\ 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.sqlizeDropColumn
import unibz.cs.semint.kprime.adapter.service.liquibase.sqlizeDropTable
import unibz.cs.semint.kprime.domain.ddl.DatabaseTrademark
import unibz.cs.semint.kprime.domain.dml.DropColumn
import unibz.cs.semint.kprime.domain.dml.DropTable
import kotlin.test.assertEquals
class SqlizeDropTableTest {
@Test
fun test_drop_table_liquibase_sqlize() {
// given
val dropTable1 = DropTable()
dropTable1.catalog = "cat1"
dropTable1.schemaName = "schema1"
dropTable1.cascadeConstraints = true
dropTable1.tableName = "tableDbName"
val dropTable2 = DropTable()
dropTable2.catalog = "cat1"
dropTable2.schemaName = "schema1"
dropTable2.tableName = "tableDbName"
// when
val sqlize = sqlizeDropTable(DatabaseTrademark.H2, listOf(dropTable1,dropTable2))
// then
val sqlizeresult = sqlize.joinToString(System.lineSeparator())
assertEquals("""
DROP TABLE schema1.tableDbName CASCADE
DROP TABLE schema1.tableDbName
""".trimIndent(),sqlizeresult)
}
}
\ 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