Search code examples
jooq

Adhoc converter for row-value expressions with higher degree than 22


Is it possible in jOOQ to use an adhoc converter for converting nested row-value expressions?

I'm already using an adhoc converter for smaller degree mappings:

row(
  TABLE_A.relation1().FIRST_NAME,
  TABLE_A.relation1().LAST_NAME,
).mapping { firstname, lastname -> listOfNotNull(firstname, lastname).joinToString(" ") },

However, I would like something similar for higher degree mappings:

row(
  TABLE_B.FIRST_FIELD,
  ...
  TABLE_B.TWENTYTHIRD_FIELD,
).mapping { record -> custom converter logic, e.g. MyDataClass(record[TABLE_B.TWENTYTHIRD_FIELD]) }

Is it possible to project these type of row value expressions? I'm using jOOQ 3.16.


Solution

  • In jOOQ 3.15 - 3.16, there has been a missing RowN::mapping method which has been added to 3.17 with #12515.

    As a workaround, you can use auxiliary nested records to avoid projecting all of your columns, e.g.:

    row(
      row(
        TABLE_A.relation1().FIRST_NAME,
        TABLE_A.relation1().LAST_NAME
      ).mapping { f, l -> listOfNotNull(f, l).joinToString(" ") },
      ...
    ).mapping { rest -> ... }
    

    Or, alternatively, move some of that logic to SQL. Specifically that joinToString(" ") method is just DSL.concat():

    row(
      concat(
        TABLE_A.relation1().FIRST_NAME,
        inline(" "),
        TABLE_A.relation1().LAST_NAME
      ),
      ...
    ).mapping { rest -> ... }
    

    Or, finally, do this (which is what these Row[N].mapping(...) methods are just convenience for):

    field(row(
      TABLE_A.relation1().FIRST_NAME,
      TABLE_A.relation1().LAST_NAME,
      ...
    )).convertFrom(r -> ...)