Search code examples
javajooq

Jooq fetch into a Map using SHOW VARIABLES results


I'm trying to use jooq to execute a SHOW VARIABLES query for mysql/maria. I'm trying to use jooq only because I have jooq for everything else but this SQL isn't something jooq can "abstract". I could also use jdbc directly but am trying to stay consistent and also learn jooq better.

SHOW VARIABLES returns two string columns, Variable_name and Value.

I've been trying to find a way to get a Map of these key/values, but nothing is working.

Map<String, String> dbVars = create.fetch("SHOW VARIABLES").intoMap(...?)

Seems like this works, but not sure if there's a simpler way:

Field<String> key = field("Variable_name", String.class);
Field<String> value = field("Value", String.class);
Map<String, String> dbVars = create.fetch("SHOW VARIABLES").intoMap(key, value);

Solution

  • By "simpler", you probably mean:

    • No need to declare column names
    • No need to declare local variables for key and value (that's not necessary in your example, though)

    You could use this, as an alternative:

    create.fetch("SHOW VARIABLES").intoMap(
      r -> r.get(0, String.class), 
      r -> r.get(1, String.class)
    );