1

A common question among jOOQ users is how a <forcedType> can be applied to a stored function return type in the code generator. The manual specifies that <includeExpression> matches qualified or unqualified identifiers, so given this HSQLDB function:

CREATE FUNCTION stored_functions.f_1 (p_i int)
RETURNS int
BEGIN ATOMIC
  RETURN p_i;
END

The parameter of the function can be forced to String using:

<forcedType>
  <userType>java.lang.String</userType>
  <converter>
    org.jooq.Converter.ofNullable(Integer.class, String.class, Object::toString, Integer::valueOf)
  </converter>
  <includeExpression>(?i:f_1\.p_i)</includeExpression>
</forcedType>

This produces the following Parameter specification:

/**
 * The parameter <code>STORED_FUNCTIONS.F_1.P_I</code>.
 */
public static final Parameter<String> P_I = Internal.createParameter(
  "P_I", org.jooq.impl.SQLDataType.INTEGER, false, false, 
  org.jooq.Converter.ofNullable(Integer.class, String.class, Object::toString, Integer::valueOf)
);

How to do the same for the return value?

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509

1 Answers1

0

There is a synthetic parameter name that can be used. It's called return_value, which is also the name of the generated Parameter literal.

The following specification will apply the same converter to both the P_I parameter and RETURN_VALUE:

<forcedType>
  <userType>java.lang.String</userType>
  <converter>
    org.jooq.Converter.ofNullable(Integer.class, String.class, Object::toString, Integer::valueOf)
  </converter>
  <includeExpression>(?i:f_1\.p_i|return_value)</includeExpression>
</forcedType>

This produces the following Parameter specifications:

/**
 * The parameter <code>STORED_FUNCTIONS.F_1.RETURN_VALUE</code>.
 */
public static final Parameter<String> RETURN_VALUE = Internal.createParameter(
  "RETURN_VALUE", org.jooq.impl.SQLDataType.INTEGER, false, false, 
  org.jooq.Converter.ofNullable(Integer.class, String.class, Object::toString, Integer::valueOf)
);

/**
 * The parameter <code>STORED_FUNCTIONS.F_1.P_I</code>.
 */
public static final Parameter<String> P_I = Internal.createParameter(
  "P_I", org.jooq.impl.SQLDataType.INTEGER, false, false, 
  org.jooq.Converter.ofNullable(Integer.class, String.class, Object::toString, Integer::valueOf)
);
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • What if PARAMTER type and RETURN_VALUE type there are DIFFERENT types (e.g. PARAMETER type is of `org.jooq.impl.SQLDataType.BIGINT` type and RETURN_VALUE is of type `org.jooq.JSON`)? BTW, PL flavor is PL/pgSQL. – NikolaS Jun 26 '20 at 09:14
  • Then you create two `` elements. – Lukas Eder Jun 26 '20 at 12:20
  • Could you please provide an example (**different types** of **return value** and **input parameter** of **stored function**) in your answer? I've tried to implement it and it didn't work...probably I'm doing something wrong, so such an example would be of a great help. Thank you in advance. – NikolaS Jun 26 '20 at 17:38
  • I've tried to that (to create two `` elements), but I've ran into problems and therefore I created another [question](https://stackoverflow.com/q/62601302/6805866). Please take a look. – NikolaS Jun 27 '20 at 07:59