ps0604
ps0604

Reputation: 1081

Creating a condition dynamically in jOOQ

I have the following two jOOQ lines, this works fine:

    Formulas f = FORMULAS.as("f");
    Condition condition = x > y ? f.SK.eq(sk) : f.SK.eq(0);

Now, since I have 100 tables that need the same condition and the condition is much more complex than the example above, I would like to create a method like so:

public Condition getCondition(Table table, int x, int y, int sk) {
        Condition condition = // write here a generic condition
        return condition;
}

But I don't know how to generically assign a column to a table. How to approach this?

Upvotes: 1

Views: 285

Answers (1)

Lesiak
Lesiak

Reputation: 26094

You can use the following:

public static Condition makeCondition(Table<?> table, int x, int y, int sk) {
    Field<Integer> skField = table.field("sk", int.class);
    return x > y ? skField.eq(sk) : skField.eq(0);
}

or a more verbose, but type safe version:

public static <R extends Record> Condition makeCondition(
        Table<R> table,
        TableField<R, Integer> skField,
        int x,
        int y,
        int sk) {
    return x > y ? skField.eq(sk) : skField.eq(0);
}

// usage:
var c = makeConditon(FORMULAS, FORMULAS.SK, 1, 2, 3);

Upvotes: 3

Related Questions