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
…1207) / Move test to a separate file
  • Loading branch information
Tapac committed Apr 18, 2021
1 parent 98bdca8 commit 30b8143
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
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 Down Expand Up @@ -303,49 +299,4 @@ 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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jetbrains.exposed.sqlserver

import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.notNullValue
import org.hamcrest.MatcherAssert.assertThat
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.`java-time`.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.junit.Test
import java.time.*

class SQLServerDefaultsTest : DatabaseTestsBase() {

@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 30b8143

Please sign in to comment.