Default Values in Oracle Functions

Simon Martinelli :

Assume the following function declaration:

FUNCTION ARTTEXTJN
(p_art_id     in number
,p_arttextart in varchar2 default 'basis'
,p_sprache    in varchar2 default null
,p_aufart     in number   default null
,p_fallback_arttextart in varchar2  default 'J' 
)
RETURN VARCHAR2

Expect the first parameter all parameter have a default value.

jOOQ generate a package method like this:

public static Field<String> arttextjn(Field<? extends Number> pArtId, Field<String> pArttextart, 
              Field<String> pSprache, Field<? extends Number> pAufart, Field<String> pFallbackArttextart) {
    Arttextjn f = new Arttextjn();
    f.setPArtId(pArtId);
    f.setPArttextart(pArttextart);
    f.setPSprache(pSprache);
    f.setPAufart(pAufart);
    f.setPFallbackArttextart(pFallbackArttextart);
    return f.asField();
}

If I want to use it in a query I have to pass null to the function:

dsl.select(KAMPARTIKEL.ARTNR, KAMPARTIKEL.ARTNRKAMP,
           PaBez.arttextjn(KAMPARTIKEL.ART_ID, null, null, null, null))

But then Oracle does not use the default values.

Is there a way to tell jOOQ to generate overloaded methods with all possible combinations? Otherwise I'm not able to use that function in a select clause.

Lukas Eder :

Is there a way to tell jOOQ to generate overloaded methods with all possible combinations?

No, there would be way too many combinations. Of course, you could extend the code generator yourself, but I would advise against it.

Otherwise I'm not able to use that function in a select clause.

Yes, you can use it! But not using that auxiliary method PaBez.arttextjn. It's possible to call it as a standalone function call:

Arttextjn f = new Arttextjn();
f.setPArtId(1);
f.execute();
String result = f.getReturnValue();

It should be possible to use in a SQL statement as well:

Arttextjn f = new Arttextjn();
f.setPArtId(KAMPARTIKEL.ART_ID);

var result =
dsl.select(KAMPARTIKEL.ARTNR, KAMPARTIKEL.ARTNRKAMP, f.asField())
   .from(KAMPARTIKEL)
   .fetch();

In your case, this should work out of the box.

Note that as of jOOQ 3.11, and in Oracle, jOOQ passes function arguments by index in this case, not by name (as it does for PostgreSQL). The generated SQL is:

select KAMPARTIKEL.ARTNR, KAMPARTIKEL.ARTNRKAMP, pa_bez.arttextjn(KAMPARTIKEL.ART_ID)
from KAMPARTIKEL

Which works because you're using only the first parameter, applying defaults to the rest. It wouldn't work if you would pass the last parameter, in case of which the generated SQL would have to use named parameters:

select 
  KAMPARTIKEL.ARTNR, 
  KAMPARTIKEL.ARTNRKAMP, 
  pa_bez.arttextjn(p_art_id => KAMPARTIKEL.ART_ID)
from KAMPARTIKEL

I've created an issue to fix this for jOOQ 3.12: https://github.com/jOOQ/jOOQ/issues/8560

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157821&siteId=1