Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary parentheses for functions #1642

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.exposed.sql.vendors

import org.jetbrains.exposed.exceptions.throwUnsupportedException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.Function
import org.jetbrains.exposed.sql.transactions.TransactionManager
import java.nio.ByteBuffer
import java.util.*
Expand Down Expand Up @@ -109,6 +110,7 @@ abstract class DataTypeProvider {
/** Returns the SQL representation of the specified expression, for it to be used as a column default value. */
open fun processForDefaultValue(e: Expression<*>): String = when {
e is LiteralOp<*> -> "$e"
e is Function<*> -> "$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 @@ -359,4 +359,23 @@ class DefaultsTest : DatabaseTestsBase() {
assertEquals(1, count)
}
}

@Test
fun testConsistentSchemeWithFunctionAsDefaultExpression() {
val foo = object : IntIdTable("foo") {
val name = text("name")
val defaultDateTime = datetime("defaultDateTime").defaultExpression(CurrentDateTime)
}

withDb {
try {
SchemaUtils.create(foo)

val actual = SchemaUtils.statementsRequiredToActualizeScheme(foo)
assertTrue(actual.isEmpty())
} finally {
SchemaUtils.drop(foo)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@file:OptIn(ExperimentalTime::class)

package org.jetbrains.exposed.sql.kotlin.datetime

import kotlinx.datetime.*
Expand All @@ -24,6 +25,7 @@ import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.h2Mode
import org.junit.Test
import org.junit.runners.model.MultipleFailureException
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
Expand Down Expand Up @@ -230,6 +232,7 @@ class DefaultsTest : DatabaseTestsBase() {
fun Expression<*>.itOrNull() = when {
currentDialectTest.isAllowedAsColumnDefault(this) ->
"DEFAULT ${currentDialectTest.dataTypeProvider.processForDefaultValue(this)} NOT NULL"

else -> "NULL"
}

Expand Down Expand Up @@ -352,9 +355,9 @@ class DefaultsTest : DatabaseTestsBase() {

withTables(foo) {
val d2020 = LocalDate(2020, 1, 1)
val dt2020 = d2020.atTime(0,0,0)
val dt2020m1w = d2020.minus(DateTimeUnit.WEEK).atTime(0,0,0)
val dt2020p1w = d2020.plus(DateTimeUnit.WEEK).atTime(0,0,0)
val dt2020 = d2020.atTime(0, 0, 0)
val dt2020m1w = d2020.minus(DateTimeUnit.WEEK).atTime(0, 0, 0)
val dt2020p1w = d2020.plus(DateTimeUnit.WEEK).atTime(0, 0, 0)

foo.insert { it[dt] = LocalDateTime(2019, 1, 1, 1, 1) }
foo.insert { it[dt] = dt2020 }
Expand All @@ -363,4 +366,23 @@ class DefaultsTest : DatabaseTestsBase() {
assertEquals(1, count)
}
}

@Test
fun testConsistentSchemeWithFunctionAsDefaultExpression() {
val foo = object : IntIdTable("foo") {
val name = text("name")
val defaultDateTime = datetime("defaultDateTime").defaultExpression(CurrentDateTime)
}

withDb {
try {
SchemaUtils.create(foo)

val actual = SchemaUtils.statementsRequiredToActualizeScheme(foo)
assertTrue(actual.isEmpty())
} finally {
SchemaUtils.drop(foo)
}
}
}
}