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

Support for using custom types as tables ids #2154

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -234,6 +234,9 @@ class EntityIDColumnType<T : Comparable<T>>(

override fun readObject(rs: ResultSet, index: Int): Any? = idColumn.columnType.readObject(rs, index)

override fun setParameter(stmt: PreparedStatementApi, index: Int, value: Any?) =
idColumn.columnType.setParameter(stmt, index, value)

override fun equals(other: Any?): Boolean {
if (this === other) return true

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.jetbrains.exposed.sql.tests.shared.entities

import org.jetbrains.exposed.dao.Entity
import org.jetbrains.exposed.dao.EntityClass
import org.jetbrains.exposed.dao.UUIDEntity
import org.jetbrains.exposed.dao.UUIDEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.dao.with
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.entities.UUIDTables.Cities
import org.jetbrains.exposed.sql.tests.shared.entities.UUIDTables.City.Companion.referrersOn
import org.jetbrains.exposed.sql.tests.shared.entities.UUIDTables.Town
import org.jetbrains.exposed.sql.tests.shared.entities.UUIDTables.Towns
import org.junit.Test
import java.sql.ResultSet
import java.util.*
import kotlin.test.assertNotNull

data class CityId(
val value: String
) : Comparable<CityId> {
override fun toString() = value
override fun compareTo(other: CityId) = value.compareTo(other.value)
}

fun Table.cityId(name: String): Column<CityId> = registerColumn(name, CityIdColumnType())

class CityIdColumnType : ColumnType<CityId>() {
private val varcharColumnType = VarCharColumnType(10)

override fun sqlType() = varcharColumnType.sqlType()

override fun valueFromDB(value: Any): CityId = varcharColumnType.valueFromDB(value).let { CityId(it) }

override fun nonNullValueToString(value: CityId) = varcharColumnType.nonNullValueToString(value.value)

override fun setParameter(stmt: PreparedStatementApi, index: Int, value: Any?): Unit =
varcharColumnType.setParameter(stmt, index, value?.toString())

override fun readObject(rs: ResultSet, index: Int) = varcharColumnType.readObject(rs, index)
}


@Suppress("MemberNameEqualsClassName")
object CustomIdTables {
object Cities : IdTable<CityId>() {
override val id = cityId("id").entityId()
override val primaryKey = PrimaryKey(id)

val name = varchar("name", 50)
}

class City(id: EntityID<CityId>) : Entity<CityId>(id) {
companion object : EntityClass<CityId, City>(Cities)

var name by Cities.name
}
}

class CustomIdTableEntityTest : DatabaseTestsBase() {
@Test
fun `create tables`() {
withTables(CustomIdTables.Cities) {
assertEquals(true, CustomIdTables.Cities.exists())
}
}

@Test
fun `create records`() {
withTables(CustomIdTables.Cities) {
CustomIdTables.City.new(CityId("M")) { name = "Mumbai" }

val allCities = CustomIdTables.City.all().map { it.name }
assertEquals(true, allCities.contains<String>("Mumbai"))
}
}

@Test
fun `find by id`() {
withTables(CustomIdTables.Cities) {
CustomIdTables.City.new(CityId("M")) { name = "Mumbai" }

val city = CustomIdTables.City.findById(CityId("M"))

assertNotNull(city)
assertEquals("Mumbai", city.name)
}
}

}
Loading