Skip to content

Commit

Permalink
fix: Incorrect SQL statements when creating a table with a dot in its…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
joc-a committed Sep 28, 2023
1 parent 934f48b commit 587510a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1192,8 +1192,10 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
private fun <T> Column<T>.cloneWithAutoInc(idSeqName: String?): Column<T> = when (columnType) {
is AutoIncColumnType -> this
is ColumnType -> {
val q = if (tableName.contains('.')) "\"" else ""
val fallbackSeqName = "$q${tableName.replace("\"", "")}_${name}_seq$q"
this.withColumnType(
AutoIncColumnType(columnType, idSeqName, "${tableName?.replace("\"", "")}_${name}_seq")
AutoIncColumnType(columnType, idSeqName, fallbackSeqName)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ abstract class IdentifierManagerApi {
}

fun quoteIfNecessary(identity: String): String {
return if (identity.contains('.')) {
return if (identity.contains('.') && !identity.isAlreadyQuoted()) {
identity.split('.').joinToString(".") { quoteTokenIfNecessary(it) }
} else {
quoteTokenIfNecessary(identity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
Expand Down Expand Up @@ -629,4 +630,26 @@ class CreateTableTests : DatabaseTestsBase() {
}
}
}

@Test
fun `create table with dot in name without creating schema beforehand`() {
withDb(excludeSettings = listOf(TestDB.ORACLE)) {
val q = db.identifierManager.quoteString
val tableName = "${q}SomeNamespace.SomeTable$q"

val tester = object : IntIdTable(tableName) {
val text_col = text("text_col")
}

try {
SchemaUtils.create(tester)

val id = tester.insertAndGetId { it[text_col] = "Inserted text" }
tester.update({ tester.id eq id }) { it[text_col] = "Updated text" }
tester.deleteWhere { tester.id eq id }
} finally {
SchemaUtils.drop(tester)
}
}
}
}

0 comments on commit 587510a

Please sign in to comment.