diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt index 51bfd17ff5..6cbeda99f6 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt @@ -70,7 +70,9 @@ open class InsertStatement( if (firstAutoIncColumn != null || returnedColumns.isNotEmpty()) { while (rs?.next() == true) { try { - val returnedValues = returnedColumns.associateTo(mutableMapOf()) { it.first to rs.getObject(it.second) } + val returnedValues = returnedColumns.associateTo(mutableMapOf()) { + it.first to it.first.columnType.readObject(rs, it.second) + } if (returnedValues.isEmpty() && firstAutoIncColumn != null) { returnedValues[firstAutoIncColumn] = rs.getObject(1) } diff --git a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt index 0f65656c4d..94765248b6 100644 --- a/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt +++ b/exposed-java-time/src/test/kotlin/org/jetbrains/exposed/DefaultsTest.kt @@ -1,5 +1,7 @@ package org.jetbrains.exposed +import org.jetbrains.exposed.dao.Entity +import org.jetbrains.exposed.dao.EntityClass import org.jetbrains.exposed.dao.IntEntity import org.jetbrains.exposed.dao.IntEntityClass import org.jetbrains.exposed.dao.flushCache @@ -32,6 +34,10 @@ import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertTrue + +private val dbTimestampNow: CustomFunction + get() = object : CustomFunction("now", JavaOffsetDateTimeColumnType()) {} + class DefaultsTest : DatabaseTestsBase() { object TableWithDBDefault : IntIdTable() { var cIndex = 0 @@ -555,4 +561,38 @@ class DefaultsTest : DatabaseTestsBase() { assertEquals(0, statements.size) } } + + object DefaultTimestampTable : IntIdTable("test_table") { + val timestamp: Column = + timestampWithTimeZone("timestamp").defaultExpression(dbTimestampNow) + } + + class DefaultTimestampEntity(id: EntityID) : Entity(id) { + companion object : EntityClass(DefaultTimestampTable) + + var timestamp: OffsetDateTime by DefaultTimestampTable.timestamp + } + + @Test + fun testCustomDefaultTimestampFunctionWithEntity() { + withTables(excludeSettings = TestDB.ALL - TestDB.ALL_POSTGRES - TestDB.MYSQL_V8 - TestDB.ALL_H2, DefaultTimestampTable) { + val entity = DefaultTimestampEntity.new {} + + val timestamp = DefaultTimestampTable.selectAll().first()[DefaultTimestampTable.timestamp] + + assertEquals(timestamp, entity.timestamp) + } + } + + @Test + fun testCustomDefaultTimestampFunctionWithInsertStatement() { + // Only Postgres allows to get timestamp values directly from the insert statement due to implicit 'returning *' + withTables(excludeSettings = TestDB.ALL - TestDB.ALL_POSTGRES, DefaultTimestampTable) { + val entity = DefaultTimestampTable.insert { } + + val timestamp = DefaultTimestampTable.selectAll().first()[DefaultTimestampTable.timestamp] + + assertEquals(timestamp, entity[DefaultTimestampTable.timestamp]) + } + } }