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 27, 2023
1 parent 934f48b commit b7e97f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,18 @@ open class Transaction(
}

fun identity(table: Table): String =
(table as? Alias<*>)?.let { "${identity(it.delegate)} ${db.identifierManager.quoteIfNecessary(it.alias)}" }
?: db.identifierManager.quoteIfNecessary(table.tableName.inProperCase())
(table as? Alias<*>)?.let { "${identity(it.delegate)} ${db.identifierManager.quoteTokenIfNecessary(it.alias)}" }
?: db.identifierManager.quoteTokenIfNecessary(table.tableName.inProperCase())

fun fullIdentity(column: Column<*>): String = QueryBuilder(false).also {
fullIdentity(column, it)
}.toString()

internal fun fullIdentity(column: Column<*>, queryBuilder: QueryBuilder) = queryBuilder {
if (column.table is Alias<*>) {
append(db.identifierManager.quoteIfNecessary(column.table.alias))
append(db.identifierManager.quoteTokenIfNecessary(column.table.alias))
} else {
append(db.identifierManager.quoteIfNecessary(column.table.tableName.inProperCase()))
append(db.identifierManager.quoteTokenIfNecessary(column.table.tableName.inProperCase()))
}
append('.')
append(identity(column))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ abstract class IdentifierManagerApi {

fun cutIfNecessaryAndQuote(identity: String) = quoteIfNecessary(identity.take(identifierLengthLimit))

private fun quoteTokenIfNecessary(token: String): String = if (needQuotes(token)) quote(token) else token
internal fun quoteTokenIfNecessary(token: String): String = if (needQuotes(token)) quote(token) else token

private fun quote(identity: String) = quotedIdentifiersCache.getOrPut(identity) { "$quoteString$identity$quoteString".trim() }
}
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,17 @@ class CreateTableTests : DatabaseTestsBase() {
}
}
}

@Test
fun `create table with dot in name`() {
val tester = object : IntIdTable("SomeNamespace.SomeTable") {
val text_col = text("text_col")
}

withTables(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 }
}
}
}

0 comments on commit b7e97f6

Please sign in to comment.