We are using JOOQ to map an existing database. Every table (bar 1) in this database shares a similar set of columns:
resource_id: Int (FK)
id: Int (PK)
identifier: UUID
date_created: DateTime
date_modified: DateTime
There is a general rule in the database for all operations that looks something like this:
select *
from table t
join resource r on r.id = t.resource_id
where r.is_archived = false
Using JOOQs generation feature, I can successfully create a repository class that implements the basic query operations:
@Repository
class UserRepository(val dsl:DSLContext): IRepository {
val record = Tables.AUTHENTICATION_USER!! //generated by jooq
val resource = Tables.RESOURCE!! //generated by jooq
val pojo = AuthenticationUser::class.java //generated by jooq
// this is the general query logic that should be applied to all the things
override fun selectQuery(): SelectConditionStep<Record> {
return dsl
.select(record.asterisk())
.from(record)
.join(resource).on(resource.ID.eq(record.RESOURCE_ID))
.where(resource.IS_ARCHIVED.eq(false))
}
override fun asyncCount(): Mono<Int> = Mono.fromCallable {
selectQuery()
.count()
}
fun asyncGetPage(from: Int,
size: Int,
orderBy: SortField<*> = record.DATE_CREATED.asc(),
) = Mono.fromCallable {
selectQuery()
.orderBy(orderBy)
.limit(size)
.offset(from)
.fetchInto(pojo)
.let { Collections.unmodifiableList(it) }
}
Now, this is all well and good, but I am having trouble converting this to a generic implementation that can
table
of type T
I can get this to work:
class GeneralRepository<T: Table<R>, R: Record>(val table: T, val record: R, val dsl:DSLContext) {
fun asyncGetAll(): Mono<List<R>> = Mono.fromCallable {
dsl
.select()
.from(table)
.fetchInto(record::class.java)
}
}
// manually construct it ignoring Springs DI for development
val resourceRepository = GeneralRepository(
Tables.RESOURCE,
ResourceRecord(),
JooqConfiguration().dsl(),
)
The problem comes when I try to do anything with the table T
. Since T
is of a generic table type, the compiler has no way of knowing about the fields id
, identifier
, resource_id
, date_created
etc, meaning queries like this cannot compile:
fun asyncGetAllNonArchived() =
dsl
.select()
.from(table)
.where(table.DATE_CREATED > ...)
Question: Is there a way to tell the compiler or declare the generics in such a way that the common fields are available.
EDIT: Thanks to Lukas Eder below for the advice. Adapting that solution we were able to create a generic repository.
First off, changing the generator strategy to Kotlin let the Auditable
interface act correctly :)
Secondly, we couldn't find a way with the generic class to implement the Table
types in terms of the Record
types, so you have to very slightly extend the signature.
interface Auditable {
fun RESOURCE_ID(): TableField<Record, Int> =
(this as Table<Record>).field("resource_id") as TableField<Record, Int>
fun ID(): TableField<Record, Int> =
(this as Table<Record>).field("id") as TableField<Record, Int>
fun IDENTIFIER(): TableField<Record, UUID> =
(this as Table<Record>).field("identifier") as TableField<Record, UUID>
fun DATE_CREATED(): TableField<Record, LocalDateTime> =
(this as Table<Record>).field("date_created") as TableField<Record, LocalDateTime>
fun DATE_MODIFIED(): TableField<Record, LocalDateTime> =
(this as Table<Record>).field("date_modified") as TableField<Record, LocalDateTime>
}
open class GeneralRepository<R, T>(val table: T) where R: Record, T: Table<R>, T: Auditable {
val resource = Resource.RESOURCE
fun selectQuery() =
dsl
.select(table.asterisk())
.from(table)
.join(resource).on(resource.ID.eq(table.RESOURCE_ID()))
.where(resource.IS_ARCHIVED.eq(false))
You can then easily implement generic extensions:
class UserRepo: GeneralRepository<UserRecord, User>(table = User.USER) {
fun getEmail(email: String) = Mono.fromCallable {
selectQuery().and(table.EMAIL.eq(email))
}
}
You could generate an interface that provides access to these columns:
interface Auditable {
fun RESOURCE_ID(): TableField<Record, Int?> =
(this as Table<Record>).field("resource_id") as TableField<Record, Int?>
fun ID(): TableField<Record, Int?> =
(this as Table<Record>).field("id") as TableField<Record, Int?>
fun identifier(): TableField<Record, UUID?> =
(this as Table<Record>).field("identifier") as TableField<Record, UUID?>
fun DATE_CREATED(): TableField<Record, LocalDateTime?> =
(this as Table<Record>).field("date_created") as TableField<Record, LocalDateTime?>
fun DATE_MODIFIED(): TableField<Record, LocalDateTime?> =
(this as Table<Record>).field("date_modified") as TableField<Record, LocalDateTime?>
}
and then attach the interface to all generated tables using a generator strategy:
<strategy>
<matchers>
<tables>
<table>
<expression>T_.*</expression>
<tableImplements>com.example.Auditable</tableImplements>
</table>
</tables>
</matchers>
</strategy>
It is currently not possible to declare properties in the interface because it's not possible to detect whether an override
modifier is required, see https://github.com/jOOQ/jOOQ/issues/10776.
But this may be good enough for now. You can then create generic conditions like this:
fun <T> dateCreatedCondition(table: T, dt: LocalDateTime): Condition
where T : Table<*>, T : Auditable = table.DATE_CREATED().gt(dt)