Generate method for primary key condition on Table implementations

1 view
Skip to first unread message

Sven Haberer (Schroenser)

unread,
Jul 2, 2024, 3:33:58 AM (yesterday) Jul 2
to jOOQ User Group
Feature idea
Generate a method like

public Condition getPrimaryKeyCondition(String strongly,  Integer typed, LocalDateTime parameters) {
    return FOO.STRONGLY.eq(strongly).and(FOO.TYPED.eq(typed)).and(FOO.PARAMETERS.eq(parameters));
}

on each implementation of the Table interface.

Benefits
This method would allow to write type safe and compile time safe statements that deal with the primary key, for example:

int numberOfDeletedRecords = dsl.delete(FOO).where(FOO.getPrimaryKeyCondition(strongly, typed, parameters)).execute();

Also, it gets rid of the boilerplate code of creating the combined where condition, which might be hard to read but easy to mess up for a human developer.

Lukas Eder

unread,
Jul 2, 2024, 3:39:10 AM (yesterday) Jul 2
to jooq...@googlegroups.com
Thanks for your suggestion.

This  attempts to address the underlying issue at the wrong place: the creation of specific types of predicates, when the feature you're really looking for is a type safe representation of a key. With such a type safe representation, *all* the key interactions will be type safe, not just the creation of predicates. For example, your suggestion doesn't take into account the multiple other kinds of predicates that may be interesting to form, such as IN predicates, != predicates, NOT IN predicates, etc.

The good news is: we already have that feature! It's called embedded keys:

Once you turn that on, you will never be able to compare keys with scalar values anymore. You'll always have to wrap the value(s) in an EmbeddableRecord

I hope this helps,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/3496fa39-690a-4fd2-88ff-262b21d6ab9an%40googlegroups.com.

Lukas Eder

unread,
Jul 2, 2024, 3:39:57 AM (yesterday) Jul 2
to jooq...@googlegroups.com
As an aside, you can always extend the code generator and generate "custom code sections" like the one in your suggestion:

Sven Haberer (Schroenser)

unread,
Jul 2, 2024, 10:02:56 AM (yesterday) Jul 2
to jOOQ User Group
I was talking to my technical lead about extending the code generator, when he suggested to make a suggestion here.

But your point about the other usages of primary keys is a really good reason against my suggested feature. Also, I wasn't aware of the embedded and composite keys.

Thanks for your quick reply.

</closed> ;)
Reply all
Reply to author
Forward
0 new messages