Skip to content

Commit

Permalink
Allow the SQL Server default keyword as default value for a column (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayanm001 authored Apr 18, 2021
1 parent de1c027 commit f7b926b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ abstract class DataTypeProvider {
open fun processForDefaultValue(e: Expression<*>): String = when {
e is LiteralOp<*> -> "$e"
currentDialect is MysqlDialect -> "$e"
currentDialect is SQLServerDialect -> "$e"
else -> "($e)"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ open class SQLServerDialect : VendorDialect(dialectName, SQLServerDataTypeProvid
override val supportsSequenceAsGeneratedKeys: Boolean = false
override val supportsOnlyIdentifiersInGeneratedKeys: Boolean = true

override fun isAllowedAsColumnDefault(e: Expression<*>): Boolean = true
private val nonAcceptableDefaults = arrayOf("DEFAULT")

override fun isAllowedAsColumnDefault(e: Expression<*>): Boolean {
val columnDefault = e.toString().toUpperCase().trim()
return columnDefault !in nonAcceptableDefaults
}

override fun modifyColumn(column: Column<*>): String =
super.modifyColumn(column).replace("MODIFY COLUMN", "ALTER COLUMN")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.jetbrains.exposed

import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.notNullValue
import org.hamcrest.MatcherAssert.assertThat
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.flushCache
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.`java-time`.*
import org.jetbrains.exposed.sql.statements.BatchDataInconsistentException
Expand All @@ -17,12 +21,12 @@ import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections
import org.jetbrains.exposed.sql.tests.shared.assertEqualLists
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.expectException
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.junit.Test
import java.time.*
import java.util.UUID

class DefaultsTest : DatabaseTestsBase() {
object TableWithDBDefault : IntIdTable() {
Expand Down Expand Up @@ -294,4 +298,47 @@ class DefaultsTest : DatabaseTestsBase() {
assertEquals(1, count)
}
}

@Test
fun testDefaultExpressionsForTemporalTable() {

fun databaseGeneratedTimestamp() = object : ExpressionWithColumnType<LocalDateTime>() {
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { +"DEFAULT" }
override val columnType: IColumnType = JavaLocalDateTimeColumnType()
}

val temporalTable = object : UUIDTable("TemporalTable") {
val name = text("name")
val sysStart = datetime("sysStart").defaultExpression(databaseGeneratedTimestamp())
val sysEnd = datetime("sysEnd").defaultExpression(databaseGeneratedTimestamp())
}

withDb(TestDB.SQLSERVER) {
try {
exec("""
CREATE TABLE TemporalTable
(
id uniqueidentifier PRIMARY KEY,
"name" VARCHAR(100) NOT NULL,
sysStart DATETIME2 GENERATED ALWAYS AS ROW START,
sysEnd DATETIME2 GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME ([sysStart], [sysEnd])
)
""".trimIndent())

val names = listOf("name")
val batchInsert: List<ResultRow> =
temporalTable.batchInsert(names, shouldReturnGeneratedValues = true) { name ->
this[temporalTable.name] = "name"
}
val id = batchInsert.first()[temporalTable.id]
val result = temporalTable.select { temporalTable.id eq id }.single()
assertThat(result[temporalTable.name], `is`("name"))
assertThat(result[temporalTable.sysStart], notNullValue())
assertThat(result[temporalTable.sysEnd], notNullValue())
} finally {
SchemaUtils.drop(temporalTable)
}
}
}
}

0 comments on commit f7b926b

Please sign in to comment.