-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: EXPOSED-310 Add support for ULongIdTable and ULongEntity
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/ULongEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.jetbrains.exposed.dao | ||
|
||
import org.jetbrains.exposed.dao.id.EntityID | ||
import org.jetbrains.exposed.dao.id.IdTable | ||
|
||
/** Base class for an [Entity] instance identified by an [id] comprised of a wrapped `ULong` value. */ | ||
abstract class ULongEntity(id: EntityID<ULong>) : Entity<ULong>(id) | ||
|
||
/** | ||
* Base class representing the [EntityClass] that manages [ULongEntity] instances and | ||
* maintains their relation to the provided [table]. | ||
* | ||
* @param [table] The [IdTable] object that stores rows mapped to entities of this class. | ||
* @param [entityType] The expected [ULongEntity] type. This can be left `null` if it is the class of type | ||
* argument [E] provided to this [ULongEntityClass] instance. If this `ULongEntityClass` is defined as a companion | ||
* object of a custom `ULongEntity` class, the parameter will be set to this immediately enclosing class by default. | ||
* @sample org.jetbrains.exposed.sql.tests.shared.DDLTests.testDropTableFlushesCache | ||
* @param [entityCtor] The function invoked to instantiate a [ULongEntity] using a provided [EntityID] value. | ||
* If a reference to a specific constructor or a custom function is not passed as an argument, reflection will | ||
* be used to determine the primary constructor of the associated entity class on first access. If this `ULongEntityClass` | ||
* is defined as a companion object of a custom `ULongEntity` class, the constructor will be set to that of the | ||
* immediately enclosing class by default. | ||
* @sample org.jetbrains.exposed.sql.tests.shared.entities.EntityTests.testExplicitEntityConstructor | ||
*/ | ||
abstract class ULongEntityClass<out E : ULongEntity>( | ||
table: IdTable<ULong>, | ||
entityType: Class<E>? = null, | ||
entityCtor: ((EntityID<ULong>) -> E)? = null | ||
) : EntityClass<ULong, E>(table, entityType, entityCtor) |
105 changes: 105 additions & 0 deletions
105
...src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/ULongIdTableEntityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package org.jetbrains.exposed.sql.tests.shared.entities | ||
|
||
import org.jetbrains.exposed.dao.ULongEntity | ||
import org.jetbrains.exposed.dao.ULongEntityClass | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
import org.jetbrains.exposed.dao.id.ULongIdTable | ||
import org.jetbrains.exposed.sql.exists | ||
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase | ||
import org.jetbrains.exposed.sql.tests.shared.assertEquals | ||
import org.junit.Test | ||
|
||
class ULongIdTableEntityTest : DatabaseTestsBase() { | ||
|
||
@Test | ||
fun `create tables`() { | ||
withTables(ULongIdTables.Cities, ULongIdTables.People) { | ||
assertEquals(true, ULongIdTables.Cities.exists()) | ||
assertEquals(true, ULongIdTables.People.exists()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `create records`() { | ||
withTables(ULongIdTables.Cities, ULongIdTables.People) { | ||
val mumbai = ULongIdTables.City.new { name = "Mumbai" } | ||
val pune = ULongIdTables.City.new { name = "Pune" } | ||
ULongIdTables.Person.new { | ||
name = "David D'souza" | ||
city = mumbai | ||
} | ||
ULongIdTables.Person.new { | ||
name = "Tushar Mumbaikar" | ||
city = mumbai | ||
} | ||
ULongIdTables.Person.new { | ||
name = "Tanu Arora" | ||
city = pune | ||
} | ||
|
||
val allCities = ULongIdTables.City.all().map { it.name } | ||
assertEquals(true, allCities.contains<String>("Mumbai")) | ||
assertEquals(true, allCities.contains<String>("Pune")) | ||
assertEquals(false, allCities.contains<String>("Chennai")) | ||
|
||
val allPeople = ULongIdTables.Person.all().map { Pair(it.name, it.city.name) } | ||
assertEquals(true, allPeople.contains(Pair("David D'souza", "Mumbai"))) | ||
assertEquals(false, allPeople.contains(Pair("David D'souza", "Pune"))) | ||
} | ||
} | ||
|
||
@Test | ||
fun `update and delete records`() { | ||
withTables(ULongIdTables.Cities, ULongIdTables.People) { | ||
val mumbai = ULongIdTables.City.new { name = "Mumbai" } | ||
val pune = ULongIdTables.City.new { name = "Pune" } | ||
ULongIdTables.Person.new { | ||
name = "David D'souza" | ||
city = mumbai | ||
} | ||
ULongIdTables.Person.new { | ||
name = "Tushar Mumbaikar" | ||
city = mumbai | ||
} | ||
val tanu = ULongIdTables.Person.new { | ||
name = "Tanu Arora" | ||
city = pune | ||
} | ||
|
||
tanu.delete() | ||
pune.delete() | ||
|
||
val allCities = ULongIdTables.City.all().map { it.name } | ||
assertEquals(true, allCities.contains<String>("Mumbai")) | ||
assertEquals(false, allCities.contains<String>("Pune")) | ||
|
||
val allPeople = ULongIdTables.Person.all().map { Pair(it.name, it.city.name) } | ||
assertEquals(true, allPeople.contains(Pair("David D'souza", "Mumbai"))) | ||
assertEquals(false, allPeople.contains(Pair("Tanu Arora", "Pune"))) | ||
} | ||
} | ||
} | ||
|
||
object ULongIdTables { | ||
object Cities : ULongIdTable() { | ||
val name = varchar("name", 50) | ||
} | ||
|
||
class City(id: EntityID<ULong>) : ULongEntity(id) { | ||
companion object : ULongEntityClass<City>(Cities) | ||
|
||
var name by Cities.name | ||
} | ||
|
||
object People : ULongIdTable() { | ||
val name = varchar("name", 80) | ||
val cityId = reference("city_id", Cities) | ||
} | ||
|
||
class Person(id: EntityID<ULong>) : ULongEntity(id) { | ||
companion object : ULongEntityClass<Person>(People) | ||
|
||
var name by People.name | ||
var city by City referencedOn People.cityId | ||
} | ||
} |