Default Values in Oracle Functions

风流意气都作罢 提交于 2021-01-27 17:10:40

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/55810226/default-values-in-oracle-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!